2 * Copyright (C) 2008 The Android Open Source Project
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <stdatomic.h>
32 #include "private/bionic_futex.h"
34 #define ONCE_INITIALIZATION_NOT_YET_STARTED 0
35 #define ONCE_INITIALIZATION_UNDERWAY 1
36 #define ONCE_INITIALIZATION_COMPLETE 2
38 /* NOTE: this implementation doesn't support a init function that throws a C++ exception
41 int pthread_once(pthread_once_t* once_control, void (*init_routine)(void)) {
42 static_assert(sizeof(atomic_int) == sizeof(pthread_once_t),
43 "pthread_once_t should actually be atomic_int in implementation.");
45 // We prefer casting to atomic_int instead of declaring pthread_once_t to be atomic_int directly.
46 // Because using the second method pollutes pthread.h, and causes an error when compiling libcxx.
47 atomic_int* once_control_ptr = reinterpret_cast<atomic_int*>(once_control);
49 // First check if the once is already initialized. This will be the common
50 // case and we want to make this as fast as possible. Note that this still
51 // requires a load_acquire operation here to ensure that all the
52 // stores performed by the initialization function are observable on
53 // this CPU after we exit.
54 int old_value = atomic_load_explicit(once_control_ptr, memory_order_acquire);
57 if (__predict_true(old_value == ONCE_INITIALIZATION_COMPLETE)) {
61 // Try to atomically set the initialization underway flag. This requires a compare exchange
62 // in a loop, and we may need to exit prematurely if the initialization is complete.
63 if (!atomic_compare_exchange_weak_explicit(once_control_ptr, &old_value,
64 ONCE_INITIALIZATION_UNDERWAY,
65 memory_order_acquire, memory_order_acquire)) {
69 if (old_value == ONCE_INITIALIZATION_NOT_YET_STARTED) {
70 // We got here first, we can handle the initialization.
73 // Do a store_release indicating that initialization is complete.
74 atomic_store_explicit(once_control_ptr, ONCE_INITIALIZATION_COMPLETE, memory_order_release);
76 // Wake up any waiters, if any.
77 __futex_wake_ex(once_control_ptr, 0, INT_MAX);
81 // The initialization is underway, wait for its finish.
82 __futex_wait_ex(once_control_ptr, 0, old_value, false, nullptr);
83 old_value = atomic_load_explicit(once_control_ptr, memory_order_acquire);