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
24 #include <sys/types.h>
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
30 WINE_DECLARE_DEBUG_CHANNEL(relay
);
32 /* Define the atomic exchange/inc/dec functions.
33 * These are available in kernel32.dll already,
34 * but we don't want to import kernel32 from ntdll.
41 inline static PVOID
interlocked_cmpxchg( PVOID
*dest
, PVOID xchg
, PVOID compare
)
44 __asm__
__volatile__( "lock; cmpxchgl %2,(%1)"
45 : "=a" (ret
) : "r" (dest
), "r" (xchg
), "0" (compare
) : "memory" );
48 inline static LONG
interlocked_inc( PLONG dest
)
51 __asm__
__volatile__( "lock; xaddl %0,(%1)"
52 : "=r" (ret
) : "r" (dest
), "0" (1) : "memory" );
55 inline static LONG
interlocked_dec( PLONG dest
)
58 __asm__
__volatile__( "lock; xaddl %0,(%1)"
59 : "=r" (ret
) : "r" (dest
), "0" (-1) : "memory" );
65 PVOID WINAPI
interlocked_cmpxchg( PVOID
*dest
, PVOID xchg
, PVOID compare
);
66 __ASM_GLOBAL_FUNC(interlocked_cmpxchg
,
67 "movl 12(%esp),%eax\n\t"
68 "movl 8(%esp),%ecx\n\t"
69 "movl 4(%esp),%edx\n\t"
70 "lock; cmpxchgl %ecx,(%edx)\n\t"
72 LONG WINAPI
interlocked_inc( PLONG dest
);
73 __ASM_GLOBAL_FUNC(interlocked_inc
,
74 "movl 4(%esp),%edx\n\t"
76 "lock; xaddl %eax,(%edx)\n\t"
79 LONG WINAPI
interlocked_dec( PLONG dest
);
80 __ASM_GLOBAL_FUNC(interlocked_dec
,
81 "movl 4(%esp),%edx\n\t"
83 "lock; xaddl %eax,(%edx)\n\t"
86 # endif /* __GNUC__ */
88 #elif defined(__sparc__) && defined(__sun__)
90 * As the earlier Sparc processors lack necessary atomic instructions,
91 * I'm simply falling back to the library-provided _lwp_mutex routines
92 * to ensure mutual exclusion in a way appropriate for the current
95 * FIXME: If we have the compare-and-swap instruction (Sparc v9 and above)
96 * we could use this to speed up the Interlocked operations ...
99 static lwp_mutex_t interlocked_mutex
= DEFAULTMUTEX
;
101 static PVOID
interlocked_cmpxchg( PVOID
*dest
, PVOID xchg
, PVOID compare
)
103 _lwp_mutex_lock( &interlocked_mutex
);
104 if ( *dest
== compare
)
108 _lwp_mutex_unlock( &interlocked_mutex
);
112 static LONG
interlocked_inc( PLONG dest
)
115 _lwp_mutex_lock( &interlocked_mutex
);
117 _lwp_mutex_unlock( &interlocked_mutex
);
121 static LONG
interlocked_dec( PLONG dest
)
124 _lwp_mutex_lock( &interlocked_mutex
);
126 _lwp_mutex_unlock( &interlocked_mutex
);
130 # error You must implement the interlocked* functions for your CPU
134 /***********************************************************************
137 static inline HANDLE
get_semaphore( RTL_CRITICAL_SECTION
*crit
)
139 HANDLE ret
= crit
->LockSemaphore
;
143 if (NtCreateSemaphore( &sem
, SEMAPHORE_ALL_ACCESS
, NULL
, 0, 1 )) return 0;
144 if (!(ret
= (HANDLE
)interlocked_cmpxchg( (PVOID
*)&crit
->LockSemaphore
,
148 NtClose(sem
); /* somebody beat us to it */
153 /***********************************************************************
154 * RtlInitializeCriticalSection (NTDLL.@)
156 NTSTATUS WINAPI
RtlInitializeCriticalSection( RTL_CRITICAL_SECTION
*crit
)
158 crit
->DebugInfo
= NULL
;
159 crit
->LockCount
= -1;
160 crit
->RecursionCount
= 0;
161 crit
->OwningThread
= 0;
162 crit
->LockSemaphore
= 0;
163 return STATUS_SUCCESS
;
166 /***********************************************************************
167 * RtlInitializeCriticalSectionAndSpinCount (NTDLL.@)
168 * The InitializeCriticalSectionAndSpinCount (KERNEL32) function is
169 * available on NT4SP3 or later, and Win98 or later.
170 * I am assuming that this is the correct definition given the MSDN
171 * docs for the kernel32 functions.
173 NTSTATUS WINAPI
RtlInitializeCriticalSectionAndSpinCount( RTL_CRITICAL_SECTION
*crit
, DWORD spincount
)
175 if(spincount
) TRACE("critsection=%p: spincount=%ld not supported\n", crit
, spincount
);
176 crit
->SpinCount
= spincount
;
177 return RtlInitializeCriticalSection( crit
);
181 /***********************************************************************
182 * RtlDeleteCriticalSection (NTDLL.@)
184 NTSTATUS WINAPI
RtlDeleteCriticalSection( RTL_CRITICAL_SECTION
*crit
)
186 crit
->LockCount
= -1;
187 crit
->RecursionCount
= 0;
188 crit
->OwningThread
= 0;
189 if (crit
->LockSemaphore
) NtClose( crit
->LockSemaphore
);
190 crit
->LockSemaphore
= 0;
191 return STATUS_SUCCESS
;
195 /***********************************************************************
196 * RtlpWaitForCriticalSection (NTDLL.@)
198 NTSTATUS WINAPI
RtlpWaitForCriticalSection( RTL_CRITICAL_SECTION
*crit
)
202 EXCEPTION_RECORD rec
;
203 HANDLE sem
= get_semaphore( crit
);
205 DWORD res
= WaitForSingleObject( sem
, 5000L );
206 if ( res
== WAIT_TIMEOUT
)
208 const char *name
= (char *)crit
->DebugInfo
;
209 if (!name
|| IsBadStringPtrA(name
,80)) name
= "?";
210 ERR( "section %p %s wait timed out, retrying (60 sec) fs=%04x\n",
211 crit
, debugstr_a(name
), __get_fs() );
212 res
= WaitForSingleObject( sem
, 60000L );
213 if ( res
== WAIT_TIMEOUT
&& TRACE_ON(relay
) )
215 ERR( "section %p %s wait timed out, retrying (5 min) fs=%04x\n",
216 crit
, debugstr_a(name
), __get_fs() );
217 res
= WaitForSingleObject( sem
, 300000L );
220 if (res
== STATUS_WAIT_0
) return STATUS_SUCCESS
;
222 rec
.ExceptionCode
= EXCEPTION_CRITICAL_SECTION_WAIT
;
223 rec
.ExceptionFlags
= 0;
224 rec
.ExceptionRecord
= NULL
;
225 rec
.ExceptionAddress
= RtlRaiseException
; /* sic */
226 rec
.NumberParameters
= 1;
227 rec
.ExceptionInformation
[0] = (DWORD
)crit
;
228 RtlRaiseException( &rec
);
233 /***********************************************************************
234 * RtlpUnWaitCriticalSection (NTDLL.@)
236 NTSTATUS WINAPI
RtlpUnWaitCriticalSection( RTL_CRITICAL_SECTION
*crit
)
238 HANDLE sem
= get_semaphore( crit
);
239 NTSTATUS res
= NtReleaseSemaphore( sem
, 1, NULL
);
240 if (res
) RtlRaiseStatus( res
);
245 /***********************************************************************
246 * RtlEnterCriticalSection (NTDLL.@)
248 NTSTATUS WINAPI
RtlEnterCriticalSection( RTL_CRITICAL_SECTION
*crit
)
250 if (interlocked_inc( &crit
->LockCount
))
252 if (crit
->OwningThread
== GetCurrentThreadId())
254 crit
->RecursionCount
++;
255 return STATUS_SUCCESS
;
258 /* Now wait for it */
259 RtlpWaitForCriticalSection( crit
);
261 crit
->OwningThread
= GetCurrentThreadId();
262 crit
->RecursionCount
= 1;
263 return STATUS_SUCCESS
;
267 /***********************************************************************
268 * RtlTryEnterCriticalSection (NTDLL.@)
270 BOOL WINAPI
RtlTryEnterCriticalSection( RTL_CRITICAL_SECTION
*crit
)
273 if (interlocked_cmpxchg( (PVOID
*)&crit
->LockCount
, (PVOID
)0L, (PVOID
)-1L ) == (PVOID
)-1L)
275 crit
->OwningThread
= GetCurrentThreadId();
276 crit
->RecursionCount
= 1;
279 else if (crit
->OwningThread
== GetCurrentThreadId())
281 interlocked_inc( &crit
->LockCount
);
282 crit
->RecursionCount
++;
289 /***********************************************************************
290 * RtlLeaveCriticalSection (NTDLL.@)
292 NTSTATUS WINAPI
RtlLeaveCriticalSection( RTL_CRITICAL_SECTION
*crit
)
294 if (--crit
->RecursionCount
) interlocked_dec( &crit
->LockCount
);
297 crit
->OwningThread
= 0;
298 if (interlocked_dec( &crit
->LockCount
) >= 0)
300 /* someone is waiting */
301 RtlpUnWaitCriticalSection( crit
);
304 return STATUS_SUCCESS
;