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>
33 #include "wine/debug.h"
34 #include "ntdll_misc.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
37 WINE_DECLARE_DEBUG_CHANNEL(relay);
39 inline static LONG interlocked_inc( PLONG dest )
41 return interlocked_xchg_add( dest, 1 ) + 1;
44 inline static LONG interlocked_dec( PLONG dest )
46 return interlocked_xchg_add( dest, -1 ) - 1;
49 inline static void small_pause(void)
52 __asm__ __volatile__( "rep;nop" : : : "memory" );
54 __asm__ __volatile__( "" : : : "memory" );
58 /***********************************************************************
61 static inline HANDLE get_semaphore( RTL_CRITICAL_SECTION *crit )
63 HANDLE ret = crit->LockSemaphore;
67 if (NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 )) return 0;
68 if (!(ret = (HANDLE)interlocked_cmpxchg_ptr( (PVOID *)&crit->LockSemaphore,
72 NtClose(sem); /* somebody beat us to it */
77 /***********************************************************************
78 * RtlInitializeCriticalSection (NTDLL.@)
80 * Initialises a new critical section.
83 * crit [O] Critical section to initialise
89 * RtlInitializeCriticalSectionAndSpinCount(), RtlDeleteCriticalSection(),
90 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
91 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
93 NTSTATUS WINAPI RtlInitializeCriticalSection( RTL_CRITICAL_SECTION *crit )
95 return RtlInitializeCriticalSectionAndSpinCount( crit, 0 );
98 /***********************************************************************
99 * RtlInitializeCriticalSectionAndSpinCount (NTDLL.@)
101 * Initialises a new critical section with a given spin count.
104 * crit [O] Critical section to initialise
105 * spincount [I] Spin count for crit
111 * Available on NT4 SP3 or later.
114 * RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
115 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
116 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
118 NTSTATUS WINAPI RtlInitializeCriticalSectionAndSpinCount( RTL_CRITICAL_SECTION *crit, ULONG spincount )
120 if (!GetProcessHeap()) crit->DebugInfo = NULL;
123 crit->DebugInfo = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(CRITICAL_SECTION_DEBUG));
126 crit->DebugInfo->Type = 0;
127 crit->DebugInfo->CreatorBackTraceIndex = 0;
128 crit->DebugInfo->CriticalSection = crit;
129 crit->DebugInfo->ProcessLocksList.Blink = &(crit->DebugInfo->ProcessLocksList);
130 crit->DebugInfo->ProcessLocksList.Flink = &(crit->DebugInfo->ProcessLocksList);
131 crit->DebugInfo->EntryCount = 0;
132 crit->DebugInfo->ContentionCount = 0;
133 crit->DebugInfo->Spare[0] = 0;
134 crit->DebugInfo->Spare[1] = 0;
137 crit->LockCount = -1;
138 crit->RecursionCount = 0;
139 crit->OwningThread = 0;
140 crit->LockSemaphore = 0;
141 crit->SpinCount = spincount;
142 return STATUS_SUCCESS;
145 /***********************************************************************
146 * RtlSetCriticalSectionSpinCount (NTDLL.@)
148 * Sets the spin count of a critical section.
151 * crit [I/O] Critical section
152 * spincount [I] Spin count for crit
155 * The previous spin count.
158 * If the system is not SMP, spincount is ignored and set to 0.
161 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
162 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
163 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
165 ULONG WINAPI RtlSetCriticalSectionSpinCount( RTL_CRITICAL_SECTION *crit, ULONG spincount )
167 ULONG oldspincount = crit->SpinCount;
168 if (NtCurrentTeb()->Peb->NumberOfProcessors <= 1) spincount = 0;
169 crit->SpinCount = spincount;
173 /***********************************************************************
174 * RtlDeleteCriticalSection (NTDLL.@)
176 * Frees the resources used by a critical section.
179 * crit [I/O] Critical section to free
185 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
186 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
187 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
189 NTSTATUS WINAPI RtlDeleteCriticalSection( RTL_CRITICAL_SECTION *crit )
191 crit->LockCount = -1;
192 crit->RecursionCount = 0;
193 crit->OwningThread = 0;
194 if (crit->LockSemaphore) NtClose( crit->LockSemaphore );
195 crit->LockSemaphore = 0;
198 /* only free the ones we made in here */
199 if (!crit->DebugInfo->Spare[1])
201 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
202 crit->DebugInfo = NULL;
205 return STATUS_SUCCESS;
209 /***********************************************************************
210 * RtlpWaitForCriticalSection (NTDLL.@)
212 * Waits for a busy critical section to become free.
215 * crit [I/O] Critical section to wait for
221 * Use RtlEnterCriticalSection() instead of this function as it is often much
225 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
226 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
227 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
229 NTSTATUS WINAPI RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION *crit )
233 EXCEPTION_RECORD rec;
234 HANDLE sem = get_semaphore( crit );
238 time.QuadPart = -5000 * 10000; /* 5 seconds */
239 status = NtWaitForSingleObject( sem, FALSE, &time );
240 if ( status == WAIT_TIMEOUT )
242 const char *name = NULL;
243 if (crit->DebugInfo) name = (char *)crit->DebugInfo->Spare[1];
244 if (!name) name = "?";
245 ERR( "section %p %s wait timed out in thread %04lx, blocked by %04lx, retrying (60 sec)\n",
246 crit, debugstr_a(name), GetCurrentThreadId(), (DWORD)crit->OwningThread );
247 time.QuadPart = -60000 * 10000;
248 status = NtWaitForSingleObject( sem, FALSE, &time );
249 if ( status == WAIT_TIMEOUT && TRACE_ON(relay) )
251 ERR( "section %p %s wait timed out in thread %04lx, blocked by %04lx, retrying (5 min)\n",
252 crit, debugstr_a(name), GetCurrentThreadId(), (DWORD) crit->OwningThread );
253 time.QuadPart = -300000 * (ULONGLONG)10000;
254 status = NtWaitForSingleObject( sem, FALSE, &time );
257 if (status == STATUS_WAIT_0) return STATUS_SUCCESS;
259 /* Throw exception only for Wine internal locks */
260 if ((!crit->DebugInfo) || (!crit->DebugInfo->Spare[1])) continue;
262 rec.ExceptionCode = STATUS_POSSIBLE_DEADLOCK;
263 rec.ExceptionFlags = 0;
264 rec.ExceptionRecord = NULL;
265 rec.ExceptionAddress = RtlRaiseException; /* sic */
266 rec.NumberParameters = 1;
267 rec.ExceptionInformation[0] = (DWORD)crit;
268 RtlRaiseException( &rec );
273 /***********************************************************************
274 * RtlpUnWaitCriticalSection (NTDLL.@)
276 * Notifies other threads waiting on the busy critical section that it has
280 * crit [I/O] Critical section
283 * Success: STATUS_SUCCESS.
284 * Failure: Any error returned by NtReleaseSemaphore()
287 * Use RtlLeaveCriticalSection() instead of this function as it is often much
291 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
292 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
293 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
295 NTSTATUS WINAPI RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION *crit )
297 HANDLE sem = get_semaphore( crit );
298 NTSTATUS res = NtReleaseSemaphore( sem, 1, NULL );
299 if (res) RtlRaiseStatus( res );
304 /***********************************************************************
305 * RtlEnterCriticalSection (NTDLL.@)
307 * Enters a critical section, waiting for it to become available if necessary.
310 * crit [I/O] Critical section to enter
313 * STATUS_SUCCESS. The critical section is held by the caller.
316 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
317 * RtlDeleteCriticalSection(), RtlSetCriticalSectionSpinCount(),
318 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
320 NTSTATUS WINAPI RtlEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
326 if (RtlTryEnterCriticalSection( crit )) return STATUS_SUCCESS;
327 for (count = crit->SpinCount; count > 0; count--)
329 if (crit->LockCount > 0) break; /* more than one waiter, don't bother spinning */
330 if (crit->LockCount == -1) /* try again */
332 if (interlocked_cmpxchg( &crit->LockCount, 0, -1 ) == -1) goto done;
338 if (interlocked_inc( &crit->LockCount ))
340 if (crit->OwningThread == (HANDLE)GetCurrentThreadId())
342 crit->RecursionCount++;
343 return STATUS_SUCCESS;
346 /* Now wait for it */
347 RtlpWaitForCriticalSection( crit );
350 crit->OwningThread = (HANDLE)GetCurrentThreadId();
351 crit->RecursionCount = 1;
352 return STATUS_SUCCESS;
356 /***********************************************************************
357 * RtlTryEnterCriticalSection (NTDLL.@)
359 * Tries to enter a critical section without waiting.
362 * crit [I/O] Critical section to enter
365 * Success: TRUE. The critical section is held by the caller.
366 * Failure: FALSE. The critical section is currently held by another thread.
369 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
370 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
371 * RtlLeaveCriticalSection(), RtlSetCriticalSectionSpinCount()
373 BOOL WINAPI RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
376 if (interlocked_cmpxchg( &crit->LockCount, 0L, -1 ) == -1)
378 crit->OwningThread = (HANDLE)GetCurrentThreadId();
379 crit->RecursionCount = 1;
382 else if (crit->OwningThread == (HANDLE)GetCurrentThreadId())
384 interlocked_inc( &crit->LockCount );
385 crit->RecursionCount++;
392 /***********************************************************************
393 * RtlLeaveCriticalSection (NTDLL.@)
395 * Leaves a critical section.
398 * crit [I/O] Critical section to leave.
404 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
405 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
406 * RtlSetCriticalSectionSpinCount(), RtlTryEnterCriticalSection()
408 NTSTATUS WINAPI RtlLeaveCriticalSection( RTL_CRITICAL_SECTION *crit )
410 if (--crit->RecursionCount) interlocked_dec( &crit->LockCount );
413 crit->OwningThread = 0;
414 if (interlocked_dec( &crit->LockCount ) >= 0)
416 /* someone is waiting */
417 RtlpUnWaitCriticalSection( crit );
420 return STATUS_SUCCESS;