-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.go
More file actions
41 lines (31 loc) · 724 Bytes
/
list.go
File metadata and controls
41 lines (31 loc) · 724 Bytes
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
40
41
package process
import "sync"
// list is a simple list safe for concurrent use. The stored items
// must also be safe for concurrent use.
type list[T any] struct {
list []T
µ sync.RWMutex
}
// newList creates a list.
func newList[T any](vals ...T) *list[T] {
return &list[T]{list: vals}
}
func (list *list[T]) Add(val T) {
list.µ.Lock()
defer list.µ.Unlock()
list.list = append(list.list, val)
}
// Clear empties the list, returning its prior contents as a slice.
func (list *list[T]) Clear() []T {
list.µ.Lock()
defer list.µ.Unlock()
if len(list.list) == 0 {
return nil
}
result := make([]T, len(list.list))
for i, v := range list.list {
result[i] = v
}
list.list = nil
return result
}