2 * Copyright 2010-2012 PathScale, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
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.
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.
28 * guard.cc: Functions for thread-safe static initialisation.
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.
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
50 // Older GCC doesn't define __LITTLE_ENDIAN__
51 #ifndef __LITTLE_ENDIAN__
52 // If __BYTE_ORDER__ is defined, use that instead
53 # ifdef __BYTE_ORDER__
54 # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
55 # define __LITTLE_ENDIAN__
57 // x86 and ARM are the most common little-endian CPUs, so let's have a
58 // special case for them (ARM is already special cased). Assume everything
59 // else is big endian.
60 # elif defined(__x86_64) || defined(__i386)
61 # define __LITTLE_ENDIAN__
67 * The least significant bit of the guard variable indicates that the object
68 * has been initialised, the most significant bit is used for a spinlock.
71 // ARM ABI - 32-bit guards.
72 typedef uint32_t guard_t;
73 static const uint32_t LOCKED = ((guard_t)1) << 31;
74 static const uint32_t INITIALISED = 1;
76 typedef uint64_t guard_t;
77 # if defined(__LITTLE_ENDIAN__)
78 static const guard_t LOCKED = ((guard_t)1) << 63;
79 static const guard_t INITIALISED = 1;
81 static const guard_t LOCKED = 1;
82 static const guard_t INITIALISED = ((guard_t)1) << 56;
87 * Acquires a lock on a guard, returning 0 if the object has already been
88 * initialised, and 1 if it has not. If the object is already constructed then
89 * this function just needs to read a byte from memory and return.
91 extern "C" int __cxa_guard_acquire(volatile guard_t *guard_object)
93 // Not an atomic read, doesn't establish a happens-before relationship, but
94 // if one is already established and we end up seeing an initialised state
95 // then it's a fast path, otherwise we'll do something more expensive than
96 // this test anyway...
97 if ((INITIALISED == *guard_object)) { return 0; }
98 // Spin trying to do the initialisation
101 // Loop trying to move the value of the guard from 0 (not
102 // locked, not initialised) to the locked-uninitialised
104 switch (__sync_val_compare_and_swap(guard_object, 0, LOCKED))
106 // If the old value was 0, we succeeded, so continue
110 // If this was already initialised, return and let the caller skip
111 // initialising it again.
114 // If it is locked by another thread, relinquish the CPU and try
117 case LOCKED | INITIALISED:
120 // If it is some other value, then something has gone badly wrong.
123 fprintf(stderr, "Invalid state detected attempting to lock static initialiser.\n");
127 //__builtin_unreachable();
132 * Releases the lock without marking the object as initialised. This function
133 * is called if initialising a static causes an exception to be thrown.
135 extern "C" void __cxa_guard_abort(volatile guard_t *guard_object)
137 __attribute__((unused))
138 bool reset = __sync_bool_compare_and_swap(guard_object, LOCKED, 0);
142 * Releases the guard and marks the object as initialised. This function is
143 * called after successful initialisation of a static.
145 extern "C" void __cxa_guard_release(volatile guard_t *guard_object)
147 __attribute__((unused))
148 bool reset = __sync_bool_compare_and_swap(guard_object, LOCKED, INITIALISED);