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" );
62 static void *no_debug_info_marker
= (void *)(ULONG_PTR
)-1;
64 static BOOL
crit_section_has_debuginfo(const RTL_CRITICAL_SECTION
*crit
)
66 return crit
->DebugInfo
!= NULL
&& crit
->DebugInfo
!= no_debug_info_marker
;
71 static int wait_op
= 128; /*FUTEX_WAIT|FUTEX_PRIVATE_FLAG*/
72 static int wake_op
= 129; /*FUTEX_WAKE|FUTEX_PRIVATE_FLAG*/
74 static inline int futex_wait( int *addr
, int val
, struct timespec
*timeout
)
76 return syscall( __NR_futex
, addr
, wait_op
, val
, timeout
, 0, 0 );
79 static inline int futex_wake( int *addr
, int val
)
81 return syscall( __NR_futex
, addr
, wake_op
, val
, NULL
, 0, 0 );
84 static inline int use_futexes(void)
86 static int supported
= -1;
90 futex_wait( &supported
, 10, NULL
);
93 wait_op
= 0; /*FUTEX_WAIT*/
94 wake_op
= 1; /*FUTEX_WAKE*/
95 futex_wait( &supported
, 10, NULL
);
97 supported
= (errno
!= ENOSYS
);
102 static inline NTSTATUS
fast_wait( RTL_CRITICAL_SECTION
*crit
, int timeout
)
105 struct timespec timespec
;
107 if (!use_futexes()) return STATUS_NOT_IMPLEMENTED
;
109 timespec
.tv_sec
= timeout
;
110 timespec
.tv_nsec
= 0;
111 while ((val
= interlocked_cmpxchg( (int *)&crit
->LockSemaphore
, 0, 1 )) != 1)
113 /* note: this may wait longer than specified in case of signals or */
114 /* multiple wake-ups, but that shouldn't be a problem */
115 if (futex_wait( (int *)&crit
->LockSemaphore
, val
, ×pec
) == -1 && errno
== ETIMEDOUT
)
116 return STATUS_TIMEOUT
;
118 return STATUS_WAIT_0
;
121 static inline NTSTATUS
fast_wake( RTL_CRITICAL_SECTION
*crit
)
123 if (!use_futexes()) return STATUS_NOT_IMPLEMENTED
;
125 *(int *)&crit
->LockSemaphore
= 1;
126 futex_wake( (int *)&crit
->LockSemaphore
, 1 );
127 return STATUS_SUCCESS
;
130 static inline void close_semaphore( RTL_CRITICAL_SECTION
*crit
)
132 if (!use_futexes()) NtClose( crit
->LockSemaphore
);
135 #elif defined(__APPLE__)
137 #include <mach/mach.h>
138 #include <mach/task.h>
139 #include <mach/semaphore.h>
141 static inline semaphore_t
get_mach_semaphore( RTL_CRITICAL_SECTION
*crit
)
143 semaphore_t ret
= *(int *)&crit
->LockSemaphore
;
147 if (semaphore_create( mach_task_self(), &sem
, SYNC_POLICY_FIFO
, 0 )) return 0;
148 if (!(ret
= interlocked_cmpxchg( (int *)&crit
->LockSemaphore
, sem
, 0 )))
151 semaphore_destroy( mach_task_self(), sem
); /* somebody beat us to it */
156 static inline NTSTATUS
fast_wait( RTL_CRITICAL_SECTION
*crit
, int timeout
)
158 mach_timespec_t timespec
;
159 semaphore_t sem
= get_mach_semaphore( crit
);
161 timespec
.tv_sec
= timeout
;
162 timespec
.tv_nsec
= 0;
165 switch( semaphore_timedwait( sem
, timespec
))
168 return STATUS_WAIT_0
;
170 continue; /* got a signal, restart */
171 case KERN_OPERATION_TIMED_OUT
:
172 return STATUS_TIMEOUT
;
174 return STATUS_INVALID_HANDLE
;
179 static inline NTSTATUS
fast_wake( RTL_CRITICAL_SECTION
*crit
)
181 semaphore_t sem
= get_mach_semaphore( crit
);
182 semaphore_signal( sem
);
183 return STATUS_SUCCESS
;
186 static inline void close_semaphore( RTL_CRITICAL_SECTION
*crit
)
188 semaphore_destroy( mach_task_self(), *(int *)&crit
->LockSemaphore
);
191 #else /* __APPLE__ */
193 static inline NTSTATUS
fast_wait( RTL_CRITICAL_SECTION
*crit
, int timeout
)
195 return STATUS_NOT_IMPLEMENTED
;
198 static inline NTSTATUS
fast_wake( RTL_CRITICAL_SECTION
*crit
)
200 return STATUS_NOT_IMPLEMENTED
;
203 static inline void close_semaphore( RTL_CRITICAL_SECTION
*crit
)
205 NtClose( crit
->LockSemaphore
);
210 /***********************************************************************
213 static inline HANDLE
get_semaphore( RTL_CRITICAL_SECTION
*crit
)
215 HANDLE ret
= crit
->LockSemaphore
;
219 if (NtCreateSemaphore( &sem
, SEMAPHORE_ALL_ACCESS
, NULL
, 0, 1 )) return 0;
220 if (!(ret
= interlocked_cmpxchg_ptr( &crit
->LockSemaphore
, sem
, 0 )))
223 NtClose(sem
); /* somebody beat us to it */
228 /***********************************************************************
231 static inline NTSTATUS
wait_semaphore( RTL_CRITICAL_SECTION
*crit
, int timeout
)
235 /* debug info is cleared by MakeCriticalSectionGlobal */
236 if (!crit_section_has_debuginfo( crit
) || ((ret
= fast_wait( crit
, timeout
)) == STATUS_NOT_IMPLEMENTED
))
238 HANDLE sem
= get_semaphore( crit
);
240 select_op_t select_op
;
242 time
.QuadPart
= timeout
* (LONGLONG
)-10000000;
243 select_op
.wait
.op
= SELECT_WAIT
;
244 select_op
.wait
.handles
[0] = wine_server_obj_handle( sem
);
245 ret
= server_select( &select_op
, offsetof( select_op_t
, wait
.handles
[1] ), 0, &time
);
250 /***********************************************************************
251 * RtlInitializeCriticalSection (NTDLL.@)
253 * Initialises a new critical section.
256 * crit [O] Critical section to initialise
262 * RtlInitializeCriticalSectionEx(),
263 * RtlInitializeCriticalSectionAndSpinCount(), RtlDeleteCriticalSection(),
264 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
265 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
267 NTSTATUS WINAPI
RtlInitializeCriticalSection( RTL_CRITICAL_SECTION
*crit
)
269 return RtlInitializeCriticalSectionEx( crit
, 0, 0 );
272 /***********************************************************************
273 * RtlInitializeCriticalSectionAndSpinCount (NTDLL.@)
275 * Initialises a new critical section with a given spin count.
278 * crit [O] Critical section to initialise
279 * spincount [I] Spin count for crit
285 * Available on NT4 SP3 or later.
288 * RtlInitializeCriticalSectionEx(),
289 * RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
290 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
291 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
293 NTSTATUS WINAPI
RtlInitializeCriticalSectionAndSpinCount( RTL_CRITICAL_SECTION
*crit
, ULONG spincount
)
295 return RtlInitializeCriticalSectionEx( crit
, spincount
, 0 );
298 /***********************************************************************
299 * RtlInitializeCriticalSectionEx (NTDLL.@)
301 * Initialises a new critical section with a given spin count and flags.
304 * crit [O] Critical section to initialise.
305 * spincount [I] Number of times to spin upon contention.
306 * flags [I] RTL_CRITICAL_SECTION_FLAG_ flags from winnt.h.
312 * Available on Vista or later.
315 * RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
316 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
317 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
319 NTSTATUS WINAPI
RtlInitializeCriticalSectionEx( RTL_CRITICAL_SECTION
*crit
, ULONG spincount
, ULONG flags
)
321 if (flags
& (RTL_CRITICAL_SECTION_FLAG_DYNAMIC_SPIN
|RTL_CRITICAL_SECTION_FLAG_STATIC_INIT
))
322 FIXME("(%p,%u,0x%08x) semi-stub\n", crit
, spincount
, flags
);
324 /* FIXME: if RTL_CRITICAL_SECTION_FLAG_STATIC_INIT is given, we should use
325 * memory from a static pool to hold the debug info. Then heap.c could pass
326 * this flag rather than initialising the process heap CS by hand. If this
327 * is done, then debug info should be managed through Rtlp[Allocate|Free]DebugInfo
328 * so (e.g.) MakeCriticalSectionGlobal() doesn't free it using HeapFree().
330 if (flags
& RTL_CRITICAL_SECTION_FLAG_NO_DEBUG_INFO
)
331 crit
->DebugInfo
= no_debug_info_marker
;
334 crit
->DebugInfo
= RtlAllocateHeap(GetProcessHeap(), 0, sizeof(RTL_CRITICAL_SECTION_DEBUG
));
337 crit
->DebugInfo
->Type
= 0;
338 crit
->DebugInfo
->CreatorBackTraceIndex
= 0;
339 crit
->DebugInfo
->CriticalSection
= crit
;
340 crit
->DebugInfo
->ProcessLocksList
.Blink
= &(crit
->DebugInfo
->ProcessLocksList
);
341 crit
->DebugInfo
->ProcessLocksList
.Flink
= &(crit
->DebugInfo
->ProcessLocksList
);
342 crit
->DebugInfo
->EntryCount
= 0;
343 crit
->DebugInfo
->ContentionCount
= 0;
344 memset( crit
->DebugInfo
->Spare
, 0, sizeof(crit
->DebugInfo
->Spare
) );
347 crit
->LockCount
= -1;
348 crit
->RecursionCount
= 0;
349 crit
->OwningThread
= 0;
350 crit
->LockSemaphore
= 0;
351 if (NtCurrentTeb()->Peb
->NumberOfProcessors
<= 1) spincount
= 0;
352 crit
->SpinCount
= spincount
& ~0x80000000;
353 return STATUS_SUCCESS
;
356 /***********************************************************************
357 * RtlSetCriticalSectionSpinCount (NTDLL.@)
359 * Sets the spin count of a critical section.
362 * crit [I/O] Critical section
363 * spincount [I] Spin count for crit
366 * The previous spin count.
369 * If the system is not SMP, spincount is ignored and set to 0.
372 * RtlInitializeCriticalSectionEx(),
373 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
374 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
375 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
377 ULONG WINAPI
RtlSetCriticalSectionSpinCount( RTL_CRITICAL_SECTION
*crit
, ULONG spincount
)
379 ULONG oldspincount
= crit
->SpinCount
;
380 if (NtCurrentTeb()->Peb
->NumberOfProcessors
<= 1) spincount
= 0;
381 crit
->SpinCount
= spincount
;
385 /***********************************************************************
386 * RtlDeleteCriticalSection (NTDLL.@)
388 * Frees the resources used by a critical section.
391 * crit [I/O] Critical section to free
397 * RtlInitializeCriticalSectionEx(),
398 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
399 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
400 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
402 NTSTATUS WINAPI
RtlDeleteCriticalSection( RTL_CRITICAL_SECTION
*crit
)
404 crit
->LockCount
= -1;
405 crit
->RecursionCount
= 0;
406 crit
->OwningThread
= 0;
407 if (crit_section_has_debuginfo( crit
))
409 /* only free the ones we made in here */
410 if (!crit
->DebugInfo
->Spare
[0])
412 RtlFreeHeap( GetProcessHeap(), 0, crit
->DebugInfo
);
413 crit
->DebugInfo
= NULL
;
415 close_semaphore( crit
);
417 else NtClose( crit
->LockSemaphore
);
418 crit
->LockSemaphore
= 0;
419 return STATUS_SUCCESS
;
423 /***********************************************************************
424 * RtlpWaitForCriticalSection (NTDLL.@)
426 * Waits for a busy critical section to become free.
429 * crit [I/O] Critical section to wait for
435 * Use RtlEnterCriticalSection() instead of this function as it is often much
439 * RtlInitializeCriticalSectionEx(),
440 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
441 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
442 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
444 NTSTATUS WINAPI
RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION
*crit
)
446 LONGLONG timeout
= NtCurrentTeb()->Peb
->CriticalSectionTimeout
.QuadPart
/ -10000000;
448 /* Don't allow blocking on a critical section during process termination */
449 if (RtlDllShutdownInProgress())
451 WARN( "process %s is shutting down, returning STATUS_SUCCESS\n",
452 debugstr_w(NtCurrentTeb()->Peb
->ProcessParameters
->ImagePathName
.Buffer
) );
453 return STATUS_SUCCESS
;
458 EXCEPTION_RECORD rec
;
459 NTSTATUS status
= wait_semaphore( crit
, 5 );
462 if ( status
== STATUS_TIMEOUT
)
464 const char *name
= NULL
;
465 if (crit_section_has_debuginfo( crit
)) name
= (char *)crit
->DebugInfo
->Spare
[0];
466 if (!name
) name
= "?";
467 ERR( "section %p %s wait timed out in thread %04x, blocked by %04x, retrying (60 sec)\n",
468 crit
, debugstr_a(name
), GetCurrentThreadId(), HandleToULong(crit
->OwningThread
) );
469 status
= wait_semaphore( crit
, 60 );
472 if ( status
== STATUS_TIMEOUT
&& TRACE_ON(relay
) )
474 ERR( "section %p %s wait timed out in thread %04x, blocked by %04x, retrying (5 min)\n",
475 crit
, debugstr_a(name
), GetCurrentThreadId(), HandleToULong(crit
->OwningThread
) );
476 status
= wait_semaphore( crit
, 300 );
480 if (status
== STATUS_WAIT_0
) break;
482 /* Throw exception only for Wine internal locks */
483 if (!crit_section_has_debuginfo( crit
) || !crit
->DebugInfo
->Spare
[0]) continue;
485 /* only throw deadlock exception if configured timeout is reached */
486 if (timeout
> 0) continue;
488 rec
.ExceptionCode
= STATUS_POSSIBLE_DEADLOCK
;
489 rec
.ExceptionFlags
= 0;
490 rec
.ExceptionRecord
= NULL
;
491 rec
.ExceptionAddress
= RtlRaiseException
; /* sic */
492 rec
.NumberParameters
= 1;
493 rec
.ExceptionInformation
[0] = (ULONG_PTR
)crit
;
494 RtlRaiseException( &rec
);
496 if (crit_section_has_debuginfo( crit
)) crit
->DebugInfo
->ContentionCount
++;
497 return STATUS_SUCCESS
;
501 /***********************************************************************
502 * RtlpUnWaitCriticalSection (NTDLL.@)
504 * Notifies other threads waiting on the busy critical section that it has
508 * crit [I/O] Critical section
511 * Success: STATUS_SUCCESS.
512 * Failure: Any error returned by NtReleaseSemaphore()
515 * Use RtlLeaveCriticalSection() instead of this function as it is often much
519 * RtlInitializeCriticalSectionEx(),
520 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
521 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
522 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
524 NTSTATUS WINAPI
RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION
*crit
)
528 /* debug info is cleared by MakeCriticalSectionGlobal */
529 if (!crit_section_has_debuginfo( crit
) || ((ret
= fast_wake( crit
)) == STATUS_NOT_IMPLEMENTED
))
531 HANDLE sem
= get_semaphore( crit
);
532 ret
= NtReleaseSemaphore( sem
, 1, NULL
);
534 if (ret
) RtlRaiseStatus( ret
);
539 /***********************************************************************
540 * RtlEnterCriticalSection (NTDLL.@)
542 * Enters a critical section, waiting for it to become available if necessary.
545 * crit [I/O] Critical section to enter
548 * STATUS_SUCCESS. The critical section is held by the caller.
551 * RtlInitializeCriticalSectionEx(),
552 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
553 * RtlDeleteCriticalSection(), RtlSetCriticalSectionSpinCount(),
554 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
556 NTSTATUS WINAPI
RtlEnterCriticalSection( RTL_CRITICAL_SECTION
*crit
)
562 if (RtlTryEnterCriticalSection( crit
)) return STATUS_SUCCESS
;
563 for (count
= crit
->SpinCount
; count
> 0; count
--)
565 if (crit
->LockCount
> 0) break; /* more than one waiter, don't bother spinning */
566 if (crit
->LockCount
== -1) /* try again */
568 if (interlocked_cmpxchg( &crit
->LockCount
, 0, -1 ) == -1) goto done
;
574 if (interlocked_inc( &crit
->LockCount
))
576 if (crit
->OwningThread
== ULongToHandle(GetCurrentThreadId()))
578 crit
->RecursionCount
++;
579 return STATUS_SUCCESS
;
582 /* Now wait for it */
583 RtlpWaitForCriticalSection( crit
);
586 crit
->OwningThread
= ULongToHandle(GetCurrentThreadId());
587 crit
->RecursionCount
= 1;
588 return STATUS_SUCCESS
;
592 /***********************************************************************
593 * RtlTryEnterCriticalSection (NTDLL.@)
595 * Tries to enter a critical section without waiting.
598 * crit [I/O] Critical section to enter
601 * Success: TRUE. The critical section is held by the caller.
602 * Failure: FALSE. The critical section is currently held by another thread.
605 * RtlInitializeCriticalSectionEx(),
606 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
607 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
608 * RtlLeaveCriticalSection(), RtlSetCriticalSectionSpinCount()
610 BOOL WINAPI
RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION
*crit
)
613 if (interlocked_cmpxchg( &crit
->LockCount
, 0, -1 ) == -1)
615 crit
->OwningThread
= ULongToHandle(GetCurrentThreadId());
616 crit
->RecursionCount
= 1;
619 else if (crit
->OwningThread
== ULongToHandle(GetCurrentThreadId()))
621 interlocked_inc( &crit
->LockCount
);
622 crit
->RecursionCount
++;
629 /***********************************************************************
630 * RtlIsCriticalSectionLocked (NTDLL.@)
632 * Checks if the critical section is locked by any thread.
635 * crit [I/O] Critical section to check.
638 * Success: TRUE. The critical section is locked.
639 * Failure: FALSE. The critical section is not locked.
641 BOOL WINAPI
RtlIsCriticalSectionLocked( RTL_CRITICAL_SECTION
*crit
)
643 return crit
->RecursionCount
!= 0;
647 /***********************************************************************
648 * RtlIsCriticalSectionLockedByThread (NTDLL.@)
650 * Checks if the critical section is locked by the current thread.
653 * crit [I/O] Critical section to check.
656 * Success: TRUE. The critical section is locked.
657 * Failure: FALSE. The critical section is not locked.
659 BOOL WINAPI
RtlIsCriticalSectionLockedByThread( RTL_CRITICAL_SECTION
*crit
)
661 return crit
->OwningThread
== ULongToHandle(GetCurrentThreadId()) &&
662 crit
->RecursionCount
;
666 /***********************************************************************
667 * RtlLeaveCriticalSection (NTDLL.@)
669 * Leaves a critical section.
672 * crit [I/O] Critical section to leave.
678 * RtlInitializeCriticalSectionEx(),
679 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
680 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
681 * RtlSetCriticalSectionSpinCount(), RtlTryEnterCriticalSection()
683 NTSTATUS WINAPI
RtlLeaveCriticalSection( RTL_CRITICAL_SECTION
*crit
)
685 if (--crit
->RecursionCount
)
687 if (crit
->RecursionCount
> 0) interlocked_dec( &crit
->LockCount
);
688 else ERR( "section %p is not acquired\n", crit
);
692 crit
->OwningThread
= 0;
693 if (interlocked_dec( &crit
->LockCount
) >= 0)
695 /* someone is waiting */
696 RtlpUnWaitCriticalSection( crit
);
699 return STATUS_SUCCESS
;