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