kernel32: Move ReadConsole and WriteConsole to kernelbase.
[wine.git] / dlls / ntdll / critsection.c
blobfe7d933c0fa8eb82dc4ecdacc00aba51a289b032
1 /*
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
21 #include <assert.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <time.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winternl.h"
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)
39 #ifdef __i386__
40 __asm__ __volatile__( "rep;nop" : : : "memory" );
41 #else
42 __asm__ __volatile__( "" : : : "memory" );
43 #endif
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 /***********************************************************************
54 * get_semaphore
56 static inline HANDLE get_semaphore( RTL_CRITICAL_SECTION *crit )
58 HANDLE ret = crit->LockSemaphore;
59 if (!ret)
61 HANDLE sem;
62 if (NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 )) return 0;
63 if (!(ret = InterlockedCompareExchangePointer( &crit->LockSemaphore, sem, 0 )))
64 ret = sem;
65 else
66 NtClose(sem); /* somebody beat us to it */
68 return ret;
71 /***********************************************************************
72 * wait_semaphore
74 static inline NTSTATUS wait_semaphore( RTL_CRITICAL_SECTION *crit, int timeout )
76 NTSTATUS ret;
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 );
83 LARGE_INTEGER time;
85 time.QuadPart = timeout * (LONGLONG)-10000000;
86 ret = NtWaitForSingleObject( sem, FALSE, &time );
88 return ret;
91 /***********************************************************************
92 * RtlInitializeCriticalSection (NTDLL.@)
94 * Initialises a new critical section.
96 * PARAMS
97 * crit [O] Critical section to initialise
99 * RETURNS
100 * STATUS_SUCCESS.
102 * SEE
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.
118 * PARAMS
119 * crit [O] Critical section to initialise
120 * spincount [I] Spin count for crit
122 * RETURNS
123 * STATUS_SUCCESS.
125 * NOTES
126 * Available on NT4 SP3 or later.
128 * SEE
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.
144 * PARAMS
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.
149 * RETURNS
150 * STATUS_SUCCESS.
152 * NOTES
153 * Available on Vista or later.
155 * SEE
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;
173 else
175 crit->DebugInfo = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(RTL_CRITICAL_SECTION_DEBUG));
176 if (crit->DebugInfo)
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.
202 * PARAMS
203 * crit [I/O] Critical section
204 * spincount [I] Spin count for crit
206 * RETURNS
207 * The previous spin count.
209 * NOTES
210 * If the system is not SMP, spincount is ignored and set to 0.
212 * SEE
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;
223 return oldspincount;
226 /***********************************************************************
227 * RtlDeleteCriticalSection (NTDLL.@)
229 * Frees the resources used by a critical section.
231 * PARAMS
232 * crit [I/O] Critical section to free
234 * RETURNS
235 * STATUS_SUCCESS.
237 * SEE
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.
270 * PARAMS
271 * crit [I/O] Critical section to wait for
273 * RETURNS
274 * STATUS_SUCCESS.
276 * NOTES
277 * Use RtlEnterCriticalSection() instead of this function as it is often much
278 * faster.
280 * SEE
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;
298 for (;;)
300 EXCEPTION_RECORD rec;
301 NTSTATUS status = wait_semaphore( crit, 5 );
302 timeout -= 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 );
312 timeout -= 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 );
319 timeout -= 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
347 * become free.
349 * PARAMS
350 * crit [I/O] Critical section
352 * RETURNS
353 * Success: STATUS_SUCCESS.
354 * Failure: Any error returned by NtReleaseSemaphore()
356 * NOTES
357 * Use RtlLeaveCriticalSection() instead of this function as it is often much
358 * faster.
360 * SEE
361 * RtlInitializeCriticalSectionEx(),
362 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
363 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
364 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
366 NTSTATUS WINAPI RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION *crit )
368 NTSTATUS ret;
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 );
378 return ret;
382 /***********************************************************************
383 * RtlEnterCriticalSection (NTDLL.@)
385 * Enters a critical section, waiting for it to become available if necessary.
387 * PARAMS
388 * crit [I/O] Critical section to enter
390 * RETURNS
391 * STATUS_SUCCESS. The critical section is held by the caller.
393 * SEE
394 * RtlInitializeCriticalSectionEx(),
395 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
396 * RtlDeleteCriticalSection(), RtlSetCriticalSectionSpinCount(),
397 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
399 NTSTATUS WINAPI RtlEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
401 if (crit->SpinCount)
403 ULONG count;
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;
413 small_pause();
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 );
428 done:
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.
440 * PARAMS
441 * crit [I/O] Critical section to enter
443 * RETURNS
444 * Success: TRUE. The critical section is held by the caller.
445 * Failure: FALSE. The critical section is currently held by another thread.
447 * SEE
448 * RtlInitializeCriticalSectionEx(),
449 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
450 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
451 * RtlLeaveCriticalSection(), RtlSetCriticalSectionSpinCount()
453 BOOL WINAPI RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
455 BOOL ret = FALSE;
456 if (InterlockedCompareExchange( &crit->LockCount, 0, -1 ) == -1)
458 crit->OwningThread = ULongToHandle(GetCurrentThreadId());
459 crit->RecursionCount = 1;
460 ret = TRUE;
462 else if (crit->OwningThread == ULongToHandle(GetCurrentThreadId()))
464 InterlockedIncrement( &crit->LockCount );
465 crit->RecursionCount++;
466 ret = TRUE;
468 return ret;
472 /***********************************************************************
473 * RtlIsCriticalSectionLocked (NTDLL.@)
475 * Checks if the critical section is locked by any thread.
477 * PARAMS
478 * crit [I/O] Critical section to check.
480 * RETURNS
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.
495 * PARAMS
496 * crit [I/O] Critical section to check.
498 * RETURNS
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.
514 * PARAMS
515 * crit [I/O] Critical section to leave.
517 * RETURNS
518 * STATUS_SUCCESS.
520 * SEE
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 );
533 else
535 crit->OwningThread = 0;
536 if (InterlockedDecrement( &crit->LockCount ) >= 0)
538 /* someone is waiting */
539 RtlpUnWaitCriticalSection( crit );
542 return STATUS_SUCCESS;