- Timestamp:
- Jul 22, 2002, 7:58:02 AM (23 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/odincrt/critsect.cpp
r8895 r8902 1 /* $Id: critsect.cpp,v 1. 4 2002-07-19 11:06:45 sandervlExp $ */1 /* $Id: critsect.cpp,v 1.5 2002-07-22 05:58:02 achimha Exp $ */ 2 2 /* 3 * Critical sections 3 * Critical sections in the Win32 sense 4 4 * 5 * Copyright 2002 Sander van Leeuwen (sandervl@xs4all.nl) 6 * OS/2 port 7 * 8 * Based on Wine code 9 * 10 * Copyright 1998 Alexandre Julliard (991031 Port) 11 * 5 * Copyright 2002 Sander van Leeuwen <sandervl@innotek.de> 12 6 * 13 7 */ … … 30 24 #endif 31 25 26 //****************************************************************************** 27 // This is an OS/2 implementation of what Win32 treats as "critical sections" 28 // It is an implementation that is highly optimized for the case where there is 29 // only one thread trying to access the critical section, i.e. it is available 30 // most of the time. Therefore we can use these critical sections for all our 31 // serialization and not lose any performance when concurrent access is unlikely. 32 // 33 // In case there is multiple access, we use the OS/2 kernel even semaphores. 34 //****************************************************************************** 35 36 37 // encode PID and TID into one 32bit value 32 38 #define MAKE_THREADID(processid, threadid) ((processid << 16) | threadid) 33 39 … … 70 76 APIRET rc; 71 77 78 // initialize lock count with special value -1, meaning noone posesses it 72 79 crit->LockCount = -1; 73 80 crit->RecursionCount = 0; … … 130 137 DWORD threadid = GetCurrentThreadId(); 131 138 139 // create crit sect just in time... 132 140 if (!crit->hmtxLock) 133 141 { 134 142 DosInitializeCriticalSection(crit, NULL); 135 143 } 136 if (DosInterlockedIncrement( &crit->LockCount )) 144 // do an atomic increase of the lockcounter and see if it is > 0 145 // (i.e. it is already posessed) 146 if (DosInterlockedIncrement(&crit->LockCount)) 137 147 { 138 148 testenter: 149 // if the same thread is requesting it again, memorize it 139 150 if (crit->OwningThread == threadid) 140 151 { … … 142 153 return; 143 154 } 144 145 if(DosInterlockedCompareExchange((PLONG)&crit->OwningThread, threadid, 0)) 146 { 155 // do an atomic operation where we compare the owning thread id with 0 156 // and if this is true, exchange it with the id of the current thread. 157 if(DosInterlockedCompareExchange((PLONG)&crit->OwningThread, threadid, 0)) 158 { 159 // the compare did not return equal, i.e. the crit sect is in use 160 147 161 ULONG ulnrposts; 148 162 … … 154 168 } 155 169 DosResetEventSem(crit->hmtxLock, &ulnrposts); 170 // multiple waiters could be running now. Repeat the logic so that 171 // only one actually can get the critical section 156 172 goto testenter; 157 173 } … … 202 218 } 203 219 } 204
Note:
See TracChangeset
for help on using the changeset viewer.