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 (InterlockedIncrement( &crit
->LockCount
))
69 if (crit
->OwningThread
== GetCurrentThreadId())
71 crit
->RecursionCount
++;
76 if ( crit
->Reserved
&& crit
->Reserved
!= GetCurrentProcessId() )
78 FIXME_(win32
)("Crst %p belongs to process %ld, current is %ld!\n",
79 crit
, crit
->Reserved
, GetCurrentProcessId() );
83 res
= WaitForSingleObject( crit
->LockSemaphore
, 5000L );
84 if ( res
== WAIT_TIMEOUT
)
86 ERR_(win32
)("Critical section %p wait timed out, retrying (60 sec)\n", crit
);
87 res
= WaitForSingleObject( crit
->LockSemaphore
, 60000L );
89 if ( res
== WAIT_TIMEOUT
&& TRACE_ON(relay
) )
91 ERR_(win32
)("Critical section %p wait timed out, retrying (5 min)\n", crit
);
92 res
= WaitForSingleObject( crit
->LockSemaphore
, 300000L );
94 if (res
!= STATUS_WAIT_0
)
96 ERR_(win32
)("Critical section %p wait failed err=%lx\n", crit
, res
);
97 /* FIXME: should raise an exception */
100 crit
->OwningThread
= GetCurrentThreadId();
101 crit
->RecursionCount
= 1;
105 /***********************************************************************
106 * TryEnterCriticalSection (KERNEL32.898) (NTDLL.969)
108 BOOL WINAPI
TryEnterCriticalSection( CRITICAL_SECTION
*crit
)
110 if (InterlockedIncrement( &crit
->LockCount
))
112 if (crit
->OwningThread
== GetCurrentThreadId())
114 crit
->RecursionCount
++;
117 /* FIXME: this doesn't work */
118 InterlockedDecrement( &crit
->LockCount
);
121 crit
->OwningThread
= GetCurrentThreadId();
122 crit
->RecursionCount
= 1;
127 /***********************************************************************
128 * LeaveCriticalSection (KERNEL32.494) (NTDLL.426)
130 void WINAPI
LeaveCriticalSection( CRITICAL_SECTION
*crit
)
132 if (crit
->OwningThread
!= GetCurrentThreadId()) return;
134 if (--crit
->RecursionCount
)
136 InterlockedDecrement( &crit
->LockCount
);
139 crit
->OwningThread
= 0;
140 if (InterlockedDecrement( &crit
->LockCount
) >= 0)
142 /* Someone is waiting */
143 ReleaseSemaphore( crit
->LockSemaphore
, 1, NULL
);
148 /***********************************************************************
149 * MakeCriticalSectionGlobal (KERNEL32.515)
151 void WINAPI
MakeCriticalSectionGlobal( CRITICAL_SECTION
*crit
)
153 crit
->LockSemaphore
= ConvertToGlobalHandle( crit
->LockSemaphore
);
158 /***********************************************************************
159 * ReinitializeCriticalSection (KERNEL32.581)
161 void WINAPI
ReinitializeCriticalSection( CRITICAL_SECTION
*crit
)
163 if ( !crit
->LockSemaphore
)
164 InitializeCriticalSection( crit
);
166 else if ( crit
->Reserved
&& crit
->Reserved
!= GetCurrentProcessId() )
168 FIXME_(win32
)("(%p) called for %08lx first, %08lx now: making global\n",
169 crit
, crit
->Reserved
, GetCurrentProcessId() );
171 MakeCriticalSectionGlobal( crit
);
176 /***********************************************************************
177 * UninitializeCriticalSection (KERNEL32.703)
179 void WINAPI
UninitializeCriticalSection( CRITICAL_SECTION
*crit
)
181 if ( crit
->LockSemaphore
)
183 if ( crit
->Reserved
) /* not global */
184 DeleteCriticalSection( crit
);
186 FIXME_(win32
)("(%p) for %08lx: Crst is global, don't know whether to delete\n",
187 crit
, GetCurrentProcessId() );