Added generic signal handling mechanism based on pipes to synchronize
[wine/multimedia.git] / dlls / ntdll / critsection.c
blob023323a017179046f113ecbef8f2c6444fdbc404
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include "winerror.h"
28 #include "winternl.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 /***********************************************************************
45 * get_semaphore
47 static inline HANDLE get_semaphore( RTL_CRITICAL_SECTION *crit )
49 HANDLE ret = crit->LockSemaphore;
50 if (!ret)
52 HANDLE sem;
53 if (NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 )) return 0;
54 if (!(ret = (HANDLE)interlocked_cmpxchg_ptr( (PVOID *)&crit->LockSemaphore,
55 (PVOID)sem, 0 )))
56 ret = sem;
57 else
58 NtClose(sem); /* somebody beat us to it */
60 return ret;
63 /***********************************************************************
64 * RtlInitializeCriticalSection (NTDLL.@)
66 * Initialise a new RTL_CRITICAL_SECTION.
68 * PARAMS
69 * crit [O] Critical section to initialise
71 * RETURN
72 * STATUS_SUCCESS.
74 NTSTATUS WINAPI RtlInitializeCriticalSection( RTL_CRITICAL_SECTION *crit )
76 crit->DebugInfo = NULL;
77 crit->LockCount = -1;
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.
89 * PARAMS
90 * crit [O] Critical section to initialise
91 * spincount [I] Spin count for crit
93 * RETURNS
94 * STATUS_SUCCESS.
96 * NOTES
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.
115 * PARAMS
116 * crit [I/O] Critical section to free
118 * RETURNS
119 * STATUS_SUCCESS.
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.
137 * PARAMS
138 * crit [I/O] Critical section to wait for
140 * RETURNS
141 * STATUS_SUCCESS.
143 NTSTATUS WINAPI RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION *crit )
145 for (;;)
147 EXCEPTION_RECORD rec;
148 HANDLE sem = get_semaphore( crit );
150 DWORD res = WaitForSingleObject( sem, 5000L );
151 if ( res == WAIT_TIMEOUT )
153 const char *name = (char *)crit->DebugInfo;
154 if (!name) name = "?";
155 ERR( "section %p %s wait timed out, retrying (60 sec) tid=%04lx\n",
156 crit, debugstr_a(name), GetCurrentThreadId() );
157 res = WaitForSingleObject( sem, 60000L );
158 if ( res == WAIT_TIMEOUT && TRACE_ON(relay) )
160 ERR( "section %p %s wait timed out, retrying (5 min) tid=%04lx\n",
161 crit, debugstr_a(name), GetCurrentThreadId() );
162 res = WaitForSingleObject( sem, 300000L );
165 if (res == STATUS_WAIT_0) return STATUS_SUCCESS;
167 /* Throw exception only for Wine internal locks */
168 if (!crit->DebugInfo) continue;
170 rec.ExceptionCode = EXCEPTION_CRITICAL_SECTION_WAIT;
171 rec.ExceptionFlags = 0;
172 rec.ExceptionRecord = NULL;
173 rec.ExceptionAddress = RtlRaiseException; /* sic */
174 rec.NumberParameters = 1;
175 rec.ExceptionInformation[0] = (DWORD)crit;
176 RtlRaiseException( &rec );
181 /***********************************************************************
182 * RtlpUnWaitCriticalSection (NTDLL.@)
184 NTSTATUS WINAPI RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION *crit )
186 HANDLE sem = get_semaphore( crit );
187 NTSTATUS res = NtReleaseSemaphore( sem, 1, NULL );
188 if (res) RtlRaiseStatus( res );
189 return res;
193 /***********************************************************************
194 * RtlEnterCriticalSection (NTDLL.@)
196 * Enter an RTL_CRITICAL_SECTION.
198 * PARAMS
199 * crit [I/O] Critical section to enter
201 * RETURNS
202 * STATUS_SUCCESS. The critical section is held by the caller.
204 * NOTES
205 * The caller will wait until the critical section is availale.
207 NTSTATUS WINAPI RtlEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
209 if (interlocked_inc( &crit->LockCount ))
211 if (crit->OwningThread == (HANDLE)GetCurrentThreadId())
213 crit->RecursionCount++;
214 return STATUS_SUCCESS;
217 /* Now wait for it */
218 RtlpWaitForCriticalSection( crit );
220 crit->OwningThread = (HANDLE)GetCurrentThreadId();
221 crit->RecursionCount = 1;
222 return STATUS_SUCCESS;
226 /***********************************************************************
227 * RtlTryEnterCriticalSection (NTDLL.@)
229 * Enter an RTL_CRITICAL_SECTION without waiting.
231 * PARAMS
232 * crit [I/O] Critical section to enter
234 * RETURNS
235 * Success: TRUE. The critical section is held by the caller.
236 * Failure: FALSE. The critical section is currently held by another thread.
238 BOOL WINAPI RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION *crit )
240 BOOL ret = FALSE;
241 if (interlocked_cmpxchg( &crit->LockCount, 0L, -1 ) == -1)
243 crit->OwningThread = (HANDLE)GetCurrentThreadId();
244 crit->RecursionCount = 1;
245 ret = TRUE;
247 else if (crit->OwningThread == (HANDLE)GetCurrentThreadId())
249 interlocked_inc( &crit->LockCount );
250 crit->RecursionCount++;
251 ret = TRUE;
253 return ret;
257 /***********************************************************************
258 * RtlLeaveCriticalSection (NTDLL.@)
260 * Leave an RTL_CRITICAL_SECTION.
262 * PARAMS
263 * crit [I/O] Critical section to enter
265 * RETURNS
266 * STATUS_SUCCESS.
268 NTSTATUS WINAPI RtlLeaveCriticalSection( RTL_CRITICAL_SECTION *crit )
270 if (--crit->RecursionCount) interlocked_dec( &crit->LockCount );
271 else
273 crit->OwningThread = 0;
274 if (interlocked_dec( &crit->LockCount ) >= 0)
276 /* someone is waiting */
277 RtlpUnWaitCriticalSection( crit );
280 return STATUS_SUCCESS;