Fix PrintDlg collate icons. Add orientation icons.
[wine.git] / scheduler / critsection.c
blob8c2c4bfd4e5d277b2350a641b9ed42d64755b2dd
1 /*
2 * Win32 critical sections
4 * Copyright 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <errno.h>
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include "winerror.h"
12 #include "winbase.h"
13 #include "ntddk.h"
14 #include "heap.h"
15 #include "debugtools.h"
16 #include "thread.h"
18 DEFAULT_DEBUG_CHANNEL(win32);
19 DECLARE_DEBUG_CHANNEL(relay);
21 /***********************************************************************
22 * get_semaphore
24 static inline HANDLE get_semaphore( CRITICAL_SECTION *crit )
26 HANDLE ret = crit->LockSemaphore;
27 if (!ret)
29 HANDLE sem = CreateSemaphoreA( NULL, 0, 1, NULL );
30 if (!(ret = (HANDLE)InterlockedCompareExchange( (PVOID *)&crit->LockSemaphore,
31 (PVOID)sem, 0 )))
32 ret = sem;
33 else
34 CloseHandle(sem); /* somebody beat us to it */
36 return ret;
39 /***********************************************************************
40 * InitializeCriticalSection (KERNEL32.472) (NTDLL.406)
42 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
44 crit->LockCount = -1;
45 crit->RecursionCount = 0;
46 crit->OwningThread = 0;
47 crit->LockSemaphore = 0;
51 /***********************************************************************
52 * DeleteCriticalSection (KERNEL32.185) (NTDLL.327)
54 void WINAPI DeleteCriticalSection( CRITICAL_SECTION *crit )
56 if (crit->RecursionCount && crit->OwningThread != GetCurrentThreadId())
57 ERR("Deleting owned critical section (%p)\n", crit );
59 crit->LockCount = -1;
60 crit->RecursionCount = 0;
61 crit->OwningThread = 0;
62 if (crit->LockSemaphore) CloseHandle( crit->LockSemaphore );
63 crit->LockSemaphore = 0;
67 /***********************************************************************
68 * RtlpWaitForCriticalSection (NTDLL.@)
70 void WINAPI RtlpWaitForCriticalSection( CRITICAL_SECTION *crit )
72 for (;;)
74 EXCEPTION_RECORD rec;
75 HANDLE sem = get_semaphore( crit );
77 DWORD res = WaitForSingleObject( sem, 5000L );
78 if ( res == WAIT_TIMEOUT )
80 ERR("Critical section %p wait timed out, retrying (60 sec)\n", crit );
81 res = WaitForSingleObject( sem, 60000L );
82 if ( res == WAIT_TIMEOUT && TRACE_ON(relay) )
84 ERR("Critical section %p wait timed out, retrying (5 min)\n", crit );
85 res = WaitForSingleObject( sem, 300000L );
88 if (res == STATUS_WAIT_0) break;
90 rec.ExceptionCode = EXCEPTION_CRITICAL_SECTION_WAIT;
91 rec.ExceptionFlags = 0;
92 rec.ExceptionRecord = NULL;
93 rec.ExceptionAddress = RtlRaiseException; /* sic */
94 rec.NumberParameters = 1;
95 rec.ExceptionInformation[0] = (DWORD)crit;
96 RtlRaiseException( &rec );
101 /***********************************************************************
102 * RtlpUnWaitCriticalSection (NTDLL.@)
104 void WINAPI RtlpUnWaitCriticalSection( CRITICAL_SECTION *crit )
106 HANDLE sem = get_semaphore( crit );
107 ReleaseSemaphore( sem, 1, NULL );
111 /***********************************************************************
112 * EnterCriticalSection (KERNEL32.195) (NTDLL.344)
114 void WINAPI EnterCriticalSection( CRITICAL_SECTION *crit )
116 if (InterlockedIncrement( &crit->LockCount ))
118 if (crit->OwningThread == GetCurrentThreadId())
120 crit->RecursionCount++;
121 return;
124 /* Now wait for it */
125 RtlpWaitForCriticalSection( crit );
127 crit->OwningThread = GetCurrentThreadId();
128 crit->RecursionCount = 1;
132 /***********************************************************************
133 * TryEnterCriticalSection (KERNEL32.898) (NTDLL.969)
135 BOOL WINAPI TryEnterCriticalSection( CRITICAL_SECTION *crit )
137 BOOL ret = FALSE;
138 if (InterlockedCompareExchange( (PVOID *)&crit->LockCount,
139 (PVOID)0L, (PVOID)-1L ) == (PVOID)-1L)
141 crit->OwningThread = GetCurrentThreadId();
142 crit->RecursionCount = 1;
143 ret = TRUE;
145 else if (crit->OwningThread == GetCurrentThreadId())
147 InterlockedIncrement( &crit->LockCount );
148 crit->RecursionCount++;
149 ret = TRUE;
151 return ret;
155 /***********************************************************************
156 * LeaveCriticalSection (KERNEL32.494) (NTDLL.426)
158 void WINAPI LeaveCriticalSection( CRITICAL_SECTION *crit )
160 if (crit->OwningThread != GetCurrentThreadId()) return;
162 if (--crit->RecursionCount)
164 InterlockedDecrement( &crit->LockCount );
165 return;
167 crit->OwningThread = 0;
168 if (InterlockedDecrement( &crit->LockCount ) >= 0)
170 /* Someone is waiting */
171 RtlpUnWaitCriticalSection( crit );
176 /***********************************************************************
177 * MakeCriticalSectionGlobal (KERNEL32.515)
179 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
181 crit->LockSemaphore = ConvertToGlobalHandle( get_semaphore( crit ) );
185 /***********************************************************************
186 * ReinitializeCriticalSection (KERNEL32.581)
188 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
190 if ( !crit->LockSemaphore )
191 InitializeCriticalSection( crit );
195 /***********************************************************************
196 * UninitializeCriticalSection (KERNEL32.703)
198 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
200 DeleteCriticalSection( crit );
203 #ifdef __i386__
205 /***********************************************************************
206 * InterlockCompareExchange (KERNEL32.@)
208 PVOID WINAPI InterlockedCompareExchange( PVOID *dest, PVOID xchg, PVOID compare );
209 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
210 "movl 12(%esp),%eax\n\t"
211 "movl 8(%esp),%ecx\n\t"
212 "movl 4(%esp),%edx\n\t"
213 "lock; cmpxchgl %ecx,(%edx)\n\t"
214 "ret $12");
216 /***********************************************************************
217 * InterlockedExchange (KERNEL32.@)
219 LONG WINAPI InterlockedExchange( PLONG dest, LONG val );
220 __ASM_GLOBAL_FUNC(InterlockedExchange,
221 "movl 8(%esp),%eax\n\t"
222 "movl 4(%esp),%edx\n\t"
223 "lock; xchgl %eax,(%edx)\n\t"
224 "ret $8");
226 /***********************************************************************
227 * InterlockedExchangeAdd (KERNEL32.@)
229 LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr );
230 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
231 "movl 8(%esp),%eax\n\t"
232 "movl 4(%esp),%edx\n\t"
233 "lock; xaddl %eax,(%edx)\n\t"
234 "ret $8");
236 /***********************************************************************
237 * InterlockedIncrement (KERNEL32.@)
239 LONG WINAPI InterlockedIncrement( PLONG dest );
240 __ASM_GLOBAL_FUNC(InterlockedIncrement,
241 "movl 4(%esp),%edx\n\t"
242 "movl $1,%eax\n\t"
243 "lock; xaddl %eax,(%edx)\n\t"
244 "incl %eax\n\t"
245 "ret $4");
247 /***********************************************************************
248 * InterlockDecrement (KERNEL32.@)
250 LONG WINAPI InterlockedDecrement( PLONG dest );
251 __ASM_GLOBAL_FUNC(InterlockedDecrement,
252 "movl 4(%esp),%edx\n\t"
253 "movl $-1,%eax\n\t"
254 "lock; xaddl %eax,(%edx)\n\t"
255 "decl %eax\n\t"
256 "ret $4");
258 #elif defined(__sparc__) && defined(__sun__)
261 * As the earlier Sparc processors lack necessary atomic instructions,
262 * I'm simply falling back to the library-provided _lwp_mutex routines
263 * to ensure mutual exclusion in a way appropriate for the current
264 * architecture.
266 * FIXME: If we have the compare-and-swap instruction (Sparc v9 and above)
267 * we could use this to speed up the Interlocked operations ...
270 #include <synch.h>
271 static lwp_mutex_t interlocked_mutex = DEFAULTMUTEX;
273 /***********************************************************************
274 * InterlockedCompareExchange (KERNEL32.@)
276 PVOID WINAPI InterlockedCompareExchange( PVOID *dest, PVOID xchg, PVOID compare )
278 _lwp_mutex_lock( &interlocked_mutex );
280 if ( *dest == compare )
281 *dest = xchg;
282 else
283 compare = *dest;
285 _lwp_mutex_unlock( &interlocked_mutex );
286 return compare;
289 /***********************************************************************
290 * InterlockedExchange (KERNEL32.@)
292 LONG WINAPI InterlockedExchange( PLONG dest, LONG val )
294 LONG retv;
295 _lwp_mutex_lock( &interlocked_mutex );
297 retv = *dest;
298 *dest = val;
300 _lwp_mutex_unlock( &interlocked_mutex );
301 return retv;
304 /***********************************************************************
305 * InterlockedExchangeAdd (KERNEL32.@)
307 LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr )
309 LONG retv;
310 _lwp_mutex_lock( &interlocked_mutex );
312 retv = *dest;
313 *dest += incr;
315 _lwp_mutex_unlock( &interlocked_mutex );
316 return retv;
319 /***********************************************************************
320 * InterlockedIncrement (KERNEL32.@)
322 LONG WINAPI InterlockedIncrement( PLONG dest )
324 LONG retv;
325 _lwp_mutex_lock( &interlocked_mutex );
327 retv = ++*dest;
329 _lwp_mutex_unlock( &interlocked_mutex );
330 return retv;
333 /***********************************************************************
334 * InterlockedDecrement (KERNEL32.@)
336 LONG WINAPI InterlockedDecrement( PLONG dest )
338 LONG retv;
339 _lwp_mutex_lock( &interlocked_mutex );
341 retv = --*dest;
343 _lwp_mutex_unlock( &interlocked_mutex );
344 return retv;
347 #else
348 #error You must implement the Interlocked* functions for your CPU
349 #endif