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 // Temporary hack since we build this against prebuilts/ndk when building the 49 // NDK but against the shipped platforms in the tests (FORCE_REBUILD case). We 50 // need to update the headers in prebuilts/ndk, but we can't do that until we 51 // have an NDK built with the unified headers. 52 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER 53 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP 54 #endif 55 56 /* In this implementation, we use a single global mutex+condvar pair. 57 * 58 * Pros: portable and doesn't require playing with futexes, atomics 59 * and memory barriers. 60 * 61 * Cons: Slower than necessary. 62 */ 63 static pthread_mutex_t sMutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER; 64 static pthread_cond_t sCond = PTHREAD_COND_INITIALIZER; 65 66 67 extern "C" int __cxa_guard_acquire(int volatile * gv) 68 { 69 pthread_mutex_lock(&sMutex); 70 for (;;) { 71 // while gv points to a volatile value, we use the 72 // previous pthread_mutex_lock or pthread_cond_wait 73 // as a trivial memory barrier 74 int guard = *gv; 75 if ((guard & 1) != 0) { 76 /* already initialized - return 0 */ 77 pthread_mutex_unlock(&sMutex); 78 return 0; 79 } 80 81 // we use bit 8 to indicate that the guard value is being 82 // initialized, and bit 9 to indicate that there is another 83 // thread waiting for its completion. 84 if ((guard & 0x100) == 0) { 85 // nobody is initializing this yet, so mark the guard value 86 // first. and allow initialization to proceed. 87 *gv = 0x100; 88 pthread_mutex_unlock(&sMutex); 89 return 1; 90 } 91 92 // already being initialized by amother thread, 93 // we must indicate that there is a waiter, then 94 // wait to be woken up before trying again. 95 *gv = guard | 0x200; 96 pthread_cond_wait(&sCond, &sMutex); 97 } 98 } 99 100 extern "C" void __cxa_guard_release(int volatile * gv) 101 { 102 pthread_mutex_lock(&sMutex); 103 104 int guard = *gv; 105 // this indicates initialization for our two ABIs. 106 *gv = 0x1; 107 if ((guard & 0x200) != 0) 108 pthread_cond_broadcast(&sCond); 109 110 pthread_mutex_unlock(&sMutex); 111 } 112 113 extern "C" void __cxa_guard_abort(int volatile * gv) 114 { 115 pthread_mutex_lock(&sMutex); 116 117 int guard = *gv; 118 *gv = 0; 119 if ((guard & 0x200) != 0) 120 pthread_cond_broadcast(&sCond); 121 122 pthread_mutex_unlock(&sMutex); 123 } 124