2016-07-20 01:37:28 +00:00
|
|
|
package locksutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2016-07-20 08:48:35 +00:00
|
|
|
// Takes in a map, indexed by string and creates new 'sync.RWMutex' items.
|
|
|
|
// This utility creates 'count' number of mutexes (with a cap of 256) and
|
|
|
|
// places them in the map. The indices will be 2 character hexadecimal
|
|
|
|
// string values from 0 to count.
|
|
|
|
func CreateLocks(p map[string]*sync.RWMutex, count int64) error {
|
|
|
|
// Since the indices of the map entries are based on 2 character
|
|
|
|
// hex values, this utility can only create upto 256 locks.
|
2016-07-20 09:37:38 +00:00
|
|
|
if count <= 0 || count > 256 {
|
2016-07-20 08:48:35 +00:00
|
|
|
return fmt.Errorf("invalid count: %d", count)
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:36:58 +00:00
|
|
|
if p == nil {
|
|
|
|
return fmt.Errorf("map of locks is not initialized")
|
|
|
|
}
|
|
|
|
|
2016-07-20 01:37:28 +00:00
|
|
|
for i := int64(0); i < count; i++ {
|
|
|
|
p[fmt.Sprintf("%02x", i)] = &sync.RWMutex{}
|
|
|
|
}
|
2016-07-20 08:48:35 +00:00
|
|
|
|
|
|
|
return nil
|
2016-07-20 01:37:28 +00:00
|
|
|
}
|