wined3d: Filter out some shader compilation spam.
[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( (int *)dest, 1 ) + 1;
43 }
44
45 static inline LONG interlocked_dec( PLONG dest )
46 {
47     return interlocked_xchg_add( (int *)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 = (HANDLE)interlocked_cmpxchg_ptr( (PVOID *)&crit->LockSemaphore,
212                                                      (PVOID)sem, 0 )))
213             ret = sem;
214         else
215             NtClose(sem);  /* somebody beat us to it */
216     }
217     return ret;
218 }
219
220 /***********************************************************************
221  *           wait_semaphore
222  */
223 static inline NTSTATUS wait_semaphore( RTL_CRITICAL_SECTION *crit, int timeout )
224 {
225     NTSTATUS ret;
226
227     /* debug info is cleared by MakeCriticalSectionGlobal */
228     if (!crit->DebugInfo || ((ret = fast_wait( crit, timeout )) == STATUS_NOT_IMPLEMENTED))
229     {
230         HANDLE sem = get_semaphore( crit );
231         LARGE_INTEGER time;
232
233         time.QuadPart = timeout * (LONGLONG)-10000000;
234         ret = NTDLL_wait_for_multiple_objects( 1, &sem, 0, &time, 0 );
235     }
236     return ret;
237 }
238
239 /***********************************************************************
240  *           RtlInitializeCriticalSection   (NTDLL.@)
241  *
242  * Initialises a new critical section.
243  *
244  * PARAMS
245  *  crit [O] Critical section to initialise
246  *
247  * RETURNS
248  *  STATUS_SUCCESS.
249  *
250  * SEE
251  *  RtlInitializeCriticalSectionAndSpinCount(), RtlDeleteCriticalSection(),
252  *  RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
253  *  RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
254  */
255 NTSTATUS WINAPI RtlInitializeCriticalSection( RTL_CRITICAL_SECTION *crit )
256 {
257     return RtlInitializeCriticalSectionAndSpinCount( crit, 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  *  RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
277  *  RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
278  *  RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
279  */
280 NTSTATUS WINAPI RtlInitializeCriticalSectionAndSpinCount( RTL_CRITICAL_SECTION *crit, ULONG spincount )
281 {
282     crit->DebugInfo = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(RTL_CRITICAL_SECTION_DEBUG));
283     if (crit->DebugInfo)
284     {
285         crit->DebugInfo->Type = 0;
286         crit->DebugInfo->CreatorBackTraceIndex = 0;
287         crit->DebugInfo->CriticalSection = crit;
288         crit->DebugInfo->ProcessLocksList.Blink = &(crit->DebugInfo->ProcessLocksList);
289         crit->DebugInfo->ProcessLocksList.Flink = &(crit->DebugInfo->ProcessLocksList);
290         crit->DebugInfo->EntryCount = 0;
291         crit->DebugInfo->ContentionCount = 0;
292         memset( crit->DebugInfo->Spare, 0, sizeof(crit->DebugInfo->Spare) );
293     }
294     crit->LockCount      = -1;
295     crit->RecursionCount = 0;
296     crit->OwningThread   = 0;
297     crit->LockSemaphore  = 0;
298     if (NtCurrentTeb()->Peb->NumberOfProcessors <= 1) spincount = 0;
299     crit->SpinCount = spincount & ~0x80000000;
300     return STATUS_SUCCESS;
301 }
302
303 /***********************************************************************
304  *           RtlSetCriticalSectionSpinCount   (NTDLL.@)
305  *
306  * Sets the spin count of a critical section.
307  *
308  * PARAMS
309  *   crit      [I/O] Critical section
310  *   spincount [I] Spin count for crit
311  *
312  * RETURNS
313  *  The previous spin count.
314  *
315  * NOTES
316  *  If the system is not SMP, spincount is ignored and set to 0.
317  *
318  * SEE
319  *  RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
320  *  RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
321  *  RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
322  */
323 ULONG WINAPI RtlSetCriticalSectionSpinCount( RTL_CRITICAL_SECTION *crit, ULONG spincount )
324 {
325     ULONG oldspincount = crit->SpinCount;
326     if (NtCurrentTeb()->Peb->NumberOfProcessors <= 1) spincount = 0;
327     crit->SpinCount = spincount;
328     return oldspincount;
329 }
330
331 /***********************************************************************
332  *           RtlDeleteCriticalSection   (NTDLL.@)
333  *
334  * Frees the resources used by a critical section.
335  *
336  * PARAMS
337  *  crit [I/O] Critical section to free
338  *
339  * RETURNS
340  *  STATUS_SUCCESS.
341  *
342  * SEE
343  *  RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
344  *  RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
345  *  RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
346  */
347 NTSTATUS WINAPI RtlDeleteCriticalSection( RTL_CRITICAL_SECTION *crit )
348 {
349     crit->LockCount      = -1;
350     crit->RecursionCount = 0;
351     crit->OwningThread   = 0;
352     if (crit->DebugInfo)
353     {
354         /* only free the ones we made in here */
355         if (!crit->DebugInfo->Spare[0])
356         {
357             RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
358             crit->DebugInfo = NULL;
359         }
360         close_semaphore( crit );
361     }
362     else NtClose( crit->LockSemaphore );
363     crit->LockSemaphore = 0;
364     return STATUS_SUCCESS;
365 }
366
367
368 /***********************************************************************
369  *           RtlpWaitForCriticalSection   (NTDLL.@)
370  *
371  * Waits for a busy critical section to become free.
372  * 
373  * PARAMS
374  *  crit [I/O] Critical section to wait for
375  *
376  * RETURNS
377  *  STATUS_SUCCESS.
378  *
379  * NOTES
380  *  Use RtlEnterCriticalSection() instead of this function as it is often much
381  *  faster.
382  *
383  * SEE
384  *  RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
385  *  RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
386  *  RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
387  */
388 NTSTATUS WINAPI RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION *crit )
389 {
390     for (;;)
391     {
392         EXCEPTION_RECORD rec;
393         NTSTATUS status = wait_semaphore( crit, 5 );
394
395         if ( status == STATUS_TIMEOUT )
396         {
397             const char *name = NULL;
398             if (crit->DebugInfo) name = (char *)crit->DebugInfo->Spare[0];
399             if (!name) name = "?";
400             ERR( "section %p %s wait timed out in thread %04x, blocked by %04x, retrying (60 sec)\n",
401                  crit, debugstr_a(name), GetCurrentThreadId(), HandleToULong(crit->OwningThread) );
402             status = wait_semaphore( crit, 60 );
403             if ( status == STATUS_TIMEOUT && TRACE_ON(relay) )
404             {
405                 ERR( "section %p %s wait timed out in thread %04x, blocked by %04x, retrying (5 min)\n",
406                      crit, debugstr_a(name), GetCurrentThreadId(), HandleToULong(crit->OwningThread) );
407                 status = wait_semaphore( crit, 300 );
408             }
409         }
410         if (status == STATUS_WAIT_0) break;
411
412         /* Throw exception only for Wine internal locks */
413         if ((!crit->DebugInfo) || (!crit->DebugInfo->Spare[0])) continue;
414
415         rec.ExceptionCode    = STATUS_POSSIBLE_DEADLOCK;
416         rec.ExceptionFlags   = 0;
417         rec.ExceptionRecord  = NULL;
418         rec.ExceptionAddress = RtlRaiseException;  /* sic */
419         rec.NumberParameters = 1;
420         rec.ExceptionInformation[0] = (ULONG_PTR)crit;
421         RtlRaiseException( &rec );
422     }
423     if (crit->DebugInfo) crit->DebugInfo->ContentionCount++;
424     return STATUS_SUCCESS;
425 }
426
427
428 /***********************************************************************
429  *           RtlpUnWaitCriticalSection   (NTDLL.@)
430  *
431  * Notifies other threads waiting on the busy critical section that it has
432  * become free.
433  * 
434  * PARAMS
435  *  crit [I/O] Critical section
436  *
437  * RETURNS
438  *  Success: STATUS_SUCCESS.
439  *  Failure: Any error returned by NtReleaseSemaphore()
440  *
441  * NOTES
442  *  Use RtlLeaveCriticalSection() instead of this function as it is often much
443  *  faster.
444  *
445  * SEE
446  *  RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
447  *  RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
448  *  RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
449  */
450 NTSTATUS WINAPI RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION *crit )
451 {
452     NTSTATUS ret;
453
454     /* debug info is cleared by MakeCriticalSectionGlobal */
455     if (!crit->DebugInfo || ((ret = fast_wake( crit )) == STATUS_NOT_IMPLEMENTED))
456     {
457         HANDLE sem = get_semaphore( crit );
458         ret = NtReleaseSemaphore( sem, 1, NULL );
459     }
460     if (ret) RtlRaiseStatus( ret );
461     return ret;
462 }
463
464
465 /***********************************************************************
466  *           RtlEnterCriticalSection   (NTDLL.@)
467  *
468  * Enters a critical section, waiting for it to become available if necessary.
469  *
470  * PARAMS
471  *  crit [I/O] Critical section to enter
472  *
473  * RETURNS
474  *  STATUS_SUCCESS. The critical section is held by the caller.
475  *  
476  * SEE
477  *  RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
478  *  RtlDeleteCriticalSection(), RtlSetCriticalSectionSpinCount(),
479  *  RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
480  */
481 NTSTATUS WINAPI RtlEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
482 {
483     if (crit->SpinCount)
484     {
485         ULONG count;
486
487         if (RtlTryEnterCriticalSection( crit )) return STATUS_SUCCESS;
488         for (count = crit->SpinCount; count > 0; count--)
489         {
490             if (crit->LockCount > 0) break;  /* more than one waiter, don't bother spinning */
491             if (crit->LockCount == -1)       /* try again */
492             {
493                 if (interlocked_cmpxchg( (int *)&crit->LockCount, 0, -1 ) == -1) goto done;
494             }
495             small_pause();
496         }
497     }
498
499     if (interlocked_inc( &crit->LockCount ))
500     {
501         if (crit->OwningThread == ULongToHandle(GetCurrentThreadId()))
502         {
503             crit->RecursionCount++;
504             return STATUS_SUCCESS;
505         }
506
507         /* Now wait for it */
508         RtlpWaitForCriticalSection( crit );
509     }
510 done:
511     crit->OwningThread   = ULongToHandle(GetCurrentThreadId());
512     crit->RecursionCount = 1;
513     return STATUS_SUCCESS;
514 }
515
516
517 /***********************************************************************
518  *           RtlTryEnterCriticalSection   (NTDLL.@)
519  *
520  * Tries to enter a critical section without waiting.
521  *
522  * PARAMS
523  *  crit [I/O] Critical section to enter
524  *
525  * RETURNS
526  *  Success: TRUE. The critical section is held by the caller.
527  *  Failure: FALSE. The critical section is currently held by another thread.
528  *
529  * SEE
530  *  RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
531  *  RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
532  *  RtlLeaveCriticalSection(), RtlSetCriticalSectionSpinCount()
533  */
534 BOOL WINAPI RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
535 {
536     BOOL ret = FALSE;
537     if (interlocked_cmpxchg( (int *)&crit->LockCount, 0, -1 ) == -1)
538     {
539         crit->OwningThread   = ULongToHandle(GetCurrentThreadId());
540         crit->RecursionCount = 1;
541         ret = TRUE;
542     }
543     else if (crit->OwningThread == ULongToHandle(GetCurrentThreadId()))
544     {
545         interlocked_inc( &crit->LockCount );
546         crit->RecursionCount++;
547         ret = TRUE;
548     }
549     return ret;
550 }
551
552
553 /***********************************************************************
554  *           RtlLeaveCriticalSection   (NTDLL.@)
555  *
556  * Leaves a critical section.
557  *
558  * PARAMS
559  *  crit [I/O] Critical section to leave.
560  *
561  * RETURNS
562  *  STATUS_SUCCESS.
563  *
564  * SEE
565  *  RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
566  *  RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
567  *  RtlSetCriticalSectionSpinCount(), RtlTryEnterCriticalSection()
568  */
569 NTSTATUS WINAPI RtlLeaveCriticalSection( RTL_CRITICAL_SECTION *crit )
570 {
571     if (--crit->RecursionCount) interlocked_dec( &crit->LockCount );
572     else
573     {
574         crit->OwningThread = 0;
575         if (interlocked_dec( &crit->LockCount ) >= 0)
576         {
577             /* someone is waiting */
578             RtlpUnWaitCriticalSection( crit );
579         }
580     }
581     return STATUS_SUCCESS;
582 }