1 // Copyright (c) 2012 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.
5 #include "ppapi/shared_impl/proxy_lock.h"
7 #include "base/lazy_instance.h"
8 #include "base/synchronization/lock.h"
9 #include "base/threading/thread_local.h"
10 #include "ppapi/shared_impl/ppapi_globals.h"
14 base::LazyInstance<base::Lock>::Leaky
15 g_proxy_lock = LAZY_INSTANCE_INITIALIZER;
17 bool g_disable_locking = false;
18 base::LazyInstance<base::ThreadLocalBoolean>::Leaky
19 g_disable_locking_for_thread = LAZY_INSTANCE_INITIALIZER;
21 // Simple single-thread deadlock detector for the proxy lock.
22 // |true| when the current thread has the lock.
23 base::LazyInstance<base::ThreadLocalBoolean>::Leaky
24 g_proxy_locked_on_thread = LAZY_INSTANCE_INITIALIZER;
27 base::Lock* ProxyLock::Get() {
28 if (g_disable_locking || g_disable_locking_for_thread.Get().Get())
30 return &g_proxy_lock.Get();
33 // Functions below should only access the lock via Get to ensure that they don't
34 // try to use the lock on the host side of the proxy, where locking is
35 // unnecessary and wrong (because we haven't coded the host side to account for
39 void ProxyLock::Acquire() {
40 base::Lock* lock = Get();
42 // This thread does not already hold the lock.
43 const bool deadlock = g_proxy_locked_on_thread.Get().Get();
47 g_proxy_locked_on_thread.Get().Set(true);
52 void ProxyLock::Release() {
53 base::Lock* lock = Get();
55 // This thread currently holds the lock.
56 const bool locked = g_proxy_locked_on_thread.Get().Get();
59 g_proxy_locked_on_thread.Get().Set(false);
65 void ProxyLock::AssertAcquired() {
66 base::Lock* lock = Get();
68 // This thread currently holds the lock.
69 const bool locked = g_proxy_locked_on_thread.Get().Get();
72 lock->AssertAcquired();
77 void ProxyLock::DisableLocking() {
78 // Note, we don't DCHECK that this flag isn't already set, because multiple
79 // unit tests may run in succession and all set it.
80 g_disable_locking = true;
84 void ProxyLock::DisableLockingOnThreadForTest() {
85 // Note, we don't DCHECK that this flag isn't already set, because multiple
86 // unit tests may run in succession and all set it.
87 g_disable_locking_for_thread.Get().Set(true);
91 void ProxyLock::EnableLockingOnThreadForTest() {
92 g_disable_locking_for_thread.Get().Set(false);
95 void CallWhileUnlocked(const base::Closure& closure) {