Serge Ivanov
[wine.git] / scheduler / critsection.c
blob4437d0e794da5712a48e9770902330053233c8ef
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 * 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++;
77 return;
80 /* Now wait for it */
81 for (;;)
83 EXCEPTION_RECORD rec;
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 )
118 BOOL ret = FALSE;
119 if (InterlockedCompareExchange( (PVOID *)&crit->LockCount,
120 (PVOID)0L, (PVOID)-1L ) == (PVOID)-1L)
122 crit->OwningThread = GetCurrentThreadId();
123 crit->RecursionCount = 1;
124 ret = TRUE;
126 else if (crit->OwningThread == GetCurrentThreadId())
128 InterlockedIncrement( &crit->LockCount );
129 crit->RecursionCount++;
130 ret = TRUE;
132 return ret;
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 );
146 return;
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 );
185 #ifdef __i386__
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"
193 "ret $12");
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"
200 "ret $8");
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"
207 "ret $8");
209 LONG WINAPI InterlockedIncrement( PLONG dest );
210 __ASM_GLOBAL_FUNC(InterlockedIncrement,
211 "movl 4(%esp),%edx\n\t"
212 "movl $1,%eax\n\t"
213 "lock; xaddl %eax,(%edx)\n\t"
214 "incl %eax\n\t"
215 "ret $4");
217 LONG WINAPI InterlockedDecrement( PLONG dest );
218 __ASM_GLOBAL_FUNC(InterlockedDecrement,
219 "movl 4(%esp),%edx\n\t"
220 "movl $-1,%eax\n\t"
221 "lock; xaddl %eax,(%edx)\n\t"
222 "decl %eax\n\t"
223 "ret $4");
225 #else /* __i386__ */
226 #error You must implement the Interlocked* functions for your CPU
227 #endif /* __i386__ */