rwmutex.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2016 CoreOS, Inc.
  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. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  17. v3 "github.com/coreos/etcd/clientv3"
  18. "github.com/coreos/etcd/storage/storagepb"
  19. )
  20. type RWMutex struct {
  21. client *v3.Client
  22. ctx context.Context
  23. key string
  24. myKey *EphemeralKV
  25. }
  26. func NewRWMutex(client *v3.Client, key string) *RWMutex {
  27. return &RWMutex{client, context.TODO(), key, nil}
  28. }
  29. func (rwm *RWMutex) RLock() error {
  30. rk, err := NewUniqueEphemeralKey(rwm.client, rwm.key+"/read")
  31. if err != nil {
  32. return err
  33. }
  34. rwm.myKey = rk
  35. // if there are nodes with "write-" and a lower
  36. // revision number than us we must wait
  37. resp, err := rwm.client.Get(rwm.ctx, rwm.key+"/write", v3.WithFirstRev()...)
  38. if err != nil {
  39. return err
  40. }
  41. if len(resp.Kvs) == 0 || resp.Kvs[0].ModRevision > rk.Revision() {
  42. // no blocking since no write key
  43. return nil
  44. }
  45. return rwm.waitOnLowest()
  46. }
  47. func (rwm *RWMutex) Lock() error {
  48. rk, err := NewUniqueEphemeralKey(rwm.client, rwm.key+"/write")
  49. if err != nil {
  50. return err
  51. }
  52. rwm.myKey = rk
  53. for {
  54. // find any key of lower rev number blocks the write lock
  55. opts := append(v3.WithLastRev(), v3.WithRev(rk.Revision()-1))
  56. resp, err := rwm.client.Get(rwm.ctx, rwm.key, opts...)
  57. if err != nil {
  58. return err
  59. }
  60. if len(resp.Kvs) == 0 {
  61. // no matching for revision before myKey; acquired
  62. break
  63. }
  64. if err := rwm.waitOnLowest(); err != nil {
  65. return err
  66. }
  67. // get the new lowest, etc until this is the only one left
  68. }
  69. return nil
  70. }
  71. func (rwm *RWMutex) waitOnLowest() error {
  72. // must block; get key before ek for waiting
  73. opts := append(v3.WithLastRev(), v3.WithRev(rwm.myKey.Revision()-1))
  74. lastKey, err := rwm.client.Get(rwm.ctx, rwm.key, opts...)
  75. if err != nil {
  76. return err
  77. }
  78. // wait for release on prior key
  79. _, err = WaitEvents(
  80. rwm.client,
  81. string(lastKey.Kvs[0].Key),
  82. rwm.myKey.Revision(),
  83. []storagepb.Event_EventType{storagepb.DELETE})
  84. return err
  85. }
  86. func (rwm *RWMutex) RUnlock() error { return rwm.myKey.Delete() }
  87. func (rwm *RWMutex) Unlock() error { return rwm.myKey.Delete() }