ntdll: Avoid one warning around assert(0).
[wine] / dlls / ntdll / critsection.c
1 /*
2  * Win32 critical sections
3  *
4  * Copyright 1998 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
38 WINE_DECLARE_DEBUG_CHANNEL(relay);
39
40 static inline LONG interlocked_inc( PLONG dest )
41 {
42     return interlocked_xchg_add( dest, 1 ) + 1;
43 }
44
45 static inline LONG interlocked_dec( PLONG dest )
46 {
47     return interlocked_xchg_add( dest, -1 ) - 1;
48 }
49
50 static inline void small_pause(void)
51 {
52 #ifdef __i386__
53     __asm__ __volatile__( "rep;nop" : : : "memory" );
54 #else
55     __asm__ __volatile__( "" : : : "memory" );
56 #endif
57 }
58
59 #if defined(linux) && defined(__i386__)
60
61 static inline int futex_wait( int *addr, int val, struct timespec *timeout )
62 {
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;
71 }
72
73 static inline int futex_wake( int *addr, int val )
74 {
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;
83 }
84
85 static inline int use_futexes(void)
86 {
87     static int supported = -1;
88
89     if (supported == -1) supported = (futex_wait( &supported, 10, NULL ) != -ENOSYS);
90     return supported;
91 }
92
93 static inline NTSTATUS fast_wait( RTL_CRITICAL_SECTION *crit, int timeout )
94 {
95     int val;
96     struct timespec timespec;
97
98     if (!use_futexes()) return STATUS_NOT_IMPLEMENTED;
99
100     timespec.tv_sec  = timeout;
101     timespec.tv_nsec = 0;
102     while ((val = interlocked_cmpxchg( (int *)&crit->LockSemaphore, 0, 1 )) != 1)
103     {
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;
108     }
109     return STATUS_WAIT_0;
110 }
111
112 static inline NTSTATUS fast_wake( RTL_CRITICAL_SECTION *crit )
113 {
114     if (!use_futexes()) return STATUS_NOT_IMPLEMENTED;
115
116     *(int *)&crit->LockSemaphore = 1;
117     futex_wake( (int *)&crit->LockSemaphore, 1 );
118     return STATUS_SUCCESS;
119 }
120
121 static inline void close_semaphore( RTL_CRITICAL_SECTION *crit )
122 {
123     if (!use_futexes()) NtClose( crit->LockSemaphore );
124 }
125
126 #elif defined(__APPLE__)
127
128 #include <mach/mach.h>
129 #include <mach/task.h>
130 #include <mach/semaphore.h>
131
132 static inline semaphore_t get_mach_semaphore( RTL_CRITICAL_SECTION *crit )
133 {
134     semaphore_t ret = *(int *)&crit->LockSemaphore;
135     if (!ret)
136     {
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 */
143     }
144     return ret;
145 }
146
147 static inline NTSTATUS fast_wait( RTL_CRITICAL_SECTION *crit, int timeout )
148 {
149     mach_timespec_t timespec;
150     semaphore_t sem = get_mach_semaphore( crit );
151
152     timespec.tv_sec = timeout;
153     timespec.tv_nsec = 0;
154     for (;;)
155     {
156         switch( semaphore_timedwait( sem, timespec ))
157         {
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;
166         }
167     }
168 }
169
170 static inline NTSTATUS fast_wake( RTL_CRITICAL_SECTION *crit )
171 {
172     semaphore_t sem = get_mach_semaphore( crit );
173     semaphore_signal( sem );
174     return STATUS_SUCCESS;
175 }
176
177 static inline void close_semaphore( RTL_CRITICAL_SECTION *crit )
178 {
179     semaphore_destroy( mach_task_self(), *(int *)&crit->LockSemaphore );
180 }
181
182 #else  /* __APPLE__ */
183
184 static inline NTSTATUS fast_wait( RTL_CRITICAL_SECTION *crit, int timeout )
185 {
186     return STATUS_NOT_IMPLEMENTED;
187 }
188
189 static inline NTSTATUS fast_wake( RTL_CRITICAL_SECTION *crit )
190 {
191     return STATUS_NOT_IMPLEMENTED;
192 }
193
194 static inline void close_semaphore( RTL_CRITICAL_SECTION *crit )
195 {
196     NtClose( crit->LockSemaphore );
197 }
198
199 #endif
200
201 /***********************************************************************
202  *           get_semaphore
203  */
204 static inline HANDLE get_semaphore( RTL_CRITICAL_SECTION *crit )
205 {
206     HANDLE ret = crit->LockSemaphore;
207     if (!ret)
208     {
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 */
215     }
216     return ret;
217 }
218
219 /***********************************************************************
220  *           wait_semaphore
221  */
222 static inline NTSTATUS wait_semaphore( RTL_CRITICAL_SECTION *crit, int timeout )
223 {
224     NTSTATUS ret;
225
226     /* debug info is cleared by MakeCriticalSectionGlobal */
227     if (!crit->DebugInfo || ((ret = fast_wait( crit, timeout )) == STATUS_NOT_IMPLEMENTED))
228     {
229         HANDLE sem = get_semaphore( crit );
230         LARGE_INTEGER time;
231
232         time.QuadPart = timeout * (LONGLONG)-10000000;
233         ret = NTDLL_wait_for_multiple_objects( 1, &sem, 0, &time, 0 );
234     }
235     return ret;
236 }
237
238 /***********************************************************************
239  *           RtlInitializeCriticalSection   (NTDLL.@)
240  *
241  * Initialises a new critical section.
242  *
243  * PARAMS
244  *  crit [O] Critical section to initialise
245  *
246  * RETURNS
247  *  STATUS_SUCCESS.
248  *
249  * SEE
250  *  RtlInitializeCriticalSectionEx(),
251  *  RtlInitializeCriticalSectionAndSpinCount(), RtlDeleteCriticalSection(),
252  *  RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
253  *  RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
254  */
255 NTSTATUS WINAPI RtlInitializeCriticalSection( RTL_CRITICAL_SECTION *crit )
256 {
257     return RtlInitializeCriticalSectionEx( crit, 0, 0 );
258 }
259
260 /***********************************************************************
261  *           RtlInitializeCriticalSectionAndSpinCount   (NTDLL.@)
262  *
263  * Initialises a new critical section with a given spin count.
264  *
265  * PARAMS
266  *   crit      [O] Critical section to initialise
267  *   spincount [I] Spin count for crit
268  * 
269  * RETURNS
270  *  STATUS_SUCCESS.
271  *
272  * NOTES
273  *  Available on NT4 SP3 or later.
274  *
275  * SEE
276  *  RtlInitializeCriticalSectionEx(),
277  *  RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
278  *  RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
279  *  RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
280  */
281 NTSTATUS WINAPI RtlInitializeCriticalSectionAndSpinCount( RTL_CRITICAL_SECTION *crit, ULONG spincount )
282 {
283     return RtlInitializeCriticalSectionEx( crit, spincount, 0 );
284 }
285
286 /***********************************************************************
287  *           RtlInitializeCriticalSectionEx   (NTDLL.@)
288  *
289  * Initialises a new critical section with a given spin count and flags.
290  *
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.
295  *
296  * RETURNS
297  *  STATUS_SUCCESS.
298  *
299  * NOTES
300  *  Available on Vista or later.
301  *
302  * SEE
303  *  RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
304  *  RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
305  *  RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
306  */
307 NTSTATUS WINAPI RtlInitializeCriticalSectionEx( RTL_CRITICAL_SECTION *crit, ULONG spincount, ULONG flags )
308 {
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);
311
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().
317      */
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));
322
323     if (crit->DebugInfo)
324     {
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) );
333     }
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;
341 }
342
343 /***********************************************************************
344  *           RtlSetCriticalSectionSpinCount   (NTDLL.@)
345  *
346  * Sets the spin count of a critical section.
347  *
348  * PARAMS
349  *   crit      [I/O] Critical section
350  *   spincount [I] Spin count for crit
351  *
352  * RETURNS
353  *  The previous spin count.
354  *
355  * NOTES
356  *  If the system is not SMP, spincount is ignored and set to 0.
357  *
358  * SEE
359  *  RtlInitializeCriticalSectionEx(),
360  *  RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
361  *  RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
362  *  RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
363  */
364 ULONG WINAPI RtlSetCriticalSectionSpinCount( RTL_CRITICAL_SECTION *crit, ULONG spincount )
365 {
366     ULONG oldspincount = crit->SpinCount;
367     if (NtCurrentTeb()->Peb->NumberOfProcessors <= 1) spincount = 0;
368     crit->SpinCount = spincount;
369     return oldspincount;
370 }
371
372 /***********************************************************************
373  *           RtlDeleteCriticalSection   (NTDLL.@)
374  *
375  * Frees the resources used by a critical section.
376  *
377  * PARAMS
378  *  crit [I/O] Critical section to free
379  *
380  * RETURNS
381  *  STATUS_SUCCESS.
382  *
383  * SEE
384  *  RtlInitializeCriticalSectionEx(),
385  *  RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
386  *  RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
387  *  RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
388  */
389 NTSTATUS WINAPI RtlDeleteCriticalSection( RTL_CRITICAL_SECTION *crit )
390 {
391     crit->LockCount      = -1;
392     crit->RecursionCount = 0;
393     crit->OwningThread   = 0;
394     if (crit->DebugInfo)
395     {
396         /* only free the ones we made in here */
397         if (!crit->DebugInfo->Spare[0])
398         {
399             RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
400             crit->DebugInfo = NULL;
401         }
402         close_semaphore( crit );
403     }
404     else NtClose( crit->LockSemaphore );
405     crit->LockSemaphore = 0;
406     return STATUS_SUCCESS;
407 }
408
409
410 /***********************************************************************
411  *           RtlpWaitForCriticalSection   (NTDLL.@)
412  *
413  * Waits for a busy critical section to become free.
414  * 
415  * PARAMS
416  *  crit [I/O] Critical section to wait for
417  *
418  * RETURNS
419  *  STATUS_SUCCESS.
420  *
421  * NOTES
422  *  Use RtlEnterCriticalSection() instead of this function as it is often much
423  *  faster.
424  *
425  * SEE
426  *  RtlInitializeCriticalSectionEx(),
427  *  RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
428  *  RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
429  *  RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
430  */
431 NTSTATUS WINAPI RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION *crit )
432 {
433     for (;;)
434     {
435         EXCEPTION_RECORD rec;
436         NTSTATUS status = wait_semaphore( crit, 5 );
437
438         if ( status == STATUS_TIMEOUT )
439         {
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) )
447             {
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 );
451             }
452         }
453         if (status == STATUS_WAIT_0) break;
454
455         /* Throw exception only for Wine internal locks */
456         if ((!crit->DebugInfo) || (!crit->DebugInfo->Spare[0])) continue;
457
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 );
465     }
466     if (crit->DebugInfo) crit->DebugInfo->ContentionCount++;
467     return STATUS_SUCCESS;
468 }
469
470
471 /***********************************************************************
472  *           RtlpUnWaitCriticalSection   (NTDLL.@)
473  *
474  * Notifies other threads waiting on the busy critical section that it has
475  * become free.
476  * 
477  * PARAMS
478  *  crit [I/O] Critical section
479  *
480  * RETURNS
481  *  Success: STATUS_SUCCESS.
482  *  Failure: Any error returned by NtReleaseSemaphore()
483  *
484  * NOTES
485  *  Use RtlLeaveCriticalSection() instead of this function as it is often much
486  *  faster.
487  *
488  * SEE
489  *  RtlInitializeCriticalSectionEx(),
490  *  RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
491  *  RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
492  *  RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
493  */
494 NTSTATUS WINAPI RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION *crit )
495 {
496     NTSTATUS ret;
497
498     /* debug info is cleared by MakeCriticalSectionGlobal */
499     if (!crit->DebugInfo || ((ret = fast_wake( crit )) == STATUS_NOT_IMPLEMENTED))
500     {
501         HANDLE sem = get_semaphore( crit );
502         ret = NtReleaseSemaphore( sem, 1, NULL );
503     }
504     if (ret) RtlRaiseStatus( ret );
505     return ret;
506 }
507
508
509 /***********************************************************************
510  *           RtlEnterCriticalSection   (NTDLL.@)
511  *
512  * Enters a critical section, waiting for it to become available if necessary.
513  *
514  * PARAMS
515  *  crit [I/O] Critical section to enter
516  *
517  * RETURNS
518  *  STATUS_SUCCESS. The critical section is held by the caller.
519  *  
520  * SEE
521  *  RtlInitializeCriticalSectionEx(),
522  *  RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
523  *  RtlDeleteCriticalSection(), RtlSetCriticalSectionSpinCount(),
524  *  RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
525  */
526 NTSTATUS WINAPI RtlEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
527 {
528     if (crit->SpinCount)
529     {
530         ULONG count;
531
532         if (RtlTryEnterCriticalSection( crit )) return STATUS_SUCCESS;
533         for (count = crit->SpinCount; count > 0; count--)
534         {
535             if (crit->LockCount > 0) break;  /* more than one waiter, don't bother spinning */
536             if (crit->LockCount == -1)       /* try again */
537             {
538                 if (interlocked_cmpxchg( &crit->LockCount, 0, -1 ) == -1) goto done;
539             }
540             small_pause();
541         }
542     }
543
544     if (interlocked_inc( &crit->LockCount ))
545     {
546         if (crit->OwningThread == ULongToHandle(GetCurrentThreadId()))
547         {
548             crit->RecursionCount++;
549             return STATUS_SUCCESS;
550         }
551
552         /* Now wait for it */
553         RtlpWaitForCriticalSection( crit );
554     }
555 done:
556     crit->OwningThread   = ULongToHandle(GetCurrentThreadId());
557     crit->RecursionCount = 1;
558     return STATUS_SUCCESS;
559 }
560
561
562 /***********************************************************************
563  *           RtlTryEnterCriticalSection   (NTDLL.@)
564  *
565  * Tries to enter a critical section without waiting.
566  *
567  * PARAMS
568  *  crit [I/O] Critical section to enter
569  *
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.
573  *
574  * SEE
575  *  RtlInitializeCriticalSectionEx(),
576  *  RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
577  *  RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
578  *  RtlLeaveCriticalSection(), RtlSetCriticalSectionSpinCount()
579  */
580 BOOL WINAPI RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
581 {
582     BOOL ret = FALSE;
583     if (interlocked_cmpxchg( &crit->LockCount, 0, -1 ) == -1)
584     {
585         crit->OwningThread   = ULongToHandle(GetCurrentThreadId());
586         crit->RecursionCount = 1;
587         ret = TRUE;
588     }
589     else if (crit->OwningThread == ULongToHandle(GetCurrentThreadId()))
590     {
591         interlocked_inc( &crit->LockCount );
592         crit->RecursionCount++;
593         ret = TRUE;
594     }
595     return ret;
596 }
597
598
599 /***********************************************************************
600  *           RtlLeaveCriticalSection   (NTDLL.@)
601  *
602  * Leaves a critical section.
603  *
604  * PARAMS
605  *  crit [I/O] Critical section to leave.
606  *
607  * RETURNS
608  *  STATUS_SUCCESS.
609  *
610  * SEE
611  *  RtlInitializeCriticalSectionEx(),
612  *  RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
613  *  RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
614  *  RtlSetCriticalSectionSpinCount(), RtlTryEnterCriticalSection()
615  */
616 NTSTATUS WINAPI RtlLeaveCriticalSection( RTL_CRITICAL_SECTION *crit )
617 {
618     if (--crit->RecursionCount) interlocked_dec( &crit->LockCount );
619     else
620     {
621         crit->OwningThread = 0;
622         if (interlocked_dec( &crit->LockCount ) >= 0)
623         {
624             /* someone is waiting */
625             RtlpUnWaitCriticalSection( crit );
626         }
627     }
628     return STATUS_SUCCESS;
629 }