guard.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright 2010-2012 PathScale, Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. *
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
  15. * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  16. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  21. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  23. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /**
  27. * guard.cc: Functions for thread-safe static initialisation.
  28. *
  29. * Static values in C++ can be initialised lazily their first use. This file
  30. * contains functions that are used to ensure that two threads attempting to
  31. * initialize the same static do not call the constructor twice. This is
  32. * important because constructors can have side effects, so calling the
  33. * constructor twice may be very bad.
  34. *
  35. * Statics that require initialisation are protected by a 64-bit value. Any
  36. * platform that can do 32-bit atomic test and set operations can use this
  37. * value as a low-overhead lock. Because statics (in most sane code) are
  38. * accessed far more times than they are initialised, this lock implementation
  39. * is heavily optimised towards the case where the static has already been
  40. * initialised.
  41. */
  42. #include <stdint.h>
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <pthread.h>
  46. #include <assert.h>
  47. #include "atomic.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 least significant bit of the guard variable indicates that the object
  64. * has been initialised, the most significant bit is used for a spinlock.
  65. */
  66. #ifdef __arm__
  67. // ARM ABI - 32-bit guards.
  68. typedef uint32_t guard_t;
  69. typedef uint32_t guard_lock_t;
  70. static const uint32_t LOCKED = static_cast<guard_t>(1) << 31;
  71. static const uint32_t INITIALISED = 1;
  72. #define LOCK_PART(guard) (guard)
  73. #define INIT_PART(guard) (guard)
  74. #elif defined(_LP64)
  75. typedef uint64_t guard_t;
  76. typedef uint64_t guard_lock_t;
  77. # if defined(__LITTLE_ENDIAN__)
  78. static const guard_t LOCKED = static_cast<guard_t>(1) << 63;
  79. static const guard_t INITIALISED = 1;
  80. # else
  81. static const guard_t LOCKED = 1;
  82. static const guard_t INITIALISED = static_cast<guard_t>(1) << 56;
  83. # endif
  84. #define LOCK_PART(guard) (guard)
  85. #define INIT_PART(guard) (guard)
  86. #else
  87. typedef uint32_t guard_lock_t;
  88. # if defined(__LITTLE_ENDIAN__)
  89. typedef struct {
  90. uint32_t init_half;
  91. uint32_t lock_half;
  92. } guard_t;
  93. static const uint32_t LOCKED = static_cast<guard_lock_t>(1) << 31;
  94. static const uint32_t INITIALISED = 1;
  95. # else
  96. typedef struct {
  97. uint32_t init_half;
  98. uint32_t lock_half;
  99. } guard_t;
  100. static_assert(sizeof(guard_t) == sizeof(uint64_t), "");
  101. static const uint32_t LOCKED = 1;
  102. static const uint32_t INITIALISED = static_cast<guard_lock_t>(1) << 24;
  103. # endif
  104. #define LOCK_PART(guard) (&(guard)->lock_half)
  105. #define INIT_PART(guard) (&(guard)->init_half)
  106. #endif
  107. static const guard_lock_t INITIAL = 0;
  108. /**
  109. * Acquires a lock on a guard, returning 0 if the object has already been
  110. * initialised, and 1 if it has not. If the object is already constructed then
  111. * this function just needs to read a byte from memory and return.
  112. */
  113. extern "C" int __cxa_guard_acquire(volatile guard_t *guard_object)
  114. {
  115. guard_lock_t old;
  116. // Not an atomic read, doesn't establish a happens-before relationship, but
  117. // if one is already established and we end up seeing an initialised state
  118. // then it's a fast path, otherwise we'll do something more expensive than
  119. // this test anyway...
  120. if (INITIALISED == __atomic_load_n(INIT_PART(guard_object), __ATOMIC_RELAXED))
  121. return 0;
  122. // Spin trying to do the initialisation
  123. for (;;)
  124. {
  125. // Loop trying to move the value of the guard from 0 (not
  126. // locked, not initialised) to the locked-uninitialised
  127. // position.
  128. old = __sync_val_compare_and_swap(LOCK_PART(guard_object),
  129. INITIAL, LOCKED);
  130. if (old == INITIAL) {
  131. // Lock obtained. If lock and init bit are
  132. // in separate words, check for init race.
  133. if (INIT_PART(guard_object) == LOCK_PART(guard_object))
  134. return 1;
  135. if (INITIALISED != *INIT_PART(guard_object))
  136. return 1;
  137. // No need for a memory barrier here,
  138. // see first comment.
  139. __atomic_store_n(LOCK_PART(guard_object), INITIAL, __ATOMIC_RELAXED);
  140. return 0;
  141. }
  142. // If lock and init bit are in the same word, check again
  143. // if we are done.
  144. if (INIT_PART(guard_object) == LOCK_PART(guard_object) &&
  145. old == INITIALISED)
  146. return 0;
  147. assert(old == LOCKED);
  148. // Another thread holds the lock.
  149. // If lock and init bit are in different words, check
  150. // if we are done before yielding and looping.
  151. if (INIT_PART(guard_object) != LOCK_PART(guard_object) &&
  152. INITIALISED == *INIT_PART(guard_object))
  153. return 0;
  154. sched_yield();
  155. }
  156. }
  157. /**
  158. * Releases the lock without marking the object as initialised. This function
  159. * is called if initialising a static causes an exception to be thrown.
  160. */
  161. extern "C" void __cxa_guard_abort(volatile guard_t *guard_object)
  162. {
  163. __attribute__((unused))
  164. bool reset = __sync_bool_compare_and_swap(LOCK_PART(guard_object),
  165. LOCKED, INITIAL);
  166. assert(reset);
  167. }
  168. /**
  169. * Releases the guard and marks the object as initialised. This function is
  170. * called after successful initialisation of a static.
  171. */
  172. extern "C" void __cxa_guard_release(volatile guard_t *guard_object)
  173. {
  174. guard_lock_t old;
  175. if (INIT_PART(guard_object) == LOCK_PART(guard_object))
  176. old = LOCKED;
  177. else
  178. old = INITIAL;
  179. __attribute__((unused))
  180. bool reset = __sync_bool_compare_and_swap(INIT_PART(guard_object),
  181. old, INITIALISED);
  182. assert(reset);
  183. if (INIT_PART(guard_object) != LOCK_PART(guard_object))
  184. *LOCK_PART(guard_object) = INITIAL;
  185. }