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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
/* $OpenBSD: rthread_cancel.c,v 1.5 2012/03/22 01:11:47 guenther Exp $ */
/* $snafu: libc_tag.c,v 1.4 2004/11/30 07:00:06 marc Exp $ */
/* PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */
#define _POSIX_THREADS
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/msg.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/resource.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdarg.h>
#include <termios.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include "thread_private.h" /* in libc/include */
#include "rthread.h"
int _thread_sys_accept(int, struct sockaddr *, socklen_t *);
int _thread_sys_close(int);
int _thread_sys_closefrom(int);
int _thread_sys_connect(int, const struct sockaddr *, socklen_t);
int _thread_sys_fcntl(int, int, ...);
int _thread_sys_fsync(int);
int _thread_sys_msgrcv(int, void *, size_t, long, int);
int _thread_sys_msgsnd(int, const void *, size_t, int);
int _thread_sys_msync(void *, size_t, int);
int _thread_sys_nanosleep(const struct timespec *, struct timespec *);
int _thread_sys_open(const char *, int, ...);
int _thread_sys_openat(int, const char *, int, ...);
int _thread_sys_poll(struct pollfd *, nfds_t, int);
ssize_t _thread_sys_pread(int, void *, size_t, off_t);
ssize_t _thread_sys_preadv(int, const struct iovec *, int, off_t);
ssize_t _thread_sys_pwrite(int, const void *, size_t, off_t);
ssize_t _thread_sys_pwritev(int, const struct iovec *, int, off_t);
ssize_t _thread_sys_read(int, void *, size_t);
ssize_t _thread_sys_readv(int, const struct iovec *, int);
ssize_t _thread_sys_recvfrom(int, void *, size_t, int, struct sockaddr *,
socklen_t *);
ssize_t _thread_sys_recvmsg(int, struct msghdr *, int);
int _thread_sys_select(int, fd_set *, fd_set *, fd_set *,
struct timeval *);
ssize_t _thread_sys_sendmsg(int, const struct msghdr *, int);
ssize_t _thread_sys_sendto(int, const void *, size_t, int,
const struct sockaddr *, socklen_t);
int _thread_sys_sigsuspend(const sigset_t *);
int _thread_sys_tcdrain(int);
pid_t _thread_sys_wait4(pid_t, int *, int, struct rusage *);
ssize_t _thread_sys_write(int, const void *, size_t);
ssize_t _thread_sys_writev(int, const struct iovec *, int);
void
_enter_cancel(pthread_t self)
{
if (self->flags & THREAD_CANCEL_ENABLE) {
self->cancel_point++;
if (IS_CANCELED(self))
pthread_exit(PTHREAD_CANCELED);
}
}
void
_leave_cancel(pthread_t self)
{
if (self->flags & THREAD_CANCEL_ENABLE)
self->cancel_point--;
}
void
_enter_delayed_cancel(pthread_t self)
{
if (self->flags & THREAD_CANCEL_ENABLE) {
self->delayed_cancel = 0;
self->cancel_point++;
if (IS_CANCELED(self))
pthread_exit(PTHREAD_CANCELED);
_rthread_setflag(self, THREAD_CANCEL_DELAY);
}
}
void
_leave_delayed_cancel(pthread_t self, int can_cancel)
{
if (self->flags & THREAD_CANCEL_ENABLE) {
if (self->flags & THREAD_CANCEL_DELAY) {
self->cancel_point--;
_rthread_clearflag(self, THREAD_CANCEL_DELAY);
}
if (IS_CANCELED(self) && can_cancel)
pthread_exit(PTHREAD_CANCELED);
self->delayed_cancel = 0;
}
}
int
accept(int fd, struct sockaddr *addr, socklen_t *addrlen)
{
pthread_t self = pthread_self();
int rv;
_enter_cancel(self);
rv = _thread_sys_accept(fd, addr, addrlen);
_leave_cancel(self);
return (rv);
}
#if 0
aio_suspend() /* don't have yet */
clock_nanosleep() /* don't have yet */
#endif
int
close(int fd)
{
pthread_t self = pthread_self();
int rv;
_enter_cancel(self);
rv = _thread_sys_close(fd);
_leave_cancel(self);
return (rv);
}
int
closefrom(int fd)
{
pthread_t self = pthread_self();
int rv;
_enter_cancel(self);
rv = _thread_sys_closefrom(fd);
_leave_cancel(self);
return (rv);
}
int
connect(int fd, const struct sockaddr *addr, socklen_t addrlen)
{
pthread_t self = pthread_self();
int rv;
_enter_cancel(self);
rv = _thread_sys_connect(fd, addr, addrlen);
_leave_cancel(self);
return (rv);
}
#if 0
creat() /* built on open() */
#endif
int
fcntl(int fd, int cmd, ...)
{
va_list ap;
int rv;
va_start(ap, cmd);
switch (cmd) {
case F_DUPFD:
case F_DUPFD_CLOEXEC:
case F_SETFD:
case F_SETFL:
case F_SETOWN:
rv = _thread_sys_fcntl(fd, cmd, va_arg(ap, int));
break;
case F_GETFD:
case F_GETFL:
case F_GETOWN:
rv = _thread_sys_fcntl(fd, cmd);
break;
case F_GETLK:
case F_SETLK:
rv = _thread_sys_fcntl(fd, cmd, va_arg(ap, struct flock *));
break;
case F_SETLKW:
{
pthread_t self = pthread_self();
_enter_cancel(self);
rv = _thread_sys_fcntl(fd, cmd, va_arg(ap, struct flock *));
_leave_cancel(self);
break;
}
default: /* should never happen? */
rv = _thread_sys_fcntl(fd, cmd, va_arg(ap, void *));
break;
}
va_end(ap);
return (rv);
}
#if 0
fdatasync() /* don't have yet */
#endif
int
fsync(int fd)
{
pthread_t self = pthread_self();
int rv;
_enter_cancel(self);
rv = _thread_sys_fsync(fd);
_leave_cancel(self);
return (rv);
}
#if 0
getmsg() /* don't have: dumb STREAMS stuff */
getpmsg() /* don't have: dumb STREAMS stuff */
lockf() /* built on fcntl() */
mq_receive() /* don't have yet */
mq_send() /* don't have yet */
mq_timedreceive() /* don't have yet */
mq_timedsend() /* don't have yet */
#endif
int
msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
{
pthread_t self = pthread_self();
int rv;
_enter_cancel(self);
rv = _thread_sys_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg);
_leave_cancel(self);
return (rv);
}
int
msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg)
{
pthread_t self = pthread_self();
int rv;
_enter_cancel(self);
rv = _thread_sys_msgsnd(msqid, msgp, msgsz, msgflg);
_leave_cancel(self);
return (rv);
}
int
msync(void *addr, size_t len, int flags)
{
pthread_t self = pthread_self();
int rv;
_enter_cancel(self);
rv = _thread_sys_msync(addr, len, flags);
_leave_cancel(self);
return (rv);
}
int
nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
{
pthread_t self = pthread_self();
int rv;
_enter_cancel(self);
rv = _thread_sys_nanosleep(rqtp, rmtp);
_leave_cancel(self);
return (rv);
}
int
open(const char *path, int flags, ...)
{
pthread_t self = pthread_self();
va_list ap;
mode_t mode = 0;
int rv;
if (flags & O_CREAT) {
va_start(ap, flags);
mode = va_arg(ap, int);
va_end(ap);
}
_enter_cancel(self);
rv = _thread_sys_open(path, flags, mode);
_leave_cancel(self);
return (rv);
}
int
openat(int fd, const char *path, int flags, ...)
{
pthread_t self = pthread_self();
va_list ap;
mode_t mode = 0;
int rv;
if (flags & O_CREAT) {
va_start(ap, flags);
mode = va_arg(ap, int);
va_end(ap);
}
_enter_cancel(self);
rv = _thread_sys_openat(fd, path, flags, mode);
_leave_cancel(self);
return (rv);
}
#if 0
pause() /* built on sigsuspend() */
#endif
int
poll(struct pollfd *fds, nfds_t nfds, int timeout)
{
pthread_t self = pthread_self();
int rv;
_enter_cancel(self);
rv = _thread_sys_poll(fds, nfds, timeout);
_leave_cancel(self);
return (rv);
}
ssize_t
pread(int fd, void *buf, size_t nbytes, off_t offset)
{
pthread_t self = pthread_self();
ssize_t rv;
_enter_cancel(self);
rv = _thread_sys_pread(fd, buf, nbytes, offset);
_leave_cancel(self);
return (rv);
}
ssize_t
preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset)
{
pthread_t self = pthread_self();
ssize_t rv;
_enter_cancel(self);
rv = _thread_sys_preadv(fd, iov, iovcnt, offset);
_leave_cancel(self);
return (rv);
}
#if 0
pselect() /* don't have yet */
putmsg() /* don't have: dumb STREAMS stuff */
putpmsg() /* don't have: dumb STREAMS stuff */
#endif
ssize_t
pwrite(int fd, const void *buf, size_t nbytes, off_t offset)
{
pthread_t self = pthread_self();
ssize_t rv;
_enter_cancel(self);
rv = _thread_sys_pwrite(fd, buf, nbytes, offset);
_leave_cancel(self);
return (rv);
}
ssize_t
pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset)
{
pthread_t self = pthread_self();
ssize_t rv;
_enter_cancel(self);
rv = _thread_sys_pwritev(fd, iov, iovcnt, offset);
_leave_cancel(self);
return (rv);
}
ssize_t
read(int fd, void *buf, size_t nbytes)
{
pthread_t self = pthread_self();
ssize_t rv;
_enter_cancel(self);
rv = _thread_sys_read(fd, buf, nbytes);
_leave_cancel(self);
return (rv);
}
ssize_t
readv(int fd, const struct iovec *iov, int iovcnt)
{
pthread_t self = pthread_self();
ssize_t rv;
_enter_cancel(self);
rv = _thread_sys_readv(fd, iov, iovcnt);
_leave_cancel(self);
return (rv);
}
#if 0
recv() /* built on recvfrom() */
#endif
ssize_t
recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *addr,
socklen_t *addrlen)
{
pthread_t self = pthread_self();
ssize_t rv;
_enter_cancel(self);
rv = _thread_sys_recvfrom(fd, buf, len, flags, addr, addrlen);
_leave_cancel(self);
return (rv);
}
ssize_t
recvmsg(int fd, struct msghdr *msg, int flags)
{
pthread_t self = pthread_self();
ssize_t rv;
_enter_cancel(self);
rv = _thread_sys_recvmsg(fd, msg, flags);
_leave_cancel(self);
return (rv);
}
int
select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
struct timeval *timeout)
{
pthread_t self = pthread_self();
int rv;
_enter_cancel(self);
rv = _thread_sys_select(nfds, readfds, writefds, exceptfds, timeout);
_leave_cancel(self);
return (rv);
}
#if 0
sem_timedwait() /* in rthread_sem.c */
sem_wait() /* in rthread_sem.c */
send() /* built on sendto() */
#endif
ssize_t
sendmsg(int fd, const struct msghdr *msg, int flags)
{
pthread_t self = pthread_self();
ssize_t rv;
_enter_cancel(self);
rv = _thread_sys_sendmsg(fd, msg, flags);
_leave_cancel(self);
return (rv);
}
ssize_t
sendto(int fd, const void *msg, size_t len, int flags,
const struct sockaddr *to, socklen_t tolen)
{
pthread_t self = pthread_self();
ssize_t rv;
_enter_cancel(self);
rv = _thread_sys_sendto(fd, msg, len, flags, to, tolen);
_leave_cancel(self);
return (rv);
}
int
sigsuspend(const sigset_t *sigmask)
{
pthread_t self = pthread_self();
sigset_t set = *sigmask;
int rv;
sigdelset(&set, SIGTHR);
_enter_cancel(self);
rv = _thread_sys_sigsuspend(&set);
_leave_cancel(self);
return (rv);
}
#if 0
sigtimedwait() /* don't have yet */
sigwait() /* in rthread_sig.c */
sigwaitinfo() /* don't have yet */
sleep() /* built on nanosleep() */
system() /* built on wait4()? XXX */
#endif
int
tcdrain(int fd)
{
pthread_t self = pthread_self();
int rv;
_enter_cancel(self);
rv = ioctl(fd, TIOCDRAIN, 0);
_leave_cancel(self);
return (rv);
}
#if 0
wait() /* built on wait4() */
waitid() /* don't have yet */
waitpid() /* built on wait4() */
#endif
pid_t
wait4(pid_t wpid, int *status, int options, struct rusage *rusage)
{
pthread_t self = pthread_self();
pid_t rv;
_enter_cancel(self);
rv = _thread_sys_wait4(wpid, status, options, rusage);
_leave_cancel(self);
return (rv);
}
ssize_t
write(int fd, const void *buf, size_t nbytes)
{
pthread_t self = pthread_self();
ssize_t rv;
_enter_cancel(self);
rv = _thread_sys_write(fd, buf, nbytes);
_leave_cancel(self);
return (rv);
}
ssize_t
writev(int fd, const struct iovec *iov, int iovcnt)
{
pthread_t self = pthread_self();
ssize_t rv;
_enter_cancel(self);
rv = _thread_sys_writev(fd, iov, iovcnt);
_leave_cancel(self);
return (rv);
}
|