rwmutex.go 2.6 KB

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