@@ -9,7 +9,7 @@ const ReadOnLeft int32 = -1
9
9
const ReadOnRight int32 = 1
10
10
11
11
// LeftRightPrimitive provides the basic core of the leftt-right pattern.
12
- type LeftRightPrimitive struct {
12
+ type LeftRightPrimitive [ T any ] struct {
13
13
// readIndicators is an array of 2 read-indicators, counting the reader numbers on the left/right instance
14
14
readIndicators [2 ]ReadIndicator
15
15
// versionIndex is the index for readIndicators, 0 means reading on left, 1 means reading on right
@@ -19,9 +19,9 @@ type LeftRightPrimitive struct {
19
19
}
20
20
21
21
// New creates a LeftRightPrimitive
22
- func New () * LeftRightPrimitive {
22
+ func New [ T any ] () * LeftRightPrimitive [ T ] {
23
23
24
- m := & LeftRightPrimitive {
24
+ m := & LeftRightPrimitive [ T ] {
25
25
readIndicators : [2 ]ReadIndicator {
26
26
newDistributedAtomicReadIndicator (),
27
27
newDistributedAtomicReadIndicator (),
@@ -37,19 +37,19 @@ func New() *LeftRightPrimitive {
37
37
}
38
38
39
39
// readerArrive shall be called by the reader goroutine before start reading
40
- func (lr * LeftRightPrimitive ) readerArrive () int {
40
+ func (lr * LeftRightPrimitive [ T ] ) readerArrive () int {
41
41
idx := atomic .LoadInt32 (lr .versionIndex )
42
42
lr .readIndicators [idx ].arrive ()
43
43
return int (idx )
44
44
}
45
45
46
46
// readerDepart shall be called by the reader goroutine after finish reading
47
- func (lr * LeftRightPrimitive ) readerDepart (localVI int ) {
47
+ func (lr * LeftRightPrimitive [ T ] ) readerDepart (localVI int ) {
48
48
lr .readIndicators [localVI ].depart ()
49
49
}
50
50
51
51
// writerToggleVersionAndWait shall be called by a single writer goroutine when applying the modification
52
- func (lr * LeftRightPrimitive ) writerToggleVersionAndWait () {
52
+ func (lr * LeftRightPrimitive [ T ] ) writerToggleVersionAndWait () {
53
53
54
54
localVI := atomic .LoadInt32 (lr .versionIndex )
55
55
prevVI := int (localVI % 2 )
@@ -70,7 +70,7 @@ func (lr *LeftRightPrimitive) writerToggleVersionAndWait() {
70
70
}
71
71
72
72
// ApplyReadFn applies read operation on the chosen instance, oh, I really need generics, interface type is ugly
73
- func (lr * LeftRightPrimitive ) ApplyReadFn (l interface {} , r interface {} , fn func (interface {} )) {
73
+ func (lr * LeftRightPrimitive [ T ] ) ApplyReadFn (l T , r T , fn func (T )) {
74
74
75
75
lvi := lr .readerArrive ()
76
76
@@ -87,7 +87,7 @@ func (lr *LeftRightPrimitive) ApplyReadFn(l interface{}, r interface{}, fn func(
87
87
88
88
// ApplyWriteFn applies write operation on the chosen instance, write operation is done twice, on the left and right
89
89
// instance respectively, this might make writing longer, but the readers are wait-free.
90
- func (lr * LeftRightPrimitive ) ApplyWriteFn (l interface {} , r interface {} , fn func (interface {} )) {
90
+ func (lr * LeftRightPrimitive [ T ] ) ApplyWriteFn (l T , r T , fn func (T )) {
91
91
92
92
side := atomic .LoadInt32 (lr .sideToRead )
93
93
if side == ReadOnLeft {
0 commit comments