guard.cc revision 227825
1/** 2 * guard.cc: Functions for thread-safe static initialisation. 3 * 4 * Static values in C++ can be initialised lazily their first use. This file 5 * contains functions that are used to ensure that two threads attempting to 6 * initialize the same static do not call the constructor twice. This is 7 * important because constructors can have side effects, so calling the 8 * constructor twice may be very bad. 9 * 10 * Statics that require initialisation are protected by a 64-bit value. Any 11 * platform that can do 32-bit atomic test and set operations can use this 12 * value as a low-overhead lock. Because statics (in most sane code) are 13 * accessed far more times than they are initialised, this lock implementation 14 * is heavily optimised towards the case where the static has already been 15 * initialised. 16 */ 17#include <stdint.h> 18#include <pthread.h> 19 20/** 21 * Returns a pointer to the low 32 bits in a 64-bit value, respecting the 22 * platform's byte order. 23 */ 24static int32_t *low_32_bits(volatile int64_t *ptr) 25{ 26 int32_t *low= (int32_t*)ptr; 27 // Test if the machine is big endian - constant propagation at compile time 28 // should eliminate this completely. 29 int one = 1; 30 if (*(char*)&one != 1) 31 { 32 low++; 33 } 34 return low; 35} 36 37/** 38 * Acquires a lock on a guard, returning 0 if the object has already been 39 * initialised, and 1 if it has not. If the object is already constructed then 40 * this function just needs to read a byte from memory and return. 41 */ 42extern "C" int __cxa_guard_acquire(volatile int64_t *guard_object) 43{ 44 char first_byte = (*guard_object) >> 56; 45 if (1 == first_byte) { return 0; } 46 int32_t *lock = low_32_bits(guard_object); 47 // Simple spin lock using the low 32 bits. We assume that concurrent 48 // attempts to initialize statics are very rare, so we don't need to 49 // optimise for the case where we have lots of threads trying to acquire 50 // the lock at the same time. 51 while (!__sync_bool_compare_and_swap_4(lock, 0, 1)) 52 { 53 sched_yield(); 54 } 55 // We have to test the guard again, in case another thread has performed 56 // the initialisation while we were trying to acquire the lock. 57 first_byte = (*guard_object) >> 56; 58 return (1 != first_byte); 59} 60 61/** 62 * Releases the lock without marking the object as initialised. This function 63 * is called if initialising a static causes an exception to be thrown. 64 */ 65extern "C" void __cxa_guard_abort(int64_t *guard_object) 66{ 67 int32_t *lock = low_32_bits(guard_object); 68 *lock = 0; 69} 70/** 71 * Releases the guard and marks the object as initialised. This function is 72 * called after successful initialisation of a static. 73 */ 74extern "C" void __cxa_guard_release(int64_t *guard_object) 75{ 76 // Set the first byte to 1 77 *guard_object |= ((int64_t)1) << 56; 78 __cxa_guard_abort(guard_object); 79} 80 81