Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / ntdll / critsection.c
blob284634353b4c525406edaff2829b6b8860a813c6
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include <time.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winternl.h"
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)
52 #ifdef __i386__
53 __asm__ __volatile__( "rep;nop" : : : "memory" );
54 #else
55 __asm__ __volatile__( "" : : : "memory" );
56 #endif
59 #if defined(linux) && defined(__i386__)
61 static inline int futex_wait( int *addr, int val, struct timespec *timeout )
63 int res;
64 __asm__ __volatile__( "xchgl %2,%%ebx\n\t"
65 "int $0x80\n\t"
66 "xchgl %2,%%ebx"
67 : "=a" (res)
68 : "0" (240) /* SYS_futex */, "D" (addr),
69 "c" (0) /* FUTEX_WAIT */, "d" (val), "S" (timeout) );
70 return res;
73 static inline int futex_wake( int *addr, int val )
75 int res;
76 __asm__ __volatile__( "xchgl %2,%%ebx\n\t"
77 "int $0x80\n\t"
78 "xchgl %2,%%ebx"
79 : "=a" (res)
80 : "0" (240) /* SYS_futex */, "D" (addr),
81 "c" (1) /* FUTEX_WAKE */, "d" (val) );
82 return res;
85 static inline int use_futexes(void)
87 static int supported = -1;
89 if (supported == -1) supported = (futex_wait( &supported, 10, NULL ) != -ENOSYS);
90 return supported;
93 static inline NTSTATUS fast_wait( RTL_CRITICAL_SECTION *crit, int timeout )
95 int val;
96 struct timespec timespec;
98 if (!use_futexes()) return STATUS_NOT_IMPLEMENTED;
100 timespec.tv_sec = timeout;
101 timespec.tv_nsec = 0;
102 while ((val = interlocked_cmpxchg( (int *)&crit->LockSemaphore, 0, 1 )) != 1)
104 /* note: this may wait longer than specified in case of signals or */
105 /* multiple wake-ups, but that shouldn't be a problem */
106 if (futex_wait( (int *)&crit->LockSemaphore, val, &timespec ) == -ETIMEDOUT)
107 return STATUS_TIMEOUT;
109 return STATUS_WAIT_0;
112 static inline NTSTATUS fast_wake( RTL_CRITICAL_SECTION *crit )
114 if (!use_futexes()) return STATUS_NOT_IMPLEMENTED;
116 *(int *)&crit->LockSemaphore = 1;
117 futex_wake( (int *)&crit->LockSemaphore, 1 );
118 return STATUS_SUCCESS;
121 static inline void close_semaphore( RTL_CRITICAL_SECTION *crit )
123 if (!use_futexes()) NtClose( crit->LockSemaphore );
126 #elif defined(__APPLE__)
128 #include <mach/mach.h>
129 #include <mach/task.h>
130 #include <mach/semaphore.h>
132 static inline semaphore_t get_mach_semaphore( RTL_CRITICAL_SECTION *crit )
134 semaphore_t ret = *(int *)&crit->LockSemaphore;
135 if (!ret)
137 semaphore_t sem;
138 if (semaphore_create( mach_task_self(), &sem, SYNC_POLICY_FIFO, 0 )) return 0;
139 if (!(ret = interlocked_cmpxchg( (int *)&crit->LockSemaphore, sem, 0 )))
140 ret = sem;
141 else
142 semaphore_destroy( mach_task_self(), sem ); /* somebody beat us to it */
144 return ret;
147 static inline NTSTATUS fast_wait( RTL_CRITICAL_SECTION *crit, int timeout )
149 mach_timespec_t timespec;
150 semaphore_t sem = get_mach_semaphore( crit );
152 timespec.tv_sec = timeout;
153 timespec.tv_nsec = 0;
154 for (;;)
156 switch( semaphore_timedwait( sem, timespec ))
158 case KERN_SUCCESS:
159 return STATUS_WAIT_0;
160 case KERN_ABORTED:
161 continue; /* got a signal, restart */
162 case KERN_OPERATION_TIMED_OUT:
163 return STATUS_TIMEOUT;
164 default:
165 return STATUS_INVALID_HANDLE;
170 static inline NTSTATUS fast_wake( RTL_CRITICAL_SECTION *crit )
172 semaphore_t sem = get_mach_semaphore( crit );
173 semaphore_signal( sem );
174 return STATUS_SUCCESS;
177 static inline void close_semaphore( RTL_CRITICAL_SECTION *crit )
179 semaphore_destroy( mach_task_self(), *(int *)&crit->LockSemaphore );
182 #else /* __APPLE__ */
184 static inline NTSTATUS fast_wait( RTL_CRITICAL_SECTION *crit, int timeout )
186 return STATUS_NOT_IMPLEMENTED;
189 static inline NTSTATUS fast_wake( RTL_CRITICAL_SECTION *crit )
191 return STATUS_NOT_IMPLEMENTED;
194 static inline void close_semaphore( RTL_CRITICAL_SECTION *crit )
196 NtClose( crit->LockSemaphore );
199 #endif
201 /***********************************************************************
202 * get_semaphore
204 static inline HANDLE get_semaphore( RTL_CRITICAL_SECTION *crit )
206 HANDLE ret = crit->LockSemaphore;
207 if (!ret)
209 HANDLE sem;
210 if (NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 )) return 0;
211 if (!(ret = interlocked_cmpxchg_ptr( &crit->LockSemaphore, sem, 0 )))
212 ret = sem;
213 else
214 NtClose(sem); /* somebody beat us to it */
216 return ret;
219 /***********************************************************************
220 * wait_semaphore
222 static inline NTSTATUS wait_semaphore( RTL_CRITICAL_SECTION *crit, int timeout )
224 NTSTATUS ret;
226 /* debug info is cleared by MakeCriticalSectionGlobal */
227 if (!crit->DebugInfo || ((ret = fast_wait( crit, timeout )) == STATUS_NOT_IMPLEMENTED))
229 HANDLE sem = get_semaphore( crit );
230 LARGE_INTEGER time;
232 time.QuadPart = timeout * (LONGLONG)-10000000;
233 ret = NTDLL_wait_for_multiple_objects( 1, &sem, 0, &time, 0 );
235 return ret;
238 /***********************************************************************
239 * RtlInitializeCriticalSection (NTDLL.@)
241 * Initialises a new critical section.
243 * PARAMS
244 * crit [O] Critical section to initialise
246 * RETURNS
247 * STATUS_SUCCESS.
249 * SEE
250 * RtlInitializeCriticalSectionEx(),
251 * RtlInitializeCriticalSectionAndSpinCount(), RtlDeleteCriticalSection(),
252 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
253 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
255 NTSTATUS WINAPI RtlInitializeCriticalSection( RTL_CRITICAL_SECTION *crit )
257 return RtlInitializeCriticalSectionEx( crit, 0, 0 );
260 /***********************************************************************
261 * RtlInitializeCriticalSectionAndSpinCount (NTDLL.@)
263 * Initialises a new critical section with a given spin count.
265 * PARAMS
266 * crit [O] Critical section to initialise
267 * spincount [I] Spin count for crit
269 * RETURNS
270 * STATUS_SUCCESS.
272 * NOTES
273 * Available on NT4 SP3 or later.
275 * SEE
276 * RtlInitializeCriticalSectionEx(),
277 * RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
278 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
279 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
281 NTSTATUS WINAPI RtlInitializeCriticalSectionAndSpinCount( RTL_CRITICAL_SECTION *crit, ULONG spincount )
283 return RtlInitializeCriticalSectionEx( crit, spincount, 0 );
286 /***********************************************************************
287 * RtlInitializeCriticalSectionEx (NTDLL.@)
289 * Initialises a new critical section with a given spin count and flags.
291 * PARAMS
292 * crit [O] Critical section to initialise.
293 * spincount [I] Number of times to spin upon contention.
294 * flags [I] RTL_CRITICAL_SECTION_FLAG_ flags from winnt.h.
296 * RETURNS
297 * STATUS_SUCCESS.
299 * NOTES
300 * Available on Vista or later.
302 * SEE
303 * RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
304 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
305 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
307 NTSTATUS WINAPI RtlInitializeCriticalSectionEx( RTL_CRITICAL_SECTION *crit, ULONG spincount, ULONG flags )
309 if (flags & (RTL_CRITICAL_SECTION_FLAG_DYNAMIC_SPIN|RTL_CRITICAL_SECTION_FLAG_STATIC_INIT))
310 FIXME("(%p,%u,0x%08x) semi-stub\n", crit, spincount, flags);
312 /* FIXME: if RTL_CRITICAL_SECTION_FLAG_STATIC_INIT is given, we should use
313 * memory from a static pool to hold the debug info. Then heap.c could pass
314 * this flag rather than initialising the process heap CS by hand. If this
315 * is done, then debug info should be managed through Rtlp[Allocate|Free]DebugInfo
316 * so (e.g.) MakeCriticalSectionGlobal() doesn't free it using HeapFree().
318 if (flags & RTL_CRITICAL_SECTION_FLAG_NO_DEBUG_INFO)
319 crit->DebugInfo = NULL;
320 else
321 crit->DebugInfo = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(RTL_CRITICAL_SECTION_DEBUG));
323 if (crit->DebugInfo)
325 crit->DebugInfo->Type = 0;
326 crit->DebugInfo->CreatorBackTraceIndex = 0;
327 crit->DebugInfo->CriticalSection = crit;
328 crit->DebugInfo->ProcessLocksList.Blink = &(crit->DebugInfo->ProcessLocksList);
329 crit->DebugInfo->ProcessLocksList.Flink = &(crit->DebugInfo->ProcessLocksList);
330 crit->DebugInfo->EntryCount = 0;
331 crit->DebugInfo->ContentionCount = 0;
332 memset( crit->DebugInfo->Spare, 0, sizeof(crit->DebugInfo->Spare) );
334 crit->LockCount = -1;
335 crit->RecursionCount = 0;
336 crit->OwningThread = 0;
337 crit->LockSemaphore = 0;
338 if (NtCurrentTeb()->Peb->NumberOfProcessors <= 1) spincount = 0;
339 crit->SpinCount = spincount & ~0x80000000;
340 return STATUS_SUCCESS;
343 /***********************************************************************
344 * RtlSetCriticalSectionSpinCount (NTDLL.@)
346 * Sets the spin count of a critical section.
348 * PARAMS
349 * crit [I/O] Critical section
350 * spincount [I] Spin count for crit
352 * RETURNS
353 * The previous spin count.
355 * NOTES
356 * If the system is not SMP, spincount is ignored and set to 0.
358 * SEE
359 * RtlInitializeCriticalSectionEx(),
360 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
361 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
362 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
364 ULONG WINAPI RtlSetCriticalSectionSpinCount( RTL_CRITICAL_SECTION *crit, ULONG spincount )
366 ULONG oldspincount = crit->SpinCount;
367 if (NtCurrentTeb()->Peb->NumberOfProcessors <= 1) spincount = 0;
368 crit->SpinCount = spincount;
369 return oldspincount;
372 /***********************************************************************
373 * RtlDeleteCriticalSection (NTDLL.@)
375 * Frees the resources used by a critical section.
377 * PARAMS
378 * crit [I/O] Critical section to free
380 * RETURNS
381 * STATUS_SUCCESS.
383 * SEE
384 * RtlInitializeCriticalSectionEx(),
385 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
386 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
387 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
389 NTSTATUS WINAPI RtlDeleteCriticalSection( RTL_CRITICAL_SECTION *crit )
391 crit->LockCount = -1;
392 crit->RecursionCount = 0;
393 crit->OwningThread = 0;
394 if (crit->DebugInfo)
396 /* only free the ones we made in here */
397 if (!crit->DebugInfo->Spare[0])
399 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
400 crit->DebugInfo = NULL;
402 close_semaphore( crit );
404 else NtClose( crit->LockSemaphore );
405 crit->LockSemaphore = 0;
406 return STATUS_SUCCESS;
410 /***********************************************************************
411 * RtlpWaitForCriticalSection (NTDLL.@)
413 * Waits for a busy critical section to become free.
415 * PARAMS
416 * crit [I/O] Critical section to wait for
418 * RETURNS
419 * STATUS_SUCCESS.
421 * NOTES
422 * Use RtlEnterCriticalSection() instead of this function as it is often much
423 * faster.
425 * SEE
426 * RtlInitializeCriticalSectionEx(),
427 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
428 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
429 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
431 NTSTATUS WINAPI RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION *crit )
433 for (;;)
435 EXCEPTION_RECORD rec;
436 NTSTATUS status = wait_semaphore( crit, 5 );
438 if ( status == STATUS_TIMEOUT )
440 const char *name = NULL;
441 if (crit->DebugInfo) name = (char *)crit->DebugInfo->Spare[0];
442 if (!name) name = "?";
443 ERR( "section %p %s wait timed out in thread %04x, blocked by %04x, retrying (60 sec)\n",
444 crit, debugstr_a(name), GetCurrentThreadId(), HandleToULong(crit->OwningThread) );
445 status = wait_semaphore( crit, 60 );
446 if ( status == STATUS_TIMEOUT && TRACE_ON(relay) )
448 ERR( "section %p %s wait timed out in thread %04x, blocked by %04x, retrying (5 min)\n",
449 crit, debugstr_a(name), GetCurrentThreadId(), HandleToULong(crit->OwningThread) );
450 status = wait_semaphore( crit, 300 );
453 if (status == STATUS_WAIT_0) break;
455 /* Throw exception only for Wine internal locks */
456 if ((!crit->DebugInfo) || (!crit->DebugInfo->Spare[0])) continue;
458 rec.ExceptionCode = STATUS_POSSIBLE_DEADLOCK;
459 rec.ExceptionFlags = 0;
460 rec.ExceptionRecord = NULL;
461 rec.ExceptionAddress = RtlRaiseException; /* sic */
462 rec.NumberParameters = 1;
463 rec.ExceptionInformation[0] = (ULONG_PTR)crit;
464 RtlRaiseException( &rec );
466 if (crit->DebugInfo) crit->DebugInfo->ContentionCount++;
467 return STATUS_SUCCESS;
471 /***********************************************************************
472 * RtlpUnWaitCriticalSection (NTDLL.@)
474 * Notifies other threads waiting on the busy critical section that it has
475 * become free.
477 * PARAMS
478 * crit [I/O] Critical section
480 * RETURNS
481 * Success: STATUS_SUCCESS.
482 * Failure: Any error returned by NtReleaseSemaphore()
484 * NOTES
485 * Use RtlLeaveCriticalSection() instead of this function as it is often much
486 * faster.
488 * SEE
489 * RtlInitializeCriticalSectionEx(),
490 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
491 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
492 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
494 NTSTATUS WINAPI RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION *crit )
496 NTSTATUS ret;
498 /* debug info is cleared by MakeCriticalSectionGlobal */
499 if (!crit->DebugInfo || ((ret = fast_wake( crit )) == STATUS_NOT_IMPLEMENTED))
501 HANDLE sem = get_semaphore( crit );
502 ret = NtReleaseSemaphore( sem, 1, NULL );
504 if (ret) RtlRaiseStatus( ret );
505 return ret;
509 /***********************************************************************
510 * RtlEnterCriticalSection (NTDLL.@)
512 * Enters a critical section, waiting for it to become available if necessary.
514 * PARAMS
515 * crit [I/O] Critical section to enter
517 * RETURNS
518 * STATUS_SUCCESS. The critical section is held by the caller.
520 * SEE
521 * RtlInitializeCriticalSectionEx(),
522 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
523 * RtlDeleteCriticalSection(), RtlSetCriticalSectionSpinCount(),
524 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
526 NTSTATUS WINAPI RtlEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
528 if (crit->SpinCount)
530 ULONG count;
532 if (RtlTryEnterCriticalSection( crit )) return STATUS_SUCCESS;
533 for (count = crit->SpinCount; count > 0; count--)
535 if (crit->LockCount > 0) break; /* more than one waiter, don't bother spinning */
536 if (crit->LockCount == -1) /* try again */
538 if (interlocked_cmpxchg( &crit->LockCount, 0, -1 ) == -1) goto done;
540 small_pause();
544 if (interlocked_inc( &crit->LockCount ))
546 if (crit->OwningThread == ULongToHandle(GetCurrentThreadId()))
548 crit->RecursionCount++;
549 return STATUS_SUCCESS;
552 /* Now wait for it */
553 RtlpWaitForCriticalSection( crit );
555 done:
556 crit->OwningThread = ULongToHandle(GetCurrentThreadId());
557 crit->RecursionCount = 1;
558 return STATUS_SUCCESS;
562 /***********************************************************************
563 * RtlTryEnterCriticalSection (NTDLL.@)
565 * Tries to enter a critical section without waiting.
567 * PARAMS
568 * crit [I/O] Critical section to enter
570 * RETURNS
571 * Success: TRUE. The critical section is held by the caller.
572 * Failure: FALSE. The critical section is currently held by another thread.
574 * SEE
575 * RtlInitializeCriticalSectionEx(),
576 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
577 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
578 * RtlLeaveCriticalSection(), RtlSetCriticalSectionSpinCount()
580 BOOL WINAPI RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
582 BOOL ret = FALSE;
583 if (interlocked_cmpxchg( &crit->LockCount, 0, -1 ) == -1)
585 crit->OwningThread = ULongToHandle(GetCurrentThreadId());
586 crit->RecursionCount = 1;
587 ret = TRUE;
589 else if (crit->OwningThread == ULongToHandle(GetCurrentThreadId()))
591 interlocked_inc( &crit->LockCount );
592 crit->RecursionCount++;
593 ret = TRUE;
595 return ret;
599 /***********************************************************************
600 * RtlLeaveCriticalSection (NTDLL.@)
602 * Leaves a critical section.
604 * PARAMS
605 * crit [I/O] Critical section to leave.
607 * RETURNS
608 * STATUS_SUCCESS.
610 * SEE
611 * RtlInitializeCriticalSectionEx(),
612 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
613 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
614 * RtlSetCriticalSectionSpinCount(), RtlTryEnterCriticalSection()
616 NTSTATUS WINAPI RtlLeaveCriticalSection( RTL_CRITICAL_SECTION *crit )
618 if (--crit->RecursionCount) interlocked_dec( &crit->LockCount );
619 else
621 crit->OwningThread = 0;
622 if (interlocked_dec( &crit->LockCount ) >= 0)
624 /* someone is waiting */
625 RtlpUnWaitCriticalSection( crit );
628 return STATUS_SUCCESS;