-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafemap.go
49 lines (40 loc) · 893 Bytes
/
safemap.go
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
42
43
44
45
46
47
48
49
package main
import (
"errors"
"sync"
"unsafe"
)
//safemap---------------------------------
type safemap struct {
smap map[unsafe.Pointer]unsafe.Pointer
*sync.RWMutex
}
func new_safemap() *safemap {
return &safemap{
make(map[unsafe.Pointer]unsafe.Pointer),
new(sync.RWMutex),
}
}
func (this *safemap) query(key unsafe.Pointer) unsafe.Pointer {
if this == nil || this.smap == nil {
panic("the safemap is nil")
}
this.RLock()
defer this.RUnlock()
if v, ok := this.smap[key]; ok {
return v
}
return nil
}
func (this *safemap) insert(key, value unsafe.Pointer) (isUpdate bool, err error) {
if this == nil || this.smap == nil {
return false, errors.New("the safemap is nil")
}
this.Lock()
defer this.Unlock()
if _, ok := this.smap[key]; ok {
isUpdate = true
}
this.smap[key] = value
return isUpdate, nil
}