ipc/chromium/src/base/lazy_instance.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 // Copyright (c) 2008 The Chromium 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.
     5 #include "base/lazy_instance.h"
     7 #include "base/at_exit.h"
     8 #include "base/atomicops.h"
     9 #include "base/basictypes.h"
    10 #include "base/platform_thread.h"
    12 namespace base {
    14 void LazyInstanceHelper::EnsureInstance(void* instance,
    15                                         void (*ctor)(void*),
    16                                         void (*dtor)(void*)) {
    17   // Try to create the instance, if we're the first, will go from EMPTY
    18   // to CREATING, otherwise we've already been beaten here.
    19   if (base::subtle::Acquire_CompareAndSwap(
    20           &state_, STATE_EMPTY, STATE_CREATING) == STATE_EMPTY) {
    21     // Created the instance in the space provided by |instance|.
    22     ctor(instance);
    23     // Instance is created, go from CREATING to CREATED.
    24     base::subtle::Release_Store(&state_, STATE_CREATED);
    25     // Register the destructor callback with AtExitManager.
    26     base::AtExitManager::RegisterCallback(dtor, instance);
    27   } else {
    28     // It's either in the process of being created, or already created.  Spin.
    29     while (base::subtle::NoBarrier_Load(&state_) != STATE_CREATED)
    30       PlatformThread::YieldCurrentThread();
    31   }
    32 }
    34 }  // namespace base

mercurial