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 if (!GetProcessHeap()) crit->DebugInfo = NULL;
120 crit->DebugInfo = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(RTL_CRITICAL_SECTION_DEBUG));
123 crit->DebugInfo->Type = 0;
124 crit->DebugInfo->CreatorBackTraceIndex = 0;
125 crit->DebugInfo->CriticalSection = crit;
126 crit->DebugInfo->ProcessLocksList.Blink = &(crit->DebugInfo->ProcessLocksList);
127 crit->DebugInfo->ProcessLocksList.Flink = &(crit->DebugInfo->ProcessLocksList);
128 crit->DebugInfo->EntryCount = 0;
129 crit->DebugInfo->ContentionCount = 0;
130 crit->DebugInfo->Spare[0] = 0;
131 crit->DebugInfo->Spare[1] = 0;
134 crit->LockCount = -1;
135 crit->RecursionCount = 0;
136 crit->OwningThread = 0;
137 crit->LockSemaphore = 0;
138 if (NtCurrentTeb()->Peb->NumberOfProcessors <= 1) spincount = 0;
139 crit->SpinCount = spincount & ~0x80000000;
140 return STATUS_SUCCESS;
143 /***********************************************************************
144 * RtlSetCriticalSectionSpinCount (NTDLL.@)
146 * Sets the spin count of a critical section.
149 * crit [I/O] Critical section
150 * spincount [I] Spin count for crit
153 * The previous spin count.
156 * If the system is not SMP, spincount is ignored and set to 0.
159 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
160 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
161 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
163 ULONG WINAPI RtlSetCriticalSectionSpinCount( RTL_CRITICAL_SECTION *crit, ULONG spincount )
165 ULONG oldspincount = crit->SpinCount;
166 if (NtCurrentTeb()->Peb->NumberOfProcessors <= 1) spincount = 0;
167 crit->SpinCount = spincount;
171 /***********************************************************************
172 * RtlDeleteCriticalSection (NTDLL.@)
174 * Frees the resources used by a critical section.
177 * crit [I/O] Critical section to free
183 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
184 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
185 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
187 NTSTATUS WINAPI RtlDeleteCriticalSection( RTL_CRITICAL_SECTION *crit )
189 crit->LockCount = -1;
190 crit->RecursionCount = 0;
191 crit->OwningThread = 0;
192 if (crit->LockSemaphore) NtClose( crit->LockSemaphore );
193 crit->LockSemaphore = 0;
196 /* only free the ones we made in here */
197 if (!crit->DebugInfo->Spare[1])
199 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
200 crit->DebugInfo = NULL;
203 return STATUS_SUCCESS;
207 /***********************************************************************
208 * RtlpWaitForCriticalSection (NTDLL.@)
210 * Waits for a busy critical section to become free.
213 * crit [I/O] Critical section to wait for
219 * Use RtlEnterCriticalSection() instead of this function as it is often much
223 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
224 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
225 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
227 NTSTATUS WINAPI RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION *crit )
231 EXCEPTION_RECORD rec;
232 HANDLE sem = get_semaphore( crit );
236 time.QuadPart = -5000 * 10000; /* 5 seconds */
237 status = NtWaitForSingleObject( sem, FALSE, &time );
238 if ( status == STATUS_TIMEOUT )
240 const char *name = NULL;
241 if (crit->DebugInfo) name = (char *)crit->DebugInfo->Spare[1];
242 if (!name) name = "?";
243 ERR( "section %p %s wait timed out in thread %04lx, blocked by %04lx, retrying (60 sec)\n",
244 crit, debugstr_a(name), GetCurrentThreadId(), (DWORD)crit->OwningThread );
245 time.QuadPart = -60000 * 10000;
246 status = NtWaitForSingleObject( sem, FALSE, &time );
247 if ( status == STATUS_TIMEOUT && TRACE_ON(relay) )
249 ERR( "section %p %s wait timed out in thread %04lx, blocked by %04lx, retrying (5 min)\n",
250 crit, debugstr_a(name), GetCurrentThreadId(), (DWORD) crit->OwningThread );
251 time.QuadPart = -300000 * (ULONGLONG)10000;
252 status = NtWaitForSingleObject( sem, FALSE, &time );
255 if (status == STATUS_WAIT_0) return STATUS_SUCCESS;
257 /* Throw exception only for Wine internal locks */
258 if ((!crit->DebugInfo) || (!crit->DebugInfo->Spare[1])) continue;
260 rec.ExceptionCode = STATUS_POSSIBLE_DEADLOCK;
261 rec.ExceptionFlags = 0;
262 rec.ExceptionRecord = NULL;
263 rec.ExceptionAddress = RtlRaiseException; /* sic */
264 rec.NumberParameters = 1;
265 rec.ExceptionInformation[0] = (ULONG_PTR)crit;
266 RtlRaiseException( &rec );
271 /***********************************************************************
272 * RtlpUnWaitCriticalSection (NTDLL.@)
274 * Notifies other threads waiting on the busy critical section that it has
278 * crit [I/O] Critical section
281 * Success: STATUS_SUCCESS.
282 * Failure: Any error returned by NtReleaseSemaphore()
285 * Use RtlLeaveCriticalSection() instead of this function as it is often much
289 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
290 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
291 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
293 NTSTATUS WINAPI RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION *crit )
295 HANDLE sem = get_semaphore( crit );
296 NTSTATUS res = NtReleaseSemaphore( sem, 1, NULL );
297 if (res) RtlRaiseStatus( res );
302 /***********************************************************************
303 * RtlEnterCriticalSection (NTDLL.@)
305 * Enters a critical section, waiting for it to become available if necessary.
308 * crit [I/O] Critical section to enter
311 * STATUS_SUCCESS. The critical section is held by the caller.
314 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
315 * RtlDeleteCriticalSection(), RtlSetCriticalSectionSpinCount(),
316 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
318 NTSTATUS WINAPI RtlEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
324 if (RtlTryEnterCriticalSection( crit )) return STATUS_SUCCESS;
325 for (count = crit->SpinCount; count > 0; count--)
327 if (crit->LockCount > 0) break; /* more than one waiter, don't bother spinning */
328 if (crit->LockCount == -1) /* try again */
330 if (interlocked_cmpxchg( &crit->LockCount, 0, -1 ) == -1) goto done;
336 if (interlocked_inc( &crit->LockCount ))
338 if (crit->OwningThread == (HANDLE)GetCurrentThreadId())
340 crit->RecursionCount++;
341 return STATUS_SUCCESS;
344 /* Now wait for it */
345 RtlpWaitForCriticalSection( crit );
348 crit->OwningThread = (HANDLE)GetCurrentThreadId();
349 crit->RecursionCount = 1;
350 return STATUS_SUCCESS;
354 /***********************************************************************
355 * RtlTryEnterCriticalSection (NTDLL.@)
357 * Tries to enter a critical section without waiting.
360 * crit [I/O] Critical section to enter
363 * Success: TRUE. The critical section is held by the caller.
364 * Failure: FALSE. The critical section is currently held by another thread.
367 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
368 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
369 * RtlLeaveCriticalSection(), RtlSetCriticalSectionSpinCount()
371 BOOL WINAPI RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
374 if (interlocked_cmpxchg( &crit->LockCount, 0L, -1 ) == -1)
376 crit->OwningThread = (HANDLE)GetCurrentThreadId();
377 crit->RecursionCount = 1;
380 else if (crit->OwningThread == (HANDLE)GetCurrentThreadId())
382 interlocked_inc( &crit->LockCount );
383 crit->RecursionCount++;
390 /***********************************************************************
391 * RtlLeaveCriticalSection (NTDLL.@)
393 * Leaves a critical section.
396 * crit [I/O] Critical section to leave.
402 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
403 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
404 * RtlSetCriticalSectionSpinCount(), RtlTryEnterCriticalSection()
406 NTSTATUS WINAPI RtlLeaveCriticalSection( RTL_CRITICAL_SECTION *crit )
408 if (--crit->RecursionCount) interlocked_dec( &crit->LockCount );
411 crit->OwningThread = 0;
412 if (interlocked_dec( &crit->LockCount ) >= 0)
414 /* someone is waiting */
415 RtlpUnWaitCriticalSection( crit );
418 return STATUS_SUCCESS;