File tree 1 file changed +24
-10
lines changed
1 file changed +24
-10
lines changed Original file line number Diff line number Diff line change 1
1
package ishell
2
2
3
+ import "sync"
4
+
3
5
// Context is an ishell context. It embeds ishell.Actions.
4
6
type Context struct {
5
7
contextValues
@@ -30,32 +32,44 @@ func (c *Context) ProgressBar() ProgressBar {
30
32
}
31
33
32
34
// contextValues is the map for values in the context.
33
- type contextValues map [string ]interface {}
35
+ type contextValues struct {
36
+ vals map [string ]interface {}
37
+ * sync.RWMutex
38
+ }
34
39
35
40
// Get returns the value associated with this context for key, or nil
36
41
// if no value is associated with key. Successive calls to Get with
37
42
// the same key returns the same result.
38
- func (c contextValues ) Get (key string ) interface {} {
39
- return c [key ]
43
+ func (c * contextValues ) Get (key string ) interface {} {
44
+ c .RLock ()
45
+ defer c .RUnlock ()
46
+ return c .vals [key ]
40
47
}
41
48
42
49
// Set sets the key in this context to value.
43
50
func (c * contextValues ) Set (key string , value interface {}) {
44
- if * c == nil {
45
- * c = make (map [string ]interface {})
51
+ if c .vals == nil {
52
+ c .vals = make (map [string ]interface {})
53
+ c .RWMutex = & sync.RWMutex {}
46
54
}
47
- (* c )[key ] = value
55
+ c .Lock ()
56
+ c .vals [key ] = value
57
+ c .Unlock ()
48
58
}
49
59
50
60
// Del deletes key and its value in this context.
51
- func (c contextValues ) Del (key string ) {
52
- delete (c , key )
61
+ func (c * contextValues ) Del (key string ) {
62
+ c .Lock ()
63
+ delete (c .vals , key )
64
+ c .Unlock ()
53
65
}
54
66
55
67
// Keys returns all keys in the context.
56
- func (c contextValues ) Keys () (keys []string ) {
57
- for key := range c {
68
+ func (c * contextValues ) Keys () (keys []string ) {
69
+ c .RLock ()
70
+ for key := range c .vals {
58
71
keys = append (keys , key )
59
72
}
73
+ c .RUnlock ()
60
74
return
61
75
}
You can’t perform that action at this time.
0 commit comments