ipc/chromium/src/base/lazy_instance.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/lazy_instance.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,34 @@
     1.4 +// Copyright (c) 2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#include "base/lazy_instance.h"
     1.9 +
    1.10 +#include "base/at_exit.h"
    1.11 +#include "base/atomicops.h"
    1.12 +#include "base/basictypes.h"
    1.13 +#include "base/platform_thread.h"
    1.14 +
    1.15 +namespace base {
    1.16 +
    1.17 +void LazyInstanceHelper::EnsureInstance(void* instance,
    1.18 +                                        void (*ctor)(void*),
    1.19 +                                        void (*dtor)(void*)) {
    1.20 +  // Try to create the instance, if we're the first, will go from EMPTY
    1.21 +  // to CREATING, otherwise we've already been beaten here.
    1.22 +  if (base::subtle::Acquire_CompareAndSwap(
    1.23 +          &state_, STATE_EMPTY, STATE_CREATING) == STATE_EMPTY) {
    1.24 +    // Created the instance in the space provided by |instance|.
    1.25 +    ctor(instance);
    1.26 +    // Instance is created, go from CREATING to CREATED.
    1.27 +    base::subtle::Release_Store(&state_, STATE_CREATED);
    1.28 +    // Register the destructor callback with AtExitManager.
    1.29 +    base::AtExitManager::RegisterCallback(dtor, instance);
    1.30 +  } else {
    1.31 +    // It's either in the process of being created, or already created.  Spin.
    1.32 +    while (base::subtle::NoBarrier_Load(&state_) != STATE_CREATED)
    1.33 +      PlatformThread::YieldCurrentThread();
    1.34 +  }
    1.35 +}
    1.36 +
    1.37 +}  // namespace base

mercurial