guard.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright 2010-2012 PathScale, Inc. All rights reserved.
  3. * Copyright 2021 David Chisnall. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
  16. * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /**
  28. * guard.cc: Functions for thread-safe static initialisation.
  29. *
  30. * Static values in C++ can be initialised lazily their first use. This file
  31. * contains functions that are used to ensure that two threads attempting to
  32. * initialize the same static do not call the constructor twice. This is
  33. * important because constructors can have side effects, so calling the
  34. * constructor twice may be very bad.
  35. *
  36. * Statics that require initialisation are protected by a 64-bit value. Any
  37. * platform that can do 32-bit atomic test and set operations can use this
  38. * value as a low-overhead lock. Because statics (in most sane code) are
  39. * accessed far more times than they are initialised, this lock implementation
  40. * is heavily optimised towards the case where the static has already been
  41. * initialised.
  42. */
  43. #include "atomic.h"
  44. #include <assert.h>
  45. #include <pthread.h>
  46. #include <stdint.h>
  47. #include <stdlib.h>
  48. // Older GCC doesn't define __LITTLE_ENDIAN__
  49. #ifndef __LITTLE_ENDIAN__
  50. // If __BYTE_ORDER__ is defined, use that instead
  51. # ifdef __BYTE_ORDER__
  52. # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  53. # define __LITTLE_ENDIAN__
  54. # endif
  55. // x86 and ARM are the most common little-endian CPUs, so let's have a
  56. // special case for them (ARM is already special cased). Assume everything
  57. // else is big endian.
  58. # elif defined(__x86_64) || defined(__i386)
  59. # define __LITTLE_ENDIAN__
  60. # endif
  61. #endif
  62. /*
  63. * The Itanium C++ ABI defines guard words that are 64-bit (32-bit on AArch32)
  64. * values with one bit defined to indicate that the guarded variable is and
  65. * another bit to indicate that it's currently locked (initialisation in
  66. * progress). The bit to use depends on the byte order of the target.
  67. *
  68. * On many 32-bit platforms, 64-bit atomics are unavailable (or slow) and so we
  69. * treat the two halves of the 64-bit word as independent values and establish
  70. * an ordering on them such that the guard word is never modified unless the
  71. * lock word is in the locked state. This means that we can do double-checked
  72. * locking by loading the guard word and, if it is not initialised, trying to
  73. * transition the lock word from the unlocked to locked state, and then
  74. * manipulate the guard word.
  75. */
  76. namespace
  77. {
  78. /**
  79. * The state of the guard variable when an attempt is made to lock it.
  80. */
  81. enum class GuardState
  82. {
  83. /**
  84. * The lock is not held but is not needed because initialisation is
  85. * one.
  86. */
  87. InitDone,
  88. /**
  89. * Initialisation is not done but the lock is held by the caller.
  90. */
  91. InitLockSucceeded,
  92. /**
  93. * Attempting to acquire the lock failed.
  94. */
  95. InitLockFailed
  96. };
  97. /**
  98. * Class encapsulating a single atomic word being used to represent the
  99. * guard. The word size is defined by the type of `GuardWord`. The bit
  100. * used to indicate the locked state is `1<<LockedBit`, the bit used to
  101. * indicate the initialised state is `1<<InitBit`.
  102. */
  103. template<typename GuardWord, int LockedBit, int InitBit>
  104. struct SingleWordGuard
  105. {
  106. /**
  107. * The value indicating that the lock bit is set (and no other bits).
  108. */
  109. static constexpr GuardWord locked = static_cast<GuardWord>(1)
  110. << LockedBit;
  111. /**
  112. * The value indicating that the initialised bit is set (and all other
  113. * bits are zero).
  114. */
  115. static constexpr GuardWord initialised = static_cast<GuardWord>(1)
  116. << InitBit;
  117. /**
  118. * The guard variable.
  119. */
  120. atomic<GuardWord> val;
  121. public:
  122. /**
  123. * Release the lock and set the initialised state. In the single-word
  124. * implementation here, these are both done by a single store.
  125. */
  126. void unlock(bool isInitialised)
  127. {
  128. val.store(isInitialised ? initialised : 0, memory_order::release);
  129. #ifndef NDEBUG
  130. GuardWord init_state = initialised;
  131. assert(*reinterpret_cast<uint8_t*>(&init_state) != 0);
  132. #endif
  133. }
  134. /**
  135. * Try to acquire the lock. This has a tri-state return, indicating
  136. * either that the lock was acquired, it wasn't acquired because it was
  137. * contended, or it wasn't acquired because the guarded variable is
  138. * already initialised.
  139. */
  140. GuardState try_lock()
  141. {
  142. GuardWord old = 0;
  143. // Try to acquire the lock, assuming that we are in the state where
  144. // the lock is not held and the variable is not initialised (so the
  145. // expected value is 0).
  146. if (val.compare_exchange(old, locked))
  147. {
  148. return GuardState::InitLockSucceeded;
  149. }
  150. // If the CAS failed and the old value indicates that this is
  151. // initialised, return that initialisation is done and skip further
  152. // retries.
  153. if (old == initialised)
  154. {
  155. return GuardState::InitDone;
  156. }
  157. // Otherwise, report failure.
  158. return GuardState::InitLockFailed;
  159. }
  160. /**
  161. * Check whether the guard indicates that the variable is initialised.
  162. */
  163. bool is_initialised()
  164. {
  165. return (val.load(memory_order::acquire) & initialised) ==
  166. initialised;
  167. }
  168. };
  169. /**
  170. * Class encapsulating using two 32-bit atomic values to represent a 64-bit
  171. * guard variable.
  172. */
  173. template<int LockedBit, int InitBit>
  174. class DoubleWordGuard
  175. {
  176. /**
  177. * The value of `lock_word` when the lock is held.
  178. */
  179. static constexpr uint32_t locked = static_cast<uint32_t>(1)
  180. << LockedBit;
  181. /**
  182. * The value of `init_word` when the guarded variable is initialised.
  183. */
  184. static constexpr uint32_t initialised = static_cast<uint32_t>(1)
  185. << InitBit;
  186. /**
  187. * The word used for the initialised flag. This is always the first
  188. * word irrespective of endian because the generated code compares the
  189. * first byte in memory against 0.
  190. */
  191. atomic<uint32_t> init_word;
  192. /**
  193. * The word used for the lock.
  194. */
  195. atomic<uint32_t> lock_word;
  196. public:
  197. /**
  198. * Try to acquire the lock. This has a tri-state return, indicating
  199. * either that the lock was acquired, it wasn't acquired because it was
  200. * contended, or it wasn't acquired because the guarded variable is
  201. * already initialised.
  202. */
  203. GuardState try_lock()
  204. {
  205. uint32_t old = 0;
  206. // Try to acquire the lock
  207. if (lock_word.compare_exchange(old, locked))
  208. {
  209. // If we succeeded, check if initialisation has happened. In
  210. // this version, we don't have atomic manipulation of both the
  211. // lock and initialised bits together. Instead, we have an
  212. // ordering rule that the initialised bit is only ever updated
  213. // with the lock held.
  214. if (is_initialised())
  215. {
  216. // If another thread did manage to initialise this, release
  217. // the lock and notify the caller that initialisation is
  218. // done.
  219. lock_word.store(0, memory_order::release);
  220. return GuardState::InitDone;
  221. }
  222. return GuardState::InitLockSucceeded;
  223. }
  224. return GuardState::InitLockFailed;
  225. }
  226. /**
  227. * Set the initialised state and release the lock. In this
  228. * implementation, this is ordered, not atomic: the initialise bit is
  229. * set while the lock is held.
  230. */
  231. void unlock(bool isInitialised)
  232. {
  233. init_word.store(isInitialised ? initialised : 0,
  234. memory_order::release);
  235. lock_word.store(0, memory_order::release);
  236. assert((*reinterpret_cast<uint8_t*>(this) != 0) == isInitialised);
  237. }
  238. /**
  239. * Return whether the guarded variable is initialised.
  240. */
  241. bool is_initialised()
  242. {
  243. return (init_word.load(memory_order::acquire) & initialised) ==
  244. initialised;
  245. }
  246. };
  247. // Check that the two implementations are the correct size.
  248. static_assert(sizeof(SingleWordGuard<uint32_t, 31, 0>) == sizeof(uint32_t),
  249. "Single-word 32-bit guard must be 32 bits");
  250. static_assert(sizeof(SingleWordGuard<uint64_t, 63, 0>) == sizeof(uint64_t),
  251. "Single-word 64-bit guard must be 64 bits");
  252. static_assert(sizeof(DoubleWordGuard<31, 0>) == sizeof(uint64_t),
  253. "Double-word guard must be 64 bits");
  254. #ifdef __arm__
  255. /**
  256. * The Arm PCS defines a variant of the Itanium ABI with 32-bit lock words.
  257. */
  258. using Guard = SingleWordGuard<uint32_t, 31, 0>;
  259. #elif defined(_LP64)
  260. # if defined(__LITTLE_ENDIAN__)
  261. /**
  262. * On little-endian 64-bit platforms the guard word is a single 64-bit
  263. * atomic with the lock in the high bit and the initialised flag in the low
  264. * bit.
  265. */
  266. using Guard = SingleWordGuard<uint64_t, 63, 0>;
  267. # else
  268. /**
  269. * On bit-endian 64-bit platforms, the guard word is a single 64-bit atomic
  270. * with the lock in the low bit and the initialised bit in the highest
  271. * byte.
  272. */
  273. using Guard = SingleWordGuard<uint64_t, 0, 56>;
  274. # endif
  275. #else
  276. # if defined(__LITTLE_ENDIAN__)
  277. /**
  278. * 32-bit platforms use the same layout as 64-bit.
  279. */
  280. using Guard = DoubleWordGuard<31, 0>;
  281. # else
  282. /**
  283. * 32-bit platforms use the same layout as 64-bit.
  284. */
  285. using Guard = DoubleWordGuard<0, 24>;
  286. # endif
  287. #endif
  288. } // namespace
  289. /**
  290. * Acquires a lock on a guard, returning 0 if the object has already been
  291. * initialised, and 1 if it has not. If the object is already constructed then
  292. * this function just needs to read a byte from memory and return.
  293. */
  294. extern "C" int __cxa_guard_acquire(Guard *guard_object)
  295. {
  296. // Check if this is already initialised. If so, we don't have to do
  297. // anything.
  298. if (guard_object->is_initialised())
  299. {
  300. return 0;
  301. }
  302. // Spin trying to acquire the lock. If we fail to acquire the lock the
  303. // first time then another thread will *probably* initialise it, but if the
  304. // constructor throws an exception then we may have to try again in this
  305. // thread.
  306. for (;;)
  307. {
  308. // Try to acquire the lock.
  309. switch (guard_object->try_lock())
  310. {
  311. // If we failed to acquire the lock but another thread has
  312. // initialised the lock while we were waiting, return immediately
  313. // indicating that initialisation is not required.
  314. case GuardState::InitDone:
  315. return 0;
  316. // If we acquired the lock, return immediately to start
  317. // initialisation.
  318. case GuardState::InitLockSucceeded:
  319. return 1;
  320. // If we didn't acquire the lock, pause and retry.
  321. case GuardState::InitLockFailed:
  322. break;
  323. }
  324. sched_yield();
  325. }
  326. }
  327. /**
  328. * Releases the lock without marking the object as initialised. This function
  329. * is called if initialising a static causes an exception to be thrown.
  330. */
  331. extern "C" void __cxa_guard_abort(Guard *guard_object)
  332. {
  333. guard_object->unlock(false);
  334. }
  335. /**
  336. * Releases the guard and marks the object as initialised. This function is
  337. * called after successful initialisation of a static.
  338. */
  339. extern "C" void __cxa_guard_release(Guard *guard_object)
  340. {
  341. guard_object->unlock(true);
  342. }