Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame^] | 1 | /* |
| 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> |
| 30 | |
| 31 | #include "private/bionic_atomic_inline.h" |
| 32 | #include "private/bionic_futex.h" |
| 33 | |
| 34 | #define ONCE_INITIALIZING (1 << 0) |
| 35 | #define ONCE_COMPLETED (1 << 1) |
| 36 | |
| 37 | /* NOTE: this implementation doesn't support a init function that throws a C++ exception |
| 38 | * or calls fork() |
| 39 | */ |
| 40 | int pthread_once(pthread_once_t* once_control, void (*init_routine)(void)) { |
| 41 | volatile pthread_once_t* once_control_ptr = once_control; |
| 42 | |
| 43 | // PTHREAD_ONCE_INIT is 0, we use the following bit flags |
| 44 | // bit 0 set -> initialization is under way |
| 45 | // bit 1 set -> initialization is complete |
| 46 | |
| 47 | // First check if the once is already initialized. This will be the common |
| 48 | // case and we want to make this as fast as possible. Note that this still |
| 49 | // requires a load_acquire operation here to ensure that all the |
| 50 | // stores performed by the initialization function are observable on |
| 51 | // this CPU after we exit. |
| 52 | if (__predict_true((*once_control_ptr & ONCE_COMPLETED) != 0)) { |
| 53 | ANDROID_MEMBAR_FULL(); |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | while (true) { |
| 58 | // Try to atomically set the INITIALIZING flag. |
| 59 | // This requires a cmpxchg loop, and we may need |
| 60 | // to exit prematurely if we detect that |
| 61 | // COMPLETED is now set. |
| 62 | int32_t old_value, new_value; |
| 63 | |
| 64 | do { |
| 65 | old_value = *once_control_ptr; |
| 66 | if ((old_value & ONCE_COMPLETED) != 0) { |
| 67 | break; |
| 68 | } |
| 69 | |
| 70 | new_value = old_value | ONCE_INITIALIZING; |
| 71 | } while (__bionic_cmpxchg(old_value, new_value, once_control_ptr) != 0); |
| 72 | |
| 73 | if ((old_value & ONCE_COMPLETED) != 0) { |
| 74 | // We detected that COMPLETED was set while in our loop. |
| 75 | ANDROID_MEMBAR_FULL(); |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | if ((old_value & ONCE_INITIALIZING) == 0) { |
| 80 | // We got there first, we can jump out of the loop to handle the initialization. |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | // Another thread is running the initialization and hasn't completed |
| 85 | // yet, so wait for it, then try again. |
| 86 | __futex_wait_ex(once_control_ptr, 0, old_value, NULL); |
| 87 | } |
| 88 | |
| 89 | // Call the initialization function. |
| 90 | (*init_routine)(); |
| 91 | |
| 92 | // Do a store_release indicating that initialization is complete. |
| 93 | ANDROID_MEMBAR_FULL(); |
| 94 | *once_control_ptr = ONCE_COMPLETED; |
| 95 | |
| 96 | // Wake up any waiters, if any. |
| 97 | __futex_wake_ex(once_control_ptr, 0, INT_MAX); |
| 98 | |
| 99 | return 0; |
| 100 | } |