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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 inline static LONG interlocked_inc( PLONG dest )
42 return interlocked_xchg_add( (int *)dest, 1 ) + 1;
45 inline static LONG interlocked_dec( PLONG dest )
47 return interlocked_xchg_add( (int *)dest, -1 ) - 1;
50 inline static 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 )
64 __asm__ __volatile__( "xchgl %2,%%ebx\n\t"
68 : "0" (240) /* SYS_futex */, "D" (addr),
69 "c" (0) /* FUTEX_WAIT */, "d" (val), "S" (timeout) );
73 static inline int futex_wake( int *addr, int val )
76 __asm__ __volatile__( "xchgl %2,%%ebx\n\t"
80 : "0" (240) /* SYS_futex */, "D" (addr),
81 "c" (1) /* FUTEX_WAKE */, "d" (val) );
85 static inline int use_futexes(void)
87 static int supported = -1;
89 if (supported == -1) supported = (futex_wait( &supported, 10, NULL ) != -ENOSYS);
95 static inline int futex_wait( int *addr, int val, struct timespec *timeout ) { return -ENOSYS; }
96 static inline int futex_wake( int *addr, int val ) { return -ENOSYS; }
97 static inline int use_futexes(void) { return 0; }
101 /***********************************************************************
104 static inline HANDLE get_semaphore( RTL_CRITICAL_SECTION *crit )
106 HANDLE ret = crit->LockSemaphore;
110 if (NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 )) return 0;
111 if (!(ret = (HANDLE)interlocked_cmpxchg_ptr( (PVOID *)&crit->LockSemaphore,
115 NtClose(sem); /* somebody beat us to it */
120 /***********************************************************************
123 static inline NTSTATUS wait_semaphore( RTL_CRITICAL_SECTION *crit, int timeout )
125 if (use_futexes() && crit->DebugInfo) /* debug info is cleared by MakeCriticalSectionGlobal */
128 struct timespec timespec;
130 timespec.tv_sec = timeout;
131 timespec.tv_nsec = 0;
132 while ((val = interlocked_cmpxchg( (int *)&crit->LockSemaphore, 0, 1 )) != 1)
134 /* note: this may wait longer than specified in case of signals or */
135 /* multiple wake-ups, but that shouldn't be a problem */
136 if (futex_wait( (int *)&crit->LockSemaphore, val, ×pec ) == -ETIMEDOUT)
137 return STATUS_TIMEOUT;
139 return STATUS_WAIT_0;
143 HANDLE sem = get_semaphore( crit );
146 time.QuadPart = timeout * (LONGLONG)-10000000;
147 return NtWaitForSingleObject( sem, FALSE, &time );
151 /***********************************************************************
152 * RtlInitializeCriticalSection (NTDLL.@)
154 * Initialises a new critical section.
157 * crit [O] Critical section to initialise
163 * RtlInitializeCriticalSectionAndSpinCount(), RtlDeleteCriticalSection(),
164 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
165 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
167 NTSTATUS WINAPI RtlInitializeCriticalSection( RTL_CRITICAL_SECTION *crit )
169 return RtlInitializeCriticalSectionAndSpinCount( crit, 0 );
172 /***********************************************************************
173 * RtlInitializeCriticalSectionAndSpinCount (NTDLL.@)
175 * Initialises a new critical section with a given spin count.
178 * crit [O] Critical section to initialise
179 * spincount [I] Spin count for crit
185 * Available on NT4 SP3 or later.
188 * RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
189 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
190 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
192 NTSTATUS WINAPI RtlInitializeCriticalSectionAndSpinCount( RTL_CRITICAL_SECTION *crit, ULONG spincount )
194 crit->DebugInfo = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(RTL_CRITICAL_SECTION_DEBUG));
197 crit->DebugInfo->Type = 0;
198 crit->DebugInfo->CreatorBackTraceIndex = 0;
199 crit->DebugInfo->CriticalSection = crit;
200 crit->DebugInfo->ProcessLocksList.Blink = &(crit->DebugInfo->ProcessLocksList);
201 crit->DebugInfo->ProcessLocksList.Flink = &(crit->DebugInfo->ProcessLocksList);
202 crit->DebugInfo->EntryCount = 0;
203 crit->DebugInfo->ContentionCount = 0;
204 memset( crit->DebugInfo->Spare, 0, sizeof(crit->DebugInfo->Spare) );
206 crit->LockCount = -1;
207 crit->RecursionCount = 0;
208 crit->OwningThread = 0;
209 crit->LockSemaphore = 0;
210 if (NtCurrentTeb()->Peb->NumberOfProcessors <= 1) spincount = 0;
211 crit->SpinCount = spincount & ~0x80000000;
212 return STATUS_SUCCESS;
215 /***********************************************************************
216 * RtlSetCriticalSectionSpinCount (NTDLL.@)
218 * Sets the spin count of a critical section.
221 * crit [I/O] Critical section
222 * spincount [I] Spin count for crit
225 * The previous spin count.
228 * If the system is not SMP, spincount is ignored and set to 0.
231 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
232 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
233 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
235 ULONG WINAPI RtlSetCriticalSectionSpinCount( RTL_CRITICAL_SECTION *crit, ULONG spincount )
237 ULONG oldspincount = crit->SpinCount;
238 if (NtCurrentTeb()->Peb->NumberOfProcessors <= 1) spincount = 0;
239 crit->SpinCount = spincount;
243 /***********************************************************************
244 * RtlDeleteCriticalSection (NTDLL.@)
246 * Frees the resources used by a critical section.
249 * crit [I/O] Critical section to free
255 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
256 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
257 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
259 NTSTATUS WINAPI RtlDeleteCriticalSection( RTL_CRITICAL_SECTION *crit )
261 crit->LockCount = -1;
262 crit->RecursionCount = 0;
263 crit->OwningThread = 0;
264 if (crit->LockSemaphore) NtClose( crit->LockSemaphore );
265 crit->LockSemaphore = 0;
268 /* only free the ones we made in here */
269 if (!crit->DebugInfo->Spare[0])
271 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
272 crit->DebugInfo = NULL;
275 return STATUS_SUCCESS;
279 /***********************************************************************
280 * RtlpWaitForCriticalSection (NTDLL.@)
282 * Waits for a busy critical section to become free.
285 * crit [I/O] Critical section to wait for
291 * Use RtlEnterCriticalSection() instead of this function as it is often much
295 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
296 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
297 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
299 NTSTATUS WINAPI RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION *crit )
303 EXCEPTION_RECORD rec;
304 NTSTATUS status = wait_semaphore( crit, 5 );
306 if ( status == STATUS_TIMEOUT )
308 const char *name = NULL;
309 if (crit->DebugInfo) name = (char *)crit->DebugInfo->Spare[0];
310 if (!name) name = "?";
311 ERR( "section %p %s wait timed out in thread %04lx, blocked by %04lx, retrying (60 sec)\n",
312 crit, debugstr_a(name), GetCurrentThreadId(), (DWORD)crit->OwningThread );
313 status = wait_semaphore( crit, 60 );
314 if ( status == STATUS_TIMEOUT && TRACE_ON(relay) )
316 ERR( "section %p %s wait timed out in thread %04lx, blocked by %04lx, retrying (5 min)\n",
317 crit, debugstr_a(name), GetCurrentThreadId(), (DWORD) crit->OwningThread );
318 status = wait_semaphore( crit, 300 );
321 if (status == STATUS_WAIT_0) break;
323 /* Throw exception only for Wine internal locks */
324 if ((!crit->DebugInfo) || (!crit->DebugInfo->Spare[0])) continue;
326 rec.ExceptionCode = STATUS_POSSIBLE_DEADLOCK;
327 rec.ExceptionFlags = 0;
328 rec.ExceptionRecord = NULL;
329 rec.ExceptionAddress = RtlRaiseException; /* sic */
330 rec.NumberParameters = 1;
331 rec.ExceptionInformation[0] = (ULONG_PTR)crit;
332 RtlRaiseException( &rec );
334 if (crit->DebugInfo) crit->DebugInfo->ContentionCount++;
335 return STATUS_SUCCESS;
339 /***********************************************************************
340 * RtlpUnWaitCriticalSection (NTDLL.@)
342 * Notifies other threads waiting on the busy critical section that it has
346 * crit [I/O] Critical section
349 * Success: STATUS_SUCCESS.
350 * Failure: Any error returned by NtReleaseSemaphore()
353 * Use RtlLeaveCriticalSection() instead of this function as it is often much
357 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
358 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
359 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
361 NTSTATUS WINAPI RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION *crit )
363 if (use_futexes() && crit->DebugInfo) /* debug info is cleared by MakeCriticalSectionGlobal */
365 *(int *)&crit->LockSemaphore = 1;
366 futex_wake( (int *)&crit->LockSemaphore, 1 );
367 return STATUS_SUCCESS;
371 HANDLE sem = get_semaphore( crit );
372 NTSTATUS res = NtReleaseSemaphore( sem, 1, NULL );
373 if (res) RtlRaiseStatus( res );
379 /***********************************************************************
380 * RtlEnterCriticalSection (NTDLL.@)
382 * Enters a critical section, waiting for it to become available if necessary.
385 * crit [I/O] Critical section to enter
388 * STATUS_SUCCESS. The critical section is held by the caller.
391 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
392 * RtlDeleteCriticalSection(), RtlSetCriticalSectionSpinCount(),
393 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
395 NTSTATUS WINAPI RtlEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
401 if (RtlTryEnterCriticalSection( crit )) return STATUS_SUCCESS;
402 for (count = crit->SpinCount; count > 0; count--)
404 if (crit->LockCount > 0) break; /* more than one waiter, don't bother spinning */
405 if (crit->LockCount == -1) /* try again */
407 if (interlocked_cmpxchg( (int *)&crit->LockCount, 0, -1 ) == -1) goto done;
413 if (interlocked_inc( &crit->LockCount ))
415 if (crit->OwningThread == (HANDLE)GetCurrentThreadId())
417 crit->RecursionCount++;
418 return STATUS_SUCCESS;
421 /* Now wait for it */
422 RtlpWaitForCriticalSection( crit );
425 crit->OwningThread = (HANDLE)GetCurrentThreadId();
426 crit->RecursionCount = 1;
427 return STATUS_SUCCESS;
431 /***********************************************************************
432 * RtlTryEnterCriticalSection (NTDLL.@)
434 * Tries to enter a critical section without waiting.
437 * crit [I/O] Critical section to enter
440 * Success: TRUE. The critical section is held by the caller.
441 * Failure: FALSE. The critical section is currently held by another thread.
444 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
445 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
446 * RtlLeaveCriticalSection(), RtlSetCriticalSectionSpinCount()
448 BOOL WINAPI RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
451 if (interlocked_cmpxchg( (int *)&crit->LockCount, 0, -1 ) == -1)
453 crit->OwningThread = (HANDLE)GetCurrentThreadId();
454 crit->RecursionCount = 1;
457 else if (crit->OwningThread == (HANDLE)GetCurrentThreadId())
459 interlocked_inc( &crit->LockCount );
460 crit->RecursionCount++;
467 /***********************************************************************
468 * RtlLeaveCriticalSection (NTDLL.@)
470 * Leaves a critical section.
473 * crit [I/O] Critical section to leave.
479 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
480 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
481 * RtlSetCriticalSectionSpinCount(), RtlTryEnterCriticalSection()
483 NTSTATUS WINAPI RtlLeaveCriticalSection( RTL_CRITICAL_SECTION *crit )
485 if (--crit->RecursionCount) interlocked_dec( &crit->LockCount );
488 crit->OwningThread = 0;
489 if (interlocked_dec( &crit->LockCount ) >= 0)
491 /* someone is waiting */
492 RtlpUnWaitCriticalSection( crit );
495 return STATUS_SUCCESS;