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 PVOID WINAPI
InterlockedCompareExchange( PVOID
*dest
, PVOID xchg
, PVOID compare
);
188 __ASM_GLOBAL_FUNC(InterlockedCompareExchange
,
189 "movl 12(%esp),%eax\n\t"
190 "movl 8(%esp),%ecx\n\t"
191 "movl 4(%esp),%edx\n\t"
192 "lock; cmpxchgl %ecx,(%edx)\n\t"
195 LONG WINAPI
InterlockedExchange( PLONG dest
, LONG val
);
196 __ASM_GLOBAL_FUNC(InterlockedExchange
,
197 "movl 8(%esp),%eax\n\t"
198 "movl 4(%esp),%edx\n\t"
199 "lock; xchgl %eax,(%edx)\n\t"
202 LONG WINAPI
InterlockedExchangeAdd( PLONG dest
, LONG incr
);
203 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd
,
204 "movl 8(%esp),%eax\n\t"
205 "movl 4(%esp),%edx\n\t"
206 "lock; xaddl %eax,(%edx)\n\t"
209 LONG WINAPI
InterlockedIncrement( PLONG dest
);
210 __ASM_GLOBAL_FUNC(InterlockedIncrement
,
211 "movl 4(%esp),%edx\n\t"
213 "lock; xaddl %eax,(%edx)\n\t"
217 LONG WINAPI
InterlockedDecrement( PLONG dest
);
218 __ASM_GLOBAL_FUNC(InterlockedDecrement
,
219 "movl 4(%esp),%edx\n\t"
221 "lock; xaddl %eax,(%edx)\n\t"
226 #error You must implement the Interlocked* functions for your CPU
227 #endif /* __i386__ */