source: trunk/src/kernel32/critsection.cpp@ 2007

Last change on this file since 2007 was 1741, checked in by sandervl, 26 years ago

added dprintf2's + exception handler fix

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