1c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes/* 2c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * Copyright (C) 2008 The Android Open Source Project 3c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * All rights reserved. 4c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * 5c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * Redistribution and use in source and binary forms, with or without 6c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * modification, are permitted provided that the following conditions 7c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * are met: 8c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * * Redistributions of source code must retain the above copyright 9c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * notice, this list of conditions and the following disclaimer. 10c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * * Redistributions in binary form must reproduce the above copyright 11c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * notice, this list of conditions and the following disclaimer in 12c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * the documentation and/or other materials provided with the 13c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * distribution. 14c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * 15c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * SUCH DAMAGE. 27c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes */ 28c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 29c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes#include <pthread.h> 30c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 31c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes#include "private/bionic_atomic_inline.h" 32c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes#include "private/bionic_futex.h" 33c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 34c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes#define ONCE_INITIALIZING (1 << 0) 35c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes#define ONCE_COMPLETED (1 << 1) 36c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 37c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes/* NOTE: this implementation doesn't support a init function that throws a C++ exception 38c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * or calls fork() 39c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes */ 40c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesint pthread_once(pthread_once_t* once_control, void (*init_routine)(void)) { 41c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes volatile pthread_once_t* once_control_ptr = once_control; 42c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 43c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // PTHREAD_ONCE_INIT is 0, we use the following bit flags 44c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // bit 0 set -> initialization is under way 45c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // bit 1 set -> initialization is complete 46c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 47c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // First check if the once is already initialized. This will be the common 48c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // case and we want to make this as fast as possible. Note that this still 49c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // requires a load_acquire operation here to ensure that all the 50c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // stores performed by the initialization function are observable on 51c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // this CPU after we exit. 52c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes if (__predict_true((*once_control_ptr & ONCE_COMPLETED) != 0)) { 53c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes ANDROID_MEMBAR_FULL(); 54c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes return 0; 55c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes } 56c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 57c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes while (true) { 58c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // Try to atomically set the INITIALIZING flag. 59c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // This requires a cmpxchg loop, and we may need 60c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // to exit prematurely if we detect that 61c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // COMPLETED is now set. 62c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes int32_t old_value, new_value; 63c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 64c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes do { 65c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes old_value = *once_control_ptr; 66c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes if ((old_value & ONCE_COMPLETED) != 0) { 67c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes break; 68c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes } 69c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 70c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes new_value = old_value | ONCE_INITIALIZING; 71c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes } while (__bionic_cmpxchg(old_value, new_value, once_control_ptr) != 0); 72c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 73c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes if ((old_value & ONCE_COMPLETED) != 0) { 74c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // We detected that COMPLETED was set while in our loop. 75c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes ANDROID_MEMBAR_FULL(); 76c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes return 0; 77c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes } 78c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 79c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes if ((old_value & ONCE_INITIALIZING) == 0) { 80c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // We got there first, we can jump out of the loop to handle the initialization. 81c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes break; 82c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes } 83c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 84c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // Another thread is running the initialization and hasn't completed 85c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // yet, so wait for it, then try again. 86c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes __futex_wait_ex(once_control_ptr, 0, old_value, NULL); 87c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes } 88c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 89c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // Call the initialization function. 90c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes (*init_routine)(); 91c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 92c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // Do a store_release indicating that initialization is complete. 93c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes ANDROID_MEMBAR_FULL(); 94c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes *once_control_ptr = ONCE_COMPLETED; 95c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 96c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // Wake up any waiters, if any. 97c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes __futex_wake_ex(once_control_ptr, 0, INT_MAX); 98c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 99c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes return 0; 100c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes} 101