Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Side by Side Diff: ppapi/shared_impl/proxy_lock.cc

Issue 19492014: PPAPI: Purposely leak ProxyLock, fix shutdown race (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: test cleanup Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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::ThreadLocalBoolean g_disable_locking_for_thread;
19
14 // Simple single-thread deadlock detector for the proxy lock. 20 // Simple single-thread deadlock detector for the proxy lock.
15 // |true| when the current thread has the lock. 21 // |true| when the current thread has the lock.
16 base::LazyInstance<base::ThreadLocalBoolean>::Leaky 22 base::LazyInstance<base::ThreadLocalBoolean>::Leaky
17 g_proxy_locked_on_thread = LAZY_INSTANCE_INITIALIZER; 23 g_proxy_locked_on_thread = LAZY_INSTANCE_INITIALIZER;
18 24
19 // static 25 // static
26 base::Lock* ProxyLock::Get() {
27 if (g_disable_locking || g_disable_locking_for_thread.Get())
28 return NULL;
29 return &g_proxy_lock.Get();
30 }
31
32 // Functions below should only access the lock via Get to ensure that they don't
33 // try to use the lock on the host side of the proxy, where locking is
34 // unnecessary and wrong (because we haven't coded the host side to account for
35 // locking).
36
37 // static
20 void ProxyLock::Acquire() { 38 void ProxyLock::Acquire() {
21 base::Lock* lock(PpapiGlobals::Get()->GetProxyLock()); 39 base::Lock* lock = Get();
22 if (lock) { 40 if (lock) {
23 // This thread does not already hold the lock. 41 // This thread does not already hold the lock.
24 const bool deadlock = g_proxy_locked_on_thread.Get().Get(); 42 const bool deadlock = g_proxy_locked_on_thread.Get().Get();
25 CHECK(!deadlock); 43 CHECK(!deadlock);
26 44
27 lock->Acquire(); 45 lock->Acquire();
28 g_proxy_locked_on_thread.Get().Set(true); 46 g_proxy_locked_on_thread.Get().Set(true);
29 } 47 }
30 } 48 }
31 49
32 // static 50 // static
33 void ProxyLock::Release() { 51 void ProxyLock::Release() {
34 base::Lock* lock(PpapiGlobals::Get()->GetProxyLock()); 52 base::Lock* lock = Get();
35 if (lock) { 53 if (lock) {
36 // This thread currently holds the lock. 54 // This thread currently holds the lock.
37 const bool locked = g_proxy_locked_on_thread.Get().Get(); 55 const bool locked = g_proxy_locked_on_thread.Get().Get();
38 CHECK(locked); 56 CHECK(locked);
39 57
40 g_proxy_locked_on_thread.Get().Set(false); 58 g_proxy_locked_on_thread.Get().Set(false);
41 lock->Release(); 59 lock->Release();
42 } 60 }
43 } 61 }
44 62
45 // static 63 // static
46 void ProxyLock::AssertAcquired() { 64 void ProxyLock::AssertAcquired() {
47 base::Lock* lock(PpapiGlobals::Get()->GetProxyLock()); 65 base::Lock* lock = Get();
48 if (lock) { 66 if (lock) {
49 // This thread currently holds the lock. 67 // This thread currently holds the lock.
50 const bool locked = g_proxy_locked_on_thread.Get().Get(); 68 const bool locked = g_proxy_locked_on_thread.Get().Get();
51 CHECK(locked); 69 CHECK(locked);
52 70
53 lock->AssertAcquired(); 71 lock->AssertAcquired();
54 } 72 }
55 } 73 }
56 74
75 // static
76 void ProxyLock::DisableLocking() {
77 // Note, we don't DCHECK that this flag isn't already set, because multiple
78 // unit tests may run in succession and all set it.
79 g_disable_locking = true;
80 }
81
82 // static
83 void ProxyLock::DisableLockingOnThreadForTest() {
84 // Note, we don't DCHECK that this flag isn't already set, because multiple
85 // unit tests may run in succession and all set it.
86 g_disable_locking_for_thread.Set(true);
87 }
88
89 // static
90 void ProxyLock::EnableLockingOnThreadForTest() {
91 g_disable_locking_for_thread.Set(false);
92 }
93
57 void CallWhileUnlocked(const base::Closure& closure) { 94 void CallWhileUnlocked(const base::Closure& closure) {
58 ProxyAutoUnlock lock; 95 ProxyAutoUnlock lock;
59 closure.Run(); 96 closure.Run();
60 } 97 }
61 98
62 } // namespace ppapi 99 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698