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
25 #include <sys/types.h>
28 #define WIN32_NO_STATUS
31 #include "wine/debug.h"
32 #include "ntdll_misc.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
35 WINE_DECLARE_DEBUG_CHANNEL(relay
);
37 static inline void small_pause(void)
40 __asm__
__volatile__( "rep;nop" : : : "memory" );
42 __asm__
__volatile__( "" : : : "memory" );
46 static void *no_debug_info_marker
= (void *)(ULONG_PTR
)-1;
48 static BOOL
crit_section_has_debuginfo(const RTL_CRITICAL_SECTION
*crit
)
50 return crit
->DebugInfo
!= NULL
&& crit
->DebugInfo
!= no_debug_info_marker
;
53 /***********************************************************************
56 static inline HANDLE
get_semaphore( RTL_CRITICAL_SECTION
*crit
)
58 HANDLE ret
= crit
->LockSemaphore
;
62 if (NtCreateSemaphore( &sem
, SEMAPHORE_ALL_ACCESS
, NULL
, 0, 1 )) return 0;
63 if (!(ret
= InterlockedCompareExchangePointer( &crit
->LockSemaphore
, sem
, 0 )))
66 NtClose(sem
); /* somebody beat us to it */
71 /***********************************************************************
74 static inline NTSTATUS
wait_semaphore( RTL_CRITICAL_SECTION
*crit
, int timeout
)
78 /* debug info is cleared by MakeCriticalSectionGlobal */
79 if (!crit_section_has_debuginfo( crit
) ||
80 ((ret
= unix_funcs
->fast_RtlpWaitForCriticalSection( crit
, timeout
)) == STATUS_NOT_IMPLEMENTED
))
82 HANDLE sem
= get_semaphore( crit
);
85 time
.QuadPart
= timeout
* (LONGLONG
)-10000000;
86 ret
= NtWaitForSingleObject( sem
, FALSE
, &time
);
91 /***********************************************************************
92 * RtlInitializeCriticalSection (NTDLL.@)
94 * Initialises a new critical section.
97 * crit [O] Critical section to initialise
103 * RtlInitializeCriticalSectionEx(),
104 * RtlInitializeCriticalSectionAndSpinCount(), RtlDeleteCriticalSection(),
105 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
106 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
108 NTSTATUS WINAPI
RtlInitializeCriticalSection( RTL_CRITICAL_SECTION
*crit
)
110 return RtlInitializeCriticalSectionEx( crit
, 0, 0 );
113 /***********************************************************************
114 * RtlInitializeCriticalSectionAndSpinCount (NTDLL.@)
116 * Initialises a new critical section with a given spin count.
119 * crit [O] Critical section to initialise
120 * spincount [I] Spin count for crit
126 * Available on NT4 SP3 or later.
129 * RtlInitializeCriticalSectionEx(),
130 * RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
131 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
132 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
134 NTSTATUS WINAPI
RtlInitializeCriticalSectionAndSpinCount( RTL_CRITICAL_SECTION
*crit
, ULONG spincount
)
136 return RtlInitializeCriticalSectionEx( crit
, spincount
, 0 );
139 /***********************************************************************
140 * RtlInitializeCriticalSectionEx (NTDLL.@)
142 * Initialises a new critical section with a given spin count and flags.
145 * crit [O] Critical section to initialise.
146 * spincount [I] Number of times to spin upon contention.
147 * flags [I] RTL_CRITICAL_SECTION_FLAG_ flags from winnt.h.
153 * Available on Vista or later.
156 * RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
157 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
158 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
160 NTSTATUS WINAPI
RtlInitializeCriticalSectionEx( RTL_CRITICAL_SECTION
*crit
, ULONG spincount
, ULONG flags
)
162 if (flags
& (RTL_CRITICAL_SECTION_FLAG_DYNAMIC_SPIN
|RTL_CRITICAL_SECTION_FLAG_STATIC_INIT
))
163 FIXME("(%p,%u,0x%08x) semi-stub\n", crit
, spincount
, flags
);
165 /* FIXME: if RTL_CRITICAL_SECTION_FLAG_STATIC_INIT is given, we should use
166 * memory from a static pool to hold the debug info. Then heap.c could pass
167 * this flag rather than initialising the process heap CS by hand. If this
168 * is done, then debug info should be managed through Rtlp[Allocate|Free]DebugInfo
169 * so (e.g.) MakeCriticalSectionGlobal() doesn't free it using HeapFree().
171 if (flags
& RTL_CRITICAL_SECTION_FLAG_NO_DEBUG_INFO
)
172 crit
->DebugInfo
= no_debug_info_marker
;
175 crit
->DebugInfo
= RtlAllocateHeap(GetProcessHeap(), 0, sizeof(RTL_CRITICAL_SECTION_DEBUG
));
178 crit
->DebugInfo
->Type
= 0;
179 crit
->DebugInfo
->CreatorBackTraceIndex
= 0;
180 crit
->DebugInfo
->CriticalSection
= crit
;
181 crit
->DebugInfo
->ProcessLocksList
.Blink
= &(crit
->DebugInfo
->ProcessLocksList
);
182 crit
->DebugInfo
->ProcessLocksList
.Flink
= &(crit
->DebugInfo
->ProcessLocksList
);
183 crit
->DebugInfo
->EntryCount
= 0;
184 crit
->DebugInfo
->ContentionCount
= 0;
185 memset( crit
->DebugInfo
->Spare
, 0, sizeof(crit
->DebugInfo
->Spare
) );
188 crit
->LockCount
= -1;
189 crit
->RecursionCount
= 0;
190 crit
->OwningThread
= 0;
191 crit
->LockSemaphore
= 0;
192 if (NtCurrentTeb()->Peb
->NumberOfProcessors
<= 1) spincount
= 0;
193 crit
->SpinCount
= spincount
& ~0x80000000;
194 return STATUS_SUCCESS
;
197 /***********************************************************************
198 * RtlSetCriticalSectionSpinCount (NTDLL.@)
200 * Sets the spin count of a critical section.
203 * crit [I/O] Critical section
204 * spincount [I] Spin count for crit
207 * The previous spin count.
210 * If the system is not SMP, spincount is ignored and set to 0.
213 * RtlInitializeCriticalSectionEx(),
214 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
215 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
216 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
218 ULONG WINAPI
RtlSetCriticalSectionSpinCount( RTL_CRITICAL_SECTION
*crit
, ULONG spincount
)
220 ULONG oldspincount
= crit
->SpinCount
;
221 if (NtCurrentTeb()->Peb
->NumberOfProcessors
<= 1) spincount
= 0;
222 crit
->SpinCount
= spincount
;
226 /***********************************************************************
227 * RtlDeleteCriticalSection (NTDLL.@)
229 * Frees the resources used by a critical section.
232 * crit [I/O] Critical section to free
238 * RtlInitializeCriticalSectionEx(),
239 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
240 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
241 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
243 NTSTATUS WINAPI
RtlDeleteCriticalSection( RTL_CRITICAL_SECTION
*crit
)
245 crit
->LockCount
= -1;
246 crit
->RecursionCount
= 0;
247 crit
->OwningThread
= 0;
248 if (crit_section_has_debuginfo( crit
))
250 /* only free the ones we made in here */
251 if (!crit
->DebugInfo
->Spare
[0])
253 RtlFreeHeap( GetProcessHeap(), 0, crit
->DebugInfo
);
254 crit
->DebugInfo
= NULL
;
256 if (unix_funcs
->fast_RtlDeleteCriticalSection( crit
) == STATUS_NOT_IMPLEMENTED
)
257 NtClose( crit
->LockSemaphore
);
259 else NtClose( crit
->LockSemaphore
);
260 crit
->LockSemaphore
= 0;
261 return STATUS_SUCCESS
;
265 /***********************************************************************
266 * RtlpWaitForCriticalSection (NTDLL.@)
268 * Waits for a busy critical section to become free.
271 * crit [I/O] Critical section to wait for
277 * Use RtlEnterCriticalSection() instead of this function as it is often much
281 * RtlInitializeCriticalSectionEx(),
282 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
283 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
284 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
286 NTSTATUS WINAPI
RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION
*crit
)
288 LONGLONG timeout
= NtCurrentTeb()->Peb
->CriticalSectionTimeout
.QuadPart
/ -10000000;
290 /* Don't allow blocking on a critical section during process termination */
291 if (RtlDllShutdownInProgress())
293 WARN( "process %s is shutting down, returning STATUS_SUCCESS\n",
294 debugstr_w(NtCurrentTeb()->Peb
->ProcessParameters
->ImagePathName
.Buffer
) );
295 return STATUS_SUCCESS
;
300 EXCEPTION_RECORD rec
;
301 NTSTATUS status
= wait_semaphore( crit
, 5 );
304 if ( status
== STATUS_TIMEOUT
)
306 const char *name
= NULL
;
307 if (crit_section_has_debuginfo( crit
)) name
= (char *)crit
->DebugInfo
->Spare
[0];
308 if (!name
) name
= "?";
309 ERR( "section %p %s wait timed out in thread %04x, blocked by %04x, retrying (60 sec)\n",
310 crit
, debugstr_a(name
), GetCurrentThreadId(), HandleToULong(crit
->OwningThread
) );
311 status
= wait_semaphore( crit
, 60 );
314 if ( status
== STATUS_TIMEOUT
&& TRACE_ON(relay
) )
316 ERR( "section %p %s wait timed out in thread %04x, blocked by %04x, retrying (5 min)\n",
317 crit
, debugstr_a(name
), GetCurrentThreadId(), HandleToULong(crit
->OwningThread
) );
318 status
= wait_semaphore( crit
, 300 );
322 if (status
== STATUS_WAIT_0
) break;
324 /* Throw exception only for Wine internal locks */
325 if (!crit_section_has_debuginfo( crit
) || !crit
->DebugInfo
->Spare
[0]) continue;
327 /* only throw deadlock exception if configured timeout is reached */
328 if (timeout
> 0) continue;
330 rec
.ExceptionCode
= STATUS_POSSIBLE_DEADLOCK
;
331 rec
.ExceptionFlags
= 0;
332 rec
.ExceptionRecord
= NULL
;
333 rec
.ExceptionAddress
= RtlRaiseException
; /* sic */
334 rec
.NumberParameters
= 1;
335 rec
.ExceptionInformation
[0] = (ULONG_PTR
)crit
;
336 RtlRaiseException( &rec
);
338 if (crit_section_has_debuginfo( crit
)) crit
->DebugInfo
->ContentionCount
++;
339 return STATUS_SUCCESS
;
343 /***********************************************************************
344 * RtlpUnWaitCriticalSection (NTDLL.@)
346 * Notifies other threads waiting on the busy critical section that it has
350 * crit [I/O] Critical section
353 * Success: STATUS_SUCCESS.
354 * Failure: Any error returned by NtReleaseSemaphore()
357 * Use RtlLeaveCriticalSection() instead of this function as it is often much
361 * RtlInitializeCriticalSectionEx(),
362 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
363 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
364 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
366 NTSTATUS WINAPI
RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION
*crit
)
370 /* debug info is cleared by MakeCriticalSectionGlobal */
371 if (!crit_section_has_debuginfo( crit
) ||
372 ((ret
= unix_funcs
->fast_RtlpUnWaitCriticalSection( crit
)) == STATUS_NOT_IMPLEMENTED
))
374 HANDLE sem
= get_semaphore( crit
);
375 ret
= NtReleaseSemaphore( sem
, 1, NULL
);
377 if (ret
) RtlRaiseStatus( ret
);
382 /***********************************************************************
383 * RtlEnterCriticalSection (NTDLL.@)
385 * Enters a critical section, waiting for it to become available if necessary.
388 * crit [I/O] Critical section to enter
391 * STATUS_SUCCESS. The critical section is held by the caller.
394 * RtlInitializeCriticalSectionEx(),
395 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
396 * RtlDeleteCriticalSection(), RtlSetCriticalSectionSpinCount(),
397 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
399 NTSTATUS WINAPI
RtlEnterCriticalSection( RTL_CRITICAL_SECTION
*crit
)
405 if (RtlTryEnterCriticalSection( crit
)) return STATUS_SUCCESS
;
406 for (count
= crit
->SpinCount
; count
> 0; count
--)
408 if (crit
->LockCount
> 0) break; /* more than one waiter, don't bother spinning */
409 if (crit
->LockCount
== -1) /* try again */
411 if (InterlockedCompareExchange( &crit
->LockCount
, 0, -1 ) == -1) goto done
;
417 if (InterlockedIncrement( &crit
->LockCount
))
419 if (crit
->OwningThread
== ULongToHandle(GetCurrentThreadId()))
421 crit
->RecursionCount
++;
422 return STATUS_SUCCESS
;
425 /* Now wait for it */
426 RtlpWaitForCriticalSection( crit
);
429 crit
->OwningThread
= ULongToHandle(GetCurrentThreadId());
430 crit
->RecursionCount
= 1;
431 return STATUS_SUCCESS
;
435 /***********************************************************************
436 * RtlTryEnterCriticalSection (NTDLL.@)
438 * Tries to enter a critical section without waiting.
441 * crit [I/O] Critical section to enter
444 * Success: TRUE. The critical section is held by the caller.
445 * Failure: FALSE. The critical section is currently held by another thread.
448 * RtlInitializeCriticalSectionEx(),
449 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
450 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
451 * RtlLeaveCriticalSection(), RtlSetCriticalSectionSpinCount()
453 BOOL WINAPI
RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION
*crit
)
456 if (InterlockedCompareExchange( &crit
->LockCount
, 0, -1 ) == -1)
458 crit
->OwningThread
= ULongToHandle(GetCurrentThreadId());
459 crit
->RecursionCount
= 1;
462 else if (crit
->OwningThread
== ULongToHandle(GetCurrentThreadId()))
464 InterlockedIncrement( &crit
->LockCount
);
465 crit
->RecursionCount
++;
472 /***********************************************************************
473 * RtlIsCriticalSectionLocked (NTDLL.@)
475 * Checks if the critical section is locked by any thread.
478 * crit [I/O] Critical section to check.
481 * Success: TRUE. The critical section is locked.
482 * Failure: FALSE. The critical section is not locked.
484 BOOL WINAPI
RtlIsCriticalSectionLocked( RTL_CRITICAL_SECTION
*crit
)
486 return crit
->RecursionCount
!= 0;
490 /***********************************************************************
491 * RtlIsCriticalSectionLockedByThread (NTDLL.@)
493 * Checks if the critical section is locked by the current thread.
496 * crit [I/O] Critical section to check.
499 * Success: TRUE. The critical section is locked.
500 * Failure: FALSE. The critical section is not locked.
502 BOOL WINAPI
RtlIsCriticalSectionLockedByThread( RTL_CRITICAL_SECTION
*crit
)
504 return crit
->OwningThread
== ULongToHandle(GetCurrentThreadId()) &&
505 crit
->RecursionCount
;
509 /***********************************************************************
510 * RtlLeaveCriticalSection (NTDLL.@)
512 * Leaves a critical section.
515 * crit [I/O] Critical section to leave.
521 * RtlInitializeCriticalSectionEx(),
522 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
523 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
524 * RtlSetCriticalSectionSpinCount(), RtlTryEnterCriticalSection()
526 NTSTATUS WINAPI
RtlLeaveCriticalSection( RTL_CRITICAL_SECTION
*crit
)
528 if (--crit
->RecursionCount
)
530 if (crit
->RecursionCount
> 0) InterlockedDecrement( &crit
->LockCount
);
531 else ERR( "section %p is not acquired\n", crit
);
535 crit
->OwningThread
= 0;
536 if (InterlockedDecrement( &crit
->LockCount
) >= 0)
538 /* someone is waiting */
539 RtlpUnWaitCriticalSection( crit
);
542 return STATUS_SUCCESS
;