OLD | NEW |
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 /* This Source Code Form is subject to the terms of the Mozilla Public | 2 /* This Source Code Form is subject to the terms of the Mozilla Public |
3 * License, v. 2.0. If a copy of the MPL was not distributed with this | 3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
5 | 5 |
6 /* | 6 /* |
7 * w95cv.c -- Windows 95 Machine-Dependent Code for Condition Variables | 7 * w95cv.c -- Windows 95 Machine-Dependent Code for Condition Variables |
8 * | 8 * |
9 * We implement our own condition variable wait queue. Each thread | 9 * We implement our own condition variable wait queue. Each thread |
10 * has a semaphore object (thread->md.blocked_sema) to block on while | 10 * has a semaphore object (thread->md.blocked_sema) to block on while |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 md_PostNotifyToCvar(cv, lock, PR_FALSE); | 297 md_PostNotifyToCvar(cv, lock, PR_FALSE); |
298 return; | 298 return; |
299 } | 299 } |
300 | 300 |
301 void _PR_MD_NOTIFYALL_CV(_MDCVar *cv, _MDLock *lock) | 301 void _PR_MD_NOTIFYALL_CV(_MDCVar *cv, _MDLock *lock) |
302 { | 302 { |
303 md_PostNotifyToCvar(cv, lock, PR_TRUE); | 303 md_PostNotifyToCvar(cv, lock, PR_TRUE); |
304 return; | 304 return; |
305 } | 305 } |
306 | 306 |
| 307 typedef BOOL (WINAPI *INITIALIZECRITICALSECTIONEX)( |
| 308 CRITICAL_SECTION *lpCriticalSection, |
| 309 DWORD dwSpinCount, |
| 310 DWORD Flags); |
| 311 |
| 312 static INITIALIZECRITICALSECTIONEX sInitializeCriticalSectionEx; |
| 313 |
| 314 void _PR_MD_INIT_LOCKS(void) |
| 315 { |
| 316 /* |
| 317 * Starting with Windows Vista, every CRITICAL_SECTION allocates an extra |
| 318 * RTL_CRITICAL_SECTION_DEBUG object. Unfortunately, this debug object is |
| 319 * not reclaimed by DeleteCriticalSection(), causing an apparent memory |
| 320 * leak. This is a debugging "feature", not a bug. If we are running on |
| 321 * Vista or later, use InitializeCriticalSectionEx() to allocate |
| 322 * CRITICAL_SECTIONs without debug objects. |
| 323 */ |
| 324 HMODULE hKernel32 = GetModuleHandle("kernel32.dll"); |
| 325 PR_ASSERT(hKernel32); |
| 326 PR_ASSERT(!sInitializeCriticalSectionEx); |
| 327 sInitializeCriticalSectionEx = (INITIALIZECRITICALSECTIONEX) |
| 328 GetProcAddress(hKernel32, "InitializeCriticalSectionEx"); |
| 329 } |
| 330 |
| 331 /* |
| 332 * By default, CRITICAL_SECTIONs are initialized with a spin count of 0. |
| 333 * Joe Duffy's "Concurrent Programming on Windows" book suggests 1500 is |
| 334 * a "reasonable starting point". On single-processor systems, the spin |
| 335 * count is ignored and the critical section spin count is set to 0. |
| 336 */ |
| 337 #define LOCK_SPIN_COUNT 1500 |
| 338 |
| 339 PRStatus _PR_MD_NEW_LOCK(_MDLock *lock) |
| 340 { |
| 341 CRITICAL_SECTION *cs = &lock->mutex; |
| 342 BOOL ok; |
| 343 |
| 344 if (sInitializeCriticalSectionEx) { |
| 345 ok = sInitializeCriticalSectionEx(cs, LOCK_SPIN_COUNT, |
| 346 CRITICAL_SECTION_NO_DEBUG_INFO); |
| 347 } else { |
| 348 ok = InitializeCriticalSectionAndSpinCount(cs, LOCK_SPIN_COUNT); |
| 349 } |
| 350 if (!ok) { |
| 351 _PR_MD_MAP_DEFAULT_ERROR(GetLastError()); |
| 352 return PR_FAILURE; |
| 353 } |
| 354 |
| 355 lock->notified.length = 0; |
| 356 lock->notified.link = NULL; |
| 357 return PR_SUCCESS; |
| 358 } |
| 359 |
307 void _PR_MD_UNLOCK(_MDLock *lock) | 360 void _PR_MD_UNLOCK(_MDLock *lock) |
308 { | 361 { |
309 if (0 != lock->notified.length) { | 362 if (0 != lock->notified.length) { |
310 md_UnlockAndPostNotifies(lock, NULL, NULL); | 363 md_UnlockAndPostNotifies(lock, NULL, NULL); |
311 } else { | 364 } else { |
312 LeaveCriticalSection(&lock->mutex); | 365 LeaveCriticalSection(&lock->mutex); |
313 } | 366 } |
314 return; | |
315 } | 367 } |
OLD | NEW |