michael@0: // Copyright (c) 2011 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/memory/singleton.h" michael@0: #include "base/threading/platform_thread.h" michael@0: michael@0: namespace base { michael@0: namespace internal { michael@0: michael@0: subtle::AtomicWord WaitForInstance(subtle::AtomicWord* instance) { michael@0: // Handle the race. Another thread beat us and either: michael@0: // - Has the object in BeingCreated state michael@0: // - Already has the object created... michael@0: // We know value != NULL. It could be kBeingCreatedMarker, or a valid ptr. michael@0: // Unless your constructor can be very time consuming, it is very unlikely michael@0: // to hit this race. When it does, we just spin and yield the thread until michael@0: // the object has been created. michael@0: subtle::AtomicWord value; michael@0: while (true) { michael@0: value = subtle::NoBarrier_Load(instance); michael@0: if (value != kBeingCreatedMarker) michael@0: break; michael@0: PlatformThread::YieldCurrentThread(); michael@0: } michael@0: return value; michael@0: } michael@0: michael@0: } // namespace internal michael@0: } // namespace base michael@0: