1 | /* $Id: critsection.cpp,v 1.7 2000-10-02 18:39:33 sandervl Exp $ */
|
---|
2 | /*
|
---|
3 | * Win32 critical sections
|
---|
4 | *
|
---|
5 | *
|
---|
6 | *
|
---|
7 | * Copyright 1998 Alexandre Julliard (991031 Port)
|
---|
8 | *
|
---|
9 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
10 | *
|
---|
11 | */
|
---|
12 |
|
---|
13 | #include <os2win.h>
|
---|
14 | #include <assert.h>
|
---|
15 | #include <stdio.h>
|
---|
16 | #include "debugtools.h"
|
---|
17 | #include <misc.h>
|
---|
18 | #include <odinwrap.h>
|
---|
19 |
|
---|
20 | #define DBG_LOCALLOG DBG_critsection
|
---|
21 | #include "dbglocal.h"
|
---|
22 |
|
---|
23 | DECLARE_DEBUG_CHANNEL(relay)
|
---|
24 |
|
---|
25 |
|
---|
26 | #ifdef DEBUG
|
---|
27 | HANDLE ODIN_EXTERN(CreateSemaphoreA) (LPSECURITY_ATTRIBUTES arg1, LONG arg2, LONG arg3,
|
---|
28 | LPCSTR arg4);
|
---|
29 |
|
---|
30 | DWORD ODIN_EXTERN(WaitForSingleObject)(HANDLE hObject, DWORD timeout);
|
---|
31 |
|
---|
32 | HANDLE ODIN_EXTERN(ConvertToGlobalHandle)(HANDLE hHandle);
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | /***********************************************************************
|
---|
36 | * InitializeCriticalSection (KERNEL32.472) (NTDLL.406)
|
---|
37 | */
|
---|
38 | void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
|
---|
39 | {
|
---|
40 | dprintf(("InitializeCriticalSection %x", crit));
|
---|
41 | crit->LockCount = -1;
|
---|
42 | crit->RecursionCount = 0;
|
---|
43 | crit->OwningThread = 0;
|
---|
44 | crit->LockSemaphore = CALL_ODINFUNC(CreateSemaphoreA)( NULL, 0, 1, NULL );
|
---|
45 | crit->Reserved = GetCurrentProcessId();
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | /***********************************************************************
|
---|
50 | * DeleteCriticalSection (KERNEL32.185) (NTDLL.327)
|
---|
51 | */
|
---|
52 | void WINAPI DeleteCriticalSection( CRITICAL_SECTION *crit )
|
---|
53 | {
|
---|
54 | dprintf(("DeleteCriticalSection %x", crit));
|
---|
55 |
|
---|
56 | if (crit->LockSemaphore)
|
---|
57 | {
|
---|
58 | if (crit->RecursionCount) /* Should not happen */
|
---|
59 | dprintf(("Deleting owned critical section (%p)\n", crit ));
|
---|
60 |
|
---|
61 | crit->LockCount = -1;
|
---|
62 | crit->RecursionCount = 0;
|
---|
63 | crit->OwningThread = 0;
|
---|
64 | CloseHandle( crit->LockSemaphore );
|
---|
65 | crit->LockSemaphore = 0;
|
---|
66 | crit->Reserved = (DWORD)-1;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | /***********************************************************************
|
---|
72 | * EnterCriticalSection (KERNEL32.195) (NTDLL.344)
|
---|
73 | */
|
---|
74 | void WINAPI EnterCriticalSection( CRITICAL_SECTION *crit )
|
---|
75 | {
|
---|
76 | DWORD res;
|
---|
77 |
|
---|
78 | dprintf2(("EnterCriticalSection %x", crit));
|
---|
79 | if (!crit->LockSemaphore)
|
---|
80 | {
|
---|
81 | dprintf(("entering uninitialized section(%p)?\n",crit));
|
---|
82 | InitializeCriticalSection(crit);
|
---|
83 | }
|
---|
84 | if (InterlockedIncrement( &crit->LockCount ))
|
---|
85 | {
|
---|
86 | if (crit->OwningThread == GetCurrentThreadId())
|
---|
87 | {
|
---|
88 | crit->RecursionCount++;
|
---|
89 | return;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /* Now wait for it */
|
---|
93 | for (;;)
|
---|
94 | {
|
---|
95 | res = CALL_ODINFUNC(WaitForSingleObject)( crit->LockSemaphore, 5000L );
|
---|
96 | if ( res == WAIT_TIMEOUT )
|
---|
97 | {
|
---|
98 | dprintf(("Critical section %p wait timed out, retrying (60 sec)\n", crit ));
|
---|
99 | res = CALL_ODINFUNC(WaitForSingleObject)( crit->LockSemaphore, 60000L );
|
---|
100 | if ( res == WAIT_TIMEOUT && TRACE_ON(relay) )
|
---|
101 | {
|
---|
102 | dprintf(("Critical section %p wait timed out, retrying (5 min)\n", crit ));
|
---|
103 | res = WaitForSingleObject( crit->LockSemaphore, 300000L );
|
---|
104 | }
|
---|
105 | }
|
---|
106 | if (res == STATUS_WAIT_0) break;
|
---|
107 |
|
---|
108 | #if 0
|
---|
109 | EXCEPTION_RECORD rec;
|
---|
110 |
|
---|
111 | rec.ExceptionCode = EXCEPTION_CRITICAL_SECTION_WAIT;
|
---|
112 | rec.ExceptionFlags = 0;
|
---|
113 | rec.ExceptionRecord = NULL;
|
---|
114 | rec.ExceptionAddress = RaiseException; /* sic */
|
---|
115 | rec.NumberParameters = 1;
|
---|
116 | rec.ExceptionInformation[0] = (DWORD)crit;
|
---|
117 | RtlRaiseException( &rec );
|
---|
118 | #endif
|
---|
119 | dprintf(("ERROR: EnterCritSection: WaitForSingleObject returned %d -> RaiseException", res));
|
---|
120 | RaiseException(EXCEPTION_CRITICAL_SECTION_WAIT, 0, 1, (DWORD *)crit);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | crit->OwningThread = GetCurrentThreadId();
|
---|
124 | crit->RecursionCount = 1;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | /***********************************************************************
|
---|
129 | * TryEnterCriticalSection (KERNEL32.898) (NTDLL.969)
|
---|
130 | */
|
---|
131 | BOOL WINAPI TryEnterCriticalSection( CRITICAL_SECTION *crit )
|
---|
132 | {
|
---|
133 | dprintf2(("TryEnterCriticalSection %x", crit));
|
---|
134 | if (InterlockedIncrement( &crit->LockCount ))
|
---|
135 | {
|
---|
136 | if (crit->OwningThread == GetCurrentThreadId())
|
---|
137 | {
|
---|
138 | crit->RecursionCount++;
|
---|
139 | return TRUE;
|
---|
140 | }
|
---|
141 | /* FIXME: this doesn't work */
|
---|
142 | InterlockedDecrement( &crit->LockCount );
|
---|
143 | return FALSE;
|
---|
144 | }
|
---|
145 | crit->OwningThread = GetCurrentThreadId();
|
---|
146 | crit->RecursionCount = 1;
|
---|
147 | return TRUE;
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | /***********************************************************************
|
---|
152 | * LeaveCriticalSection (KERNEL32.494) (NTDLL.426)
|
---|
153 | */
|
---|
154 | void WINAPI LeaveCriticalSection( CRITICAL_SECTION *crit )
|
---|
155 | {
|
---|
156 | dprintf2(("LeaveCriticalSection %x", crit));
|
---|
157 | if (crit->OwningThread != GetCurrentThreadId()) return;
|
---|
158 |
|
---|
159 | if (--crit->RecursionCount)
|
---|
160 | {
|
---|
161 | InterlockedDecrement( &crit->LockCount );
|
---|
162 | return;
|
---|
163 | }
|
---|
164 | crit->OwningThread = 0;
|
---|
165 | if (InterlockedDecrement( &crit->LockCount ) >= 0)
|
---|
166 | {
|
---|
167 | /* Someone is waiting */
|
---|
168 | ReleaseSemaphore( crit->LockSemaphore, 1, NULL );
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | /***********************************************************************
|
---|
174 | * MakeCriticalSectionGlobal (KERNEL32.515)
|
---|
175 | */
|
---|
176 | void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
|
---|
177 | {
|
---|
178 | dprintf(("MakeCriticalSectionGlobal %x", crit));
|
---|
179 | crit->LockSemaphore = CALL_ODINFUNC(ConvertToGlobalHandle)( crit->LockSemaphore );
|
---|
180 | crit->Reserved = 0L;
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | /***********************************************************************
|
---|
185 | * ReinitializeCriticalSection (KERNEL32.581)
|
---|
186 | */
|
---|
187 | void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
|
---|
188 | {
|
---|
189 | dprintf(("ReinitializeCriticalSection %x", crit));
|
---|
190 |
|
---|
191 | if ( !crit->LockSemaphore )
|
---|
192 | InitializeCriticalSection( crit );
|
---|
193 |
|
---|
194 | else if ( crit->Reserved && crit->Reserved != GetCurrentProcessId() )
|
---|
195 | {
|
---|
196 | dprintf(("(%p) called for %08lx first, %08lx now: making global\n",
|
---|
197 | crit, crit->Reserved, GetCurrentProcessId() ));
|
---|
198 |
|
---|
199 | MakeCriticalSectionGlobal( crit );
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | /***********************************************************************
|
---|
205 | * UninitializeCriticalSection (KERNEL32.703)
|
---|
206 | */
|
---|
207 | void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
|
---|
208 | {
|
---|
209 | dprintf(("UninitializeCriticalSection %x", crit));
|
---|
210 | if ( crit->LockSemaphore )
|
---|
211 | {
|
---|
212 | if ( crit->Reserved ) /* not global */
|
---|
213 | DeleteCriticalSection( crit );
|
---|
214 | else
|
---|
215 | dprintf(("(%p) for %08lx: Crst is global, don't know whether to delete\n",
|
---|
216 | crit, GetCurrentProcessId() ));
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|