1 | /* $Id: critsec.cpp,v 1.1 2000-08-22 08:09:20 phaller Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * The Visual C RunTime DLL (MSVCRT/MSVCRT20/MSVCRT40)
|
---|
5 | *
|
---|
6 | * Implements Visual C run-time functionality
|
---|
7 | *
|
---|
8 | * Copyright 2000 Patrick Haller
|
---|
9 | */
|
---|
10 |
|
---|
11 |
|
---|
12 | #include <os2win.h>
|
---|
13 | #include <odinwrap.h>
|
---|
14 | #include <wchar.h>
|
---|
15 | #include <math.h>
|
---|
16 | #include <string.h>
|
---|
17 |
|
---|
18 | #include <heapstring.h>
|
---|
19 | #include <crtdll.h>
|
---|
20 |
|
---|
21 | #include <win/winbase.h>
|
---|
22 | #include <win/winnt.h>
|
---|
23 | #include "msvcrt.h"
|
---|
24 |
|
---|
25 | ODINDEBUGCHANNEL(msvcrt-critsec)
|
---|
26 |
|
---|
27 | /*****************************************************************************
|
---|
28 | * local variables *
|
---|
29 | *****************************************************************************/
|
---|
30 |
|
---|
31 | #define CRITSEC_TABLE_LOCK 0x11
|
---|
32 | #define CRITSEC_MAX_ENTRIES 0x30
|
---|
33 |
|
---|
34 | static CRITICAL_SECTION* arrpCriticalSections[CRITSEC_MAX_ENTRIES] = {0};
|
---|
35 | static CRITICAL_SECTION* pCriticalSection_01 = arrpCriticalSections[0x01];
|
---|
36 | static CRITICAL_SECTION* pCriticalSection_09 = arrpCriticalSections[0x09];
|
---|
37 | static CRITICAL_SECTION* pCriticalSection_0D = arrpCriticalSections[0x0D];
|
---|
38 | static CRITICAL_SECTION* pCriticalSection_11 = arrpCriticalSections[CRITSEC_TABLE_LOCK];
|
---|
39 |
|
---|
40 |
|
---|
41 | /*********************************************************************
|
---|
42 | * INTERNAL startup_critsec_table
|
---|
43 | */
|
---|
44 |
|
---|
45 | #if NOT_YET_COMPLETE
|
---|
46 | void startup_critsec_table(void)
|
---|
47 | {
|
---|
48 | CRITICAL_SECTION *pCS;
|
---|
49 |
|
---|
50 | for (int iIndex = 0;
|
---|
51 | iIndex < CRITSEC_MAX_ENTRIES;
|
---|
52 | iIndex++)
|
---|
53 | {
|
---|
54 | // delete all "non-critical" critical sections
|
---|
55 | pCS = arrpCriticalSections[iIndex];
|
---|
56 | if ( (pCS != pCriticalSection_01) &&
|
---|
57 | (pCS != pCriticalSection_09) &&
|
---|
58 | (pCS != pCriticalSection_0D) &&
|
---|
59 | (pCS != pCriticalSection_11) )
|
---|
60 | {
|
---|
61 | DeleteCriticalSection(pCS);
|
---|
62 | free(pCS);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | // delete important critical sections
|
---|
67 | DeleteCriticalSection(pCriticalSection_01);
|
---|
68 | DeleteCriticalSection(pCriticalSection_09);
|
---|
69 | DeleteCriticalSection(pCriticalSection_0D);
|
---|
70 | DeleteCriticalSection(pCriticalSection_11);
|
---|
71 | }
|
---|
72 |
|
---|
73 | #endif
|
---|
74 |
|
---|
75 | /*********************************************************************
|
---|
76 | * _unlock (MSVCRT.480)
|
---|
77 | */
|
---|
78 |
|
---|
79 | VOID CDECL MSVCRT__unlock(unsigned long ulIndex)
|
---|
80 | {
|
---|
81 | dprintf(("MSVCRT: _unlock %08xh\n",
|
---|
82 | ulIndex));
|
---|
83 |
|
---|
84 | CRITICAL_SECTION *pCS = arrpCriticalSections[ulIndex];
|
---|
85 | LeaveCriticalSection(pCS);
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | /*********************************************************************
|
---|
90 | * _lock (MSVCRT.318)
|
---|
91 | */
|
---|
92 | // Prototype from CRTDLL.CPP
|
---|
93 | VOID CDECL amsg_exit(int errnum);
|
---|
94 |
|
---|
95 | // @@@PH Note: subsystem startup & termination
|
---|
96 | // is absent. That's a show stopper of course!
|
---|
97 | // See MSVCRT.start() code.
|
---|
98 |
|
---|
99 | VOID CDECL MSVCRT__lock(unsigned long ulIndex)
|
---|
100 | {
|
---|
101 | dprintf(("MSVCRT: _lock %08xh\n",
|
---|
102 | ulIndex));
|
---|
103 |
|
---|
104 | if (arrpCriticalSections[ulIndex] == NULL)
|
---|
105 | {
|
---|
106 | CRITICAL_SECTION *pCSNew;
|
---|
107 |
|
---|
108 | pCSNew = (CRITICAL_SECTION*)malloc( sizeof(CRITICAL_SECTION) );
|
---|
109 | if (pCSNew == NULL)
|
---|
110 | amsg_exit(0x11); // yield error message
|
---|
111 |
|
---|
112 | MSVCRT__lock(CRITSEC_TABLE_LOCK); // lock table
|
---|
113 | // has been modified meanwhile by other thread ?
|
---|
114 | if (arrpCriticalSections[ulIndex] != NULL)
|
---|
115 | free(pCSNew);
|
---|
116 | else
|
---|
117 | {
|
---|
118 | InitializeCriticalSection(pCSNew);
|
---|
119 | arrpCriticalSections[ulIndex] = pCSNew;
|
---|
120 | }
|
---|
121 |
|
---|
122 | MSVCRT__unlock(CRITSEC_TABLE_LOCK); // unlock table
|
---|
123 | }
|
---|
124 |
|
---|
125 | EnterCriticalSection(arrpCriticalSections[ulIndex]);
|
---|
126 | }
|
---|