@@ -34,6 +34,36 @@ The `SystemMutex` is just `sync.Mutex`.
34
34
It is also worth observing that the performance started to degrade towards the
35
35
very large array sizes. This is most likely due to a cache size limitation.
36
36
37
+ # Snippets
38
+
39
+ ## Using RTM
40
+
41
+ ``` go
42
+ m := map [string ]int {
43
+ " word1" : 0 ,
44
+ }
45
+
46
+ c := NewRTMContexDefault ()
47
+ c.Atomic (func () {
48
+ // Action to be done transactionally
49
+ m[" word1" ] = m[" word1" ] + 1
50
+ })
51
+ ```
52
+
53
+ # Using HLE
54
+
55
+ ``` go
56
+ m := map [string ]int {
57
+ " word1" : 0 ,
58
+ }
59
+
60
+ var lock safetyfast.SpinHLEMutex
61
+ lock.Lock ()
62
+ // Action to be done transactionally
63
+ m[" word1" ] = m[" word1" ] + 1
64
+ lock.Unlock ()
65
+ ```
66
+
37
67
# Examples
38
68
39
69
## Checking for HLE and RTM CPU support
@@ -62,37 +92,79 @@ func main() {
62
92
## Using RTM
63
93
64
94
``` go
65
- m := map [string ]int {
66
- " word1" : 0 ,
67
- " word2" : 0 ,
68
- " word3" : 0 ,
69
- " word4" : 0 ,
70
- }
95
+ package main
71
96
72
- c := NewRTMContexDefault ()
73
- c.Atomic (func () {
74
- // Action to be done transactionally
75
- count := m[" word1" ]
76
- m[" word1" ] = count + 1
77
- })
97
+ import (
98
+ " fmt"
99
+ " sync"
100
+ " github.com/linux4life798/safetyfast"
101
+ )
102
+
103
+ func main () {
104
+ m := map [string ]int {
105
+ " word1" : 0 ,
106
+ " word2" : 0 ,
107
+ }
108
+
109
+ c := safetyfast.NewRTMContexDefault ()
110
+ var wg sync.WaitGroup
111
+
112
+ wg.Add (2 )
113
+ go c.Atomic (func () {
114
+ // Action to be done transactionally
115
+ m[" word1" ] = m[" word1" ] + 1
116
+ wg.Done ()
117
+ })
118
+ go c.Atomic (func () {
119
+ // Action to be done transactionally
120
+ m[" word1" ] = m[" word1" ] + 1
121
+ wg.Done ()
122
+ })
123
+ wg.Wait ()
124
+
125
+ fmt.Println (" word1 =" , m[" word1" ])
126
+ }
78
127
```
79
128
80
129
## Using HLE
81
130
82
131
``` go
83
- m := map [string ]int {
84
- " word1" : 0 ,
85
- " word2" : 0 ,
86
- " word3" : 0 ,
87
- " word4" : 0 ,
88
- }
132
+ package main
89
133
90
- var lock SpinHLEMutex
91
- lock.Lock ()
92
- // Action to be done transactionally
93
- count := m[" word1" ]
94
- m[" word1" ] = count + 1
95
- lock.Unlock ()
134
+ import (
135
+ " fmt"
136
+ " sync"
137
+ " github.com/linux4life798/safetyfast"
138
+ )
139
+
140
+ func main () {
141
+ m := map [string ]int {
142
+ " word1" : 0 ,
143
+ " word2" : 0 ,
144
+ }
145
+
146
+ var lock safetyfast.SpinHLEMutex
147
+ var wg sync.WaitGroup
148
+
149
+ wg.Add (2 )
150
+ go func () {
151
+ lock.Lock ()
152
+ // Action to be done transactionally
153
+ m[" word1" ] = m[" word1" ] + 1
154
+ lock.Unlock ()
155
+ wg.Done ()
156
+ }()
157
+ go func () {
158
+ lock.Lock ()
159
+ // Action to be done transactionally
160
+ m[" word1" ] = m[" word1" ] + 1
161
+ lock.Unlock ()
162
+ wg.Done ()
163
+ }()
164
+ wg.Wait ()
165
+
166
+ fmt.Println (" word1 =" , m[" word1" ])
167
+ }
96
168
```
97
169
98
170
[ wikipedia-tsx ] : https://en.wikipedia.org/wiki/Transactional_Synchronization_Extensions
0 commit comments