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"
26 #include <sys/types.h>
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
32 WINE_DECLARE_DEBUG_CHANNEL(relay);
34 inline static LONG interlocked_inc( PLONG dest )
36 return interlocked_xchg_add( dest, 1 ) + 1;
39 inline static LONG interlocked_dec( PLONG dest )
41 return interlocked_xchg_add( dest, -1 ) - 1;
44 /***********************************************************************
47 static inline HANDLE get_semaphore( RTL_CRITICAL_SECTION *crit )
49 HANDLE ret = crit->LockSemaphore;
53 if (NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 )) return 0;
54 if (!(ret = (HANDLE)interlocked_cmpxchg_ptr( (PVOID *)&crit->LockSemaphore,
58 NtClose(sem); /* somebody beat us to it */
63 /***********************************************************************
64 * RtlInitializeCriticalSection (NTDLL.@)
66 * Initialise a new RTL_CRITICAL_SECTION.
69 * crit [O] Critical section to initialise
74 NTSTATUS WINAPI RtlInitializeCriticalSection( RTL_CRITICAL_SECTION *crit )
76 crit->DebugInfo = NULL;
78 crit->RecursionCount = 0;
79 crit->OwningThread = 0;
80 crit->LockSemaphore = 0;
81 return STATUS_SUCCESS;
84 /***********************************************************************
85 * RtlInitializeCriticalSectionAndSpinCount (NTDLL.@)
87 * Initialise a new RTL_CRITICAL_SECTION with a given spin count.
90 * crit [O] Critical section to initialise
91 * spincount [I] Spin count for crit
97 * The InitializeCriticalSectionAndSpinCount() (KERNEL32) function is
98 * available on NT4SP3 or later, and Win98 or later.
99 * I am assuming that this is the correct definition given the MSDN
100 * docs for the kernel32 functions.
102 NTSTATUS WINAPI RtlInitializeCriticalSectionAndSpinCount( RTL_CRITICAL_SECTION *crit, DWORD spincount )
104 if(spincount) TRACE("critsection=%p: spincount=%ld not supported\n", crit, spincount);
105 crit->SpinCount = spincount;
106 return RtlInitializeCriticalSection( crit );
110 /***********************************************************************
111 * RtlDeleteCriticalSection (NTDLL.@)
113 * Free the resources used by an RTL_CRITICAL_SECTION.
116 * crit [I/O] Critical section to free
121 NTSTATUS WINAPI RtlDeleteCriticalSection( RTL_CRITICAL_SECTION *crit )
123 crit->LockCount = -1;
124 crit->RecursionCount = 0;
125 crit->OwningThread = 0;
126 if (crit->LockSemaphore) NtClose( crit->LockSemaphore );
127 crit->LockSemaphore = 0;
128 return STATUS_SUCCESS;
132 /***********************************************************************
133 * RtlpWaitForCriticalSection (NTDLL.@)
135 * Wait for an RTL_CRITICAL_SECTION to become free.
138 * crit [I/O] Critical section to wait for
143 NTSTATUS WINAPI RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION *crit )
147 EXCEPTION_RECORD rec;
148 HANDLE sem = get_semaphore( crit );
152 time.QuadPart = -5000 * 10000; /* 5 seconds */
153 status = NtWaitForSingleObject( sem, FALSE, &time );
154 if ( status == WAIT_TIMEOUT )
156 const char *name = (char *)crit->DebugInfo;
157 if (!name) name = "?";
158 ERR( "section %p %s wait timed out, retrying (60 sec) tid=%04lx\n",
159 crit, debugstr_a(name), GetCurrentThreadId() );
160 time.QuadPart = -60000 * 10000;
161 status = NtWaitForSingleObject( sem, FALSE, &time );
162 if ( status == WAIT_TIMEOUT && TRACE_ON(relay) )
164 ERR( "section %p %s wait timed out, retrying (5 min) tid=%04lx\n",
165 crit, debugstr_a(name), GetCurrentThreadId() );
166 time.QuadPart = -300000 * (ULONGLONG)10000;
167 status = NtWaitForSingleObject( sem, FALSE, &time );
170 if (status == STATUS_WAIT_0) return STATUS_SUCCESS;
172 /* Throw exception only for Wine internal locks */
173 if (!crit->DebugInfo) continue;
175 rec.ExceptionCode = STATUS_POSSIBLE_DEADLOCK;
176 rec.ExceptionFlags = 0;
177 rec.ExceptionRecord = NULL;
178 rec.ExceptionAddress = RtlRaiseException; /* sic */
179 rec.NumberParameters = 1;
180 rec.ExceptionInformation[0] = (DWORD)crit;
181 RtlRaiseException( &rec );
186 /***********************************************************************
187 * RtlpUnWaitCriticalSection (NTDLL.@)
189 NTSTATUS WINAPI RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION *crit )
191 HANDLE sem = get_semaphore( crit );
192 NTSTATUS res = NtReleaseSemaphore( sem, 1, NULL );
193 if (res) RtlRaiseStatus( res );
198 /***********************************************************************
199 * RtlEnterCriticalSection (NTDLL.@)
201 * Enter an RTL_CRITICAL_SECTION.
204 * crit [I/O] Critical section to enter
207 * STATUS_SUCCESS. The critical section is held by the caller.
210 * The caller will wait until the critical section is availale.
212 NTSTATUS WINAPI RtlEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
214 if (interlocked_inc( &crit->LockCount ))
216 if (crit->OwningThread == (HANDLE)GetCurrentThreadId())
218 crit->RecursionCount++;
219 return STATUS_SUCCESS;
222 /* Now wait for it */
223 RtlpWaitForCriticalSection( crit );
225 crit->OwningThread = (HANDLE)GetCurrentThreadId();
226 crit->RecursionCount = 1;
227 return STATUS_SUCCESS;
231 /***********************************************************************
232 * RtlTryEnterCriticalSection (NTDLL.@)
234 * Enter an RTL_CRITICAL_SECTION without waiting.
237 * crit [I/O] Critical section to enter
240 * Success: TRUE. The critical section is held by the caller.
241 * Failure: FALSE. The critical section is currently held by another thread.
243 BOOL WINAPI RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
246 if (interlocked_cmpxchg( &crit->LockCount, 0L, -1 ) == -1)
248 crit->OwningThread = (HANDLE)GetCurrentThreadId();
249 crit->RecursionCount = 1;
252 else if (crit->OwningThread == (HANDLE)GetCurrentThreadId())
254 interlocked_inc( &crit->LockCount );
255 crit->RecursionCount++;
262 /***********************************************************************
263 * RtlLeaveCriticalSection (NTDLL.@)
265 * Leave an RTL_CRITICAL_SECTION.
268 * crit [I/O] Critical section to enter
273 NTSTATUS WINAPI RtlLeaveCriticalSection( RTL_CRITICAL_SECTION *crit )
275 if (--crit->RecursionCount) interlocked_dec( &crit->LockCount );
278 crit->OwningThread = 0;
279 if (interlocked_dec( &crit->LockCount ) >= 0)
281 /* someone is waiting */
282 RtlpUnWaitCriticalSection( crit );
285 return STATUS_SUCCESS;