Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: src/once.cc

Issue 316133002: Move atomic ops and related files to base library (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: updates Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/once.h"
6
7 #ifdef _WIN32
8 #include <windows.h>
9 #else
10 #include <sched.h>
11 #endif
12
13 #include "src/atomicops.h"
14
15 namespace v8 {
16 namespace internal {
17
18 void CallOnceImpl(OnceType* once, PointerArgFunction init_func, void* arg) {
19 AtomicWord state = Acquire_Load(once);
20 // Fast path. The provided function was already executed.
21 if (state == ONCE_STATE_DONE) {
22 return;
23 }
24
25 // The function execution did not complete yet. The once object can be in one
26 // of the two following states:
27 // - UNINITIALIZED: We are the first thread calling this function.
28 // - EXECUTING_FUNCTION: Another thread is already executing the function.
29 //
30 // First, try to change the state from UNINITIALIZED to EXECUTING_FUNCTION
31 // atomically.
32 state = Acquire_CompareAndSwap(
33 once, ONCE_STATE_UNINITIALIZED, ONCE_STATE_EXECUTING_FUNCTION);
34 if (state == ONCE_STATE_UNINITIALIZED) {
35 // We are the first thread to call this function, so we have to call the
36 // function.
37 init_func(arg);
38 Release_Store(once, ONCE_STATE_DONE);
39 } else {
40 // Another thread has already started executing the function. We need to
41 // wait until it completes the initialization.
42 while (state == ONCE_STATE_EXECUTING_FUNCTION) {
43 #ifdef _WIN32
44 ::Sleep(0);
45 #else
46 sched_yield();
47 #endif
48 state = Acquire_Load(once);
49 }
50 }
51 }
52
53 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698