-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcethread.c
148 lines (114 loc) · 2.5 KB
/
cethread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// cethread.c
//
// Time-stamp: <03/02/01 10:11:45 keuchel@lightning>
#include "celib.h"
#include "cethread.h"
unsigned long __tlsindex = 0xffffffff;
unsigned long WINAPI _threadstart(void *);
void _freeptd(_ptiddata);
void _initptd(_ptiddata);
void
_init_multithread()
{
if((__tlsindex = TlsAlloc()) == 0xFFFFFFFF)
xceabort();
}
unsigned long
_beginthread(void (* initialcode) (void *), unsigned stacksize,
void * argument)
{
_ptiddata ptd;
unsigned long thdl;
unsigned long errcode = 0L;
if(__tlsindex == 0xFFFFFFFF)
_init_multithread();
if ( (ptd = (void *) calloc(1, sizeof(struct _tiddata))) == NULL )
goto error_return;
_initptd(ptd);
ptd->_initaddr = (void *) initialcode;
ptd->_initarg = argument;
if ( (ptd->_thandle = thdl = (unsigned long)
CreateThread( NULL,
stacksize,
_threadstart,
(LPVOID)ptd,
CREATE_SUSPENDED,
(LPDWORD)&(ptd->_tid) )) == 0L )
{
errcode = GetLastError();
goto error_return;
}
if ( ResumeThread( (HANDLE)thdl ) == (DWORD)(-1L) ) {
errcode = GetLastError();
goto error_return;
}
return(thdl);
error_return:
free(ptd);
return((unsigned long)-1L);
}
static unsigned long
_threadstart (void * ptd)
{
if ( !TlsSetValue(__tlsindex, ptd) )
xceabort();
((void(__cdecl *)(void *))(((_ptiddata)ptd)->_initaddr))
(((_ptiddata)ptd)->_initarg );
_endthread();
// not reached
return(0L);
}
void
_endthread (void)
{
_ptiddata ptd;
if((ptd = _getptd()) == NULL )
xceabort();
if(ptd->_thandle != (unsigned long)(-1L))
CloseHandle((HANDLE)(ptd->_thandle));
_freeptd(ptd);
ExitThread(0);
}
void
_initptd (_ptiddata ptd)
{
}
void
_freeptd (_ptiddata ptd)
{
if ( __tlsindex != 0xFFFFFFFF )
{
if (!ptd)
{
ptd = TlsGetValue(__tlsindex);
}
if(ptd)
{
free((void *)ptd);
}
TlsSetValue(__tlsindex, (LPVOID) 0);
}
}
_ptiddata
_getptd (void)
{
_ptiddata ptd;
DWORD TL_LastError;
TL_LastError = GetLastError();
if ((ptd = TlsGetValue(__tlsindex)) == NULL)
{
if(((ptd = (void*) calloc(1, sizeof(struct _tiddata))) != NULL) &&
TlsSetValue(__tlsindex, (LPVOID)ptd))
{
_initptd(ptd);
ptd->_tid = GetCurrentThreadId();
ptd->_thandle = (unsigned long)(-1L);
}
else
{
xceabort();
}
}
SetLastError(TL_LastError);
return(ptd);
}