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
22 #include "wine/port.h"
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_SYSCALL_H
30 #include <sys/syscall.h>
34 #define WIN32_NO_STATUS
37 #include "wine/debug.h"
38 #include "ntdll_misc.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
41 WINE_DECLARE_DEBUG_CHANNEL(relay
);
43 static inline LONG
interlocked_inc( PLONG dest
)
45 return interlocked_xchg_add( dest
, 1 ) + 1;
48 static inline LONG
interlocked_dec( PLONG dest
)
50 return interlocked_xchg_add( dest
, -1 ) - 1;
53 static inline void small_pause(void)
56 __asm__
__volatile__( "rep;nop" : : : "memory" );
58 __asm__
__volatile__( "" : : : "memory" );
64 static int wait_op
= 128; /*FUTEX_WAIT|FUTEX_PRIVATE_FLAG*/
65 static int wake_op
= 129; /*FUTEX_WAKE|FUTEX_PRIVATE_FLAG*/
67 static inline int futex_wait( int *addr
, int val
, struct timespec
*timeout
)
69 return syscall( __NR_futex
, addr
, wait_op
, val
, timeout
, 0, 0 );
72 static inline int futex_wake( int *addr
, int val
)
74 return syscall( __NR_futex
, addr
, wake_op
, val
, NULL
, 0, 0 );
77 static inline int use_futexes(void)
79 static int supported
= -1;
83 futex_wait( &supported
, 10, NULL
);
86 wait_op
= 0; /*FUTEX_WAIT*/
87 wake_op
= 1; /*FUTEX_WAKE*/
88 futex_wait( &supported
, 10, NULL
);
90 supported
= (errno
!= ENOSYS
);
95 static inline NTSTATUS
fast_wait( RTL_CRITICAL_SECTION
*crit
, int timeout
)
98 struct timespec timespec
;
100 if (!use_futexes()) return STATUS_NOT_IMPLEMENTED
;
102 timespec
.tv_sec
= timeout
;
103 timespec
.tv_nsec
= 0;
104 while ((val
= interlocked_cmpxchg( (int *)&crit
->LockSemaphore
, 0, 1 )) != 1)
106 /* note: this may wait longer than specified in case of signals or */
107 /* multiple wake-ups, but that shouldn't be a problem */
108 if (futex_wait( (int *)&crit
->LockSemaphore
, val
, ×pec
) == -1 && errno
== ETIMEDOUT
)
109 return STATUS_TIMEOUT
;
111 return STATUS_WAIT_0
;
114 static inline NTSTATUS
fast_wake( RTL_CRITICAL_SECTION
*crit
)
116 if (!use_futexes()) return STATUS_NOT_IMPLEMENTED
;
118 *(int *)&crit
->LockSemaphore
= 1;
119 futex_wake( (int *)&crit
->LockSemaphore
, 1 );
120 return STATUS_SUCCESS
;
123 static inline void close_semaphore( RTL_CRITICAL_SECTION
*crit
)
125 if (!use_futexes()) NtClose( crit
->LockSemaphore
);
128 #elif defined(__APPLE__)
130 #include <mach/mach.h>
131 #include <mach/task.h>
132 #include <mach/semaphore.h>
134 static inline semaphore_t
get_mach_semaphore( RTL_CRITICAL_SECTION
*crit
)
136 semaphore_t ret
= *(int *)&crit
->LockSemaphore
;
140 if (semaphore_create( mach_task_self(), &sem
, SYNC_POLICY_FIFO
, 0 )) return 0;
141 if (!(ret
= interlocked_cmpxchg( (int *)&crit
->LockSemaphore
, sem
, 0 )))
144 semaphore_destroy( mach_task_self(), sem
); /* somebody beat us to it */
149 static inline NTSTATUS
fast_wait( RTL_CRITICAL_SECTION
*crit
, int timeout
)
151 mach_timespec_t timespec
;
152 semaphore_t sem
= get_mach_semaphore( crit
);
154 timespec
.tv_sec
= timeout
;
155 timespec
.tv_nsec
= 0;
158 switch( semaphore_timedwait( sem
, timespec
))
161 return STATUS_WAIT_0
;
163 continue; /* got a signal, restart */
164 case KERN_OPERATION_TIMED_OUT
:
165 return STATUS_TIMEOUT
;
167 return STATUS_INVALID_HANDLE
;
172 static inline NTSTATUS
fast_wake( RTL_CRITICAL_SECTION
*crit
)
174 semaphore_t sem
= get_mach_semaphore( crit
);
175 semaphore_signal( sem
);
176 return STATUS_SUCCESS
;
179 static inline void close_semaphore( RTL_CRITICAL_SECTION
*crit
)
181 semaphore_destroy( mach_task_self(), *(int *)&crit
->LockSemaphore
);
184 #else /* __APPLE__ */
186 static inline NTSTATUS
fast_wait( RTL_CRITICAL_SECTION
*crit
, int timeout
)
188 return STATUS_NOT_IMPLEMENTED
;
191 static inline NTSTATUS
fast_wake( RTL_CRITICAL_SECTION
*crit
)
193 return STATUS_NOT_IMPLEMENTED
;
196 static inline void close_semaphore( RTL_CRITICAL_SECTION
*crit
)
198 NtClose( crit
->LockSemaphore
);
203 /***********************************************************************
206 static inline HANDLE
get_semaphore( RTL_CRITICAL_SECTION
*crit
)
208 HANDLE ret
= crit
->LockSemaphore
;
212 if (NtCreateSemaphore( &sem
, SEMAPHORE_ALL_ACCESS
, NULL
, 0, 1 )) return 0;
213 if (!(ret
= interlocked_cmpxchg_ptr( &crit
->LockSemaphore
, sem
, 0 )))
216 NtClose(sem
); /* somebody beat us to it */
221 /***********************************************************************
224 static inline NTSTATUS
wait_semaphore( RTL_CRITICAL_SECTION
*crit
, int timeout
)
228 /* debug info is cleared by MakeCriticalSectionGlobal */
229 if (!crit
->DebugInfo
|| ((ret
= fast_wait( crit
, timeout
)) == STATUS_NOT_IMPLEMENTED
))
231 HANDLE sem
= get_semaphore( crit
);
233 select_op_t select_op
;
235 time
.QuadPart
= timeout
* (LONGLONG
)-10000000;
236 select_op
.wait
.op
= SELECT_WAIT
;
237 select_op
.wait
.handles
[0] = wine_server_obj_handle( sem
);
238 ret
= server_select( &select_op
, offsetof( select_op_t
, wait
.handles
[1] ), 0, &time
);
243 /***********************************************************************
244 * RtlInitializeCriticalSection (NTDLL.@)
246 * Initialises a new critical section.
249 * crit [O] Critical section to initialise
255 * RtlInitializeCriticalSectionEx(),
256 * RtlInitializeCriticalSectionAndSpinCount(), RtlDeleteCriticalSection(),
257 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
258 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
260 NTSTATUS WINAPI
RtlInitializeCriticalSection( RTL_CRITICAL_SECTION
*crit
)
262 return RtlInitializeCriticalSectionEx( crit
, 0, 0 );
265 /***********************************************************************
266 * RtlInitializeCriticalSectionAndSpinCount (NTDLL.@)
268 * Initialises a new critical section with a given spin count.
271 * crit [O] Critical section to initialise
272 * spincount [I] Spin count for crit
278 * Available on NT4 SP3 or later.
281 * RtlInitializeCriticalSectionEx(),
282 * RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
283 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
284 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
286 NTSTATUS WINAPI
RtlInitializeCriticalSectionAndSpinCount( RTL_CRITICAL_SECTION
*crit
, ULONG spincount
)
288 return RtlInitializeCriticalSectionEx( crit
, spincount
, 0 );
291 /***********************************************************************
292 * RtlInitializeCriticalSectionEx (NTDLL.@)
294 * Initialises a new critical section with a given spin count and flags.
297 * crit [O] Critical section to initialise.
298 * spincount [I] Number of times to spin upon contention.
299 * flags [I] RTL_CRITICAL_SECTION_FLAG_ flags from winnt.h.
305 * Available on Vista or later.
308 * RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
309 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
310 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
312 NTSTATUS WINAPI
RtlInitializeCriticalSectionEx( RTL_CRITICAL_SECTION
*crit
, ULONG spincount
, ULONG flags
)
314 if (flags
& (RTL_CRITICAL_SECTION_FLAG_DYNAMIC_SPIN
|RTL_CRITICAL_SECTION_FLAG_STATIC_INIT
))
315 FIXME("(%p,%u,0x%08x) semi-stub\n", crit
, spincount
, flags
);
317 /* FIXME: if RTL_CRITICAL_SECTION_FLAG_STATIC_INIT is given, we should use
318 * memory from a static pool to hold the debug info. Then heap.c could pass
319 * this flag rather than initialising the process heap CS by hand. If this
320 * is done, then debug info should be managed through Rtlp[Allocate|Free]DebugInfo
321 * so (e.g.) MakeCriticalSectionGlobal() doesn't free it using HeapFree().
323 if (flags
& RTL_CRITICAL_SECTION_FLAG_NO_DEBUG_INFO
)
324 crit
->DebugInfo
= NULL
;
326 crit
->DebugInfo
= RtlAllocateHeap(GetProcessHeap(), 0, sizeof(RTL_CRITICAL_SECTION_DEBUG
));
330 crit
->DebugInfo
->Type
= 0;
331 crit
->DebugInfo
->CreatorBackTraceIndex
= 0;
332 crit
->DebugInfo
->CriticalSection
= crit
;
333 crit
->DebugInfo
->ProcessLocksList
.Blink
= &(crit
->DebugInfo
->ProcessLocksList
);
334 crit
->DebugInfo
->ProcessLocksList
.Flink
= &(crit
->DebugInfo
->ProcessLocksList
);
335 crit
->DebugInfo
->EntryCount
= 0;
336 crit
->DebugInfo
->ContentionCount
= 0;
337 memset( crit
->DebugInfo
->Spare
, 0, sizeof(crit
->DebugInfo
->Spare
) );
339 crit
->LockCount
= -1;
340 crit
->RecursionCount
= 0;
341 crit
->OwningThread
= 0;
342 crit
->LockSemaphore
= 0;
343 if (NtCurrentTeb()->Peb
->NumberOfProcessors
<= 1) spincount
= 0;
344 crit
->SpinCount
= spincount
& ~0x80000000;
345 return STATUS_SUCCESS
;
348 /***********************************************************************
349 * RtlSetCriticalSectionSpinCount (NTDLL.@)
351 * Sets the spin count of a critical section.
354 * crit [I/O] Critical section
355 * spincount [I] Spin count for crit
358 * The previous spin count.
361 * If the system is not SMP, spincount is ignored and set to 0.
364 * RtlInitializeCriticalSectionEx(),
365 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
366 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
367 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
369 ULONG WINAPI
RtlSetCriticalSectionSpinCount( RTL_CRITICAL_SECTION
*crit
, ULONG spincount
)
371 ULONG oldspincount
= crit
->SpinCount
;
372 if (NtCurrentTeb()->Peb
->NumberOfProcessors
<= 1) spincount
= 0;
373 crit
->SpinCount
= spincount
;
377 /***********************************************************************
378 * RtlDeleteCriticalSection (NTDLL.@)
380 * Frees the resources used by a critical section.
383 * crit [I/O] Critical section to free
389 * RtlInitializeCriticalSectionEx(),
390 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
391 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
392 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
394 NTSTATUS WINAPI
RtlDeleteCriticalSection( RTL_CRITICAL_SECTION
*crit
)
396 crit
->LockCount
= -1;
397 crit
->RecursionCount
= 0;
398 crit
->OwningThread
= 0;
401 /* only free the ones we made in here */
402 if (!crit
->DebugInfo
->Spare
[0])
404 RtlFreeHeap( GetProcessHeap(), 0, crit
->DebugInfo
);
405 crit
->DebugInfo
= NULL
;
407 close_semaphore( crit
);
409 else NtClose( crit
->LockSemaphore
);
410 crit
->LockSemaphore
= 0;
411 return STATUS_SUCCESS
;
415 /***********************************************************************
416 * RtlpWaitForCriticalSection (NTDLL.@)
418 * Waits for a busy critical section to become free.
421 * crit [I/O] Critical section to wait for
427 * Use RtlEnterCriticalSection() instead of this function as it is often much
431 * RtlInitializeCriticalSectionEx(),
432 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
433 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
434 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
436 NTSTATUS WINAPI
RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION
*crit
)
438 LONGLONG timeout
= NtCurrentTeb()->Peb
->CriticalSectionTimeout
.QuadPart
/ -10000000;
441 EXCEPTION_RECORD rec
;
442 NTSTATUS status
= wait_semaphore( crit
, 5 );
445 if ( status
== STATUS_TIMEOUT
)
447 const char *name
= NULL
;
448 if (crit
->DebugInfo
) name
= (char *)crit
->DebugInfo
->Spare
[0];
449 if (!name
) name
= "?";
450 ERR( "section %p %s wait timed out in thread %04x, blocked by %04x, retrying (60 sec)\n",
451 crit
, debugstr_a(name
), GetCurrentThreadId(), HandleToULong(crit
->OwningThread
) );
452 status
= wait_semaphore( crit
, 60 );
455 if ( status
== STATUS_TIMEOUT
&& TRACE_ON(relay
) )
457 ERR( "section %p %s wait timed out in thread %04x, blocked by %04x, retrying (5 min)\n",
458 crit
, debugstr_a(name
), GetCurrentThreadId(), HandleToULong(crit
->OwningThread
) );
459 status
= wait_semaphore( crit
, 300 );
463 if (status
== STATUS_WAIT_0
) break;
465 /* Throw exception only for Wine internal locks */
466 if ((!crit
->DebugInfo
) || (!crit
->DebugInfo
->Spare
[0])) continue;
468 /* only throw deadlock exception if configured timeout is reached */
469 if (timeout
> 0) continue;
471 rec
.ExceptionCode
= STATUS_POSSIBLE_DEADLOCK
;
472 rec
.ExceptionFlags
= 0;
473 rec
.ExceptionRecord
= NULL
;
474 rec
.ExceptionAddress
= RtlRaiseException
; /* sic */
475 rec
.NumberParameters
= 1;
476 rec
.ExceptionInformation
[0] = (ULONG_PTR
)crit
;
477 RtlRaiseException( &rec
);
479 if (crit
->DebugInfo
) crit
->DebugInfo
->ContentionCount
++;
480 return STATUS_SUCCESS
;
484 /***********************************************************************
485 * RtlpUnWaitCriticalSection (NTDLL.@)
487 * Notifies other threads waiting on the busy critical section that it has
491 * crit [I/O] Critical section
494 * Success: STATUS_SUCCESS.
495 * Failure: Any error returned by NtReleaseSemaphore()
498 * Use RtlLeaveCriticalSection() instead of this function as it is often much
502 * RtlInitializeCriticalSectionEx(),
503 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
504 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
505 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
507 NTSTATUS WINAPI
RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION
*crit
)
511 /* debug info is cleared by MakeCriticalSectionGlobal */
512 if (!crit
->DebugInfo
|| ((ret
= fast_wake( crit
)) == STATUS_NOT_IMPLEMENTED
))
514 HANDLE sem
= get_semaphore( crit
);
515 ret
= NtReleaseSemaphore( sem
, 1, NULL
);
517 if (ret
) RtlRaiseStatus( ret
);
522 /***********************************************************************
523 * RtlEnterCriticalSection (NTDLL.@)
525 * Enters a critical section, waiting for it to become available if necessary.
528 * crit [I/O] Critical section to enter
531 * STATUS_SUCCESS. The critical section is held by the caller.
534 * RtlInitializeCriticalSectionEx(),
535 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
536 * RtlDeleteCriticalSection(), RtlSetCriticalSectionSpinCount(),
537 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
539 NTSTATUS WINAPI
RtlEnterCriticalSection( RTL_CRITICAL_SECTION
*crit
)
545 if (RtlTryEnterCriticalSection( crit
)) return STATUS_SUCCESS
;
546 for (count
= crit
->SpinCount
; count
> 0; count
--)
548 if (crit
->LockCount
> 0) break; /* more than one waiter, don't bother spinning */
549 if (crit
->LockCount
== -1) /* try again */
551 if (interlocked_cmpxchg( &crit
->LockCount
, 0, -1 ) == -1) goto done
;
557 if (interlocked_inc( &crit
->LockCount
))
559 if (crit
->OwningThread
== ULongToHandle(GetCurrentThreadId()))
561 crit
->RecursionCount
++;
562 return STATUS_SUCCESS
;
565 /* Now wait for it */
566 RtlpWaitForCriticalSection( crit
);
569 crit
->OwningThread
= ULongToHandle(GetCurrentThreadId());
570 crit
->RecursionCount
= 1;
571 return STATUS_SUCCESS
;
575 /***********************************************************************
576 * RtlTryEnterCriticalSection (NTDLL.@)
578 * Tries to enter a critical section without waiting.
581 * crit [I/O] Critical section to enter
584 * Success: TRUE. The critical section is held by the caller.
585 * Failure: FALSE. The critical section is currently held by another thread.
588 * RtlInitializeCriticalSectionEx(),
589 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
590 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
591 * RtlLeaveCriticalSection(), RtlSetCriticalSectionSpinCount()
593 BOOL WINAPI
RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION
*crit
)
596 if (interlocked_cmpxchg( &crit
->LockCount
, 0, -1 ) == -1)
598 crit
->OwningThread
= ULongToHandle(GetCurrentThreadId());
599 crit
->RecursionCount
= 1;
602 else if (crit
->OwningThread
== ULongToHandle(GetCurrentThreadId()))
604 interlocked_inc( &crit
->LockCount
);
605 crit
->RecursionCount
++;
612 /***********************************************************************
613 * RtlIsCriticalSectionLocked (NTDLL.@)
615 * Checks if the critical section is locked by any thread.
618 * crit [I/O] Critical section to check.
621 * Success: TRUE. The critical section is locked.
622 * Failure: FALSE. The critical section is not locked.
624 BOOL WINAPI
RtlIsCriticalSectionLocked( RTL_CRITICAL_SECTION
*crit
)
626 return crit
->RecursionCount
!= 0;
630 /***********************************************************************
631 * RtlIsCriticalSectionLockedByThread (NTDLL.@)
633 * Checks if the critical section is locked by the current thread.
636 * crit [I/O] Critical section to check.
639 * Success: TRUE. The critical section is locked.
640 * Failure: FALSE. The critical section is not locked.
642 BOOL WINAPI
RtlIsCriticalSectionLockedByThread( RTL_CRITICAL_SECTION
*crit
)
644 return crit
->OwningThread
== ULongToHandle(GetCurrentThreadId()) &&
645 crit
->RecursionCount
;
649 /***********************************************************************
650 * RtlLeaveCriticalSection (NTDLL.@)
652 * Leaves a critical section.
655 * crit [I/O] Critical section to leave.
661 * RtlInitializeCriticalSectionEx(),
662 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
663 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
664 * RtlSetCriticalSectionSpinCount(), RtlTryEnterCriticalSection()
666 NTSTATUS WINAPI
RtlLeaveCriticalSection( RTL_CRITICAL_SECTION
*crit
)
668 if (--crit
->RecursionCount
)
670 if (crit
->RecursionCount
> 0) interlocked_dec( &crit
->LockCount
);
671 else ERR( "section %p is not acquired\n", crit
);
675 crit
->OwningThread
= 0;
676 if (interlocked_dec( &crit
->LockCount
) >= 0)
678 /* someone is waiting */
679 RtlpUnWaitCriticalSection( crit
);
682 return STATUS_SUCCESS
;