ntdll: Add a test for NtNotifyChangeDirectoryFile.
[wine/multimedia.git] / dlls / ntdll / critsection.c
blobfcc0258dd7b34144d65382b0b2df99250b92eaa3
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 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)
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 #else /* linux */
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; }
99 #endif
101 /***********************************************************************
102 * get_semaphore
104 static inline HANDLE get_semaphore( RTL_CRITICAL_SECTION *crit )
106 HANDLE ret = crit->LockSemaphore;
107 if (!ret)
109 HANDLE sem;
110 if (NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 )) return 0;
111 if (!(ret = (HANDLE)interlocked_cmpxchg_ptr( (PVOID *)&crit->LockSemaphore,
112 (PVOID)sem, 0 )))
113 ret = sem;
114 else
115 NtClose(sem); /* somebody beat us to it */
117 return ret;
120 /***********************************************************************
121 * wait_semaphore
123 static inline NTSTATUS wait_semaphore( RTL_CRITICAL_SECTION *crit, int timeout )
125 if (use_futexes() && crit->DebugInfo) /* debug info is cleared by MakeCriticalSectionGlobal */
127 int val;
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, &timespec ) == -ETIMEDOUT)
137 return STATUS_TIMEOUT;
139 return STATUS_WAIT_0;
141 else
143 HANDLE sem = get_semaphore( crit );
144 LARGE_INTEGER time;
146 time.QuadPart = timeout * (LONGLONG)-10000000;
147 return NtWaitForSingleObject( sem, FALSE, &time );
151 /***********************************************************************
152 * RtlInitializeCriticalSection (NTDLL.@)
154 * Initialises a new critical section.
156 * PARAMS
157 * crit [O] Critical section to initialise
159 * RETURNS
160 * STATUS_SUCCESS.
162 * SEE
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.
177 * PARAMS
178 * crit [O] Critical section to initialise
179 * spincount [I] Spin count for crit
181 * RETURNS
182 * STATUS_SUCCESS.
184 * NOTES
185 * Available on NT4 SP3 or later.
187 * SEE
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));
195 if (crit->DebugInfo)
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.
220 * PARAMS
221 * crit [I/O] Critical section
222 * spincount [I] Spin count for crit
224 * RETURNS
225 * The previous spin count.
227 * NOTES
228 * If the system is not SMP, spincount is ignored and set to 0.
230 * SEE
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;
240 return oldspincount;
243 /***********************************************************************
244 * RtlDeleteCriticalSection (NTDLL.@)
246 * Frees the resources used by a critical section.
248 * PARAMS
249 * crit [I/O] Critical section to free
251 * RETURNS
252 * STATUS_SUCCESS.
254 * SEE
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;
266 if (crit->DebugInfo)
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.
284 * PARAMS
285 * crit [I/O] Critical section to wait for
287 * RETURNS
288 * STATUS_SUCCESS.
290 * NOTES
291 * Use RtlEnterCriticalSection() instead of this function as it is often much
292 * faster.
294 * SEE
295 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
296 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
297 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
299 NTSTATUS WINAPI RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION *crit )
301 for (;;)
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
343 * become free.
345 * PARAMS
346 * crit [I/O] Critical section
348 * RETURNS
349 * Success: STATUS_SUCCESS.
350 * Failure: Any error returned by NtReleaseSemaphore()
352 * NOTES
353 * Use RtlLeaveCriticalSection() instead of this function as it is often much
354 * faster.
356 * SEE
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;
369 else
371 HANDLE sem = get_semaphore( crit );
372 NTSTATUS res = NtReleaseSemaphore( sem, 1, NULL );
373 if (res) RtlRaiseStatus( res );
374 return res;
379 /***********************************************************************
380 * RtlEnterCriticalSection (NTDLL.@)
382 * Enters a critical section, waiting for it to become available if necessary.
384 * PARAMS
385 * crit [I/O] Critical section to enter
387 * RETURNS
388 * STATUS_SUCCESS. The critical section is held by the caller.
390 * SEE
391 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
392 * RtlDeleteCriticalSection(), RtlSetCriticalSectionSpinCount(),
393 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
395 NTSTATUS WINAPI RtlEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
397 if (crit->SpinCount)
399 ULONG count;
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;
409 small_pause();
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 );
424 done:
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.
436 * PARAMS
437 * crit [I/O] Critical section to enter
439 * RETURNS
440 * Success: TRUE. The critical section is held by the caller.
441 * Failure: FALSE. The critical section is currently held by another thread.
443 * SEE
444 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
445 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
446 * RtlLeaveCriticalSection(), RtlSetCriticalSectionSpinCount()
448 BOOL WINAPI RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
450 BOOL ret = FALSE;
451 if (interlocked_cmpxchg( (int *)&crit->LockCount, 0, -1 ) == -1)
453 crit->OwningThread = (HANDLE)GetCurrentThreadId();
454 crit->RecursionCount = 1;
455 ret = TRUE;
457 else if (crit->OwningThread == (HANDLE)GetCurrentThreadId())
459 interlocked_inc( &crit->LockCount );
460 crit->RecursionCount++;
461 ret = TRUE;
463 return ret;
467 /***********************************************************************
468 * RtlLeaveCriticalSection (NTDLL.@)
470 * Leaves a critical section.
472 * PARAMS
473 * crit [I/O] Critical section to leave.
475 * RETURNS
476 * STATUS_SUCCESS.
478 * SEE
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 );
486 else
488 crit->OwningThread = 0;
489 if (interlocked_dec( &crit->LockCount ) >= 0)
491 /* someone is waiting */
492 RtlpUnWaitCriticalSection( crit );
495 return STATUS_SUCCESS;