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