2 * Win32 critical sections
4 * Copyright 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
28 #include <sys/types.h>
31 #define WIN32_NO_STATUS
34 #include "wine/debug.h"
35 #include "ntdll_misc.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
38 WINE_DECLARE_DEBUG_CHANNEL(relay);
40 static inline LONG interlocked_inc( PLONG dest )
42 return interlocked_xchg_add( dest, 1 ) + 1;
45 static inline LONG interlocked_dec( PLONG dest )
47 return interlocked_xchg_add( dest, -1 ) - 1;
50 static inline void small_pause(void)
53 __asm__ __volatile__( "rep;nop" : : : "memory" );
55 __asm__ __volatile__( "" : : : "memory" );
59 #if defined(linux) && defined(__i386__)
61 static inline int futex_wait( int *addr, int val, struct timespec *timeout )
63 int ret = syscall( 240/*SYS_futex*/, addr, 0/*FUTEX_WAIT*/, val, timeout, 0, 0 );
69 static inline int futex_wake( int *addr, int val )
71 int ret = syscall( 240/*SYS_futex*/, addr, 1/*FUTEX_WAKE*/, val, NULL, 0, 0 );
77 static inline int use_futexes(void)
79 static int supported = -1;
81 if (supported == -1) supported = (futex_wait( &supported, 10, NULL ) != -ENOSYS);
85 static inline NTSTATUS fast_wait( RTL_CRITICAL_SECTION *crit, int timeout )
88 struct timespec timespec;
90 if (!use_futexes()) return STATUS_NOT_IMPLEMENTED;
92 timespec.tv_sec = timeout;
94 while ((val = interlocked_cmpxchg( (int *)&crit->LockSemaphore, 0, 1 )) != 1)
96 /* note: this may wait longer than specified in case of signals or */
97 /* multiple wake-ups, but that shouldn't be a problem */
98 if (futex_wait( (int *)&crit->LockSemaphore, val, ×pec ) == -ETIMEDOUT)
99 return STATUS_TIMEOUT;
101 return STATUS_WAIT_0;
104 static inline NTSTATUS fast_wake( RTL_CRITICAL_SECTION *crit )
106 if (!use_futexes()) return STATUS_NOT_IMPLEMENTED;
108 *(int *)&crit->LockSemaphore = 1;
109 futex_wake( (int *)&crit->LockSemaphore, 1 );
110 return STATUS_SUCCESS;
113 static inline void close_semaphore( RTL_CRITICAL_SECTION *crit )
115 if (!use_futexes()) NtClose( crit->LockSemaphore );
118 #elif defined(__APPLE__)
120 #include <mach/mach.h>
121 #include <mach/task.h>
122 #include <mach/semaphore.h>
124 static inline semaphore_t get_mach_semaphore( RTL_CRITICAL_SECTION *crit )
126 semaphore_t ret = *(int *)&crit->LockSemaphore;
130 if (semaphore_create( mach_task_self(), &sem, SYNC_POLICY_FIFO, 0 )) return 0;
131 if (!(ret = interlocked_cmpxchg( (int *)&crit->LockSemaphore, sem, 0 )))
134 semaphore_destroy( mach_task_self(), sem ); /* somebody beat us to it */
139 static inline NTSTATUS fast_wait( RTL_CRITICAL_SECTION *crit, int timeout )
141 mach_timespec_t timespec;
142 semaphore_t sem = get_mach_semaphore( crit );
144 timespec.tv_sec = timeout;
145 timespec.tv_nsec = 0;
148 switch( semaphore_timedwait( sem, timespec ))
151 return STATUS_WAIT_0;
153 continue; /* got a signal, restart */
154 case KERN_OPERATION_TIMED_OUT:
155 return STATUS_TIMEOUT;
157 return STATUS_INVALID_HANDLE;
162 static inline NTSTATUS fast_wake( RTL_CRITICAL_SECTION *crit )
164 semaphore_t sem = get_mach_semaphore( crit );
165 semaphore_signal( sem );
166 return STATUS_SUCCESS;
169 static inline void close_semaphore( RTL_CRITICAL_SECTION *crit )
171 semaphore_destroy( mach_task_self(), *(int *)&crit->LockSemaphore );
174 #else /* __APPLE__ */
176 static inline NTSTATUS fast_wait( RTL_CRITICAL_SECTION *crit, int timeout )
178 return STATUS_NOT_IMPLEMENTED;
181 static inline NTSTATUS fast_wake( RTL_CRITICAL_SECTION *crit )
183 return STATUS_NOT_IMPLEMENTED;
186 static inline void close_semaphore( RTL_CRITICAL_SECTION *crit )
188 NtClose( crit->LockSemaphore );
193 /***********************************************************************
196 static inline HANDLE get_semaphore( RTL_CRITICAL_SECTION *crit )
198 HANDLE ret = crit->LockSemaphore;
202 if (NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 )) return 0;
203 if (!(ret = interlocked_cmpxchg_ptr( &crit->LockSemaphore, sem, 0 )))
206 NtClose(sem); /* somebody beat us to it */
211 /***********************************************************************
214 static inline NTSTATUS wait_semaphore( RTL_CRITICAL_SECTION *crit, int timeout )
218 /* debug info is cleared by MakeCriticalSectionGlobal */
219 if (!crit->DebugInfo || ((ret = fast_wait( crit, timeout )) == STATUS_NOT_IMPLEMENTED))
221 HANDLE sem = get_semaphore( crit );
224 time.QuadPart = timeout * (LONGLONG)-10000000;
225 ret = NTDLL_wait_for_multiple_objects( 1, &sem, 0, &time, 0 );
230 /***********************************************************************
231 * RtlInitializeCriticalSection (NTDLL.@)
233 * Initialises a new critical section.
236 * crit [O] Critical section to initialise
242 * RtlInitializeCriticalSectionEx(),
243 * RtlInitializeCriticalSectionAndSpinCount(), RtlDeleteCriticalSection(),
244 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
245 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
247 NTSTATUS WINAPI RtlInitializeCriticalSection( RTL_CRITICAL_SECTION *crit )
249 return RtlInitializeCriticalSectionEx( crit, 0, 0 );
252 /***********************************************************************
253 * RtlInitializeCriticalSectionAndSpinCount (NTDLL.@)
255 * Initialises a new critical section with a given spin count.
258 * crit [O] Critical section to initialise
259 * spincount [I] Spin count for crit
265 * Available on NT4 SP3 or later.
268 * RtlInitializeCriticalSectionEx(),
269 * RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
270 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
271 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
273 NTSTATUS WINAPI RtlInitializeCriticalSectionAndSpinCount( RTL_CRITICAL_SECTION *crit, ULONG spincount )
275 return RtlInitializeCriticalSectionEx( crit, spincount, 0 );
278 /***********************************************************************
279 * RtlInitializeCriticalSectionEx (NTDLL.@)
281 * Initialises a new critical section with a given spin count and flags.
284 * crit [O] Critical section to initialise.
285 * spincount [I] Number of times to spin upon contention.
286 * flags [I] RTL_CRITICAL_SECTION_FLAG_ flags from winnt.h.
292 * Available on Vista or later.
295 * RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
296 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
297 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
299 NTSTATUS WINAPI RtlInitializeCriticalSectionEx( RTL_CRITICAL_SECTION *crit, ULONG spincount, ULONG flags )
301 if (flags & (RTL_CRITICAL_SECTION_FLAG_DYNAMIC_SPIN|RTL_CRITICAL_SECTION_FLAG_STATIC_INIT))
302 FIXME("(%p,%u,0x%08x) semi-stub\n", crit, spincount, flags);
304 /* FIXME: if RTL_CRITICAL_SECTION_FLAG_STATIC_INIT is given, we should use
305 * memory from a static pool to hold the debug info. Then heap.c could pass
306 * this flag rather than initialising the process heap CS by hand. If this
307 * is done, then debug info should be managed through Rtlp[Allocate|Free]DebugInfo
308 * so (e.g.) MakeCriticalSectionGlobal() doesn't free it using HeapFree().
310 if (flags & RTL_CRITICAL_SECTION_FLAG_NO_DEBUG_INFO)
311 crit->DebugInfo = NULL;
313 crit->DebugInfo = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(RTL_CRITICAL_SECTION_DEBUG));
317 crit->DebugInfo->Type = 0;
318 crit->DebugInfo->CreatorBackTraceIndex = 0;
319 crit->DebugInfo->CriticalSection = crit;
320 crit->DebugInfo->ProcessLocksList.Blink = &(crit->DebugInfo->ProcessLocksList);
321 crit->DebugInfo->ProcessLocksList.Flink = &(crit->DebugInfo->ProcessLocksList);
322 crit->DebugInfo->EntryCount = 0;
323 crit->DebugInfo->ContentionCount = 0;
324 memset( crit->DebugInfo->Spare, 0, sizeof(crit->DebugInfo->Spare) );
326 crit->LockCount = -1;
327 crit->RecursionCount = 0;
328 crit->OwningThread = 0;
329 crit->LockSemaphore = 0;
330 if (NtCurrentTeb()->Peb->NumberOfProcessors <= 1) spincount = 0;
331 crit->SpinCount = spincount & ~0x80000000;
332 return STATUS_SUCCESS;
335 /***********************************************************************
336 * RtlSetCriticalSectionSpinCount (NTDLL.@)
338 * Sets the spin count of a critical section.
341 * crit [I/O] Critical section
342 * spincount [I] Spin count for crit
345 * The previous spin count.
348 * If the system is not SMP, spincount is ignored and set to 0.
351 * RtlInitializeCriticalSectionEx(),
352 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
353 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
354 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
356 ULONG WINAPI RtlSetCriticalSectionSpinCount( RTL_CRITICAL_SECTION *crit, ULONG spincount )
358 ULONG oldspincount = crit->SpinCount;
359 if (NtCurrentTeb()->Peb->NumberOfProcessors <= 1) spincount = 0;
360 crit->SpinCount = spincount;
364 /***********************************************************************
365 * RtlDeleteCriticalSection (NTDLL.@)
367 * Frees the resources used by a critical section.
370 * crit [I/O] Critical section to free
376 * RtlInitializeCriticalSectionEx(),
377 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
378 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
379 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
381 NTSTATUS WINAPI RtlDeleteCriticalSection( RTL_CRITICAL_SECTION *crit )
383 crit->LockCount = -1;
384 crit->RecursionCount = 0;
385 crit->OwningThread = 0;
388 /* only free the ones we made in here */
389 if (!crit->DebugInfo->Spare[0])
391 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
392 crit->DebugInfo = NULL;
394 close_semaphore( crit );
396 else NtClose( crit->LockSemaphore );
397 crit->LockSemaphore = 0;
398 return STATUS_SUCCESS;
402 /***********************************************************************
403 * RtlpWaitForCriticalSection (NTDLL.@)
405 * Waits for a busy critical section to become free.
408 * crit [I/O] Critical section to wait for
414 * Use RtlEnterCriticalSection() instead of this function as it is often much
418 * RtlInitializeCriticalSectionEx(),
419 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
420 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
421 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
423 NTSTATUS WINAPI RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION *crit )
427 EXCEPTION_RECORD rec;
428 NTSTATUS status = wait_semaphore( crit, 5 );
430 if ( status == STATUS_TIMEOUT )
432 const char *name = NULL;
433 if (crit->DebugInfo) name = (char *)crit->DebugInfo->Spare[0];
434 if (!name) name = "?";
435 ERR( "section %p %s wait timed out in thread %04x, blocked by %04x, retrying (60 sec)\n",
436 crit, debugstr_a(name), GetCurrentThreadId(), HandleToULong(crit->OwningThread) );
437 status = wait_semaphore( crit, 60 );
438 if ( status == STATUS_TIMEOUT && TRACE_ON(relay) )
440 ERR( "section %p %s wait timed out in thread %04x, blocked by %04x, retrying (5 min)\n",
441 crit, debugstr_a(name), GetCurrentThreadId(), HandleToULong(crit->OwningThread) );
442 status = wait_semaphore( crit, 300 );
445 if (status == STATUS_WAIT_0) break;
447 /* Throw exception only for Wine internal locks */
448 if ((!crit->DebugInfo) || (!crit->DebugInfo->Spare[0])) continue;
450 rec.ExceptionCode = STATUS_POSSIBLE_DEADLOCK;
451 rec.ExceptionFlags = 0;
452 rec.ExceptionRecord = NULL;
453 rec.ExceptionAddress = RtlRaiseException; /* sic */
454 rec.NumberParameters = 1;
455 rec.ExceptionInformation[0] = (ULONG_PTR)crit;
456 RtlRaiseException( &rec );
458 if (crit->DebugInfo) crit->DebugInfo->ContentionCount++;
459 return STATUS_SUCCESS;
463 /***********************************************************************
464 * RtlpUnWaitCriticalSection (NTDLL.@)
466 * Notifies other threads waiting on the busy critical section that it has
470 * crit [I/O] Critical section
473 * Success: STATUS_SUCCESS.
474 * Failure: Any error returned by NtReleaseSemaphore()
477 * Use RtlLeaveCriticalSection() instead of this function as it is often much
481 * RtlInitializeCriticalSectionEx(),
482 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
483 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
484 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
486 NTSTATUS WINAPI RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION *crit )
490 /* debug info is cleared by MakeCriticalSectionGlobal */
491 if (!crit->DebugInfo || ((ret = fast_wake( crit )) == STATUS_NOT_IMPLEMENTED))
493 HANDLE sem = get_semaphore( crit );
494 ret = NtReleaseSemaphore( sem, 1, NULL );
496 if (ret) RtlRaiseStatus( ret );
501 /***********************************************************************
502 * RtlEnterCriticalSection (NTDLL.@)
504 * Enters a critical section, waiting for it to become available if necessary.
507 * crit [I/O] Critical section to enter
510 * STATUS_SUCCESS. The critical section is held by the caller.
513 * RtlInitializeCriticalSectionEx(),
514 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
515 * RtlDeleteCriticalSection(), RtlSetCriticalSectionSpinCount(),
516 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
518 NTSTATUS WINAPI RtlEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
524 if (RtlTryEnterCriticalSection( crit )) return STATUS_SUCCESS;
525 for (count = crit->SpinCount; count > 0; count--)
527 if (crit->LockCount > 0) break; /* more than one waiter, don't bother spinning */
528 if (crit->LockCount == -1) /* try again */
530 if (interlocked_cmpxchg( &crit->LockCount, 0, -1 ) == -1) goto done;
536 if (interlocked_inc( &crit->LockCount ))
538 if (crit->OwningThread == ULongToHandle(GetCurrentThreadId()))
540 crit->RecursionCount++;
541 return STATUS_SUCCESS;
544 /* Now wait for it */
545 RtlpWaitForCriticalSection( crit );
548 crit->OwningThread = ULongToHandle(GetCurrentThreadId());
549 crit->RecursionCount = 1;
550 return STATUS_SUCCESS;
554 /***********************************************************************
555 * RtlTryEnterCriticalSection (NTDLL.@)
557 * Tries to enter a critical section without waiting.
560 * crit [I/O] Critical section to enter
563 * Success: TRUE. The critical section is held by the caller.
564 * Failure: FALSE. The critical section is currently held by another thread.
567 * RtlInitializeCriticalSectionEx(),
568 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
569 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
570 * RtlLeaveCriticalSection(), RtlSetCriticalSectionSpinCount()
572 BOOL WINAPI RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
575 if (interlocked_cmpxchg( &crit->LockCount, 0, -1 ) == -1)
577 crit->OwningThread = ULongToHandle(GetCurrentThreadId());
578 crit->RecursionCount = 1;
581 else if (crit->OwningThread == ULongToHandle(GetCurrentThreadId()))
583 interlocked_inc( &crit->LockCount );
584 crit->RecursionCount++;
591 /***********************************************************************
592 * RtlLeaveCriticalSection (NTDLL.@)
594 * Leaves a critical section.
597 * crit [I/O] Critical section to leave.
603 * RtlInitializeCriticalSectionEx(),
604 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
605 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
606 * RtlSetCriticalSectionSpinCount(), RtlTryEnterCriticalSection()
608 NTSTATUS WINAPI RtlLeaveCriticalSection( RTL_CRITICAL_SECTION *crit )
610 if (--crit->RecursionCount) interlocked_dec( &crit->LockCount );
613 crit->OwningThread = 0;
614 if (interlocked_dec( &crit->LockCount ) >= 0)
616 /* someone is waiting */
617 RtlpUnWaitCriticalSection( crit );
620 return STATUS_SUCCESS;