• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

enetx / g / 21222265366

21 Jan 2026 07:06PM UTC coverage: 89.815% (-0.4%) from 90.189%
21222265366

push

github

enetx
mutex, rwlock

40 of 40 new or added lines in 2 files covered. (100.0%)

29 existing lines in 4 files now uncovered.

6420 of 7148 relevant lines covered (89.82%)

313.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.46
/entry_safe.go
1
package g
2

3
import "github.com/enetx/g/ref"
4

5
// OccupiedSafeEntry represents a view into a concurrent map entry that is known
6
// to be present.
7
//
8
// It is typically obtained from MapSafe.Entry(key) when the key exists.
9
// All operations on OccupiedSafeEntry are safe for concurrent use.
10
type OccupiedSafeEntry[K comparable, V any] struct {
11
        m   *MapSafe[K, V]
12
        key K
13
}
14

15
// sealed prevents external implementations of the SafeEntry interface.
16
func (OccupiedSafeEntry[K, V]) sealed() {}
×
17

18
// Key returns the key of this entry.
19
func (e OccupiedSafeEntry[K, V]) Key() K { return e.key }
×
20

21
// Get returns the current value associated with the key.
22
//
23
// If the key is concurrently removed, the zero value of V is returned.
24
func (e OccupiedSafeEntry[K, V]) Get() V {
74✔
25
        if actual, ok := e.m.data.Load(e.key); ok {
148✔
26
                return *(actual.(*V))
74✔
27
        }
74✔
28
        var zero V
×
29
        return zero
×
30
}
31

32
// Insert replaces the value associated with the key and returns the previous value.
33
//
34
// The replacement is performed atomically with respect to other map operations.
35
func (e OccupiedSafeEntry[K, V]) Insert(value V) V {
1✔
36
        old, _ := e.m.data.Swap(e.key, &value)
1✔
37
        return *(old.(*V))
1✔
38
}
1✔
39

40
// Remove removes the entry from the map and returns the previously stored value.
41
//
42
// If the key is concurrently removed, the zero value of V is returned.
43
func (e OccupiedSafeEntry[K, V]) Remove() V {
1✔
44
        if actual, loaded := e.m.data.LoadAndDelete(e.key); loaded {
2✔
45
                return *(actual.(*V))
1✔
46
        }
1✔
47
        var zero V
×
48
        return zero
×
49
}
50

51
// OrInsert returns the existing value without modifying the map.
52
func (e OccupiedSafeEntry[K, V]) OrInsert(value V) V { return e.Get() }
63✔
53

54
// OrInsertWith returns the existing value without invoking the function.
55
func (e OccupiedSafeEntry[K, V]) OrInsertWith(fn func() V) V { return e.Get() }
8✔
56

57
// OrInsertWithKey returns the existing value without invoking the function.
58
func (e OccupiedSafeEntry[K, V]) OrInsertWithKey(fn func(K) V) V { return e.Get() }
1✔
59

60
// OrDefault returns the existing value.
61
func (e OccupiedSafeEntry[K, V]) OrDefault() V { return e.Get() }
×
62

63
// AndModify applies the provided function to the value associated with the key
64
// and returns the entry.
65
//
66
// The modification is performed using a compare-and-swap loop.
67
// The function receives a pointer to a copy of the value; the updated value
68
// is written back atomically.
69
//
70
// If the key is concurrently removed, AndModify becomes a no-op.
71
func (e OccupiedSafeEntry[K, V]) AndModify(fn func(*V)) SafeEntry[K, V] {
1,071✔
72
        for {
2,143✔
73
                actual, ok := e.m.data.Load(e.key)
1,072✔
74
                if !ok {
1,072✔
75
                        return e
×
76
                }
×
77

78
                oldPtr := actual.(*V)
1,072✔
79
                newVal := *oldPtr
1,072✔
80
                fn(&newVal)
1,072✔
81

1,072✔
82
                if e.m.data.CompareAndSwap(e.key, oldPtr, &newVal) {
2,143✔
83
                        return e
1,071✔
84
                }
1,071✔
85
        }
86
}
87

88
// VacantSafeEntry represents a view into a concurrent map entry that is known
89
// to be absent at the time of creation.
90
//
91
// It is typically obtained from MapSafe.Entry(key) when the key does not exist.
92
// All operations on VacantSafeEntry are safe for concurrent use.
93
//
94
// The modify field stores a pending modification function from AndModify,
95
// which will be applied if OrInsert loses a race with another goroutine.
96
type VacantSafeEntry[K comparable, V any] struct {
97
        m      *MapSafe[K, V]
98
        key    K
99
        modify func(*V)
100
}
101

