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 * EnterCriticalSection (KERNEL32.195) (NTDLL.344)
70 void WINAPI
EnterCriticalSection( CRITICAL_SECTION
*crit
)
72 if (InterlockedIncrement( &crit
->LockCount
))
74 if (crit
->OwningThread
== GetCurrentThreadId())
76 crit
->RecursionCount
++;
84 HANDLE sem
= get_semaphore( crit
);
86 DWORD res
= WaitForSingleObject( sem
, 5000L );
87 if ( res
== WAIT_TIMEOUT
)
89 ERR("Critical section %p wait timed out, retrying (60 sec)\n", crit
);
90 res
= WaitForSingleObject( sem
, 60000L );
91 if ( res
== WAIT_TIMEOUT
&& TRACE_ON(relay
) )
93 ERR("Critical section %p wait timed out, retrying (5 min)\n", crit
);
94 res
= WaitForSingleObject( sem
, 300000L );
97 if (res
== STATUS_WAIT_0
) break;
99 rec
.ExceptionCode
= EXCEPTION_CRITICAL_SECTION_WAIT
;
100 rec
.ExceptionFlags
= 0;
101 rec
.ExceptionRecord
= NULL
;
102 rec
.ExceptionAddress
= RtlRaiseException
; /* sic */
103 rec
.NumberParameters
= 1;
104 rec
.ExceptionInformation
[0] = (DWORD
)crit
;
105 RtlRaiseException( &rec
);
108 crit
->OwningThread
= GetCurrentThreadId();
109 crit
->RecursionCount
= 1;
113 /***********************************************************************
114 * TryEnterCriticalSection (KERNEL32.898) (NTDLL.969)
116 BOOL WINAPI
TryEnterCriticalSection( CRITICAL_SECTION
*crit
)
119 if (InterlockedCompareExchange( (PVOID
*)&crit
->LockCount
,
120 (PVOID
)0L, (PVOID
)-1L ) == (PVOID
)-1L)
122 crit
->OwningThread
= GetCurrentThreadId();
123 crit
->RecursionCount
= 1;
126 else if (crit
->OwningThread
== GetCurrentThreadId())
128 InterlockedIncrement( &crit
->LockCount
);
129 crit
->RecursionCount
++;
136 /***********************************************************************
137 * LeaveCriticalSection (KERNEL32.494) (NTDLL.426)
139 void WINAPI
LeaveCriticalSection( CRITICAL_SECTION
*crit
)
141 if (crit
->OwningThread
!= GetCurrentThreadId()) return;
143 if (--crit
->RecursionCount
)
145 InterlockedDecrement( &crit
->LockCount
);
148 crit
->OwningThread
= 0;
149 if (InterlockedDecrement( &crit
->LockCount
) >= 0)
151 /* Someone is waiting */
152 HANDLE sem
= get_semaphore( crit
);
153 ReleaseSemaphore( sem
, 1, NULL
);
158 /***********************************************************************
159 * MakeCriticalSectionGlobal (KERNEL32.515)
161 void WINAPI
MakeCriticalSectionGlobal( CRITICAL_SECTION
*crit
)
163 crit
->LockSemaphore
= ConvertToGlobalHandle( get_semaphore( crit
) );
167 /***********************************************************************
168 * ReinitializeCriticalSection (KERNEL32.581)
170 void WINAPI
ReinitializeCriticalSection( CRITICAL_SECTION
*crit
)
172 if ( !crit
->LockSemaphore
)
173 InitializeCriticalSection( crit
);
177 /***********************************************************************
178 * UninitializeCriticalSection (KERNEL32.703)
180 void WINAPI
UninitializeCriticalSection( CRITICAL_SECTION
*crit
)
182 DeleteCriticalSection( crit
);
187 /***********************************************************************
188 * InterlockCompareExchange (KERNEL32.@)
190 PVOID WINAPI
InterlockedCompareExchange( PVOID
*dest
, PVOID xchg
, PVOID compare
);
191 __ASM_GLOBAL_FUNC(InterlockedCompareExchange
,
192 "movl 12(%esp),%eax\n\t"
193 "movl 8(%esp),%ecx\n\t"
194 "movl 4(%esp),%edx\n\t"
195 "lock; cmpxchgl %ecx,(%edx)\n\t"
198 /***********************************************************************
199 * InterlockedExchange (KERNEL32.@)
201 LONG WINAPI
InterlockedExchange( PLONG dest
, LONG val
);
202 __ASM_GLOBAL_FUNC(InterlockedExchange
,
203 "movl 8(%esp),%eax\n\t"
204 "movl 4(%esp),%edx\n\t"
205 "lock; xchgl %eax,(%edx)\n\t"
208 /***********************************************************************
209 * InterlockedExchangeAdd (KERNEL32.@)
211 LONG WINAPI
InterlockedExchangeAdd( PLONG dest
, LONG incr
);
212 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd
,
213 "movl 8(%esp),%eax\n\t"
214 "movl 4(%esp),%edx\n\t"
215 "lock; xaddl %eax,(%edx)\n\t"
218 /***********************************************************************
219 * InterlockedIncrement (KERNEL32.@)
221 LONG WINAPI
InterlockedIncrement( PLONG dest
);
222 __ASM_GLOBAL_FUNC(InterlockedIncrement
,
223 "movl 4(%esp),%edx\n\t"
225 "lock; xaddl %eax,(%edx)\n\t"
229 /***********************************************************************
230 * InterlockDecrement (KERNEL32.@)
232 LONG WINAPI
InterlockedDecrement( PLONG dest
);
233 __ASM_GLOBAL_FUNC(InterlockedDecrement
,
234 "movl 4(%esp),%edx\n\t"
236 "lock; xaddl %eax,(%edx)\n\t"
240 #elif defined(__sparc__) && defined(__sun__)
243 * As the earlier Sparc processors lack necessary atomic instructions,
244 * I'm simply falling back to the library-provided _lwp_mutex routines
245 * to ensure mutual exclusion in a way appropriate for the current
248 * FIXME: If we have the compare-and-swap instruction (Sparc v9 and above)
249 * we could use this to speed up the Interlocked operations ...
253 static lwp_mutex_t interlocked_mutex
= DEFAULTMUTEX
;
255 /***********************************************************************
256 * InterlockedCompareExchange (KERNEL32.@)
258 PVOID WINAPI
InterlockedCompareExchange( PVOID
*dest
, PVOID xchg
, PVOID compare
)
260 _lwp_mutex_lock( &interlocked_mutex
);
262 if ( *dest
== compare
)
267 _lwp_mutex_unlock( &interlocked_mutex
);
271 /***********************************************************************
272 * InterlockedExchange (KERNEL32.@)
274 LONG WINAPI
InterlockedExchange( PLONG dest
, LONG val
)
277 _lwp_mutex_lock( &interlocked_mutex
);
282 _lwp_mutex_unlock( &interlocked_mutex
);
286 /***********************************************************************
287 * InterlockedExchangeAdd (KERNEL32.@)
289 LONG WINAPI
InterlockedExchangeAdd( PLONG dest
, LONG incr
)
292 _lwp_mutex_lock( &interlocked_mutex
);
297 _lwp_mutex_unlock( &interlocked_mutex
);
301 /***********************************************************************
302 * InterlockedIncrement (KERNEL32.@)
304 LONG WINAPI
InterlockedIncrement( PLONG dest
)
307 _lwp_mutex_lock( &interlocked_mutex
);
311 _lwp_mutex_unlock( &interlocked_mutex
);
315 /***********************************************************************
316 * InterlockedDecrement (KERNEL32.@)
318 LONG WINAPI
InterlockedDecrement( PLONG dest
)
321 _lwp_mutex_lock( &interlocked_mutex
);
325 _lwp_mutex_unlock( &interlocked_mutex
);
330 #error You must implement the Interlocked* functions for your CPU