Disable DumpAccessibilityTreeTest.AccessibilityInputDateTimeLocal on Mac 10.9.
[chromium-blink-merge.git] / base / win / scoped_handle.cc
blob280302169209b637129fe85e6cbbb15fc9854e23
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 "base/win/scoped_handle.h"
7 #include <unordered_map>
9 #include "base/debug/alias.h"
10 #include "base/hash.h"
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/synchronization/lock_impl.h"
15 namespace {
17 struct HandleHash {
18 size_t operator()(const HANDLE& handle) const {
19 char buffer[sizeof(handle)];
20 memcpy(buffer, &handle, sizeof(handle));
21 return base::Hash(buffer, sizeof(buffer));
25 struct Info {
26 const void* owner;
27 const void* pc1;
28 const void* pc2;
29 DWORD thread_id;
31 typedef std::unordered_map<HANDLE, Info, HandleHash> HandleMap;
33 // g_lock protects g_handle_map and g_closing.
34 typedef base::internal::LockImpl NativeLock;
35 base::LazyInstance<NativeLock>::Leaky g_lock = LAZY_INSTANCE_INITIALIZER;
36 base::LazyInstance<HandleMap>::Leaky g_handle_map = LAZY_INSTANCE_INITIALIZER;
37 bool g_closing = false;
39 // g_verifier_enabled is not protected by g_lock because that would require
40 // using the lock (hence, synchornizing multiple threads) even when the
41 // verifier is not in use. Note that this variable is initialized to track all
42 // handles, and it should only move to the disabled state, and never back to
43 // enabled, because that would crash when seeing handles created while the
44 // verifier was disabled. This also implies that it is OK if the value change is
45 // not propagated immediately to all CPUs (as would happen with a lock).
46 bool g_verifier_enabled = true;
48 bool CloseHandleWrapper(HANDLE handle) {
49 if (!::CloseHandle(handle))
50 CHECK(false);
51 return true;
54 // Simple automatic locking using a native critical section so it supports
55 // recursive locking.
56 class AutoNativeLock {
57 public:
58 explicit AutoNativeLock(NativeLock& lock) : lock_(lock) {
59 lock_.Lock();
62 ~AutoNativeLock() {
63 lock_.Unlock();
66 private:
67 NativeLock& lock_;
68 DISALLOW_COPY_AND_ASSIGN(AutoNativeLock);
71 } // namespace
73 namespace base {
74 namespace win {
76 // Static.
77 bool HandleTraits::CloseHandle(HANDLE handle) {
78 if (!g_verifier_enabled)
79 return CloseHandleWrapper(handle);
81 AutoNativeLock lock(g_lock.Get());
82 g_closing = true;
83 CloseHandleWrapper(handle);
84 g_closing = false;
86 return true;
89 // Static.
90 void VerifierTraits::StartTracking(HANDLE handle, const void* owner,
91 const void* pc1, const void* pc2) {
92 if (!g_verifier_enabled)
93 return;
95 // Grab the thread id before the lock.
96 DWORD thread_id = GetCurrentThreadId();
98 AutoNativeLock lock(g_lock.Get());
100 Info handle_info = { owner, pc1, pc2, thread_id };
101 std::pair<HANDLE, Info> item(handle, handle_info);
102 std::pair<HandleMap::iterator, bool> result = g_handle_map.Get().insert(item);
103 if (!result.second) {
104 Info other = result.first->second;
105 debug::Alias(&other);
106 CHECK(false);
110 // Static.
111 void VerifierTraits::StopTracking(HANDLE handle, const void* owner,
112 const void* pc1, const void* pc2) {
113 if (!g_verifier_enabled)
114 return;
116 AutoNativeLock lock(g_lock.Get());
117 HandleMap::iterator i = g_handle_map.Get().find(handle);
118 if (i == g_handle_map.Get().end())
119 CHECK(false);
121 Info other = i->second;
122 if (other.owner != owner) {
123 debug::Alias(&other);
124 CHECK(false);
127 g_handle_map.Get().erase(i);
130 void DisableHandleVerifier() {
131 g_verifier_enabled = false;
134 void OnHandleBeingClosed(HANDLE handle) {
135 AutoNativeLock lock(g_lock.Get());
136 if (g_closing)
137 return;
139 HandleMap::iterator i = g_handle_map.Get().find(handle);
140 if (i == g_handle_map.Get().end())
141 return;
143 Info other = i->second;
144 debug::Alias(&other);
145 CHECK(false);
148 } // namespace win
149 } // namespace base