Home | History | Annotate | Download | only in bionic
      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 #include <stdatomic.h>
     31 
     32 #include "private/bionic_futex.h"
     33 
     34 #define ONCE_INITIALIZATION_NOT_YET_STARTED   0
     35 #define ONCE_INITIALIZATION_UNDERWAY          1
     36 #define ONCE_INITIALIZATION_COMPLETE          2
     37 
     38 /* NOTE: this implementation doesn't support a init function that throws a C++ exception
     39  *       or calls fork()
     40  */
     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.");
     44 
     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);
     48 
     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);
     55 
     56   while (true) {
     57     if (__predict_true(old_value == ONCE_INITIALIZATION_COMPLETE)) {
     58       return 0;
     59     }
     60 
     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)) {
     66       continue;
     67     }
     68 
     69     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.
     82     __futex_wait_ex(once_control_ptr, 0, old_value, false, nullptr);
     83     old_value = atomic_load_explicit(once_control_ptr, memory_order_acquire);
     84   }
     85 }
     86