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