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 /***********************************************************************
52 static inline HANDLE
get_semaphore( RTL_CRITICAL_SECTION
*crit
)
54 HANDLE ret
= crit
->LockSemaphore
;
58 if (NtCreateSemaphore( &sem
, SEMAPHORE_ALL_ACCESS
, NULL
, 0, 1 )) return 0;
59 if (!(ret
= (HANDLE
)interlocked_cmpxchg_ptr( (PVOID
*)&crit
->LockSemaphore
,
63 NtClose(sem
); /* somebody beat us to it */
68 /***********************************************************************
69 * RtlInitializeCriticalSection (NTDLL.@)
71 * Initialise a new RTL_CRITICAL_SECTION.
74 * crit [O] Critical section to initialise
79 NTSTATUS WINAPI
RtlInitializeCriticalSection( RTL_CRITICAL_SECTION
*crit
)
81 if (!ntdll_get_process_heap()) crit
->DebugInfo
= NULL
;
84 crit
->DebugInfo
= RtlAllocateHeap(ntdll_get_process_heap(), 0, sizeof(CRITICAL_SECTION_DEBUG
));
87 crit
->DebugInfo
->Type
= 0;
88 crit
->DebugInfo
->CreatorBackTraceIndex
= 0;
89 crit
->DebugInfo
->CriticalSection
= crit
;
90 crit
->DebugInfo
->ProcessLocksList
.Blink
= &(crit
->DebugInfo
->ProcessLocksList
);
91 crit
->DebugInfo
->ProcessLocksList
.Flink
= &(crit
->DebugInfo
->ProcessLocksList
);
92 crit
->DebugInfo
->EntryCount
= 0;
93 crit
->DebugInfo
->ContentionCount
= 0;
94 crit
->DebugInfo
->Spare
[0] = 0;
95 crit
->DebugInfo
->Spare
[1] = 0;
99 crit
->RecursionCount
= 0;
100 crit
->OwningThread
= 0;
101 crit
->LockSemaphore
= 0;
102 return STATUS_SUCCESS
;
105 /***********************************************************************
106 * RtlInitializeCriticalSectionAndSpinCount (NTDLL.@)
108 * Initialise a new RTL_CRITICAL_SECTION with a given spin count.
111 * crit [O] Critical section to initialise
112 * spincount [I] Spin count for crit
118 * The InitializeCriticalSectionAndSpinCount() (KERNEL32) function is
119 * available on NT4SP3 or later, and Win98 or later.
120 * I am assuming that this is the correct definition given the MSDN
121 * docs for the kernel32 functions.
123 NTSTATUS WINAPI
RtlInitializeCriticalSectionAndSpinCount( RTL_CRITICAL_SECTION
*crit
, DWORD spincount
)
125 if(spincount
) TRACE("critsection=%p: spincount=%ld not supported\n", crit
, spincount
);
126 crit
->SpinCount
= spincount
;
127 return RtlInitializeCriticalSection( crit
);
131 /***********************************************************************
132 * RtlDeleteCriticalSection (NTDLL.@)
134 * Free the resources used by an RTL_CRITICAL_SECTION.
137 * crit [I/O] Critical section to free
142 NTSTATUS WINAPI
RtlDeleteCriticalSection( RTL_CRITICAL_SECTION
*crit
)
144 crit
->LockCount
= -1;
145 crit
->RecursionCount
= 0;
146 crit
->OwningThread
= 0;
147 if (crit
->LockSemaphore
) NtClose( crit
->LockSemaphore
);
148 crit
->LockSemaphore
= 0;
151 /* only free the ones we made in here */
152 if (!crit
->DebugInfo
->Spare
[1])
155 RtlFreeHeap( ntdll_get_process_heap(), 0, crit
->DebugInfo
);
156 crit
->DebugInfo
= NULL
;
159 return STATUS_SUCCESS
;
163 /***********************************************************************
164 * RtlpWaitForCriticalSection (NTDLL.@)
166 * Wait for an RTL_CRITICAL_SECTION to become free.
169 * crit [I/O] Critical section to wait for
174 NTSTATUS WINAPI
RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION
*crit
)
178 EXCEPTION_RECORD rec
;
179 HANDLE sem
= get_semaphore( crit
);
183 time
.QuadPart
= -5000 * 10000; /* 5 seconds */
184 status
= NtWaitForSingleObject( sem
, FALSE
, &time
);
185 if ( status
== WAIT_TIMEOUT
)
187 const char *name
= NULL
;
188 if (crit
->DebugInfo
) name
= (char *)crit
->DebugInfo
->Spare
[1];
189 if (!name
) name
= "?";
190 ERR( "section %p %s wait timed out in thread %04lx, blocked by %04lx, retrying (60 sec)\n",
191 crit
, debugstr_a(name
), GetCurrentThreadId(), (DWORD
)crit
->OwningThread
);
192 time
.QuadPart
= -60000 * 10000;
193 status
= NtWaitForSingleObject( sem
, FALSE
, &time
);
194 if ( status
== WAIT_TIMEOUT
&& TRACE_ON(relay
) )
196 ERR( "section %p %s wait timed out in thread %04lx, blocked by %04lx, retrying (5 min)\n",
197 crit
, debugstr_a(name
), GetCurrentThreadId(), (DWORD
) crit
->OwningThread
);
198 time
.QuadPart
= -300000 * (ULONGLONG
)10000;
199 status
= NtWaitForSingleObject( sem
, FALSE
, &time
);
202 if (status
== STATUS_WAIT_0
) return STATUS_SUCCESS
;
204 /* Throw exception only for Wine internal locks */
205 if ((!crit
->DebugInfo
) || (!crit
->DebugInfo
->Spare
[1])) continue;
207 rec
.ExceptionCode
= STATUS_POSSIBLE_DEADLOCK
;
208 rec
.ExceptionFlags
= 0;
209 rec
.ExceptionRecord
= NULL
;
210 rec
.ExceptionAddress
= RtlRaiseException
; /* sic */
211 rec
.NumberParameters
= 1;
212 rec
.ExceptionInformation
[0] = (DWORD
)crit
;
213 RtlRaiseException( &rec
);
218 /***********************************************************************
219 * RtlpUnWaitCriticalSection (NTDLL.@)
221 NTSTATUS WINAPI
RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION
*crit
)
223 HANDLE sem
= get_semaphore( crit
);
224 NTSTATUS res
= NtReleaseSemaphore( sem
, 1, NULL
);
225 if (res
) RtlRaiseStatus( res
);
230 /***********************************************************************
231 * RtlEnterCriticalSection (NTDLL.@)
233 * Enter an RTL_CRITICAL_SECTION.
236 * crit [I/O] Critical section to enter
239 * STATUS_SUCCESS. The critical section is held by the caller.
242 * The caller will wait until the critical section is availale.
244 NTSTATUS WINAPI
RtlEnterCriticalSection( RTL_CRITICAL_SECTION
*crit
)
246 if (interlocked_inc( &crit
->LockCount
))
248 if (crit
->OwningThread
== (HANDLE
)GetCurrentThreadId())
250 crit
->RecursionCount
++;
251 return STATUS_SUCCESS
;
254 /* Now wait for it */
255 RtlpWaitForCriticalSection( crit
);
257 crit
->OwningThread
= (HANDLE
)GetCurrentThreadId();
258 crit
->RecursionCount
= 1;
259 return STATUS_SUCCESS
;
263 /***********************************************************************
264 * RtlTryEnterCriticalSection (NTDLL.@)
266 * Enter an RTL_CRITICAL_SECTION without waiting.
269 * crit [I/O] Critical section to enter
272 * Success: TRUE. The critical section is held by the caller.
273 * Failure: FALSE. The critical section is currently held by another thread.
275 BOOL WINAPI
RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION
*crit
)
278 if (interlocked_cmpxchg( &crit
->LockCount
, 0L, -1 ) == -1)
280 crit
->OwningThread
= (HANDLE
)GetCurrentThreadId();
281 crit
->RecursionCount
= 1;
284 else if (crit
->OwningThread
== (HANDLE
)GetCurrentThreadId())
286 interlocked_inc( &crit
->LockCount
);
287 crit
->RecursionCount
++;
294 /***********************************************************************
295 * RtlLeaveCriticalSection (NTDLL.@)
297 * Leave an RTL_CRITICAL_SECTION.
300 * crit [I/O] Critical section to enter
305 NTSTATUS WINAPI
RtlLeaveCriticalSection( RTL_CRITICAL_SECTION
*crit
)
307 if (--crit
->RecursionCount
) interlocked_dec( &crit
->LockCount
);
310 crit
->OwningThread
= 0;
311 if (interlocked_dec( &crit
->LockCount
) >= 0)
313 /* someone is waiting */
314 RtlpUnWaitCriticalSection( crit
);
317 return STATUS_SUCCESS
;