102
// sealed prevents external implementations of the SafeEntry interface.
103
func (VacantSafeEntry[K, V]) sealed() {}
×
104

105
// Key returns the key that would be used for insertion.
106
func (e VacantSafeEntry[K, V]) Key() K { return e.key }
1✔
107

108
// Insert inserts the provided value into the map and returns the stored value.
109
//
110
// If another goroutine inserts the same key concurrently, the existing value
111
// is returned instead.
112
func (e VacantSafeEntry[K, V]) Insert(value V) V {
3✔
113
        actual, _ := e.m.data.LoadOrStore(e.key, &value)
3✔
114
        return *(actual.(*V))
3✔
115
}
3✔
116

117
// OrInsert inserts the provided value and returns the stored value.
118
//
119
// If another goroutine inserts the same key concurrently (the insert "loses
120
// the race"), the existing value is used instead. In this case, if a pending
121
// modification was registered via AndModify, it is applied atomically to the
122
// existing value before returning.
123
//
124
// This ensures that chained calls like Entry(k).AndModify(f).OrInsert(v)
125
// behave correctly under concurrent access: the modification is never lost.
126
func (e VacantSafeEntry[K, V]) OrInsert(value V) V {
11✔
127
        actual, loaded := e.m.data.LoadOrStore(e.key, &value)
11✔
128
        if loaded && e.modify != nil {
11✔
UNCOV
129
                OccupiedSafeEntry[K, V]{m: e.m, key: e.key}.AndModify(e.modify)
×
UNCOV
130
                return e.m.Get(e.key).Some()
×
UNCOV
131
        }
×
132

133
        return *(actual.(*V))
11✔
134
}
135

136
// OrInsertWith inserts the value returned by the function and returns the
137
// stored value.
138
//
139
// If another goroutine inserts the key concurrently, the function may not be
140
// invoked and the existing value is returned.
141
func (e VacantSafeEntry[K, V]) OrInsertWith(fn func() V) V {
4✔
142
        if actual, ok := e.m.data.Load(e.key); ok {
4✔
143
                return *(actual.(*V))
×
144
        }
×
145

146
        actual, _ := e.m.data.LoadOrStore(e.key, ref.Of(fn()))
4✔
147
        return *(actual.(*V))
4✔
148
}
149

150
// OrInsertWithKey inserts the value returned by the function and returns the
151
// stored value.
152
//
153
// If another goroutine inserts the key concurrently, the function may not be
154
// invoked and the existing value is returned.
155
func (e VacantSafeEntry[K, V]) OrInsertWithKey(fn func(K) V) V {
1✔
156
        if actual, ok := e.m.data.Load(e.key); ok {
1✔
157
                return *(actual.(*V))
×
158
        }
×
159

160
        actual, _ := e.m.data.LoadOrStore(e.key, ref.Of(fn(e.key)))
1✔
161
        return *(actual.(*V))
1✔
162
}
163

164
// OrDefault inserts the zero value of V into the map and returns the stored value.
165
func (e VacantSafeEntry[K, V]) OrDefault() V {
1✔
166
        var zero V
1✔
167
        return e.Insert(zero)
1✔
168
}
1✔
169

170
// AndModify registers a modification function to be applied to the value.
171
//
172
// If the key was concurrently inserted by another goroutine since this
173
// VacantSafeEntry was created, the modification is applied immediately
174
// via OccupiedSafeEntry.AndModify.
175
//
176
// Otherwise, the function is stored and will be applied later by OrInsert
177
// if it loses the race to insert the key. This ensures that the pattern
178
// Entry(k).AndModify(f).OrInsert(v) correctly increments existing values
179
// even under heavy concurrent access.
180
//
181
// Returns the appropriate SafeEntry for method chaining.
182
func (e VacantSafeEntry[K, V]) AndModify(fn func(*V)) SafeEntry[K, V] {
12✔
183
        if _, ok := e.m.data.Load(e.key); ok {
12✔
UNCOV
184
                return OccupiedSafeEntry[K, V]{m: e.m, key: e.key}.AndModify(fn)
×
UNCOV
185
        }
×
186

187
        return VacantSafeEntry[K, V]{m: e.m, key: e.key, modify: fn}
12✔
188
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc