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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
27 #include <sys/types.h>
30 #include "wine/debug.h"
31 #include "ntdll_misc.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
34 WINE_DECLARE_DEBUG_CHANNEL(relay);
36 inline static LONG interlocked_inc( PLONG dest )
38 return interlocked_xchg_add( dest, 1 ) + 1;
41 inline static LONG interlocked_dec( PLONG dest )
43 return interlocked_xchg_add( dest, -1 ) - 1;
46 inline static void small_pause(void)
49 __asm__ __volatile__( "rep;nop" : : : "memory" );
51 __asm__ __volatile__( "" : : : "memory" );
55 /***********************************************************************
58 static inline HANDLE get_semaphore( RTL_CRITICAL_SECTION *crit )
60 HANDLE ret = crit->LockSemaphore;
64 if (NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 )) return 0;
65 if (!(ret = (HANDLE)interlocked_cmpxchg_ptr( (PVOID *)&crit->LockSemaphore,
69 NtClose(sem); /* somebody beat us to it */
74 /***********************************************************************
75 * RtlInitializeCriticalSection (NTDLL.@)
77 * Initialises a new critical section.
80 * crit [O] Critical section to initialise
86 * RtlInitializeCriticalSectionAndSpinCount(), RtlDeleteCriticalSection(),
87 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
88 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
90 NTSTATUS WINAPI RtlInitializeCriticalSection( RTL_CRITICAL_SECTION *crit )
92 return RtlInitializeCriticalSectionAndSpinCount( crit, 0 );
95 /***********************************************************************
96 * RtlInitializeCriticalSectionAndSpinCount (NTDLL.@)
98 * Initialises a new critical section with a given spin count.
101 * crit [O] Critical section to initialise
102 * spincount [I] Spin count for crit
108 * Available on NT4 SP3 or later.
111 * RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
112 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
113 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
115 NTSTATUS WINAPI RtlInitializeCriticalSectionAndSpinCount( RTL_CRITICAL_SECTION *crit, ULONG spincount )
117 crit->DebugInfo = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(RTL_CRITICAL_SECTION_DEBUG));
120 crit->DebugInfo->Type = 0;
121 crit->DebugInfo->CreatorBackTraceIndex = 0;
122 crit->DebugInfo->CriticalSection = crit;
123 crit->DebugInfo->ProcessLocksList.Blink = &(crit->DebugInfo->ProcessLocksList);
124 crit->DebugInfo->ProcessLocksList.Flink = &(crit->DebugInfo->ProcessLocksList);
125 crit->DebugInfo->EntryCount = 0;
126 crit->DebugInfo->ContentionCount = 0;
127 crit->DebugInfo->Spare[0] = 0;
128 crit->DebugInfo->Spare[1] = 0;
130 crit->LockCount = -1;
131 crit->RecursionCount = 0;
132 crit->OwningThread = 0;
133 crit->LockSemaphore = 0;
134 if (NtCurrentTeb()->Peb->NumberOfProcessors <= 1) spincount = 0;
135 crit->SpinCount = spincount & ~0x80000000;
136 return STATUS_SUCCESS;
139 /***********************************************************************
140 * RtlSetCriticalSectionSpinCount (NTDLL.@)
142 * Sets the spin count of a critical section.
145 * crit [I/O] Critical section
146 * spincount [I] Spin count for crit
149 * The previous spin count.
152 * If the system is not SMP, spincount is ignored and set to 0.
155 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
156 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
157 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
159 ULONG WINAPI RtlSetCriticalSectionSpinCount( RTL_CRITICAL_SECTION *crit, ULONG spincount )
161 ULONG oldspincount = crit->SpinCount;
162 if (NtCurrentTeb()->Peb->NumberOfProcessors <= 1) spincount = 0;
163 crit->SpinCount = spincount;
167 /***********************************************************************
168 * RtlDeleteCriticalSection (NTDLL.@)
170 * Frees the resources used by a critical section.
173 * crit [I/O] Critical section to free
179 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
180 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
181 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
183 NTSTATUS WINAPI RtlDeleteCriticalSection( RTL_CRITICAL_SECTION *crit )
185 crit->LockCount = -1;
186 crit->RecursionCount = 0;
187 crit->OwningThread = 0;
188 if (crit->LockSemaphore) NtClose( crit->LockSemaphore );
189 crit->LockSemaphore = 0;
192 /* only free the ones we made in here */
193 if (!crit->DebugInfo->Spare[1])
195 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
196 crit->DebugInfo = NULL;
199 return STATUS_SUCCESS;
203 /***********************************************************************
204 * RtlpWaitForCriticalSection (NTDLL.@)
206 * Waits for a busy critical section to become free.
209 * crit [I/O] Critical section to wait for
215 * Use RtlEnterCriticalSection() instead of this function as it is often much
219 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
220 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
221 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
223 NTSTATUS WINAPI RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION *crit )
227 EXCEPTION_RECORD rec;
228 HANDLE sem = get_semaphore( crit );
232 time.QuadPart = -5000 * 10000; /* 5 seconds */
233 status = NtWaitForSingleObject( sem, FALSE, &time );
234 if ( status == STATUS_TIMEOUT )
236 const char *name = NULL;
237 if (crit->DebugInfo) name = (char *)crit->DebugInfo->Spare[1];
238 if (!name) name = "?";
239 ERR( "section %p %s wait timed out in thread %04lx, blocked by %04lx, retrying (60 sec)\n",
240 crit, debugstr_a(name), GetCurrentThreadId(), (DWORD)crit->OwningThread );
241 time.QuadPart = -60000 * 10000;
242 status = NtWaitForSingleObject( sem, FALSE, &time );
243 if ( status == STATUS_TIMEOUT && TRACE_ON(relay) )
245 ERR( "section %p %s wait timed out in thread %04lx, blocked by %04lx, retrying (5 min)\n",
246 crit, debugstr_a(name), GetCurrentThreadId(), (DWORD) crit->OwningThread );
247 time.QuadPart = -300000 * (ULONGLONG)10000;
248 status = NtWaitForSingleObject( sem, FALSE, &time );
251 if (status == STATUS_WAIT_0) return STATUS_SUCCESS;
253 /* Throw exception only for Wine internal locks */
254 if ((!crit->DebugInfo) || (!crit->DebugInfo->Spare[1])) continue;
256 rec.ExceptionCode = STATUS_POSSIBLE_DEADLOCK;
257 rec.ExceptionFlags = 0;
258 rec.ExceptionRecord = NULL;
259 rec.ExceptionAddress = RtlRaiseException; /* sic */
260 rec.NumberParameters = 1;
261 rec.ExceptionInformation[0] = (ULONG_PTR)crit;
262 RtlRaiseException( &rec );
267 /***********************************************************************
268 * RtlpUnWaitCriticalSection (NTDLL.@)
270 * Notifies other threads waiting on the busy critical section that it has
274 * crit [I/O] Critical section
277 * Success: STATUS_SUCCESS.
278 * Failure: Any error returned by NtReleaseSemaphore()
281 * Use RtlLeaveCriticalSection() instead of this function as it is often much
285 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
286 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
287 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
289 NTSTATUS WINAPI RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION *crit )
291 HANDLE sem = get_semaphore( crit );
292 NTSTATUS res = NtReleaseSemaphore( sem, 1, NULL );
293 if (res) RtlRaiseStatus( res );
298 /***********************************************************************
299 * RtlEnterCriticalSection (NTDLL.@)
301 * Enters a critical section, waiting for it to become available if necessary.
304 * crit [I/O] Critical section to enter
307 * STATUS_SUCCESS. The critical section is held by the caller.
310 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
311 * RtlDeleteCriticalSection(), RtlSetCriticalSectionSpinCount(),
312 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
314 NTSTATUS WINAPI RtlEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
320 if (RtlTryEnterCriticalSection( crit )) return STATUS_SUCCESS;
321 for (count = crit->SpinCount; count > 0; count--)
323 if (crit->LockCount > 0) break; /* more than one waiter, don't bother spinning */
324 if (crit->LockCount == -1) /* try again */
326 if (interlocked_cmpxchg( &crit->LockCount, 0, -1 ) == -1) goto done;
332 if (interlocked_inc( &crit->LockCount ))
334 if (crit->OwningThread == (HANDLE)GetCurrentThreadId())
336 crit->RecursionCount++;
337 return STATUS_SUCCESS;
340 /* Now wait for it */
341 RtlpWaitForCriticalSection( crit );
344 crit->OwningThread = (HANDLE)GetCurrentThreadId();
345 crit->RecursionCount = 1;
346 return STATUS_SUCCESS;
350 /***********************************************************************
351 * RtlTryEnterCriticalSection (NTDLL.@)
353 * Tries to enter a critical section without waiting.
356 * crit [I/O] Critical section to enter
359 * Success: TRUE. The critical section is held by the caller.
360 * Failure: FALSE. The critical section is currently held by another thread.
363 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
364 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
365 * RtlLeaveCriticalSection(), RtlSetCriticalSectionSpinCount()
367 BOOL WINAPI RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
370 if (interlocked_cmpxchg( &crit->LockCount, 0L, -1 ) == -1)
372 crit->OwningThread = (HANDLE)GetCurrentThreadId();
373 crit->RecursionCount = 1;
376 else if (crit->OwningThread == (HANDLE)GetCurrentThreadId())
378 interlocked_inc( &crit->LockCount );
379 crit->RecursionCount++;
386 /***********************************************************************
387 * RtlLeaveCriticalSection (NTDLL.@)
389 * Leaves a critical section.
392 * crit [I/O] Critical section to leave.
398 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
399 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
400 * RtlSetCriticalSectionSpinCount(), RtlTryEnterCriticalSection()
402 NTSTATUS WINAPI RtlLeaveCriticalSection( RTL_CRITICAL_SECTION *crit )
404 if (--crit->RecursionCount) interlocked_dec( &crit->LockCount );
407 crit->OwningThread = 0;
408 if (interlocked_dec( &crit->LockCount ) >= 0)
410 /* someone is waiting */
411 RtlpUnWaitCriticalSection( crit );
414 return STATUS_SUCCESS;