keymutex.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package keymutex
  14. import (
  15. "fmt"
  16. "sync"
  17. "github.com/golang/glog"
  18. )
  19. // KeyMutex is a thread-safe interface for acquiring locks on arbitrary strings.
  20. type KeyMutex interface {
  21. // Acquires a lock associated with the specified ID, creates the lock if one doesn't already exist.
  22. LockKey(id string)
  23. // Releases the lock associated with the specified ID.
  24. // Returns an error if the specified ID doesn't exist.
  25. UnlockKey(id string) error
  26. }
  27. // Returns a new instance of a key mutex.
  28. func NewKeyMutex() KeyMutex {
  29. return &keyMutex{
  30. mutexMap: make(map[string]*sync.Mutex),
  31. }
  32. }
  33. type keyMutex struct {
  34. sync.RWMutex
  35. mutexMap map[string]*sync.Mutex
  36. }
  37. // Acquires a lock associated with the specified ID (creates the lock if one doesn't already exist).
  38. func (km *keyMutex) LockKey(id string) {
  39. glog.V(5).Infof("LockKey(...) called for id %q\r\n", id)
  40. mutex := km.getOrCreateLock(id)
  41. mutex.Lock()
  42. glog.V(5).Infof("LockKey(...) for id %q completed.\r\n", id)
  43. }
  44. // Releases the lock associated with the specified ID.
  45. // Returns an error if the specified ID doesn't exist.
  46. func (km *keyMutex) UnlockKey(id string) error {
  47. glog.V(5).Infof("UnlockKey(...) called for id %q\r\n", id)
  48. km.RLock()
  49. defer km.RUnlock()
  50. mutex, exists := km.mutexMap[id]
  51. if !exists {
  52. return fmt.Errorf("id %q not found", id)
  53. }
  54. glog.V(5).Infof("UnlockKey(...) for id. Mutex found, trying to unlock it. %q\r\n", id)
  55. mutex.Unlock()
  56. glog.V(5).Infof("UnlockKey(...) for id %q completed.\r\n", id)
  57. return nil
  58. }
  59. // Returns lock associated with the specified ID, or creates the lock if one doesn't already exist.
  60. func (km *keyMutex) getOrCreateLock(id string) *sync.Mutex {
  61. km.Lock()
  62. defer km.Unlock()
  63. if _, exists := km.mutexMap[id]; !exists {
  64. km.mutexMap[id] = &sync.Mutex{}
  65. }
  66. return km.mutexMap[id]
  67. }