15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2012 the V8 project authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Redistribution and use in source and binary forms, with or without 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// modification, are permitted provided that the following conditions are 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// met: 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Redistributions of source code must retain the above copyright 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// notice, this list of conditions and the following disclaimer. 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Redistributions in binary form must reproduce the above 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// copyright notice, this list of conditions and the following 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// disclaimer in the documentation and/or other materials provided 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// with the distribution. 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Neither the name of Google Inc. nor the names of its 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// contributors may be used to endorse or promote products derived 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// from this software without specific prior written permission. 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "once.h" 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef _WIN32 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <windows.h> 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sched.h> 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "atomicops.h" 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "checks.h" 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace v8 { 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace internal { 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void CallOnceImpl(OnceType* once, PointerArgFunction init_func, void* arg) { 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AtomicWord state = Acquire_Load(once); 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Fast path. The provided function was already executed. 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (state == ONCE_STATE_DONE) { 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The function execution did not complete yet. The once object can be in one 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // of the two following states: 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // - UNINITIALIZED: We are the first thread calling this function. 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // - EXECUTING_FUNCTION: Another thread is already executing the function. 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // First, try to change the state from UNINITIALIZED to EXECUTING_FUNCTION 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // atomically. 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) state = Acquire_CompareAndSwap( 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) once, ONCE_STATE_UNINITIALIZED, ONCE_STATE_EXECUTING_FUNCTION); 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (state == ONCE_STATE_UNINITIALIZED) { 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // We are the first thread to call this function, so we have to call the 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // function. 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) init_func(arg); 62 Release_Store(once, ONCE_STATE_DONE); 63 } else { 64 // Another thread has already started executing the function. We need to 65 // wait until it completes the initialization. 66 while (state == ONCE_STATE_EXECUTING_FUNCTION) { 67#ifdef _WIN32 68 ::Sleep(0); 69#else 70 sched_yield(); 71#endif 72 state = Acquire_Load(once); 73 } 74 } 75} 76 77} } // namespace v8::internal 78