blob: f68ecaa8da9b9a494ab0bcdbc600084b9ed9938e [file] [log] [blame]
joth@chromium.orgc4285cd2011-11-07 20:11:51 +09001// Copyright (c) 2011 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.
4
5#include "base/memory/singleton.h"
6#include "base/threading/platform_thread.h"
7
8namespace base {
9namespace internal {
10
11subtle::AtomicWord WaitForInstance(subtle::AtomicWord* instance) {
12 // Handle the race. Another thread beat us and either:
13 // - Has the object in BeingCreated state
14 // - Already has the object created...
15 // We know value != NULL. It could be kBeingCreatedMarker, or a valid ptr.
16 // Unless your constructor can be very time consuming, it is very unlikely
17 // to hit this race. When it does, we just spin and yield the thread until
18 // the object has been created.
19 subtle::AtomicWord value;
20 while (true) {
glider@chromium.org2ac79e02014-03-07 17:51:32 +090021 // The load has acquire memory ordering as the thread which reads the
22 // instance pointer must acquire visibility over the associated data.
23 // The pairing Release_Store operation is in Singleton::get().
24 value = subtle::Acquire_Load(instance);
joth@chromium.orgc4285cd2011-11-07 20:11:51 +090025 if (value != kBeingCreatedMarker)
26 break;
27 PlatformThread::YieldCurrentThread();
28 }
29 return value;
30}
31
32} // namespace internal
33} // namespace base
34