40
41
// NewIDMutex create a new IDLock
43
>
impl := &idMutexImpl{
44
>
numShard: numShard,
45
>
hashFn: hashFn,
46
>
shards: make(map[uint32]*idMutexShardImpl),
47
>
}
48
>
for i := range numShard {
49
>
impl.shards[i] = &idMutexShardImpl{
50
>
mutexInfos: make(map[any]*mutexInfo),
51
>
}
52
>
}
53
55
}
56
58
>
return &mutexInfo{
59
>
waitCount: 1,
60
>
}
61
>
}
62
63
// LockID lock by specific identifier
65
>
shard := idMutex.shards[idMutex.getShardIndex(identifier)]
66
>
67
>
shard.Lock()
68
>
mutexInfo, ok := shard.mutexInfos[identifier]
69
>
if !ok {
70
>
mutexInfo := newMutexInfo()
71
>
shard.mutexInfos[identifier] = mutexInfo
72
>
shard.Unlock()
73
>
mutexInfo.Lock()
74
>
return
75
>
}
76
77
mutexInfo.waitCount++