2 * Win32 critical sections
4 * Copyright 1998 Alexandre Julliard
10 #include <sys/types.h>
15 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(win32);
19 DECLARE_DEBUG_CHANNEL(relay);
21 /***********************************************************************
24 static inline HANDLE get_semaphore( CRITICAL_SECTION *crit )
26 HANDLE ret = crit->LockSemaphore;
29 HANDLE sem = CreateSemaphoreA( NULL, 0, 1, NULL );
30 if (!(ret = (HANDLE)InterlockedCompareExchange( (PVOID *)&crit->LockSemaphore,
34 CloseHandle(sem); /* somebody beat us to it */
39 /***********************************************************************
40 * InitializeCriticalSection (KERNEL32.472) (NTDLL.406)
42 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
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 );
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 )
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++;
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 )
138 if (InterlockedCompareExchange( (PVOID *)&crit->LockCount,
139 (PVOID)0L, (PVOID)-1L ) == (PVOID)-1L)
141 crit->OwningThread = GetCurrentThreadId();
142 crit->RecursionCount = 1;
145 else if (crit->OwningThread == GetCurrentThreadId())
147 InterlockedIncrement( &crit->LockCount );
148 crit->RecursionCount++;
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 );
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 );
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"
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"
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"
236 /***********************************************************************
237 * InterlockedIncrement (KERNEL32.@)
239 LONG WINAPI InterlockedIncrement( PLONG dest );
240 __ASM_GLOBAL_FUNC(InterlockedIncrement,
241 "movl 4(%esp),%edx\n\t"
243 "lock; xaddl %eax,(%edx)\n\t"
247 /***********************************************************************
248 * InterlockDecrement (KERNEL32.@)
250 LONG WINAPI InterlockedDecrement( PLONG dest );
251 __ASM_GLOBAL_FUNC(InterlockedDecrement,
252 "movl 4(%esp),%edx\n\t"
254 "lock; xaddl %eax,(%edx)\n\t"
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
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 ...
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 )
285 _lwp_mutex_unlock( &interlocked_mutex );
289 /***********************************************************************
290 * InterlockedExchange (KERNEL32.@)
292 LONG WINAPI InterlockedExchange( PLONG dest, LONG val )
295 _lwp_mutex_lock( &interlocked_mutex );
300 _lwp_mutex_unlock( &interlocked_mutex );
304 /***********************************************************************
305 * InterlockedExchangeAdd (KERNEL32.@)
307 LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr )
310 _lwp_mutex_lock( &interlocked_mutex );
315 _lwp_mutex_unlock( &interlocked_mutex );
319 /***********************************************************************
320 * InterlockedIncrement (KERNEL32.@)
322 LONG WINAPI InterlockedIncrement( PLONG dest )
325 _lwp_mutex_lock( &interlocked_mutex );
329 _lwp_mutex_unlock( &interlocked_mutex );
333 /***********************************************************************
334 * InterlockedDecrement (KERNEL32.@)
336 LONG WINAPI InterlockedDecrement( PLONG dest )
339 _lwp_mutex_lock( &interlocked_mutex );
343 _lwp_mutex_unlock( &interlocked_mutex );
348 #error You must implement the Interlocked* functions for your CPU