Release 20030408.
[wine/gsoc-2012-control.git] / dlls / ntdll / critsection.c
blob24474ffffd64fb7251c92fa90f584655ec456d88
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 );
149 LARGE_INTEGER time;
150 DWORD status;
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 );
194 return res;
198 /***********************************************************************
199 * RtlEnterCriticalSection (NTDLL.@)
201 * Enter an RTL_CRITICAL_SECTION.
203 * PARAMS
204 * crit [I/O] Critical section to enter
206 * RETURNS
207 * STATUS_SUCCESS. The critical section is held by the caller.
209 * NOTES
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.
236 * PARAMS
237 * crit [I/O] Critical section to enter
239 * RETURNS
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 )
245 BOOL ret = FALSE;
246 if (interlocked_cmpxchg( &crit->LockCount, 0L, -1 ) == -1)
248 crit->OwningThread = (HANDLE)GetCurrentThreadId();
249 crit->RecursionCount = 1;
250 ret = TRUE;
252 else if (crit->OwningThread == (HANDLE)GetCurrentThreadId())
254 interlocked_inc( &crit->LockCount );
255 crit->RecursionCount++;
256 ret = TRUE;
258 return ret;
262 /***********************************************************************
263 * RtlLeaveCriticalSection (NTDLL.@)
265 * Leave an RTL_CRITICAL_SECTION.
267 * PARAMS
268 * crit [I/O] Critical section to enter
270 * RETURNS
271 * STATUS_SUCCESS.
273 NTSTATUS WINAPI RtlLeaveCriticalSection( RTL_CRITICAL_SECTION *crit )
275 if (--crit->RecursionCount) interlocked_dec( &crit->LockCount );
276 else
278 crit->OwningThread = 0;
279 if (interlocked_dec( &crit->LockCount ) >= 0)
281 /* someone is waiting */
282 RtlpUnWaitCriticalSection( crit );
285 return STATUS_SUCCESS;