1 // Copyright (C) 2011 The Android Open Source Project 2 // 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 6 // are met: 7 // 1. Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // 2. Redistributions in binary form must reproduce the above copyright 10 // notice, this list of conditions and the following disclaimer in the 11 // documentation and/or other materials provided with the distribution. 12 // 3. Neither the name of the project nor the names of its contributors 13 // may be used to endorse or promote products derived from this software 14 // without specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 // ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 // SUCH DAMAGE. 27 // 28 // One-time construction C++ runtime support 29 // See "3.3.2 One-time Construction API" of the Itanium C++ ABI reference 30 // And "3.2.3 Guard variables and the one-time construction API" in the ARM C++ ABI reference. 31 32 /* Note that the ARM C++ ABI defines the size of each guard variable 33 * as 32-bit, while the generic/Itanium one defines it as 64-bit. 34 * 35 * Also the ARM C++ ABI uses the least-significant bit to indicate 36 * completion, while the generic/Itanium one uses the least-significant 37 * byte. In all cases the corresponding item is set to value '1' 38 * 39 * We will treat guard variables here as 32-bit values, even on x86, 40 * given that this representation is compatible with compiler-generated 41 * variables that are 64-bits on little-endian systems. This makes the 42 * code simpler and slightly more efficient 43 */ 44 45 #include <stddef.h> 46 #include <pthread.h> 47 48 /* In this implementation, we use a single global mutex+condvar pair. 49 * 50 * Pros: portable and doesn't require playing with futexes, atomics 51 * and memory barriers. 52 * 53 * Cons: Slower than necessary. 54 */ 55 static pthread_mutex_t sMutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER; 56 static pthread_cond_t sCond = PTHREAD_COND_INITIALIZER; 57 58 59 extern "C" int __cxa_guard_acquire(int volatile * gv) 60 { 61 pthread_mutex_lock(&sMutex); 62 for (;;) { 63 // while gv points to a volatile value, we use the 64 // previous pthread_mutex_lock or pthread_cond_wait 65 // as a trivial memory barrier 66 int guard = *gv; 67 if ((guard & 1) != 0) { 68 /* already initialized - return 0 */ 69 pthread_mutex_unlock(&sMutex); 70 return 0; 71 } 72 73 // we use bit 8 to indicate that the guard value is being 74 // initialized, and bit 9 to indicate that there is another 75 // thread waiting for its completion. 76 if ((guard & 0x100) == 0) { 77 // nobody is initializing this yet, so mark the guard value 78 // first. and allow initialization to proceed. 79 *gv = 0x100; 80 pthread_mutex_unlock(&sMutex); 81 return 1; 82 } 83 84 // already being initialized by amother thread, 85 // we must indicate that there is a waiter, then 86 // wait to be woken up before trying again. 87 *gv = guard | 0x200; 88 pthread_cond_wait(&sCond, &sMutex); 89 } 90 } 91 92 extern "C" void __cxa_guard_release(int volatile * gv) 93 { 94 pthread_mutex_lock(&sMutex); 95 96 int guard = *gv; 97 // this indicates initialization for our two ABIs. 98 *gv = 0x1; 99 if ((guard & 0x200) != 0) 100 pthread_cond_broadcast(&sCond); 101 102 pthread_mutex_unlock(&sMutex); 103 } 104 105 extern "C" void __cxa_guard_abort(int volatile * gv) 106 { 107 pthread_mutex_lock(&sMutex); 108 109 int guard = *gv; 110 *gv = 0; 111 if ((guard & 0x200) != 0) 112 pthread_cond_broadcast(&sCond); 113 114 pthread_mutex_unlock(&sMutex); 115 } 116