| 1 | /* $Id: critsection.cpp,v 1.6 2000-07-26 18:06:22 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 | dprintf(("ERROR: EnterCritSection: WaitForSingleObject returned %d -> RaiseException", res));
|
|---|
| 118 | RaiseException(EXCEPTION_CRITICAL_SECTION_WAIT, 0, 1, (DWORD *)crit);
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 | crit->OwningThread = GetCurrentThreadId();
|
|---|
| 122 | crit->RecursionCount = 1;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 | /***********************************************************************
|
|---|
| 127 | * TryEnterCriticalSection (KERNEL32.898) (NTDLL.969)
|
|---|
| 128 | */
|
|---|
| 129 | BOOL WINAPI TryEnterCriticalSection( CRITICAL_SECTION *crit )
|
|---|
| 130 | {
|
|---|
| 131 | dprintf2(("TryEnterCriticalSection %x", crit));
|
|---|
| 132 | if (InterlockedIncrement( &crit->LockCount ))
|
|---|
| 133 | {
|
|---|
| 134 | if (crit->OwningThread == GetCurrentThreadId())
|
|---|
| 135 | {
|
|---|
| 136 | crit->RecursionCount++;
|
|---|
| 137 | return TRUE;
|
|---|
| 138 | }
|
|---|
| 139 | /* FIXME: this doesn't work */
|
|---|
| 140 | InterlockedDecrement( &crit->LockCount );
|
|---|
| 141 | return FALSE;
|
|---|
| 142 | }
|
|---|
| 143 | crit->OwningThread = GetCurrentThreadId();
|
|---|
| 144 | crit->RecursionCount = 1;
|
|---|
| 145 | return TRUE;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 | /***********************************************************************
|
|---|
| 150 | * LeaveCriticalSection (KERNEL32.494) (NTDLL.426)
|
|---|
| 151 | */
|
|---|
| 152 | void WINAPI LeaveCriticalSection( CRITICAL_SECTION *crit )
|
|---|
| 153 | {
|
|---|
| 154 | dprintf2(("LeaveCriticalSection %x", crit));
|
|---|
| 155 | if (crit->OwningThread != GetCurrentThreadId()) return;
|
|---|
| 156 |
|
|---|
| 157 | if (--crit->RecursionCount)
|
|---|
| 158 | {
|
|---|
| 159 | InterlockedDecrement( &crit->LockCount );
|
|---|
| 160 | return;
|
|---|
| 161 | }
|
|---|
| 162 | crit->OwningThread = 0;
|
|---|
| 163 | if (InterlockedDecrement( &crit->LockCount ) >= 0)
|
|---|
| 164 | {
|
|---|
| 165 | /* Someone is waiting */
|
|---|
| 166 | ReleaseSemaphore( crit->LockSemaphore, 1, NULL );
|
|---|
| 167 | }
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 | /***********************************************************************
|
|---|
| 172 | * MakeCriticalSectionGlobal (KERNEL32.515)
|
|---|
| 173 | */
|
|---|
| 174 | void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
|
|---|
| 175 | {
|
|---|
| 176 | dprintf(("MakeCriticalSectionGlobal %x", crit));
|
|---|
| 177 | crit->LockSemaphore = ODIN_ConvertToGlobalHandle( crit->LockSemaphore );
|
|---|
| 178 | crit->Reserved = 0L;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 | /***********************************************************************
|
|---|
| 183 | * ReinitializeCriticalSection (KERNEL32.581)
|
|---|
| 184 | */
|
|---|
| 185 | void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
|
|---|
| 186 | {
|
|---|
| 187 | dprintf(("ReinitializeCriticalSection %x", crit));
|
|---|
| 188 |
|
|---|
| 189 | if ( !crit->LockSemaphore )
|
|---|
| 190 | InitializeCriticalSection( crit );
|
|---|
| 191 |
|
|---|
| 192 | else if ( crit->Reserved && crit->Reserved != GetCurrentProcessId() )
|
|---|
| 193 | {
|
|---|
| 194 | dprintf(("(%p) called for %08lx first, %08lx now: making global\n",
|
|---|
| 195 | crit, crit->Reserved, GetCurrentProcessId() ));
|
|---|
| 196 |
|
|---|
| 197 | MakeCriticalSectionGlobal( crit );
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 | /***********************************************************************
|
|---|
| 203 | * UninitializeCriticalSection (KERNEL32.703)
|
|---|
| 204 | */
|
|---|
| 205 | void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
|
|---|
| 206 | {
|
|---|
| 207 | dprintf(("UninitializeCriticalSection %x", crit));
|
|---|
| 208 | if ( crit->LockSemaphore )
|
|---|
| 209 | {
|
|---|
| 210 | if ( crit->Reserved ) /* not global */
|
|---|
| 211 | DeleteCriticalSection( crit );
|
|---|
| 212 | else
|
|---|
| 213 | dprintf(("(%p) for %08lx: Crst is global, don't know whether to delete\n",
|
|---|
| 214 | crit, GetCurrentProcessId() ));
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|