-
Notifications
You must be signed in to change notification settings - Fork 1
/
forkit.cpp
186 lines (174 loc) · 3.57 KB
/
forkit.cpp
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#ifdef OS2
#define INCL_DOSPROCESS
#else
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <sys/wait.h>
#endif
#include "forkit.h"
Fork::Fork()
: m_read(-1)
, m_write(-1)
, m_numThreads(0)
{
}
#ifdef OS2
// for the benefit of this function and the new Fork class it may create
// the Fork class must do nothing of note in it's constructor or it's
// go() member function.
VOID APIENTRY thread_func(ULONG param)
{
THREAD_DATA *td = (THREAD_DATA *)param;
td->f = new Fork;
td->f->startit(td);
}
#endif
void Fork::startit(THREAD_DATA *td)
{
m_read = td->child_read;
m_write = td->child_write;
td->func(this, td->param, td->threadNum);
delete td->f; // delete ourself if td->f is not NULL
delete td;
exit(0);
}
void Fork::go(FUNCTION func, PVOID param, int num)
{
m_numThreads = num;
FILE_TYPE control[2];
FILE_TYPE feedback[2];
if (pipe(feedback) || pipe(control))
{
fprintf(stderr, "Can't open pipes.\n");
exit(1);
}
#ifndef OS2
m_readPoll.events = POLLIN | POLLERR | POLLHUP | POLLNVAL;
m_writePoll.events = POLLOUT | POLLERR | POLLHUP | POLLNVAL;
#endif
THREAD_DATA *td = new THREAD_DATA;
td->child_read = control[0];
td->child_write = feedback[1];
td->f = NULL;
td->param = param;
td->func = func;
for(int i = 0; i < num; i++)
{
#ifdef OS2
THREAD_DATA *tmp = new THREAD_DATA;
memcpy(tmp, td, sizeof(THREAD_DATA));
#endif
td->threadNum = i;
#ifdef OS2
// yes I know I am casting a pointer to an unsigned long
// it's the way you're supposed to do things in OS/2
TID id = 0;
if(DosCreateThread(&id, thread_func, ULONG(td), CREATE_READY, 32*1024))
{
fprintf(stderr, "Can't create a thread.\n");
exit(1);
}
#else
int p = fork();
if(p == -1)
{
fprintf(stderr, "Can't fork.\n");
exit(1);
}
if(p == 0) // child
{
m_readPoll.fd = td->child_read;
m_writePoll.fd = td->child_write;
file_close(control[1]);
file_close(feedback[0]);
srand(getpid() ^ time(NULL));
startit(td);
}
#endif
}
// now we're in the parent thread/process
m_write = control[1];
m_read = feedback[0];
#ifndef OS2
m_readPoll.fd = m_read;
m_writePoll.fd = m_write;
file_close(control[0]);
file_close(feedback[1]);
#endif
}
int Fork::wait()
{
#ifdef OS2
TID status = 0;
if(DosWaitThread(&status, DCWW_WAIT))
{
fprintf(stderr, "Can't wait for thread.\n");
return 1;
}
#else
int status = 0;
if(::wait(&status) == -1)
{
fprintf(stderr, "Can't wait for thread.\n");
return 1;
}
#endif
return 0;
}
int Fork::Read(PVOID buf, int size, int timeout)
{
#ifndef OS2
if(timeout)
{
int rc = poll(&m_readPoll, 1, timeout * 1000);
if(rc < 0)
{
fprintf(stderr, "Can't poll.\n");
return -1;
}
if(!rc)
return 0;
}
#endif
#ifdef OS2
unsigned long actual;
int rc = DosRead(m_read, buf, size, &actual);
if(rc || actual != size)
#else
if(size != read(m_read, buf, size) )
#endif
{
fprintf(stderr, "Can't read data from IPC pipe.\n");
return -1;
}
return size;
}
int Fork::Write(PVOID buf, int size, int timeout)
{
#ifndef OS2
if(timeout)
{
int rc = poll(&m_writePoll, 1, timeout * 1000);
if(rc < 0)
{
fprintf(stderr, "Can't poll for write.\n");
return -1;
}
if(!rc)
return 0;
}
#endif
#ifdef OS2
unsigned long actual;
int rc = DosWrite(m_write, buf, size, &actual);
if(rc || actual != size)
#else
if(size != write(m_write, buf, size))
#endif
{
fprintf(stderr, "Can't write data to IPC pipe.\n");
return -1;
}
return size;
}