2 * Win32 critical sections
4 * Copyright 1998 Alexandre Julliard
10 #include <sys/types.h>
11 #include "debugtools.h"
15 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(win32)
19 DECLARE_DEBUG_CHANNEL(relay)
22 /***********************************************************************
23 * InitializeCriticalSection (KERNEL32.472) (NTDLL.406)
25 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
28 crit->RecursionCount = 0;
29 crit->OwningThread = 0;
30 crit->LockSemaphore = CreateSemaphoreA( NULL, 0, 1, NULL );
31 crit->Reserved = GetCurrentProcessId();
35 /***********************************************************************
36 * DeleteCriticalSection (KERNEL32.185) (NTDLL.327)
38 void WINAPI DeleteCriticalSection( CRITICAL_SECTION *crit )
40 if (crit->LockSemaphore)
42 if (crit->RecursionCount) /* Should not happen */
43 ERR_(win32)("Deleting owned critical section (%p)\n", crit );
46 crit->RecursionCount = 0;
47 crit->OwningThread = 0;
48 CloseHandle( crit->LockSemaphore );
49 crit->LockSemaphore = 0;
50 crit->Reserved = (DWORD)-1;
55 /***********************************************************************
56 * EnterCriticalSection (KERNEL32.195) (NTDLL.344)
58 void WINAPI EnterCriticalSection( CRITICAL_SECTION *crit )
62 if (!crit->LockSemaphore)
64 FIXME_(win32)("entering uninitialized section(%p)?\n",crit);
65 InitializeCriticalSection(crit);
67 if ( crit->Reserved && crit->Reserved != GetCurrentProcessId() )
69 FIXME_(win32)("Crst %p belongs to process %ld, current is %ld!\n",
70 crit, crit->Reserved, GetCurrentProcessId() );
73 if (InterlockedIncrement( &crit->LockCount ))
75 if (crit->OwningThread == GetCurrentThreadId())
77 crit->RecursionCount++;
82 res = WaitForSingleObject( crit->LockSemaphore, 5000L );
83 if ( res == WAIT_TIMEOUT )
85 ERR_(win32)("Critical section %p wait timed out, retrying (60 sec)\n", crit );
86 res = WaitForSingleObject( crit->LockSemaphore, 60000L );
88 if ( res == WAIT_TIMEOUT && TRACE_ON(relay) )
90 ERR_(win32)("Critical section %p wait timed out, retrying (5 min)\n", crit );
91 res = WaitForSingleObject( crit->LockSemaphore, 300000L );
93 if (res != STATUS_WAIT_0)
95 ERR_(win32)("Critical section %p wait failed err=%lx\n", crit, res );
96 /* FIXME: should raise an exception */
99 crit->OwningThread = GetCurrentThreadId();
100 crit->RecursionCount = 1;
104 /***********************************************************************
105 * TryEnterCriticalSection (KERNEL32.898) (NTDLL.969)
107 BOOL WINAPI TryEnterCriticalSection( CRITICAL_SECTION *crit )
109 if (InterlockedIncrement( &crit->LockCount ))
111 if (crit->OwningThread == GetCurrentThreadId())
113 crit->RecursionCount++;
116 /* FIXME: this doesn't work */
117 InterlockedDecrement( &crit->LockCount );
120 crit->OwningThread = GetCurrentThreadId();
121 crit->RecursionCount = 1;
126 /***********************************************************************
127 * LeaveCriticalSection (KERNEL32.494) (NTDLL.426)
129 void WINAPI LeaveCriticalSection( CRITICAL_SECTION *crit )
131 if (crit->OwningThread != GetCurrentThreadId()) return;
133 if (--crit->RecursionCount)
135 InterlockedDecrement( &crit->LockCount );
138 crit->OwningThread = 0;
139 if (InterlockedDecrement( &crit->LockCount ) >= 0)
141 /* Someone is waiting */
142 ReleaseSemaphore( crit->LockSemaphore, 1, NULL );
147 /***********************************************************************
148 * MakeCriticalSectionGlobal (KERNEL32.515)
150 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
152 crit->LockSemaphore = ConvertToGlobalHandle( crit->LockSemaphore );
157 /***********************************************************************
158 * ReinitializeCriticalSection (KERNEL32.581)
160 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
162 if ( !crit->LockSemaphore )
163 InitializeCriticalSection( crit );
165 else if ( crit->Reserved && crit->Reserved != GetCurrentProcessId() )
167 FIXME_(win32)("(%p) called for %08lx first, %08lx now: making global\n",
168 crit, crit->Reserved, GetCurrentProcessId() );
170 MakeCriticalSectionGlobal( crit );
175 /***********************************************************************
176 * UninitializeCriticalSection (KERNEL32.703)
178 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
180 if ( crit->LockSemaphore )
182 if ( crit->Reserved ) /* not global */
183 DeleteCriticalSection( crit );
185 FIXME_(win32)("(%p) for %08lx: Crst is global, don't know whether to delete\n",
186 crit, GetCurrentProcessId() );