rwmutex.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2016 The etcd Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package recipe
  15. import (
  16. "context"
  17. v3 "github.com/coreos/etcd/clientv3"
  18. "github.com/coreos/etcd/clientv3/concurrency"
  19. "github.com/coreos/etcd/mvcc/mvccpb"
  20. )
  21. type RWMutex struct {
  22. s *concurrency.Session
  23. ctx context.Context
  24. pfx string
  25. myKey *EphemeralKV
  26. }
  27. func NewRWMutex(s *concurrency.Session, prefix string) *RWMutex {
  28. return &RWMutex{s, context.TODO(), prefix + "/", nil}
  29. }
  30. func (rwm *RWMutex) RLock() error {
  31. rk, err := newUniqueEphemeralKey(rwm.s, rwm.pfx+"read")
  32. if err != nil {
  33. return err
  34. }
  35. rwm.myKey = rk
  36. // wait until nodes with "write-" and a lower revision number than myKey are gone
  37. for {
  38. if done, werr := rwm.waitOnLastRev(rwm.pfx + "write"); done || werr != nil {
  39. return werr
  40. }
  41. }
  42. }
  43. func (rwm *RWMutex) Lock() error {
  44. rk, err := newUniqueEphemeralKey(rwm.s, rwm.pfx+"write")
  45. if err != nil {
  46. return err
  47. }
  48. rwm.myKey = rk
  49. // wait until all keys of lower revision than myKey are gone
  50. for {
  51. if done, werr := rwm.waitOnLastRev(rwm.pfx); done || werr != nil {
  52. return werr
  53. }
  54. // get the new lowest key until this is the only one left
  55. }
  56. }
  57. // waitOnLowest will wait on the last key with a revision < rwm.myKey.Revision with a
  58. // given prefix. If there are no keys left to wait on, return true.
  59. func (rwm *RWMutex) waitOnLastRev(pfx string) (bool, error) {
  60. client := rwm.s.Client()
  61. // get key that's blocking myKey
  62. opts := append(v3.WithLastRev(), v3.WithMaxModRev(rwm.myKey.Revision()-1))
  63. lastKey, err := client.Get(rwm.ctx, pfx, opts...)
  64. if err != nil {
  65. return false, err
  66. }
  67. if len(lastKey.Kvs) == 0 {
  68. return true, nil
  69. }
  70. // wait for release on blocking key
  71. _, err = WaitEvents(
  72. client,
  73. string(lastKey.Kvs[0].Key),
  74. rwm.myKey.Revision(),
  75. []mvccpb.Event_EventType{mvccpb.DELETE})
  76. return false, err
  77. }
  78. func (rwm *RWMutex) RUnlock() error { return rwm.myKey.Delete() }
  79. func (rwm *RWMutex) Unlock() error { return rwm.myKey.Delete() }