OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/shared_impl/proxy_lock.h" | 5 #include "ppapi/shared_impl/proxy_lock.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/synchronization/lock.h" | 8 #include "base/synchronization/lock.h" |
9 #include "base/threading/thread_local.h" | 9 #include "base/threading/thread_local.h" |
10 #include "ppapi/shared_impl/ppapi_globals.h" | 10 #include "ppapi/shared_impl/ppapi_globals.h" |
11 | 11 |
12 namespace ppapi { | 12 namespace ppapi { |
13 | 13 |
| 14 base::LazyInstance<base::Lock>::Leaky |
| 15 g_proxy_lock = LAZY_INSTANCE_INITIALIZER; |
| 16 |
| 17 bool g_disable_locking = false; |
| 18 base::LazyInstance<base::ThreadLocalBoolean>::Leaky |
| 19 g_disable_locking_for_thread = LAZY_INSTANCE_INITIALIZER; |
| 20 |
14 // Simple single-thread deadlock detector for the proxy lock. | 21 // Simple single-thread deadlock detector for the proxy lock. |
15 // |true| when the current thread has the lock. | 22 // |true| when the current thread has the lock. |
16 base::LazyInstance<base::ThreadLocalBoolean>::Leaky | 23 base::LazyInstance<base::ThreadLocalBoolean>::Leaky |
17 g_proxy_locked_on_thread = LAZY_INSTANCE_INITIALIZER; | 24 g_proxy_locked_on_thread = LAZY_INSTANCE_INITIALIZER; |
18 | 25 |
19 // static | 26 // static |
| 27 base::Lock* ProxyLock::Get() { |
| 28 if (g_disable_locking || g_disable_locking_for_thread.Get().Get()) |
| 29 return NULL; |
| 30 return &g_proxy_lock.Get(); |
| 31 } |
| 32 |
| 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 |
| 36 // locking). |
| 37 |
| 38 // static |
20 void ProxyLock::Acquire() { | 39 void ProxyLock::Acquire() { |
21 base::Lock* lock(PpapiGlobals::Get()->GetProxyLock()); | 40 base::Lock* lock = Get(); |
22 if (lock) { | 41 if (lock) { |
23 // This thread does not already hold the lock. | 42 // This thread does not already hold the lock. |
24 const bool deadlock = g_proxy_locked_on_thread.Get().Get(); | 43 const bool deadlock = g_proxy_locked_on_thread.Get().Get(); |
25 CHECK(!deadlock); | 44 CHECK(!deadlock); |
26 | 45 |
27 lock->Acquire(); | 46 lock->Acquire(); |
28 g_proxy_locked_on_thread.Get().Set(true); | 47 g_proxy_locked_on_thread.Get().Set(true); |
29 } | 48 } |
30 } | 49 } |
31 | 50 |
32 // static | 51 // static |
33 void ProxyLock::Release() { | 52 void ProxyLock::Release() { |
34 base::Lock* lock(PpapiGlobals::Get()->GetProxyLock()); | 53 base::Lock* lock = Get(); |
35 if (lock) { | 54 if (lock) { |
36 // This thread currently holds the lock. | 55 // This thread currently holds the lock. |
37 const bool locked = g_proxy_locked_on_thread.Get().Get(); | 56 const bool locked = g_proxy_locked_on_thread.Get().Get(); |
38 CHECK(locked); | 57 CHECK(locked); |
39 | 58 |
40 g_proxy_locked_on_thread.Get().Set(false); | 59 g_proxy_locked_on_thread.Get().Set(false); |
41 lock->Release(); | 60 lock->Release(); |
42 } | 61 } |
43 } | 62 } |
44 | 63 |
45 // static | 64 // static |
46 void ProxyLock::AssertAcquired() { | 65 void ProxyLock::AssertAcquired() { |
47 base::Lock* lock(PpapiGlobals::Get()->GetProxyLock()); | 66 base::Lock* lock = Get(); |
48 if (lock) { | 67 if (lock) { |
49 // This thread currently holds the lock. | 68 // This thread currently holds the lock. |
50 const bool locked = g_proxy_locked_on_thread.Get().Get(); | 69 const bool locked = g_proxy_locked_on_thread.Get().Get(); |
51 CHECK(locked); | 70 CHECK(locked); |
52 | 71 |
53 lock->AssertAcquired(); | 72 lock->AssertAcquired(); |
54 } | 73 } |
55 } | 74 } |
56 | 75 |
| 76 // static |
| 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; |
| 81 } |
| 82 |
| 83 // static |
| 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); |
| 88 } |
| 89 |
| 90 // static |
| 91 void ProxyLock::EnableLockingOnThreadForTest() { |
| 92 g_disable_locking_for_thread.Get().Set(false); |
| 93 } |
| 94 |
57 void CallWhileUnlocked(const base::Closure& closure) { | 95 void CallWhileUnlocked(const base::Closure& closure) { |
58 ProxyAutoUnlock lock; | 96 ProxyAutoUnlock lock; |
59 closure.Run(); | 97 closure.Run(); |
60 } | 98 } |
61 | 99 |
62 } // namespace ppapi | 100 } // namespace ppapi |
OLD | NEW |