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

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32

michael@0 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "base/memory/singleton.h"
michael@0 6 #include "base/threading/platform_thread.h"
michael@0 7
michael@0 8 namespace base {
michael@0 9 namespace internal {
michael@0 10
michael@0 11 subtle::AtomicWord WaitForInstance(subtle::AtomicWord* instance) {
michael@0 12 // Handle the race. Another thread beat us and either:
michael@0 13 // - Has the object in BeingCreated state
michael@0 14 // - Already has the object created...
michael@0 15 // We know value != NULL. It could be kBeingCreatedMarker, or a valid ptr.
michael@0 16 // Unless your constructor can be very time consuming, it is very unlikely
michael@0 17 // to hit this race. When it does, we just spin and yield the thread until
michael@0 18 // the object has been created.
michael@0 19 subtle::AtomicWord value;
michael@0 20 while (true) {
michael@0 21 value = subtle::NoBarrier_Load(instance);
michael@0 22 if (value != kBeingCreatedMarker)
michael@0 23 break;
michael@0 24 PlatformThread::YieldCurrentThread();
michael@0 25 }
michael@0 26 return value;
michael@0 27 }
michael@0 28
michael@0 29 } // namespace internal
michael@0 30 } // namespace base
michael@0 31

mercurial