Deleted Added
sdiff udiff text old ( 227972 ) new ( 232922 )
full compact
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.

--- 89 unchanged lines hidden (view full) ---

98 if (1 == first_byte) { return 0; }
99 int32_t *lock = low_32_bits(guard_object);
100 // Simple spin lock using the low 32 bits. We assume that concurrent
101 // attempts to initialize statics are very rare, so we don't need to
102 // optimise for the case where we have lots of threads trying to acquire
103 // the lock at the same time.
104 while (!__sync_bool_compare_and_swap_4(lock, 0, 1))
105 {
106 sched_yield();
107 }
108 // We have to test the guard again, in case another thread has performed
109 // the initialisation while we were trying to acquire the lock.
110 first_byte = (*guard_object) >> 56;
111 return (1 != first_byte);
112}
113

--- 21 unchanged lines hidden ---