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

Side by Side Diff: third_party/WebKit/Source/wtf/ThreadingPthreads.cpp

Issue 2524653002: Replace ASSERT with DCHECK in wtf/Threading* (Closed)
Patch Set: Created 4 years 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2007, 2009 Apple Inc. All rights reserved. 2 * Copyright (C) 2007, 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com) 3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com)
4 * Copyright (C) 2011 Research In Motion Limited. All rights reserved. 4 * Copyright (C) 2011 Research In Motion Limited. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 9 *
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 #if OS(LINUX) || OS(ANDROID) 56 #if OS(LINUX) || OS(ANDROID)
57 #include <unistd.h> 57 #include <unistd.h>
58 #endif 58 #endif
59 59
60 namespace WTF { 60 namespace WTF {
61 61
62 static Mutex* atomicallyInitializedStaticMutex; 62 static Mutex* atomicallyInitializedStaticMutex;
63 63
64 void initializeThreading() { 64 void initializeThreading() {
65 // This should only be called once. 65 // This should only be called once.
66 ASSERT(!atomicallyInitializedStaticMutex); 66 DCHECK(!atomicallyInitializedStaticMutex);
67 67
68 // StringImpl::empty() does not construct its static string in a threadsafe 68 // StringImpl::empty() does not construct its static string in a threadsafe
69 // fashion, so ensure it has been initialized from here. 69 // fashion, so ensure it has been initialized from here.
70 StringImpl::empty(); 70 StringImpl::empty();
71 StringImpl::empty16Bit(); 71 StringImpl::empty16Bit();
72 atomicallyInitializedStaticMutex = new Mutex; 72 atomicallyInitializedStaticMutex = new Mutex;
73 wtfThreadData(); 73 wtfThreadData();
74 initializeDates(); 74 initializeDates();
75 // Force initialization of static DoubleToStringConverter converter variable 75 // Force initialization of static DoubleToStringConverter converter variable
76 // inside EcmaScriptConverter function while we are in single thread mode. 76 // inside EcmaScriptConverter function while we are in single thread mode.
77 double_conversion::DoubleToStringConverter::EcmaScriptConverter(); 77 double_conversion::DoubleToStringConverter::EcmaScriptConverter();
78 } 78 }
79 79
80 void lockAtomicallyInitializedStaticMutex() { 80 void lockAtomicallyInitializedStaticMutex() {
81 ASSERT(atomicallyInitializedStaticMutex); 81 DCHECK(atomicallyInitializedStaticMutex);
82 atomicallyInitializedStaticMutex->lock(); 82 atomicallyInitializedStaticMutex->lock();
83 } 83 }
84 84
85 void unlockAtomicallyInitializedStaticMutex() { 85 void unlockAtomicallyInitializedStaticMutex() {
86 atomicallyInitializedStaticMutex->unlock(); 86 atomicallyInitializedStaticMutex->unlock();
87 } 87 }
88 88
89 ThreadIdentifier currentThread() { 89 ThreadIdentifier currentThread() {
90 #if OS(MACOSX) 90 #if OS(MACOSX)
91 return pthread_mach_thread_np(pthread_self()); 91 return pthread_mach_thread_np(pthread_self());
92 #elif OS(LINUX) 92 #elif OS(LINUX)
93 return syscall(__NR_gettid); 93 return syscall(__NR_gettid);
94 #elif OS(ANDROID) 94 #elif OS(ANDROID)
95 return gettid(); 95 return gettid();
96 #else 96 #else
97 return reinterpret_cast<uintptr_t>(pthread_self()); 97 return reinterpret_cast<uintptr_t>(pthread_self());
98 #endif 98 #endif
99 } 99 }
100 100
101 MutexBase::MutexBase(bool recursive) { 101 MutexBase::MutexBase(bool recursive) {
102 pthread_mutexattr_t attr; 102 pthread_mutexattr_t attr;
103 pthread_mutexattr_init(&attr); 103 pthread_mutexattr_init(&attr);
104 pthread_mutexattr_settype( 104 pthread_mutexattr_settype(
105 &attr, recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL); 105 &attr, recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL);
106 106
107 int result = pthread_mutex_init(&m_mutex.m_internalMutex, &attr); 107 int result = pthread_mutex_init(&m_mutex.m_internalMutex, &attr);
108 DCHECK_EQ(result, 0); 108 DCHECK_EQ(result, 0);
109 #if ENABLE(ASSERT) 109 #if DCHECK_IS_ON()
110 m_mutex.m_recursionCount = 0; 110 m_mutex.m_recursionCount = 0;
111 #endif 111 #endif
112 112
113 pthread_mutexattr_destroy(&attr); 113 pthread_mutexattr_destroy(&attr);
114 } 114 }
115 115
116 MutexBase::~MutexBase() { 116 MutexBase::~MutexBase() {
117 int result = pthread_mutex_destroy(&m_mutex.m_internalMutex); 117 int result = pthread_mutex_destroy(&m_mutex.m_internalMutex);
118 DCHECK_EQ(result, 0); 118 DCHECK_EQ(result, 0);
119 } 119 }
120 120
121 void MutexBase::lock() { 121 void MutexBase::lock() {
122 int result = pthread_mutex_lock(&m_mutex.m_internalMutex); 122 int result = pthread_mutex_lock(&m_mutex.m_internalMutex);
123 DCHECK_EQ(result, 0); 123 DCHECK_EQ(result, 0);
124 #if ENABLE(ASSERT) 124 #if DCHECK_IS_ON()
125 ++m_mutex.m_recursionCount; 125 ++m_mutex.m_recursionCount;
126 #endif 126 #endif
127 } 127 }
128 128
129 void MutexBase::unlock() { 129 void MutexBase::unlock() {
130 #if ENABLE(ASSERT) 130 #if DCHECK_IS_ON()
131 ASSERT(m_mutex.m_recursionCount); 131 DCHECK(m_mutex.m_recursionCount);
132 --m_mutex.m_recursionCount; 132 --m_mutex.m_recursionCount;
133 #endif 133 #endif
134 int result = pthread_mutex_unlock(&m_mutex.m_internalMutex); 134 int result = pthread_mutex_unlock(&m_mutex.m_internalMutex);
135 DCHECK_EQ(result, 0); 135 DCHECK_EQ(result, 0);
136 } 136 }
137 137
138 // There is a separate tryLock implementation for the Mutex and the 138 // There is a separate tryLock implementation for the Mutex and the
139 // RecursiveMutex since on Windows we need to manually check if tryLock should 139 // RecursiveMutex since on Windows we need to manually check if tryLock should
140 // succeed or not for the non-recursive mutex. On Linux the two implementations 140 // succeed or not for the non-recursive mutex. On Linux the two implementations
141 // are equal except we can assert the recursion count is always zero for the 141 // are equal except we can assert the recursion count is always zero for the
142 // non-recursive mutex. 142 // non-recursive mutex.
143 bool Mutex::tryLock() { 143 bool Mutex::tryLock() {
144 int result = pthread_mutex_trylock(&m_mutex.m_internalMutex); 144 int result = pthread_mutex_trylock(&m_mutex.m_internalMutex);
145 if (result == 0) { 145 if (result == 0) {
146 #if ENABLE(ASSERT) 146 #if DCHECK_IS_ON()
147 // The Mutex class is not recursive, so the recursionCount should be 147 // The Mutex class is not recursive, so the recursionCount should be
148 // zero after getting the lock. 148 // zero after getting the lock.
149 ASSERT(!m_mutex.m_recursionCount); 149 DCHECK(!m_mutex.m_recursionCount);
150 ++m_mutex.m_recursionCount; 150 ++m_mutex.m_recursionCount;
151 #endif 151 #endif
152 return true; 152 return true;
153 } 153 }
154 if (result == EBUSY) 154 if (result == EBUSY)
155 return false; 155 return false;
156 156
157 ASSERT_NOT_REACHED(); 157 NOTREACHED();
158 return false; 158 return false;
159 } 159 }
160 160
161 bool RecursiveMutex::tryLock() { 161 bool RecursiveMutex::tryLock() {
162 int result = pthread_mutex_trylock(&m_mutex.m_internalMutex); 162 int result = pthread_mutex_trylock(&m_mutex.m_internalMutex);
163 if (result == 0) { 163 if (result == 0) {
164 #if ENABLE(ASSERT) 164 #if DCHECK_IS_ON()
165 ++m_mutex.m_recursionCount; 165 ++m_mutex.m_recursionCount;
166 #endif 166 #endif
167 return true; 167 return true;
168 } 168 }
169 if (result == EBUSY) 169 if (result == EBUSY)
170 return false; 170 return false;
171 171
172 ASSERT_NOT_REACHED(); 172 NOTREACHED();
173 return false; 173 return false;
174 } 174 }
175 175
176 ThreadCondition::ThreadCondition() { 176 ThreadCondition::ThreadCondition() {
177 pthread_cond_init(&m_condition, nullptr); 177 pthread_cond_init(&m_condition, nullptr);
178 } 178 }
179 179
180 ThreadCondition::~ThreadCondition() { 180 ThreadCondition::~ThreadCondition() {
181 pthread_cond_destroy(&m_condition); 181 pthread_cond_destroy(&m_condition);
182 } 182 }
183 183
184 void ThreadCondition::wait(MutexBase& mutex) { 184 void ThreadCondition::wait(MutexBase& mutex) {
185 PlatformMutex& platformMutex = mutex.impl(); 185 PlatformMutex& platformMutex = mutex.impl();
186 int result = pthread_cond_wait(&m_condition, &platformMutex.m_internalMutex); 186 int result = pthread_cond_wait(&m_condition, &platformMutex.m_internalMutex);
187 DCHECK_EQ(result, 0); 187 DCHECK_EQ(result, 0);
188 #if ENABLE(ASSERT) 188 #if DCHECK_IS_ON()
189 ++platformMutex.m_recursionCount; 189 ++platformMutex.m_recursionCount;
190 #endif 190 #endif
191 } 191 }
192 192
193 bool ThreadCondition::timedWait(MutexBase& mutex, double absoluteTime) { 193 bool ThreadCondition::timedWait(MutexBase& mutex, double absoluteTime) {
194 if (absoluteTime < currentTime()) 194 if (absoluteTime < currentTime())
195 return false; 195 return false;
196 196
197 if (absoluteTime > INT_MAX) { 197 if (absoluteTime > INT_MAX) {
198 wait(mutex); 198 wait(mutex);
199 return true; 199 return true;
200 } 200 }
201 201
202 int timeSeconds = static_cast<int>(absoluteTime); 202 int timeSeconds = static_cast<int>(absoluteTime);
203 int timeNanoseconds = static_cast<int>((absoluteTime - timeSeconds) * 1E9); 203 int timeNanoseconds = static_cast<int>((absoluteTime - timeSeconds) * 1E9);
204 204
205 timespec targetTime; 205 timespec targetTime;
206 targetTime.tv_sec = timeSeconds; 206 targetTime.tv_sec = timeSeconds;
207 targetTime.tv_nsec = timeNanoseconds; 207 targetTime.tv_nsec = timeNanoseconds;
208 208
209 PlatformMutex& platformMutex = mutex.impl(); 209 PlatformMutex& platformMutex = mutex.impl();
210 int result = pthread_cond_timedwait( 210 int result = pthread_cond_timedwait(
211 &m_condition, &platformMutex.m_internalMutex, &targetTime); 211 &m_condition, &platformMutex.m_internalMutex, &targetTime);
212 #if ENABLE(ASSERT) 212 #if DCHECK_IS_ON()
213 ++platformMutex.m_recursionCount; 213 ++platformMutex.m_recursionCount;
214 #endif 214 #endif
215 return result == 0; 215 return result == 0;
216 } 216 }
217 217
218 void ThreadCondition::signal() { 218 void ThreadCondition::signal() {
219 int result = pthread_cond_signal(&m_condition); 219 int result = pthread_cond_signal(&m_condition);
220 DCHECK_EQ(result, 0); 220 DCHECK_EQ(result, 0);
221 } 221 }
222 222
223 void ThreadCondition::broadcast() { 223 void ThreadCondition::broadcast() {
224 int result = pthread_cond_broadcast(&m_condition); 224 int result = pthread_cond_broadcast(&m_condition);
225 DCHECK_EQ(result, 0); 225 DCHECK_EQ(result, 0);
226 } 226 }
227 227
228 #if ENABLE(ASSERT) 228 #if DCHECK_IS_ON()
229 static bool s_threadCreated = false; 229 static bool s_threadCreated = false;
230 230
231 bool isAtomicallyInitializedStaticMutexLockHeld() { 231 bool isAtomicallyInitializedStaticMutexLockHeld() {
232 return atomicallyInitializedStaticMutex && 232 return atomicallyInitializedStaticMutex &&
233 atomicallyInitializedStaticMutex->locked(); 233 atomicallyInitializedStaticMutex->locked();
234 } 234 }
235 235
236 bool isBeforeThreadCreated() { 236 bool isBeforeThreadCreated() {
237 return !s_threadCreated; 237 return !s_threadCreated;
238 } 238 }
239 239
240 void willCreateThread() { 240 void willCreateThread() {
241 s_threadCreated = true; 241 s_threadCreated = true;
242 } 242 }
243 #endif 243 #endif
244 244
245 } // namespace WTF 245 } // namespace WTF
246 246
247 #endif // OS(POSIX) 247 #endif // OS(POSIX)
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/Threading.h ('k') | third_party/WebKit/Source/wtf/ThreadingWin.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698