Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "base/lazy_instance.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "base/at_exit.h" |
michael@0 | 8 | #include "base/atomicops.h" |
michael@0 | 9 | #include "base/basictypes.h" |
michael@0 | 10 | #include "base/platform_thread.h" |
michael@0 | 11 | |
michael@0 | 12 | namespace base { |
michael@0 | 13 | |
michael@0 | 14 | void LazyInstanceHelper::EnsureInstance(void* instance, |
michael@0 | 15 | void (*ctor)(void*), |
michael@0 | 16 | void (*dtor)(void*)) { |
michael@0 | 17 | // Try to create the instance, if we're the first, will go from EMPTY |
michael@0 | 18 | // to CREATING, otherwise we've already been beaten here. |
michael@0 | 19 | if (base::subtle::Acquire_CompareAndSwap( |
michael@0 | 20 | &state_, STATE_EMPTY, STATE_CREATING) == STATE_EMPTY) { |
michael@0 | 21 | // Created the instance in the space provided by |instance|. |
michael@0 | 22 | ctor(instance); |
michael@0 | 23 | // Instance is created, go from CREATING to CREATED. |
michael@0 | 24 | base::subtle::Release_Store(&state_, STATE_CREATED); |
michael@0 | 25 | // Register the destructor callback with AtExitManager. |
michael@0 | 26 | base::AtExitManager::RegisterCallback(dtor, instance); |
michael@0 | 27 | } else { |
michael@0 | 28 | // It's either in the process of being created, or already created. Spin. |
michael@0 | 29 | while (base::subtle::NoBarrier_Load(&state_) != STATE_CREATED) |
michael@0 | 30 | PlatformThread::YieldCurrentThread(); |
michael@0 | 31 | } |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | } // namespace base |