khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 1 | //+build !go1.9 |
| 2 | |
| 3 | package concurrent |
| 4 | |
| 5 | import "sync" |
| 6 | |
| 7 | // Map implements a thread safe map for go version below 1.9 using mutex |
| 8 | type Map struct { |
| 9 | lock sync.RWMutex |
| 10 | data map[interface{}]interface{} |
| 11 | } |
| 12 | |
| 13 | // NewMap creates a thread safe map |
| 14 | func NewMap() *Map { |
| 15 | return &Map{ |
| 16 | data: make(map[interface{}]interface{}, 32), |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | // Load is same as sync.Map Load |
| 21 | func (m *Map) Load(key interface{}) (elem interface{}, found bool) { |
| 22 | m.lock.RLock() |
| 23 | elem, found = m.data[key] |
| 24 | m.lock.RUnlock() |
| 25 | return |
| 26 | } |
| 27 | |
| 28 | // Load is same as sync.Map Store |
| 29 | func (m *Map) Store(key interface{}, elem interface{}) { |
| 30 | m.lock.Lock() |
| 31 | m.data[key] = elem |
| 32 | m.lock.Unlock() |
| 33 | } |