1 | /* $Id: critsect.cpp,v 1.10 2003-07-28 17:32:58 sandervl Exp $ */
|
---|
2 | /*
|
---|
3 | * Critical sections in the Win32 sense
|
---|
4 | *
|
---|
5 | * Copyright 2002 Sander van Leeuwen <sandervl@innotek.de>
|
---|
6 | *
|
---|
7 | */
|
---|
8 | #define INCL_DOSPROCESS
|
---|
9 | #define INCL_DOSERRORS
|
---|
10 | #define INCL_DOSSEMAPHORES
|
---|
11 | #include <os2wrap.h>
|
---|
12 | #include <win32type.h>
|
---|
13 | #include <win32api.h>
|
---|
14 | #include <FastInfoBlocks.h>
|
---|
15 |
|
---|
16 | #undef fibGetPid
|
---|
17 |
|
---|
18 | #include <assert.h>
|
---|
19 | #include <stdio.h>
|
---|
20 |
|
---|
21 | #include <odincrt.h>
|
---|
22 |
|
---|
23 | #ifdef DEBUG
|
---|
24 | #define DebugInt3() _interrupt(3)
|
---|
25 | #else
|
---|
26 | #define DebugInt3()
|
---|
27 | #endif
|
---|
28 |
|
---|
29 |
|
---|
30 | //******************************************************************************
|
---|
31 | // This is an OS/2 implementation of what Win32 treats as "critical sections"
|
---|
32 | // It is an implementation that is highly optimized for the case where there is
|
---|
33 | // only one thread trying to access the critical section, i.e. it is available
|
---|
34 | // most of the time. Therefore we can use these critical sections for all our
|
---|
35 | // serialization and not lose any performance when concurrent access is unlikely.
|
---|
36 | //
|
---|
37 | // In case there is multiple access, we use the OS/2 kernel event semaphores.
|
---|
38 | //******************************************************************************
|
---|
39 |
|
---|
40 |
|
---|
41 | // encode PID and TID into one 32bit value
|
---|
42 | #define MAKE_THREADID(processid, threadid) ((processid << 16) | threadid)
|
---|
43 |
|
---|
44 | //******************************************************************************
|
---|
45 | //******************************************************************************
|
---|
46 | inline ULONG GetCurrentThreadId()
|
---|
47 | {
|
---|
48 | #ifdef fibGetPid
|
---|
49 | return MAKE_THREADID(fibGetPid(), fibGetTid());
|
---|
50 | #else
|
---|
51 | PTIB ptib;
|
---|
52 | PPIB ppib;
|
---|
53 | APIRET rc;
|
---|
54 |
|
---|
55 | rc = DosGetInfoBlocks(&ptib, &ppib);
|
---|
56 | if(rc == NO_ERROR) {
|
---|
57 | #ifdef DEBUG
|
---|
58 | if(MAKE_THREADID(ppib->pib_ulpid, ptib->tib_ptib2->tib2_ultid) == 0) {
|
---|
59 | DebugInt3();
|
---|
60 | }
|
---|
61 | #endif
|
---|
62 | return MAKE_THREADID(ppib->pib_ulpid, ptib->tib_ptib2->tib2_ultid);
|
---|
63 | }
|
---|
64 | DebugInt3();
|
---|
65 | return 0;
|
---|
66 | #endif
|
---|
67 | }
|
---|
68 | //******************************************************************************
|
---|
69 | //******************************************************************************
|
---|
70 | inline ULONG GetCurrentProcessId()
|
---|
71 | {
|
---|
72 | #ifdef fibGetPid
|
---|
73 | return fibGetPid();
|
---|
74 | #else
|
---|
75 | PTIB ptib;
|
---|
76 | PPIB ppib;
|
---|
77 | APIRET rc;
|
---|
78 |
|
---|
79 | rc = DosGetInfoBlocks(&ptib, &ppib);
|
---|
80 | if(rc == NO_ERROR) {
|
---|
81 | return ppib->pib_ulpid;
|
---|
82 | }
|
---|
83 | DebugInt3();
|
---|
84 | return 0;
|
---|
85 | #endif
|
---|
86 | }
|
---|
87 |
|
---|
88 | /***********************************************************************
|
---|
89 | * DosInitializeCriticalSection
|
---|
90 | */
|
---|
91 | ULONG WIN32API DosInitializeCriticalSection(CRITICAL_SECTION_OS2 *crit,
|
---|
92 | PSZ pszSemName, BOOL fShared)
|
---|
93 | {
|
---|
94 | APIRET rc;
|
---|
95 |
|
---|
96 | // initialize lock count with special value -1, meaning noone posesses it
|
---|
97 | crit->LockCount = -1;
|
---|
98 | crit->RecursionCount = 0;
|
---|
99 | crit->OwningThread = 0;
|
---|
100 |
|
---|
101 | rc = DosCreateEventSem(pszSemName, &crit->hmtxLock, (pszSemName || fShared) ? DC_SEM_SHARED : 0, 0);
|
---|
102 | if(rc != NO_ERROR) {
|
---|
103 | DebugInt3();
|
---|
104 | crit->hmtxLock = 0;
|
---|
105 | return rc;
|
---|
106 | }
|
---|
107 | crit->CreationCount = 1;
|
---|
108 | crit->Reserved = GetCurrentProcessId();
|
---|
109 | return NO_ERROR;
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | /***********************************************************************
|
---|
114 | * DosAccessCriticalSection
|
---|
115 | */
|
---|
116 | ULONG WIN32API DosAccessCriticalSection(CRITICAL_SECTION_OS2 *crit, PSZ pszSemName)
|
---|
117 | {
|
---|
118 | HMTX hmtxLock = 0;
|
---|
119 | APIRET rc;
|
---|
120 |
|
---|
121 | if(pszSemName == NULL && crit->hmtxLock == 0) {
|
---|
122 | DebugInt3();
|
---|
123 | return ERROR_INVALID_PARAMETER;
|
---|
124 | }
|
---|
125 | if(pszSemName == NULL) {
|
---|
126 | hmtxLock = crit->hmtxLock;
|
---|
127 | }
|
---|
128 |
|
---|
129 | rc = DosOpenEventSem(pszSemName, &hmtxLock);
|
---|
130 | if(rc != NO_ERROR) {
|
---|
131 | DebugInt3();
|
---|
132 | return rc;
|
---|
133 | }
|
---|
134 | DosInterlockedIncrement(&crit->CreationCount);
|
---|
135 | return NO_ERROR;
|
---|
136 | }
|
---|
137 | /***********************************************************************
|
---|
138 | * DosDeleteCriticalSection
|
---|
139 | */
|
---|
140 | ULONG WIN32API DosDeleteCriticalSection( CRITICAL_SECTION_OS2 *crit )
|
---|
141 | {
|
---|
142 | if (crit->hmtxLock)
|
---|
143 | {
|
---|
144 | #ifdef DEBUG
|
---|
145 | if ( (crit->LockCount != -1 && crit->CreationCount == 1)
|
---|
146 | || crit->OwningThread
|
---|
147 | || crit->RecursionCount) /* Should not happen */
|
---|
148 | {
|
---|
149 | DebugInt3();
|
---|
150 | }
|
---|
151 | #endif
|
---|
152 | DosCloseEventSem(crit->hmtxLock);
|
---|
153 | if(DosInterlockedDecrement(&crit->CreationCount) == 0)
|
---|
154 | {
|
---|
155 | crit->LockCount = -1;
|
---|
156 | crit->RecursionCount = 0;
|
---|
157 | crit->OwningThread = 0;
|
---|
158 | crit->hmtxLock = 0;
|
---|
159 | crit->Reserved = (DWORD)-1;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | return NO_ERROR;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | /***********************************************************************
|
---|
167 | * DosEnterCriticalSection
|
---|
168 | */
|
---|
169 | ULONG WIN32API DosEnterCriticalSection( CRITICAL_SECTION_OS2 *crit, ULONG ulTimeout )
|
---|
170 | {
|
---|
171 | DWORD res;
|
---|
172 | DWORD threadid = GetCurrentThreadId();
|
---|
173 |
|
---|
174 | // create crit sect just in time...
|
---|
175 | if (!crit->hmtxLock)
|
---|
176 | {
|
---|
177 | DosInitializeCriticalSection(crit, NULL);
|
---|
178 | }
|
---|
179 | // if the same thread is requesting it again, memorize it
|
---|
180 | if (crit->OwningThread == threadid)
|
---|
181 | {
|
---|
182 | crit->RecursionCount++;
|
---|
183 | return NO_ERROR;
|
---|
184 | }
|
---|
185 |
|
---|
186 | // do an atomic increase of the lockcounter
|
---|
187 | DosInterlockedIncrement(&crit->LockCount);
|
---|
188 |
|
---|
189 | // do an atomic operation where we compare the owning thread id with 0
|
---|
190 | // and if this is true, exchange it with the id of the current thread.
|
---|
191 | testenter:
|
---|
192 | if(DosInterlockedCompareExchange((PLONG)&crit->OwningThread, threadid, 0))
|
---|
193 | {
|
---|
194 | // the crit sect is in use
|
---|
195 | ULONG ulnrposts;
|
---|
196 |
|
---|
197 | // now wait for it
|
---|
198 | APIRET rc = DosWaitEventSem(crit->hmtxLock, ulTimeout);
|
---|
199 | if(rc != NO_ERROR) {
|
---|
200 | DebugInt3();
|
---|
201 | return rc;
|
---|
202 | }
|
---|
203 | DosResetEventSem(crit->hmtxLock, &ulnrposts);
|
---|
204 | // multiple waiters could be running now. Repeat the logic so that
|
---|
205 | // only one actually can get the critical section
|
---|
206 | goto testenter;
|
---|
207 | }
|
---|
208 | crit->RecursionCount = 1;
|
---|
209 | return NO_ERROR;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | /***********************************************************************
|
---|
214 | * DosLeaveCriticalSection
|
---|
215 | */
|
---|
216 | ULONG WIN32API DosLeaveCriticalSection( CRITICAL_SECTION_OS2 *crit )
|
---|
217 | {
|
---|
218 | if (crit->OwningThread != GetCurrentThreadId()) {
|
---|
219 | DebugInt3();
|
---|
220 | return ERROR_INVALID_PARAMETER;
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (--crit->RecursionCount)
|
---|
224 | {
|
---|
225 | //just return
|
---|
226 | return NO_ERROR;
|
---|
227 | }
|
---|
228 | crit->OwningThread = 0;
|
---|
229 | if (DosInterlockedDecrement( &crit->LockCount ) >= 0)
|
---|
230 | {
|
---|
231 | /* Someone is waiting */
|
---|
232 | DosPostEventSem(crit->hmtxLock);
|
---|
233 | }
|
---|
234 | return NO_ERROR;
|
---|
235 | }
|
---|