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