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"
28 #include <sys/types.h>
32 #include "wine/debug.h"
33 #include "ntdll_misc.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
36 WINE_DECLARE_DEBUG_CHANNEL(relay
);
38 inline static LONG
interlocked_inc( PLONG dest
)
40 return interlocked_xchg_add( (int *)dest
, 1 ) + 1;
43 inline static LONG
interlocked_dec( PLONG dest
)
45 return interlocked_xchg_add( (int *)dest
, -1 ) - 1;
48 inline static void small_pause(void)
51 __asm__
__volatile__( "rep;nop" : : : "memory" );
53 __asm__
__volatile__( "" : : : "memory" );
57 #if defined(linux) && defined(__i386__)
59 static inline int futex_wait( int *addr
, int val
, struct timespec
*timeout
)
62 __asm__
__volatile__( "xchgl %2,%%ebx\n\t"
66 : "0" (240) /* SYS_futex */, "D" (addr
),
67 "c" (0) /* FUTEX_WAIT */, "d" (val
), "S" (timeout
) );
71 static inline int futex_wake( int *addr
, int val
)
74 __asm__
__volatile__( "xchgl %2,%%ebx\n\t"
78 : "0" (240) /* SYS_futex */, "D" (addr
),
79 "c" (1) /* FUTEX_WAKE */, "d" (val
) );
83 static inline int use_futexes(void)
85 static int supported
= -1;
87 if (supported
== -1) supported
= (futex_wait( &supported
, 10, NULL
) != -ENOSYS
);
93 static inline int futex_wait( int *addr
, int val
, struct timespec
*timeout
) { return -ENOSYS
; }
94 static inline int futex_wake( int *addr
, int val
) { return -ENOSYS
; }
95 static inline int use_futexes(void) { return 0; }
99 /***********************************************************************
102 static inline HANDLE
get_semaphore( RTL_CRITICAL_SECTION
*crit
)
104 HANDLE ret
= crit
->LockSemaphore
;
108 if (NtCreateSemaphore( &sem
, SEMAPHORE_ALL_ACCESS
, NULL
, 0, 1 )) return 0;
109 if (!(ret
= (HANDLE
)interlocked_cmpxchg_ptr( (PVOID
*)&crit
->LockSemaphore
,
113 NtClose(sem
); /* somebody beat us to it */
118 /***********************************************************************
121 static inline NTSTATUS
wait_semaphore( RTL_CRITICAL_SECTION
*crit
, int timeout
)
123 if (use_futexes() && crit
->DebugInfo
) /* debug info is cleared by MakeCriticalSectionGlobal */
126 struct timespec timespec
;
128 timespec
.tv_sec
= timeout
;
129 timespec
.tv_nsec
= 0;
130 while ((val
= interlocked_cmpxchg( (int *)&crit
->LockSemaphore
, 0, 1 )) != 1)
132 /* note: this may wait longer than specified in case of signals or */
133 /* multiple wake-ups, but that shouldn't be a problem */
134 if (futex_wait( (int *)&crit
->LockSemaphore
, val
, ×pec
) == -ETIMEDOUT
)
135 return STATUS_TIMEOUT
;
137 return STATUS_WAIT_0
;
141 HANDLE sem
= get_semaphore( crit
);
144 time
.QuadPart
= timeout
* (LONGLONG
)-10000000;
145 return NtWaitForSingleObject( sem
, FALSE
, &time
);
149 /***********************************************************************
150 * RtlInitializeCriticalSection (NTDLL.@)
152 * Initialises a new critical section.
155 * crit [O] Critical section to initialise
161 * RtlInitializeCriticalSectionAndSpinCount(), RtlDeleteCriticalSection(),
162 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
163 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
165 NTSTATUS WINAPI
RtlInitializeCriticalSection( RTL_CRITICAL_SECTION
*crit
)
167 return RtlInitializeCriticalSectionAndSpinCount( crit
, 0 );
170 /***********************************************************************
171 * RtlInitializeCriticalSectionAndSpinCount (NTDLL.@)
173 * Initialises a new critical section with a given spin count.
176 * crit [O] Critical section to initialise
177 * spincount [I] Spin count for crit
183 * Available on NT4 SP3 or later.
186 * RtlInitializeCriticalSection(), RtlDeleteCriticalSection(),
187 * RtlEnterCriticalSection(), RtlLeaveCriticalSection(),
188 * RtlTryEnterCriticalSection(), RtlSetCriticalSectionSpinCount()
190 NTSTATUS WINAPI
RtlInitializeCriticalSectionAndSpinCount( RTL_CRITICAL_SECTION
*crit
, ULONG spincount
)
192 crit
->DebugInfo
= RtlAllocateHeap(GetProcessHeap(), 0, sizeof(RTL_CRITICAL_SECTION_DEBUG
));
195 crit
->DebugInfo
->Type
= 0;
196 crit
->DebugInfo
->CreatorBackTraceIndex
= 0;
197 crit
->DebugInfo
->CriticalSection
= crit
;
198 crit
->DebugInfo
->ProcessLocksList
.Blink
= &(crit
->DebugInfo
->ProcessLocksList
);
199 crit
->DebugInfo
->ProcessLocksList
.Flink
= &(crit
->DebugInfo
->ProcessLocksList
);
200 crit
->DebugInfo
->EntryCount
= 0;
201 crit
->DebugInfo
->ContentionCount
= 0;
202 memset( crit
->DebugInfo
->Spare
, 0, sizeof(crit
->DebugInfo
->Spare
) );
204 crit
->LockCount
= -1;
205 crit
->RecursionCount
= 0;
206 crit
->OwningThread
= 0;
207 crit
->LockSemaphore
= 0;
208 if (NtCurrentTeb()->Peb
->NumberOfProcessors
<= 1) spincount
= 0;
209 crit
->SpinCount
= spincount
& ~0x80000000;
210 return STATUS_SUCCESS
;
213 /***********************************************************************
214 * RtlSetCriticalSectionSpinCount (NTDLL.@)
216 * Sets the spin count of a critical section.
219 * crit [I/O] Critical section
220 * spincount [I] Spin count for crit
223 * The previous spin count.
226 * If the system is not SMP, spincount is ignored and set to 0.
229 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
230 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
231 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
233 ULONG WINAPI
RtlSetCriticalSectionSpinCount( RTL_CRITICAL_SECTION
*crit
, ULONG spincount
)
235 ULONG oldspincount
= crit
->SpinCount
;
236 if (NtCurrentTeb()->Peb
->NumberOfProcessors
<= 1) spincount
= 0;
237 crit
->SpinCount
= spincount
;
241 /***********************************************************************
242 * RtlDeleteCriticalSection (NTDLL.@)
244 * Frees the resources used by a critical section.
247 * crit [I/O] Critical section to free
253 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
254 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
255 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
257 NTSTATUS WINAPI
RtlDeleteCriticalSection( RTL_CRITICAL_SECTION
*crit
)
259 crit
->LockCount
= -1;
260 crit
->RecursionCount
= 0;
261 crit
->OwningThread
= 0;
262 if (crit
->LockSemaphore
) NtClose( crit
->LockSemaphore
);
263 crit
->LockSemaphore
= 0;
266 /* only free the ones we made in here */
267 if (!crit
->DebugInfo
->Spare
[0])
269 RtlFreeHeap( GetProcessHeap(), 0, crit
->DebugInfo
);
270 crit
->DebugInfo
= NULL
;
273 return STATUS_SUCCESS
;
277 /***********************************************************************
278 * RtlpWaitForCriticalSection (NTDLL.@)
280 * Waits for a busy critical section to become free.
283 * crit [I/O] Critical section to wait for
289 * Use RtlEnterCriticalSection() instead of this function as it is often much
293 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
294 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
295 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
297 NTSTATUS WINAPI
RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION
*crit
)
301 EXCEPTION_RECORD rec
;
302 NTSTATUS status
= wait_semaphore( crit
, 5 );
304 if ( status
== STATUS_TIMEOUT
)
306 const char *name
= NULL
;
307 if (crit
->DebugInfo
) name
= (char *)crit
->DebugInfo
->Spare
[0];
308 if (!name
) name
= "?";
309 ERR( "section %p %s wait timed out in thread %04lx, blocked by %04lx, retrying (60 sec)\n",
310 crit
, debugstr_a(name
), GetCurrentThreadId(), (DWORD
)crit
->OwningThread
);
311 status
= wait_semaphore( crit
, 60 );
312 if ( status
== STATUS_TIMEOUT
&& TRACE_ON(relay
) )
314 ERR( "section %p %s wait timed out in thread %04lx, blocked by %04lx, retrying (5 min)\n",
315 crit
, debugstr_a(name
), GetCurrentThreadId(), (DWORD
) crit
->OwningThread
);
316 status
= wait_semaphore( crit
, 300 );
319 if (status
== STATUS_WAIT_0
) break;
321 /* Throw exception only for Wine internal locks */
322 if ((!crit
->DebugInfo
) || (!crit
->DebugInfo
->Spare
[0])) continue;
324 rec
.ExceptionCode
= STATUS_POSSIBLE_DEADLOCK
;
325 rec
.ExceptionFlags
= 0;
326 rec
.ExceptionRecord
= NULL
;
327 rec
.ExceptionAddress
= RtlRaiseException
; /* sic */
328 rec
.NumberParameters
= 1;
329 rec
.ExceptionInformation
[0] = (ULONG_PTR
)crit
;
330 RtlRaiseException( &rec
);
332 if (crit
->DebugInfo
) crit
->DebugInfo
->ContentionCount
++;
333 return STATUS_SUCCESS
;
337 /***********************************************************************
338 * RtlpUnWaitCriticalSection (NTDLL.@)
340 * Notifies other threads waiting on the busy critical section that it has
344 * crit [I/O] Critical section
347 * Success: STATUS_SUCCESS.
348 * Failure: Any error returned by NtReleaseSemaphore()
351 * Use RtlLeaveCriticalSection() instead of this function as it is often much
355 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
356 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
357 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
359 NTSTATUS WINAPI
RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION
*crit
)
361 if (use_futexes() && crit
->DebugInfo
) /* debug info is cleared by MakeCriticalSectionGlobal */
363 *(int *)&crit
->LockSemaphore
= 1;
364 futex_wake( (int *)&crit
->LockSemaphore
, 1 );
365 return STATUS_SUCCESS
;
369 HANDLE sem
= get_semaphore( crit
);
370 NTSTATUS res
= NtReleaseSemaphore( sem
, 1, NULL
);
371 if (res
) RtlRaiseStatus( res
);
377 /***********************************************************************
378 * RtlEnterCriticalSection (NTDLL.@)
380 * Enters a critical section, waiting for it to become available if necessary.
383 * crit [I/O] Critical section to enter
386 * STATUS_SUCCESS. The critical section is held by the caller.
389 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
390 * RtlDeleteCriticalSection(), RtlSetCriticalSectionSpinCount(),
391 * RtlLeaveCriticalSection(), RtlTryEnterCriticalSection()
393 NTSTATUS WINAPI
RtlEnterCriticalSection( RTL_CRITICAL_SECTION
*crit
)
399 if (RtlTryEnterCriticalSection( crit
)) return STATUS_SUCCESS
;
400 for (count
= crit
->SpinCount
; count
> 0; count
--)
402 if (crit
->LockCount
> 0) break; /* more than one waiter, don't bother spinning */
403 if (crit
->LockCount
== -1) /* try again */
405 if (interlocked_cmpxchg( (int *)&crit
->LockCount
, 0, -1 ) == -1) goto done
;
411 if (interlocked_inc( &crit
->LockCount
))
413 if (crit
->OwningThread
== (HANDLE
)GetCurrentThreadId())
415 crit
->RecursionCount
++;
416 return STATUS_SUCCESS
;
419 /* Now wait for it */
420 RtlpWaitForCriticalSection( crit
);
423 crit
->OwningThread
= (HANDLE
)GetCurrentThreadId();
424 crit
->RecursionCount
= 1;
425 return STATUS_SUCCESS
;
429 /***********************************************************************
430 * RtlTryEnterCriticalSection (NTDLL.@)
432 * Tries to enter a critical section without waiting.
435 * crit [I/O] Critical section to enter
438 * Success: TRUE. The critical section is held by the caller.
439 * Failure: FALSE. The critical section is currently held by another thread.
442 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
443 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
444 * RtlLeaveCriticalSection(), RtlSetCriticalSectionSpinCount()
446 BOOL WINAPI
RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION
*crit
)
449 if (interlocked_cmpxchg( (int *)&crit
->LockCount
, 0, -1 ) == -1)
451 crit
->OwningThread
= (HANDLE
)GetCurrentThreadId();
452 crit
->RecursionCount
= 1;
455 else if (crit
->OwningThread
== (HANDLE
)GetCurrentThreadId())
457 interlocked_inc( &crit
->LockCount
);
458 crit
->RecursionCount
++;
465 /***********************************************************************
466 * RtlLeaveCriticalSection (NTDLL.@)
468 * Leaves a critical section.
471 * crit [I/O] Critical section to leave.
477 * RtlInitializeCriticalSection(), RtlInitializeCriticalSectionAndSpinCount(),
478 * RtlDeleteCriticalSection(), RtlEnterCriticalSection(),
479 * RtlSetCriticalSectionSpinCount(), RtlTryEnterCriticalSection()
481 NTSTATUS WINAPI
RtlLeaveCriticalSection( RTL_CRITICAL_SECTION
*crit
)
483 if (--crit
->RecursionCount
) interlocked_dec( &crit
->LockCount
);
486 crit
->OwningThread
= 0;
487 if (interlocked_dec( &crit
->LockCount
) >= 0)
489 /* someone is waiting */
490 RtlpUnWaitCriticalSection( crit
);
493 return STATUS_SUCCESS
;