| 1 | /* |
| 2 | Copyright 2018 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package keymutexscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/ |
| 18 | |
| 19 | import ( |
| 20 | "hash/fnvscip-go gomod github.com/golang/go/src go1.23 `hash/fnv`/" |
| 21 | "runtimescip-go gomod github.com/golang/go/src go1.23 runtime/" |
| 22 | "syncscip-go gomod github.com/golang/go/src go1.23 sync/" |
| 23 | ) |
| 24 | |
| 25 | // NewHashed returns a new instance of KeyMutex which hashes arbitrary keys to |
| 26 | // a fixed set of locks. `n` specifies number of locks, if n <= 0, we use |
| 27 | // number of cpus. |
| 28 | // Note that because it uses fixed set of locks, different keys may share same |
| 29 | // lock, so it's possible to wait on same lock. |
| 30 | func NewHashedscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/NewHashed().(n int) KeyMutexscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/KeyMutex# { |
| 31 | if n <= 0 { |
| 32 | n = runtimescip-go gomod github.com/golang/go/src go1.23 runtime/.NumCPUscip-go gomod github.com/golang/go/src go1.23 runtime/NumCPU().() |
| 33 | } |
| 34 | return &hashedKeyMutexscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/hashedKeyMutex#{ |
| 35 | mutexesscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/hashedKeyMutex#mutexes.: make([]syncscip-go gomod github.com/golang/go/src go1.23 sync/.Mutexscip-go gomod github.com/golang/go/src go1.23 sync/Mutex#, n), |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | type hashedKeyMutexscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/hashedKeyMutex# struct { |
| 40 | mutexesscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/hashedKeyMutex#mutexes. []syncscip-go gomod github.com/golang/go/src go1.23 sync/.Mutexscip-go gomod github.com/golang/go/src go1.23 sync/Mutex# |
| 41 | } |
| 42 | |
| 43 | // Acquires a lock associated with the specified ID. |
| 44 | func (km *hashedKeyMutexscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/hashedKeyMutex#) LockKeyscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/hashedKeyMutex#LockKey().(id string) { |
| 45 | km.mutexesscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/hashedKeyMutex#mutexes.[km.hashscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/hashedKeyMutex#hash().(id)%uint32(len(km.mutexesscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/hashedKeyMutex#mutexes.))].Lockscip-go gomod github.com/golang/go/src go1.23 sync/Mutex#Lock().() |
| 46 | } |
| 47 | |
| 48 | // Releases the lock associated with the specified ID. |
| 49 | func (km *hashedKeyMutexscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/hashedKeyMutex#) UnlockKeyscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/hashedKeyMutex#UnlockKey().(id string) error { |
| 50 | km.mutexesscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/hashedKeyMutex#mutexes.[km.hashscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/hashedKeyMutex#hash().(id)%uint32(len(km.mutexesscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/hashedKeyMutex#mutexes.))].Unlockscip-go gomod github.com/golang/go/src go1.23 sync/Mutex#Unlock().() |
| 51 | return nil |
| 52 | } |
| 53 | |
| 54 | func (km *hashedKeyMutexscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/hashedKeyMutex#) hashscip-go gomod k8s.io/utils . `k8s.io/utils/keymutex`/hashedKeyMutex#hash().(id string) uint32 { |
| 55 | h := fnvscip-go gomod github.com/golang/go/src go1.23 `hash/fnv`/.New32ascip-go gomod github.com/golang/go/src go1.23 `hash/fnv`/New32a().() |
| 56 | h.Writescip-go gomod github.com/golang/go/src go1.23 io/Writer#Write().([]byte(id)) |
| 57 | return h.Sum32scip-go gomod github.com/golang/go/src go1.23 hash/Hash32#Sum32().() |
| 58 | } |