-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathsequence.go
More file actions
39 lines (32 loc) · 1.13 KB
/
sequence.go
File metadata and controls
39 lines (32 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package disruptor
import (
"sync/atomic"
"unsafe"
)
// atomicSequence is a cache-line-padded atomic int64 used to track sequence positions without false sharing.
type atomicSequence struct {
_ [CacheLineBytes - unsafe.Sizeof(int64(0))]byte
atomic.Int64
}
func newSequence() (this *atomicSequence) {
for this = new(atomicSequence); uintptr(unsafe.Pointer(this))%CacheLineBytes != 0; this = new(atomicSequence) {
// not cache aligned, try again
}
this.Store(defaultSequenceValue)
return this
}
// newSequences allocates a contiguous, cache-line-aligned slice of *atomicSequence
func newSequences(count int) []*atomicSequence {
var contiguous []atomicSequence // single, contiguous allocation
for contiguous = make([]atomicSequence, count); uintptr(unsafe.Pointer(&contiguous[0]))%CacheLineBytes != 0; contiguous = make([]atomicSequence, count) {
// not cache aligned, try again
}
// guaranteed cache alignment of underlying sequence values which are *contiguous*
this := make([]*atomicSequence, count)
for i := range this {
this[i] = &contiguous[i]
this[i].Store(defaultSequenceValue)
}
return this
}
const defaultSequenceValue = -1