blob: 7688a23c4fa11af17a4d05598a27ae5dbccd0bde [file] [log] [blame]
Elliott Hughesc3f11402013-10-30 14:40:09 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
13 * distribution.
14 *
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
26 * SUCH DAMAGE.
27 */
28
29#include <pthread.h>
Yabin Cui80e6d6d2015-01-23 18:21:46 -080030#include <stdatomic.h>
Elliott Hughesc3f11402013-10-30 14:40:09 -070031
Elliott Hughesc3f11402013-10-30 14:40:09 -070032#include "private/bionic_futex.h"
33
Yabin Cui80e6d6d2015-01-23 18:21:46 -080034#define ONCE_INITIALIZATION_NOT_YET_STARTED 0
35#define ONCE_INITIALIZATION_UNDERWAY 1
36#define ONCE_INITIALIZATION_COMPLETE 2
Elliott Hughesc3f11402013-10-30 14:40:09 -070037
38/* NOTE: this implementation doesn't support a init function that throws a C++ exception
39 * or calls fork()
40 */
41int pthread_once(pthread_once_t* once_control, void (*init_routine)(void)) {
Yabin Cui80e6d6d2015-01-23 18:21:46 -080042 static_assert(sizeof(atomic_int) == sizeof(pthread_once_t),
43 "pthread_once_t should actually be atomic_int in implementation.");
Elliott Hughesc3f11402013-10-30 14:40:09 -070044
Yabin Cui80e6d6d2015-01-23 18:21:46 -080045 // 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);
Elliott Hughesc3f11402013-10-30 14:40:09 -070048
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.
Yabin Cui80e6d6d2015-01-23 18:21:46 -080054 int old_value = atomic_load_explicit(once_control_ptr, memory_order_acquire);
Elliott Hughesc3f11402013-10-30 14:40:09 -070055
56 while (true) {
Yabin Cui80e6d6d2015-01-23 18:21:46 -080057 if (__predict_true(old_value == ONCE_INITIALIZATION_COMPLETE)) {
Elliott Hughesc3f11402013-10-30 14:40:09 -070058 return 0;
59 }
60
Yabin Cui80e6d6d2015-01-23 18:21:46 -080061 // 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)) {
66 continue;
Elliott Hughesc3f11402013-10-30 14:40:09 -070067 }
68
Yabin Cui80e6d6d2015-01-23 18:21:46 -080069 if (old_value == ONCE_INITIALIZATION_NOT_YET_STARTED) {
70 // We got here first, we can handle the initialization.
71 (*init_routine)();
72
73 // Do a store_release indicating that initialization is complete.
74 atomic_store_explicit(once_control_ptr, ONCE_INITIALIZATION_COMPLETE, memory_order_release);
75
76 // Wake up any waiters, if any.
77 __futex_wake_ex(once_control_ptr, 0, INT_MAX);
78 return 0;
79 }
80
81 // The initialization is underway, wait for its finish.
Elliott Hughesc3f11402013-10-30 14:40:09 -070082 __futex_wait_ex(once_control_ptr, 0, old_value, NULL);
Yabin Cui80e6d6d2015-01-23 18:21:46 -080083 old_value = atomic_load_explicit(once_control_ptr, memory_order_acquire);
Elliott Hughesc3f11402013-10-30 14:40:09 -070084 }
Elliott Hughesc3f11402013-10-30 14:40:09 -070085}