1 | /* $Id: critsection.cpp,v 1.10 2003-08-01 16:25:47 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 <dbglog.h>
|
---|
17 | #include <debugtools.h>
|
---|
18 | #include <odinwrap.h>
|
---|
19 | #include <wprocess.h>
|
---|
20 |
|
---|
21 | #define DBG_LOCALLOG DBG_critsection
|
---|
22 | #include "dbglocal.h"
|
---|
23 |
|
---|
24 | DECLARE_DEBUG_CHANNEL(relay)
|
---|
25 |
|
---|
26 |
|
---|
27 |
|
---|
28 | /***********************************************************************
|
---|
29 | * InitializeCriticalSection (KERNEL32.472) (NTDLL.406)
|
---|
30 | */
|
---|
31 | void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
|
---|
32 | {
|
---|
33 | dprintf(("InitializeCriticalSection %x", crit));
|
---|
34 | crit->LockCount = -1;
|
---|
35 | crit->RecursionCount = 0;
|
---|
36 | crit->OwningThread = 0;
|
---|
37 | crit->LockSemaphore = CreateSemaphoreA( NULL, 0, 1, NULL );
|
---|
38 | crit->Reserved = ODIN_GetCurrentProcessId();
|
---|
39 | }
|
---|
40 |
|
---|
41 | /***********************************************************************
|
---|
42 | * InitializeCriticalSectionAndSpinCount (NTDLL.@)
|
---|
43 | * The InitializeCriticalSectionAndSpinCount (KERNEL32) function is
|
---|
44 | * available on NT4SP3 or later, and Win98 or later.
|
---|
45 | * I am assuming that this is the correct definition given the MSDN
|
---|
46 | * docs for the kernel32 functions.
|
---|
47 | */
|
---|
48 | BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
|
---|
49 | {
|
---|
50 | if(spincount) dprintf(("critsection=%p: spincount=%ld not supported\n", crit, spincount));
|
---|
51 | crit->Reserved = spincount;
|
---|
52 | InitializeCriticalSection( crit );
|
---|
53 | return TRUE;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /***********************************************************************
|
---|
57 | * DeleteCriticalSection (KERNEL32.185) (NTDLL.327)
|
---|
58 | */
|
---|
59 | void WINAPI DeleteCriticalSection( CRITICAL_SECTION *crit )
|
---|
60 | {
|
---|
61 | dprintf(("DeleteCriticalSection %x", crit));
|
---|
62 |
|
---|
63 | if (crit->LockSemaphore)
|
---|
64 | {
|
---|
65 | if (crit->RecursionCount) /* Should not happen */
|
---|
66 | dprintf(("Deleting owned critical section (%p)\n", crit ));
|
---|
67 |
|
---|
68 | crit->LockCount = -1;
|
---|
69 | crit->RecursionCount = 0;
|
---|
70 | crit->OwningThread = 0;
|
---|
71 | CloseHandle( crit->LockSemaphore );
|
---|
72 | crit->LockSemaphore = 0;
|
---|
73 | crit->Reserved = (DWORD)-1;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | /***********************************************************************
|
---|
79 | * EnterCriticalSection (KERNEL32.195) (NTDLL.344)
|
---|
80 | */
|
---|
81 | void WINAPI EnterCriticalSection( CRITICAL_SECTION *crit )
|
---|
82 | {
|
---|
83 | DWORD res;
|
---|
84 |
|
---|
85 | dprintf2(("EnterCriticalSection %x", crit));
|
---|
86 | if (!crit->LockSemaphore)
|
---|
87 | {
|
---|
88 | dprintf(("entering uninitialized section(%p)?\n",crit));
|
---|
89 | InitializeCriticalSection(crit);
|
---|
90 | }
|
---|
91 | if (InterlockedIncrement( &crit->LockCount ))
|
---|
92 | {
|
---|
93 | if (crit->OwningThread == ODIN_GetCurrentThreadId())
|
---|
94 | {
|
---|
95 | crit->RecursionCount++;
|
---|
96 | return;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /* Now wait for it */
|
---|
100 | for (;;)
|
---|
101 | {
|
---|
102 | res = WaitForSingleObject( crit->LockSemaphore, 5000L );
|
---|
103 | if ( res == WAIT_TIMEOUT )
|
---|
104 | {
|
---|
105 | dprintf(("Critical section %p wait timed out, retrying (60 sec)\n", crit ));
|
---|
106 | res = WaitForSingleObject( crit->LockSemaphore, 60000L );
|
---|
107 | if ( res == WAIT_TIMEOUT && TRACE_ON(relay) )
|
---|
108 | {
|
---|
109 | dprintf(("Critical section %p wait timed out, retrying (5 min)\n", crit ));
|
---|
110 | res = WaitForSingleObject( crit->LockSemaphore, 300000L );
|
---|
111 | }
|
---|
112 | }
|
---|
113 | if (res == STATUS_WAIT_0) break;
|
---|
114 |
|
---|
115 | #if 0
|
---|
116 | EXCEPTION_RECORD rec;
|
---|
117 |
|
---|
118 | rec.ExceptionCode = EXCEPTION_CRITICAL_SECTION_WAIT;
|
---|
119 | rec.ExceptionFlags = 0;
|
---|
120 | rec.ExceptionRecord = NULL;
|
---|
121 | rec.ExceptionAddress = RaiseException; /* sic */
|
---|
122 | rec.NumberParameters = 1;
|
---|
123 | rec.ExceptionInformation[0] = (DWORD)crit;
|
---|
124 | RtlRaiseException( &rec );
|
---|
125 | #endif
|
---|
126 | dprintf(("ERROR: EnterCritSection: WaitForSingleObject returned %d -> RaiseException", res));
|
---|
127 | RaiseException(EXCEPTION_CRITICAL_SECTION_WAIT, 0, 1, (DWORD *)crit);
|
---|
128 | }
|
---|
129 | }
|
---|
130 | crit->OwningThread = ODIN_GetCurrentThreadId();
|
---|
131 | crit->RecursionCount = 1;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | /***********************************************************************
|
---|
136 | * TryEnterCriticalSection (KERNEL32.898) (NTDLL.969)
|
---|
137 | */
|
---|
138 | BOOL WINAPI TryEnterCriticalSection( CRITICAL_SECTION *crit )
|
---|
139 | {
|
---|
140 | dprintf2(("TryEnterCriticalSection %x", crit));
|
---|
141 | if (InterlockedIncrement( &crit->LockCount ))
|
---|
142 | {
|
---|
143 | if (crit->OwningThread == ODIN_GetCurrentThreadId())
|
---|
144 | {
|
---|
145 | crit->RecursionCount++;
|
---|
146 | return TRUE;
|
---|
147 | }
|
---|
148 | /* FIXME: this doesn't work */
|
---|
149 | InterlockedDecrement( &crit->LockCount );
|
---|
150 | return FALSE;
|
---|
151 | }
|
---|
152 | crit->OwningThread = ODIN_GetCurrentThreadId();
|
---|
153 | crit->RecursionCount = 1;
|
---|
154 | return TRUE;
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | /***********************************************************************
|
---|
159 | * LeaveCriticalSection (KERNEL32.494) (NTDLL.426)
|
---|
160 | */
|
---|
161 | void WINAPI LeaveCriticalSection( CRITICAL_SECTION *crit )
|
---|
162 | {
|
---|
163 | dprintf2(("LeaveCriticalSection %x", crit));
|
---|
164 | if (crit->OwningThread != ODIN_GetCurrentThreadId()) return;
|
---|
165 |
|
---|
166 | if (--crit->RecursionCount)
|
---|
167 | {
|
---|
168 | InterlockedDecrement( &crit->LockCount );
|
---|
169 | return;
|
---|
170 | }
|
---|
171 | crit->OwningThread = 0;
|
---|
172 | if (InterlockedDecrement( &crit->LockCount ) >= 0)
|
---|
173 | {
|
---|
174 | /* Someone is waiting */
|
---|
175 | ReleaseSemaphore( crit->LockSemaphore, 1, NULL );
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | /***********************************************************************
|
---|
181 | * MakeCriticalSectionGlobal (KERNEL32.515)
|
---|
182 | */
|
---|
183 | void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
|
---|
184 | {
|
---|
185 | dprintf(("MakeCriticalSectionGlobal %x", crit));
|
---|
186 | crit->LockSemaphore = ConvertToGlobalHandle( crit->LockSemaphore );
|
---|
187 | crit->Reserved = 0L;
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | /***********************************************************************
|
---|
192 | * ReinitializeCriticalSection (KERNEL32.581)
|
---|
193 | */
|
---|
194 | void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
|
---|
195 | {
|
---|
196 | dprintf(("ReinitializeCriticalSection %x", crit));
|
---|
197 |
|
---|
198 | if ( !crit->LockSemaphore )
|
---|
199 | InitializeCriticalSection( crit );
|
---|
200 |
|
---|
201 | else if ( crit->Reserved && crit->Reserved != ODIN_GetCurrentProcessId() )
|
---|
202 | {
|
---|
203 | dprintf(("(%p) called for %08lx first, %08lx now: making global\n",
|
---|
204 | crit, crit->Reserved, ODIN_GetCurrentProcessId() ));
|
---|
205 |
|
---|
206 | MakeCriticalSectionGlobal( crit );
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 | /***********************************************************************
|
---|
212 | * UninitializeCriticalSection (KERNEL32.703)
|
---|
213 | */
|
---|
214 | void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
|
---|
215 | {
|
---|
216 | dprintf(("UninitializeCriticalSection %x", crit));
|
---|
217 | if ( crit->LockSemaphore )
|
---|
218 | {
|
---|
219 | if ( crit->Reserved ) /* not global */
|
---|
220 | DeleteCriticalSection( crit );
|
---|
221 | else
|
---|
222 | dprintf(("(%p) for %08lx: Crst is global, don't know whether to delete\n",
|
---|
223 | crit, ODIN_GetCurrentProcessId() ));
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|