entangle.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #pragma once
  2. /*
  3. * Copyright 2018-present Facebook, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <folly/experimental/pushmi/forwards.h>
  18. namespace pushmi {
  19. // template <class T, class Dual>
  20. // struct entangled {
  21. // T t;
  22. // entangled<Dual, T>* dual;
  23. //
  24. // ~entangled() {
  25. // if (!!dual) {
  26. // dual->dual = nullptr;
  27. // }
  28. // }
  29. // explicit entangled(T t) : t(std::move(t)), dual(nullptr) {}
  30. // entangled(entangled&& o) : t(std::move(o.t)), dual(o.dual) {
  31. // o.dual = nullptr;
  32. // if (!!dual) {
  33. // dual->dual = this;
  34. // }
  35. // }
  36. //
  37. // entangled() = delete;
  38. // entangled(const entangled&) = delete;
  39. // entangled& operator=(const entangled&) = delete;
  40. // entangled& operator=(entangled&&) = delete;
  41. //
  42. // Dual* lockPointerToDual() {
  43. // if (!!dual) {
  44. // return std::addressof(dual->t);
  45. // }
  46. // return nullptr;
  47. // }
  48. //
  49. // void unlockPointerToDual() {
  50. // }
  51. // };
  52. // This class can be used to keep a pair of values with pointers to each other
  53. // in sync, even when both objects are allowed to move. Ordinarily you'd do this
  54. // with a heap-allocated, refcounted, control block (or do something else using
  55. // external storage, like a lock table chosen by the current addresses of both
  56. // objects).
  57. // Another thing you could do is have locks, and a backoff strategy for dealing
  58. // with deadlock: lock locally, trylock your dual, if the trylock fails,
  59. // unlock yourself, wait a little while (giving a thread touching the dual a
  60. // chance to acquire the local lock), and repeat. That's kind of ugly.
  61. // This algorithm (which, to be clear, is untested and I haven't really even
  62. // thought through carefully) provides the same guarantees, but without using
  63. // external storage or backoff-based deadlock recovery.
  64. template <class T, class Dual>
  65. struct entangled {
  66. // must be constructed first so that the other.lockBoth() in the move
  67. // constructor is run before moving other.t and other.dual
  68. std::atomic<int> stateMachine;
  69. T t;
  70. // In a couple places, we can save on some atomic ops by making this atomic,
  71. // and adding a "dual == null" fast-path without locking.
  72. entangled<Dual, T>* dual;
  73. const static int kUnlocked = 0;
  74. const static int kLocked = 1;
  75. const static int kLockedAndLossAcknowledged = 2;
  76. // Note: *not* thread-safe; it's a bug for two threads to concurrently call
  77. // lockBoth() on the same entangled (just as it's a bug for two threads to
  78. // concurrently move from the same object).
  79. // However, calling lockBoth() on two entangled objects at once is
  80. // thread-safe.
  81. // Note also that this may wait indefinitely; it's not the usual non-blocking
  82. // tryLock().
  83. bool tryLockBoth() {
  84. // Try to acquire the local lock. We have to start locally, since local
  85. // addresses are the only ones we know are safe at first. The rule is, you
  86. // have to hold *both* locks to write any of either entangled object's
  87. // metadata, but need only one to read it.
  88. int expected = kUnlocked;
  89. if (!stateMachine.compare_exchange_weak(
  90. expected,
  91. kLocked,
  92. std::memory_order_seq_cst,
  93. std::memory_order_relaxed)) {
  94. return false;
  95. }
  96. // Having *either* object local-locked protects the data in both objects.
  97. // Once we hold our lock, no control data can change, in either object.
  98. if (dual == nullptr) {
  99. return true;
  100. }
  101. expected = kUnlocked;
  102. if (dual->stateMachine.compare_exchange_strong(
  103. expected, kLocked, std::memory_order_seq_cst)) {
  104. return true;
  105. }
  106. // We got here, and so hit the race; we're deadlocked if we stick to
  107. // locking. Revert to address-ordering. Note that address-ordering would
  108. // not be safe on its own, because of the lifetime issues involved; the
  109. // addresses here are only stable *because* we know both sides are locked,
  110. // and because of the invariant that you must hold both locks to modify
  111. // either piece of data.
  112. if ((uintptr_t)this < (uintptr_t)dual) {
  113. // I get to win the race. I'll acquire the locks, but have to make sure
  114. // my memory stays valid until the other thread acknowledges its loss.
  115. while (stateMachine.load(std::memory_order_relaxed) !=
  116. kLockedAndLossAcknowledged) {
  117. // Spin.
  118. }
  119. stateMachine.store(kLocked, std::memory_order_relaxed);
  120. return true;
  121. } else {
  122. // I lose the race, but have to coordinate with the winning thread, so
  123. // that it knows that I'm not about to try to touch it's data
  124. dual->stateMachine.store(
  125. kLockedAndLossAcknowledged, std::memory_order_relaxed);
  126. return false;
  127. }
  128. }
  129. void lockBoth() {
  130. while (!tryLockBoth()) {
  131. // Spin. But, note that all the unbounded spinning in tryLockBoth can be
  132. // straightforwardly futex-ified. There's a potentialy starvation issue
  133. // here, but note that it can be dealt with by adding a "priority" bit to
  134. // the state machine (i.e. if my priority bit is set, the thread for whom
  135. // I'm the local member of the pair gets to win the race, rather than
  136. // using address-ordering).
  137. }
  138. }
  139. void unlockBoth() {
  140. // Note that unlocking locally and then remotely is the right order. There
  141. // are no concurrent accesses to this object (as an API constraint -- lock
  142. // and unlock are not thread safe!), and no other thread will touch the
  143. // other object so long as its locked. Going in the other order could let
  144. // another thread incorrectly think we're going down the deadlock-avoidance
  145. // path in tryLock().
  146. stateMachine.store(kUnlocked, std::memory_order_release);
  147. if (dual != nullptr) {
  148. dual->stateMachine.store(kUnlocked, std::memory_order_release);
  149. }
  150. }
  151. entangled() = delete;
  152. entangled(const entangled&) = delete;
  153. entangled& operator=(const entangled&) = delete;
  154. entangled& operator=(entangled&&) = delete;
  155. explicit entangled(T t)
  156. : t(std::move(t)), dual(nullptr), stateMachine(kUnlocked) {}
  157. entangled(entangled&& other)
  158. : stateMachine((other.lockBoth(), kLocked)),
  159. t(std::move(other.t)),
  160. dual(std::move(other.dual)) {
  161. // Note that, above, we initialized stateMachine to the locked state; the
  162. // address of this object hasn't escaped yet, and won't (until we unlock
  163. // the dual), so it doesn't *really* matter, but it's conceptually helpful
  164. // to maintain that invariant.
  165. // Update our dual's data.
  166. if (dual != nullptr) {
  167. dual->dual = this;
  168. }
  169. // Update other's data.
  170. other.dual = nullptr;
  171. // unlock other so that its destructor can complete
  172. other.stateMachine.store(kUnlocked);
  173. // We locked on other, but will unlock on *this. The locking protocol
  174. // ensured that no accesses to other will occur after lock() returns, and
  175. // since then we updated dual's dual to be us.
  176. unlockBoth();
  177. }
  178. ~entangled() {
  179. lockBoth();
  180. if (dual != nullptr) {
  181. dual->dual = nullptr;
  182. }
  183. unlockBoth();
  184. }
  185. // Must unlock later even if dual is nullptr. This is fixable.
  186. Dual* lockPointerToDual() {
  187. lockBoth();
  188. return !!dual ? std::addressof(dual->t) : nullptr;
  189. }
  190. void unlockPointerToDual() {
  191. unlockBoth();
  192. }
  193. };
  194. template <class First, class Second>
  195. using entangled_pair =
  196. std::pair<entangled<First, Second>, entangled<Second, First>>;
  197. template <class First, class Second>
  198. auto entangle(First f, Second s) -> entangled_pair<First, Second> {
  199. entangled<First, Second> ef(std::move(f));
  200. entangled<Second, First> es(std::move(s));
  201. ef.dual = std::addressof(es);
  202. es.dual = std::addressof(ef);
  203. return {std::move(ef), std::move(es)};
  204. }
  205. template <class T, class Dual>
  206. struct locked_entangled_pair : std::pair<T*, Dual*> {
  207. entangled<T, Dual>* e;
  208. ~locked_entangled_pair() {
  209. if (!!e) {
  210. e->unlockBoth();
  211. }
  212. }
  213. explicit locked_entangled_pair(entangled<T, Dual>& e) : e(std::addressof(e)) {
  214. this->e->lockBoth();
  215. this->first = std::addressof(this->e->t);
  216. this->second = !!this->e->dual ? std::addressof(this->e->dual->t) : nullptr;
  217. }
  218. locked_entangled_pair() = delete;
  219. locked_entangled_pair(const locked_entangled_pair&) = delete;
  220. locked_entangled_pair& operator=(const locked_entangled_pair&) = delete;
  221. locked_entangled_pair(locked_entangled_pair&& o)
  222. : std::pair<T*, Dual*>(o), e(o.e) {
  223. o.e = nullptr;
  224. }
  225. locked_entangled_pair& operator=(locked_entangled_pair&& o) {
  226. static_cast<std::pair<T*, Dual*>&>(*this) =
  227. static_cast<std::pair<T*, Dual*>&&>(o);
  228. e = o.e;
  229. o.e = nullptr;
  230. return *this;
  231. }
  232. };
  233. template <class T, class Dual>
  234. locked_entangled_pair<T, Dual> lock_both(entangled<T, Dual>& e) {
  235. return locked_entangled_pair<T, Dual>{e};
  236. }
  237. template <class T, class Dual>
  238. struct shared_entangled : std::shared_ptr<T> {
  239. Dual* dual;
  240. std::mutex* lock;
  241. template <class P>
  242. explicit shared_entangled(std::shared_ptr<P>& p, T& t, Dual& d, std::mutex& l)
  243. : std::shared_ptr<T>(p, std::addressof(t)),
  244. dual(std::addressof(d)),
  245. lock(std::addressof(l)) {}
  246. shared_entangled() = delete;
  247. };
  248. template <class First, class Second>
  249. using shared_entangled_pair =
  250. std::pair<shared_entangled<First, Second>, shared_entangled<Second, First>>;
  251. template <class First, class Second>
  252. auto shared_entangle(First f, Second s)
  253. -> shared_entangled_pair<First, Second> {
  254. struct storage {
  255. storage(First&& f, Second&& s) : p((First &&) f, (Second &&) s) {}
  256. std::tuple<First, Second> p;
  257. std::mutex lock;
  258. };
  259. auto p = std::make_shared<storage>(std::move(f), std::move(s));
  260. shared_entangled<First, Second> ef(
  261. p, std::get<0>(p->p), std::get<1>(p->p), p->lock);
  262. shared_entangled<Second, First> es(
  263. p, std::get<1>(p->p), std::get<0>(p->p), p->lock);
  264. return {std::move(ef), std::move(es)};
  265. }
  266. template <class T, class Dual>
  267. struct locked_shared_entangled_pair : std::pair<T*, Dual*> {
  268. shared_entangled<T, Dual> e;
  269. ~locked_shared_entangled_pair() {
  270. if (!!e && !!e.lock) {
  271. e.lock->unlock();
  272. }
  273. }
  274. explicit locked_shared_entangled_pair(shared_entangled<T, Dual>& e)
  275. : e(std::move(e)) {
  276. this->e.lock->lock();
  277. this->first = this->e.get();
  278. this->second = this->e.dual;
  279. }
  280. locked_shared_entangled_pair() = delete;
  281. };
  282. template <class T, class Dual>
  283. locked_shared_entangled_pair<T, Dual> lock_both(shared_entangled<T, Dual>& e) {
  284. return locked_shared_entangled_pair<T, Dual>{e};
  285. }
  286. } // namespace pushmi