github.com/lirm/aeron-go@v0.0.0-20230415210743-920325491dc4/aeron/position.go (about) 1 /* 2 Copyright 2016 Stanislav Liberman 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package aeron 18 19 import ( 20 "github.com/lirm/aeron-go/aeron/atomic" 21 "github.com/lirm/aeron-go/aeron/util" 22 ) 23 24 // Position is a wrapper for a buffer location of a position counter 25 type Position struct { 26 buffer *atomic.Buffer 27 id int32 28 offset int32 29 } 30 31 // NewPosition is a factory method to create new Position wrappers 32 func NewPosition(buffer *atomic.Buffer, id int32) Position { 33 var pos Position 34 35 pos.buffer = buffer 36 pos.id = id 37 pos.offset = id * 2 * util.CacheLineLength 38 39 logger.Debugf("<+ Counter[%d]: %d", id, pos.buffer.GetInt64(pos.offset)) 40 41 return pos 42 } 43 44 //go:norace 45 func (pos *Position) get() int64 { 46 p := pos.buffer.GetInt64(pos.offset) 47 return p 48 } 49 50 //go:norace 51 func (pos *Position) set(value int64) { 52 pos.buffer.PutInt64(pos.offset, value) 53 }