1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #ifndef LIBCXXABI_SRC_INCLUDE_CXA_GUARD_IMPL_H
9 #define LIBCXXABI_SRC_INCLUDE_CXA_GUARD_IMPL_H
10 
11 /* cxa_guard_impl.h - Implements the C++ runtime support for function local
12  * static guards.
13  * The layout of the guard object is the same across ARM and Itanium.
14  *
15  * The first "guard byte" (which is checked by the compiler) is set only upon
16  * the completion of cxa release.
17  *
18  * The second "init byte" does the rest of the bookkeeping. It tracks if
19  * initialization is complete or pending, and if there are waiting threads.
20  *
21  * If the guard variable is 64-bits and the platforms supplies a 32-bit thread
22  * identifier, it is used to detect recursive initialization. The thread ID of
23  * the thread currently performing initialization is stored in the second word.
24  *
25  *  Guard Object Layout:
26  * ---------------------------------------------------------------------------
27  * | a+0: guard byte | a+1: init byte | a+2: unused ... | a+4: thread-id ... |
28  * ---------------------------------------------------------------------------
29  *
30  * Note that we don't do what the ABI docs suggest (put a mutex in the guard
31  * object which we acquire in cxa_guard_acquire and release in
32  * cxa_guard_release). Instead we use the init byte to imitate that behaviour,
33  * but without actually holding anything mutex related between aquire and
34  * release/abort.
35  *
36  *  Access Protocol:
37  *    For each implementation the guard byte is checked and set before accessing
38  *    the init byte.
39  *
40  *  Overall Design:
41  *    The implementation was designed to allow each implementation to be tested
42  *    independent of the C++ runtime or platform support.
43  *
44  */
45 
46 #include "__cxxabi_config.h"
47 #include "include/atomic_support.h" // from libc++
48 #if defined(__has_include)
49 #  if __has_include(<sys/syscall.h>)
50 #    include <sys/syscall.h>
51 #  endif
52 #  if __has_include(<unistd.h>)
53 #    include <unistd.h>
54 #  endif
55 #endif
56 
57 #include <limits.h>
58 #include <stdlib.h>
59 #include <__threading_support>
60 #ifndef _LIBCXXABI_HAS_NO_THREADS
61 #  if defined(__ELF__) && defined(_LIBCXXABI_LINK_PTHREAD_LIB)
62 #    pragma comment(lib, "pthread")
63 #  endif
64 #endif
65 
66 #if defined(__clang__)
67 #  pragma clang diagnostic push
68 #  pragma clang diagnostic ignored "-Wtautological-pointer-compare"
69 #elif defined(__GNUC__)
70 #  pragma GCC diagnostic push
71 #  pragma GCC diagnostic ignored "-Waddress"
72 #endif
73 
74 // To make testing possible, this header is included from both cxa_guard.cpp
75 // and a number of tests.
76 //
77 // For this reason we place everything in an anonymous namespace -- even though
78 // we're in a header. We want the actual implementation and the tests to have
79 // unique definitions of the types in this header (since the tests may depend
80 // on function local statics).
81 //
82 // To enforce this either `BUILDING_CXA_GUARD` or `TESTING_CXA_GUARD` must be
83 // defined when including this file. Only `src/cxa_guard.cpp` should define
84 // the former.
85 #ifdef BUILDING_CXA_GUARD
86 #  include "abort_message.h"
87 #  define ABORT_WITH_MESSAGE(...) ::abort_message(__VA_ARGS__)
88 #elif defined(TESTING_CXA_GUARD)
89 #  define ABORT_WITH_MESSAGE(...) ::abort()
90 #else
91 #  error "Either BUILDING_CXA_GUARD or TESTING_CXA_GUARD must be defined"
92 #endif
93 
94 #if __has_feature(thread_sanitizer)
95 extern "C" void __tsan_acquire(void*);
96 extern "C" void __tsan_release(void*);
97 #else
98 #  define __tsan_acquire(addr) ((void)0)
99 #  define __tsan_release(addr) ((void)0)
100 #endif
101 
102 namespace __cxxabiv1 {
103 // Use an anonymous namespace to ensure that the tests and actual implementation
104 // have unique definitions of these symbols.
105 namespace {
106 
107 //===----------------------------------------------------------------------===//
108 //                          Misc Utilities
109 //===----------------------------------------------------------------------===//
110 
111 template <class T, T (*Init)()>
112 struct LazyValue {
LazyValueLazyValue113   LazyValue() : is_init(false) {}
114 
getLazyValue115   T& get() {
116     if (!is_init) {
117       value = Init();
118       is_init = true;
119     }
120     return value;
121   }
122 
123 private:
124   T value;
125   bool is_init = false;
126 };
127 
128 template <class IntType>
129 class AtomicInt {
130 public:
131   using MemoryOrder = std::__libcpp_atomic_order;
132 
AtomicInt(IntType * b)133   explicit AtomicInt(IntType* b) : b_(b) {}
134   AtomicInt(AtomicInt const&) = delete;
135   AtomicInt& operator=(AtomicInt const&) = delete;
136 
load(MemoryOrder ord)137   IntType load(MemoryOrder ord) { return std::__libcpp_atomic_load(b_, ord); }
store(IntType val,MemoryOrder ord)138   void store(IntType val, MemoryOrder ord) { std::__libcpp_atomic_store(b_, val, ord); }
exchange(IntType new_val,MemoryOrder ord)139   IntType exchange(IntType new_val, MemoryOrder ord) { return std::__libcpp_atomic_exchange(b_, new_val, ord); }
compare_exchange(IntType * expected,IntType desired,MemoryOrder ord_success,MemoryOrder ord_failure)140   bool compare_exchange(IntType* expected, IntType desired, MemoryOrder ord_success, MemoryOrder ord_failure) {
141     return std::__libcpp_atomic_compare_exchange(b_, expected, desired, ord_success, ord_failure);
142   }
143 
144 private:
145   IntType* b_;
146 };
147 
148 //===----------------------------------------------------------------------===//
149 //                       PlatformGetThreadID
150 //===----------------------------------------------------------------------===//
151 
152 #if defined(__APPLE__) && defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
PlatformThreadID()153 uint32_t PlatformThreadID() {
154   static_assert(sizeof(mach_port_t) == sizeof(uint32_t), "");
155   return static_cast<uint32_t>(pthread_mach_thread_np(std::__libcpp_thread_get_current_id()));
156 }
157 #elif defined(SYS_gettid) && defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
PlatformThreadID()158 uint32_t PlatformThreadID() {
159   static_assert(sizeof(pid_t) == sizeof(uint32_t), "");
160   return static_cast<uint32_t>(syscall(SYS_gettid));
161 }
162 #else
163 constexpr uint32_t (*PlatformThreadID)() = nullptr;
164 #endif
165 
166 //===----------------------------------------------------------------------===//
167 //                          GuardByte
168 //===----------------------------------------------------------------------===//
169 
170 static constexpr uint8_t UNSET = 0;
171 static constexpr uint8_t COMPLETE_BIT = (1 << 0);
172 static constexpr uint8_t PENDING_BIT = (1 << 1);
173 static constexpr uint8_t WAITING_BIT = (1 << 2);
174 
175 /// Manages reads and writes to the guard byte.
176 struct GuardByte {
177   GuardByte() = delete;
178   GuardByte(GuardByte const&) = delete;
179   GuardByte& operator=(GuardByte const&) = delete;
180 
GuardByteGuardByte181   explicit GuardByte(uint8_t* const guard_byte_address) : guard_byte(guard_byte_address) {}
182 
183 public:
184   /// The guard byte portion of cxa_guard_acquire. Returns true if
185   /// initialization has already been completed.
acquireGuardByte186   bool acquire() {
187     // if guard_byte is non-zero, we have already completed initialization
188     // (i.e. release has been called)
189     return guard_byte.load(std::_AO_Acquire) != UNSET;
190   }
191 
192   /// The guard byte portion of cxa_guard_release.
releaseGuardByte193   void release() { guard_byte.store(COMPLETE_BIT, std::_AO_Release); }
194 
195   /// The guard byte portion of cxa_guard_abort.
abortGuardByte196   void abort() {} // Nothing to do
197 
198 private:
199   AtomicInt<uint8_t> guard_byte;
200 };
201 
202 //===----------------------------------------------------------------------===//
203 //                       InitByte Implementations
204 //===----------------------------------------------------------------------===//
205 //
206 // Each initialization byte implementation supports the following methods:
207 //
208 //  InitByte(uint8_t* _init_byte_address, uint32_t* _thread_id_address)
209 //    Construct the InitByte object, initializing our member variables
210 //
211 //  bool acquire()
212 //    Called before we start the initialization. Check if someone else has already started, and if
213 //    not to signal our intent to start it ourselves. We determine the current status from the init
214 //    byte, which is one of 4 possible values:
215 //      COMPLETE:           Initialization was finished by somebody else. Return true.
216 //      PENDING:            Somebody has started the initialization already, set the WAITING bit,
217 //                          then wait for the init byte to get updated with a new value.
218 //      (PENDING|WAITING):  Somebody has started the initialization already, and we're not the
219 //                          first one waiting. Wait for the init byte to get updated.
220 //      UNSET:              Initialization hasn't successfully completed, and nobody is currently
221 //                          performing the initialization. Set the PENDING bit to indicate our
222 //                          intention to start the initialization, and return false.
223 //    The return value indicates whether initialization has already been completed.
224 //
225 //  void release()
226 //    Called after successfully completing the initialization. Update the init byte to reflect
227 //    that, then if anybody else is waiting, wake them up.
228 //
229 //  void abort()
230 //    Called after an error is thrown during the initialization. Reset the init byte to UNSET to
231 //    indicate that we're no longer performing the initialization, then if anybody is waiting, wake
232 //    them up so they can try performing the initialization.
233 //
234 
235 //===----------------------------------------------------------------------===//
236 //                    Single Threaded Implementation
237 //===----------------------------------------------------------------------===//
238 
239 /// InitByteNoThreads - Doesn't use any inter-thread synchronization when
240 /// managing reads and writes to the init byte.
241 struct InitByteNoThreads {
242   InitByteNoThreads() = delete;
243   InitByteNoThreads(InitByteNoThreads const&) = delete;
244   InitByteNoThreads& operator=(InitByteNoThreads const&) = delete;
245 
InitByteNoThreadsInitByteNoThreads246   explicit InitByteNoThreads(uint8_t* _init_byte_address, uint32_t*) : init_byte_address(_init_byte_address) {}
247 
248   /// The init byte portion of cxa_guard_acquire. Returns true if
249   /// initialization has already been completed.
acquireInitByteNoThreads250   bool acquire() {
251     if (*init_byte_address == COMPLETE_BIT)
252       return true;
253     if (*init_byte_address & PENDING_BIT)
254       ABORT_WITH_MESSAGE("__cxa_guard_acquire detected recursive initialization");
255     *init_byte_address = PENDING_BIT;
256     return false;
257   }
258 
259   /// The init byte portion of cxa_guard_release.
releaseInitByteNoThreads260   void release() { *init_byte_address = COMPLETE_BIT; }
261   /// The init byte portion of cxa_guard_abort.
abortInitByteNoThreads262   void abort() { *init_byte_address = UNSET; }
263 
264 private:
265   /// The address of the byte used during initialization.
266   uint8_t* const init_byte_address;
267 };
268 
269 //===----------------------------------------------------------------------===//
270 //                     Global Mutex Implementation
271 //===----------------------------------------------------------------------===//
272 
273 struct LibcppMutex;
274 struct LibcppCondVar;
275 
276 #ifndef _LIBCXXABI_HAS_NO_THREADS
277 struct LibcppMutex {
278   LibcppMutex() = default;
279   LibcppMutex(LibcppMutex const&) = delete;
280   LibcppMutex& operator=(LibcppMutex const&) = delete;
281 
lockLibcppMutex282   bool lock() { return std::__libcpp_mutex_lock(&mutex); }
unlockLibcppMutex283   bool unlock() { return std::__libcpp_mutex_unlock(&mutex); }
284 
285 private:
286   friend struct LibcppCondVar;
287   std::__libcpp_mutex_t mutex = _LIBCPP_MUTEX_INITIALIZER;
288 };
289 
290 struct LibcppCondVar {
291   LibcppCondVar() = default;
292   LibcppCondVar(LibcppCondVar const&) = delete;
293   LibcppCondVar& operator=(LibcppCondVar const&) = delete;
294 
waitLibcppCondVar295   bool wait(LibcppMutex& mut) { return std::__libcpp_condvar_wait(&cond, &mut.mutex); }
broadcastLibcppCondVar296   bool broadcast() { return std::__libcpp_condvar_broadcast(&cond); }
297 
298 private:
299   std::__libcpp_condvar_t cond = _LIBCPP_CONDVAR_INITIALIZER;
300 };
301 #else
302 struct LibcppMutex {};
303 struct LibcppCondVar {};
304 #endif // !defined(_LIBCXXABI_HAS_NO_THREADS)
305 
306 /// InitByteGlobalMutex - Uses a global mutex and condition variable (common to
307 /// all static local variables) to manage reads and writes to the init byte.
308 template <class Mutex, class CondVar, Mutex& global_mutex, CondVar& global_cond,
309           uint32_t (*GetThreadID)() = PlatformThreadID>
310 struct InitByteGlobalMutex {
311 
InitByteGlobalMutexInitByteGlobalMutex312   explicit InitByteGlobalMutex(uint8_t* _init_byte_address, uint32_t* _thread_id_address)
313       : init_byte_address(_init_byte_address), thread_id_address(_thread_id_address),
314         has_thread_id_support(_thread_id_address != nullptr && GetThreadID != nullptr) {}
315 
316 public:
317   /// The init byte portion of cxa_guard_acquire. Returns true if
318   /// initialization has already been completed.
acquireInitByteGlobalMutex319   bool acquire() {
320     LockGuard g("__cxa_guard_acquire");
321     // Check for possible recursive initialization.
322     if (has_thread_id_support && (*init_byte_address & PENDING_BIT)) {
323       if (*thread_id_address == current_thread_id.get())
324         ABORT_WITH_MESSAGE("__cxa_guard_acquire detected recursive initialization");
325     }
326 
327     // Wait until the pending bit is not set.
328     while (*init_byte_address & PENDING_BIT) {
329       *init_byte_address |= WAITING_BIT;
330       global_cond.wait(global_mutex);
331     }
332 
333     if (*init_byte_address == COMPLETE_BIT)
334       return true;
335 
336     if (has_thread_id_support)
337       *thread_id_address = current_thread_id.get();
338 
339     *init_byte_address = PENDING_BIT;
340     return false;
341   }
342 
343   /// The init byte portion of cxa_guard_release.
releaseInitByteGlobalMutex344   void release() {
345     bool has_waiting;
346     {
347       LockGuard g("__cxa_guard_release");
348       has_waiting = *init_byte_address & WAITING_BIT;
349       *init_byte_address = COMPLETE_BIT;
350     }
351     if (has_waiting) {
352       if (global_cond.broadcast()) {
353         ABORT_WITH_MESSAGE("%s failed to broadcast", "__cxa_guard_release");
354       }
355     }
356   }
357 
358   /// The init byte portion of cxa_guard_abort.
abortInitByteGlobalMutex359   void abort() {
360     bool has_waiting;
361     {
362       LockGuard g("__cxa_guard_abort");
363       if (has_thread_id_support)
364         *thread_id_address = 0;
365       has_waiting = *init_byte_address & WAITING_BIT;
366       *init_byte_address = UNSET;
367     }
368     if (has_waiting) {
369       if (global_cond.broadcast()) {
370         ABORT_WITH_MESSAGE("%s failed to broadcast", "__cxa_guard_abort");
371       }
372     }
373   }
374 
375 private:
376   /// The address of the byte used during initialization.
377   uint8_t* const init_byte_address;
378   /// An optional address storing an identifier for the thread performing initialization.
379   /// It's used to detect recursive initialization.
380   uint32_t* const thread_id_address;
381 
382   const bool has_thread_id_support;
383   LazyValue<uint32_t, GetThreadID> current_thread_id;
384 
385 private:
386   struct LockGuard {
387     LockGuard() = delete;
388     LockGuard(LockGuard const&) = delete;
389     LockGuard& operator=(LockGuard const&) = delete;
390 
LockGuardInitByteGlobalMutex::LockGuard391     explicit LockGuard(const char* calling_func) : calling_func_(calling_func) {
392       if (global_mutex.lock())
393         ABORT_WITH_MESSAGE("%s failed to acquire mutex", calling_func_);
394     }
395 
~LockGuardInitByteGlobalMutex::LockGuard396     ~LockGuard() {
397       if (global_mutex.unlock())
398         ABORT_WITH_MESSAGE("%s failed to release mutex", calling_func_);
399     }
400 
401   private:
402     const char* const calling_func_;
403   };
404 };
405 
406 //===----------------------------------------------------------------------===//
407 //                         Futex Implementation
408 //===----------------------------------------------------------------------===//
409 
410 #if defined(SYS_futex)
PlatformFutexWait(int * addr,int expect)411 void PlatformFutexWait(int* addr, int expect) {
412   constexpr int WAIT = 0;
413   syscall(SYS_futex, addr, WAIT, expect, 0);
414   __tsan_acquire(addr);
415 }
PlatformFutexWake(int * addr)416 void PlatformFutexWake(int* addr) {
417   constexpr int WAKE = 1;
418   __tsan_release(addr);
419   syscall(SYS_futex, addr, WAKE, INT_MAX);
420 }
421 #else
422 constexpr void (*PlatformFutexWait)(int*, int) = nullptr;
423 constexpr void (*PlatformFutexWake)(int*) = nullptr;
424 #endif
425 
PlatformSupportsFutex()426 constexpr bool PlatformSupportsFutex() { return +PlatformFutexWait != nullptr; }
427 
428 /// InitByteFutex - Uses a futex to manage reads and writes to the init byte.
429 template <void (*Wait)(int*, int) = PlatformFutexWait, void (*Wake)(int*) = PlatformFutexWake,
430           uint32_t (*GetThreadIDArg)() = PlatformThreadID>
431 struct InitByteFutex {
432 
InitByteFutexInitByteFutex433   explicit InitByteFutex(uint8_t* _init_byte_address, uint32_t* _thread_id_address)
434       : init_byte(_init_byte_address),
435         has_thread_id_support(_thread_id_address != nullptr && GetThreadIDArg != nullptr),
436         thread_id(_thread_id_address),
437         base_address(reinterpret_cast<int*>(/*_init_byte_address & ~0x3*/ _init_byte_address - 1)) {}
438 
439 public:
440   /// The init byte portion of cxa_guard_acquire. Returns true if
441   /// initialization has already been completed.
acquireInitByteFutex442   bool acquire() {
443     while (true) {
444       uint8_t last_val = UNSET;
445       if (init_byte.compare_exchange(&last_val, PENDING_BIT, std::_AO_Acq_Rel, std::_AO_Acquire)) {
446         if (has_thread_id_support) {
447           thread_id.store(current_thread_id.get(), std::_AO_Relaxed);
448         }
449         return false;
450       }
451 
452       if (last_val == COMPLETE_BIT)
453         return true;
454 
455       if (last_val & PENDING_BIT) {
456 
457         // Check for recursive initialization
458         if (has_thread_id_support && thread_id.load(std::_AO_Relaxed) == current_thread_id.get()) {
459           ABORT_WITH_MESSAGE("__cxa_guard_acquire detected recursive initialization");
460         }
461 
462         if ((last_val & WAITING_BIT) == 0) {
463           // This compare exchange can fail for several reasons
464           // (1) another thread finished the whole thing before we got here
465           // (2) another thread set the waiting bit we were trying to thread
466           // (3) another thread had an exception and failed to finish
467           if (!init_byte.compare_exchange(&last_val, PENDING_BIT | WAITING_BIT, std::_AO_Acq_Rel, std::_AO_Release)) {
468             // (1) success, via someone else's work!
469             if (last_val == COMPLETE_BIT)
470               return true;
471 
472             // (3) someone else, bailed on doing the work, retry from the start!
473             if (last_val == UNSET)
474               continue;
475 
476             // (2) the waiting bit got set, so we are happy to keep waiting
477           }
478         }
479         wait_on_initialization();
480       }
481     }
482   }
483 
484   /// The init byte portion of cxa_guard_release.
releaseInitByteFutex485   void release() {
486     uint8_t old = init_byte.exchange(COMPLETE_BIT, std::_AO_Acq_Rel);
487     if (old & WAITING_BIT)
488       wake_all();
489   }
490 
491   /// The init byte portion of cxa_guard_abort.
abortInitByteFutex492   void abort() {
493     if (has_thread_id_support)
494       thread_id.store(0, std::_AO_Relaxed);
495 
496     uint8_t old = init_byte.exchange(UNSET, std::_AO_Acq_Rel);
497     if (old & WAITING_BIT)
498       wake_all();
499   }
500 
501 private:
502   /// Use the futex to wait on the current guard variable. Futex expects a
503   /// 32-bit 4-byte aligned address as the first argument, so we use the 4-byte
504   /// aligned address that encompasses the init byte (i.e. the address of the
505   /// raw guard object that was passed to __cxa_guard_acquire/release/abort).
wait_on_initializationInitByteFutex506   void wait_on_initialization() { Wait(base_address, expected_value_for_futex(PENDING_BIT | WAITING_BIT)); }
wake_allInitByteFutex507   void wake_all() { Wake(base_address); }
508 
509 private:
510   AtomicInt<uint8_t> init_byte;
511 
512   const bool has_thread_id_support;
513   // Unsafe to use unless has_thread_id_support
514   AtomicInt<uint32_t> thread_id;
515   LazyValue<uint32_t, GetThreadIDArg> current_thread_id;
516 
517   /// the 4-byte-aligned address that encompasses the init byte (i.e. the
518   /// address of the raw guard object).
519   int* const base_address;
520 
521   /// Create the expected integer value for futex `wait(int* addr, int expected)`.
522   /// We pass the base address as the first argument, So this function creates
523   /// an zero-initialized integer  with `b` copied at the correct offset.
expected_value_for_futexInitByteFutex524   static int expected_value_for_futex(uint8_t b) {
525     int dest_val = 0;
526     std::memcpy(reinterpret_cast<char*>(&dest_val) + 1, &b, 1);
527     return dest_val;
528   }
529 
530   static_assert(Wait != nullptr && Wake != nullptr, "");
531 };
532 
533 //===----------------------------------------------------------------------===//
534 //                          GuardObject
535 //===----------------------------------------------------------------------===//
536 
537 enum class AcquireResult {
538   INIT_IS_DONE,
539   INIT_IS_PENDING,
540 };
541 constexpr AcquireResult INIT_IS_DONE = AcquireResult::INIT_IS_DONE;
542 constexpr AcquireResult INIT_IS_PENDING = AcquireResult::INIT_IS_PENDING;
543 
544 /// Co-ordinates between GuardByte and InitByte.
545 template <class InitByteT>
546 struct GuardObject {
547   GuardObject() = delete;
548   GuardObject(GuardObject const&) = delete;
549   GuardObject& operator=(GuardObject const&) = delete;
550 
551 private:
552   GuardByte guard_byte;
553   InitByteT init_byte;
554 
555 public:
556   /// ARM Constructor
GuardObjectGuardObject557   explicit GuardObject(uint32_t* raw_guard_object)
558       : guard_byte(reinterpret_cast<uint8_t*>(raw_guard_object)),
559         init_byte(reinterpret_cast<uint8_t*>(raw_guard_object) + 1, nullptr) {}
560 
561   /// Itanium Constructor
GuardObjectGuardObject562   explicit GuardObject(uint64_t* raw_guard_object)
563       : guard_byte(reinterpret_cast<uint8_t*>(raw_guard_object)),
564         init_byte(reinterpret_cast<uint8_t*>(raw_guard_object) + 1, reinterpret_cast<uint32_t*>(raw_guard_object) + 1) {
565   }
566 
567   /// Implements __cxa_guard_acquire.
cxa_guard_acquireGuardObject568   AcquireResult cxa_guard_acquire() {
569     // Use short-circuit evaluation to avoid calling init_byte.acquire when
570     // guard_byte.acquire returns true. (i.e. don't call it when we know from
571     // the guard byte that initialization has already been completed)
572     if (guard_byte.acquire() || init_byte.acquire())
573       return INIT_IS_DONE;
574     return INIT_IS_PENDING;
575   }
576 
577   /// Implements __cxa_guard_release.
cxa_guard_releaseGuardObject578   void cxa_guard_release() {
579     // Update guard byte first, so if somebody is woken up by init_byte.release
580     // and comes all the way back around to __cxa_guard_acquire again, they see
581     // it as having completed initialization.
582     guard_byte.release();
583     init_byte.release();
584   }
585 
586   /// Implements __cxa_guard_abort.
cxa_guard_abortGuardObject587   void cxa_guard_abort() {
588     guard_byte.abort();
589     init_byte.abort();
590   }
591 };
592 
593 //===----------------------------------------------------------------------===//
594 //                          Convenience Classes
595 //===----------------------------------------------------------------------===//
596 
597 /// NoThreadsGuard - Manages initialization without performing any inter-thread
598 /// synchronization.
599 using NoThreadsGuard = GuardObject<InitByteNoThreads>;
600 
601 /// GlobalMutexGuard - Manages initialization using a global mutex and
602 /// condition variable.
603 template <class Mutex, class CondVar, Mutex& global_mutex, CondVar& global_cond,
604           uint32_t (*GetThreadID)() = PlatformThreadID>
605 using GlobalMutexGuard = GuardObject<InitByteGlobalMutex<Mutex, CondVar, global_mutex, global_cond, GetThreadID>>;
606 
607 /// FutexGuard - Manages initialization using atomics and the futex syscall for
608 /// waiting and waking.
609 template <void (*Wait)(int*, int) = PlatformFutexWait, void (*Wake)(int*) = PlatformFutexWake,
610           uint32_t (*GetThreadIDArg)() = PlatformThreadID>
611 using FutexGuard = GuardObject<InitByteFutex<Wait, Wake, GetThreadIDArg>>;
612 
613 //===----------------------------------------------------------------------===//
614 //
615 //===----------------------------------------------------------------------===//
616 
617 template <class T>
618 struct GlobalStatic {
619   static T instance;
620 };
621 template <class T>
622 _LIBCPP_CONSTINIT T GlobalStatic<T>::instance = {};
623 
624 enum class Implementation { NoThreads, GlobalMutex, Futex };
625 
626 template <Implementation Impl>
627 struct SelectImplementation;
628 
629 template <>
630 struct SelectImplementation<Implementation::NoThreads> {
631   using type = NoThreadsGuard;
632 };
633 
634 template <>
635 struct SelectImplementation<Implementation::GlobalMutex> {
636   using type = GlobalMutexGuard<LibcppMutex, LibcppCondVar, GlobalStatic<LibcppMutex>::instance,
637                                 GlobalStatic<LibcppCondVar>::instance, PlatformThreadID>;
638 };
639 
640 template <>
641 struct SelectImplementation<Implementation::Futex> {
642   using type = FutexGuard<PlatformFutexWait, PlatformFutexWake, PlatformThreadID>;
643 };
644 
645 // TODO(EricWF): We should prefer the futex implementation when available. But
646 // it should be done in a separate step from adding the implementation.
647 constexpr Implementation CurrentImplementation =
648 #if defined(_LIBCXXABI_HAS_NO_THREADS)
649     Implementation::NoThreads;
650 #elif defined(_LIBCXXABI_USE_FUTEX)
651     Implementation::Futex;
652 #else
653     Implementation::GlobalMutex;
654 #endif
655 
656 static_assert(CurrentImplementation != Implementation::Futex || PlatformSupportsFutex(),
657               "Futex selected but not supported");
658 
659 using SelectedImplementation = SelectImplementation<CurrentImplementation>::type;
660 
661 } // end namespace
662 } // end namespace __cxxabiv1
663 
664 #if defined(__clang__)
665 #  pragma clang diagnostic pop
666 #elif defined(__GNUC__)
667 #  pragma GCC diagnostic pop
668 #endif
669 
670 #endif // LIBCXXABI_SRC_INCLUDE_CXA_GUARD_IMPL_H
671