security/sandbox/chromium/base/memory/singleton.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/sandbox/chromium/base/memory/singleton.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,31 @@
     1.4 +// Copyright (c) 2011 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/memory/singleton.h"
     1.9 +#include "base/threading/platform_thread.h"
    1.10 +
    1.11 +namespace base {
    1.12 +namespace internal {
    1.13 +
    1.14 +subtle::AtomicWord WaitForInstance(subtle::AtomicWord* instance) {
    1.15 +  // Handle the race. Another thread beat us and either:
    1.16 +  // - Has the object in BeingCreated state
    1.17 +  // - Already has the object created...
    1.18 +  // We know value != NULL.  It could be kBeingCreatedMarker, or a valid ptr.
    1.19 +  // Unless your constructor can be very time consuming, it is very unlikely
    1.20 +  // to hit this race.  When it does, we just spin and yield the thread until
    1.21 +  // the object has been created.
    1.22 +  subtle::AtomicWord value;
    1.23 +  while (true) {
    1.24 +    value = subtle::NoBarrier_Load(instance);
    1.25 +    if (value != kBeingCreatedMarker)
    1.26 +      break;
    1.27 +    PlatformThread::YieldCurrentThread();
    1.28 +  }
    1.29 +  return value;
    1.30 +}
    1.31 +
    1.32 +}  // namespace internal
    1.33 +}  // namespace base
    1.34 +

mercurial