-
Notifications
You must be signed in to change notification settings - Fork 0
/
pth_high.c
1492 lines (1295 loc) · 47.1 KB
/
pth_high.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
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** GNU Pth - The GNU Portable Threads
** Copyright (c) 1999-2006 Ralf S. Engelschall <[email protected]>
**
** This file is part of GNU Pth, a non-preemptive thread scheduling
** library which can be found at http://www.gnu.org/software/pth/.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA, or contact Ralf S. Engelschall <[email protected]>.
**
** pth_high.c: Pth high-level replacement functions
*/
/* ``The difference between a computer
industry job and open-source software
hacking is about 30 hours a week.''
-- Ralf S. Engelschall */
/*
* These functions used by the applications instead of the
* regular Unix/POSIX functions. When the regular functions would
* block, these variants let only the thread sleep.
*/
#include "pth_p.h"
/* Pth variant of nanosleep(2) */
int pth_nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
{
pth_itime_t until;
pth_itime_t offset;
pth_itime_t now;
pth_event_t ev;
static pth_key_t ev_key = PTH_KEY_INIT;
/* consistency checks for POSIX conformance */
if (rqtp == NULL)
return pth_error(-1, EFAULT);
if (rqtp->tv_nsec < 0 || rqtp->tv_nsec > (1000*1000000))
return pth_error(-1, EINVAL);
/* short-circuit */
if (rqtp->tv_sec == 0 && rqtp->tv_nsec == 0)
return 0;
/* calculate asleep time */
offset = pth_itime((long)(rqtp->tv_sec), (long)(rqtp->tv_nsec) / 1000);
pth_time_set(&until, PTH_TIME_NOW);
pth_time_add(&until, &offset);
/* and let thread sleep until this time is elapsed */
if ((ev = pth_event(PTH_EVENT_RTIME|PTH_MODE_STATIC, &ev_key, offset)) == NULL)
return pth_error(-1, errno);
pth_wait(ev);
/* optionally provide amount of slept time */
if (rmtp != NULL) {
pth_time_set(&now, PTH_TIME_NOW);
pth_time_sub(&until, &now);
rmtp->tv_sec = until.t_sec;
rmtp->tv_nsec = until.t_usec * 1000;
}
return 0;
}
/* Pth variant of usleep(3) */
int pth_usleep(unsigned int usec)
{
pth_itime_t offset;
pth_event_t ev;
static pth_key_t ev_key = PTH_KEY_INIT;
/* short-circuit */
if (usec == 0)
return 0;
/* calculate asleep time */
offset = pth_itime((long)(usec / 1000000), (long)(usec % 1000000));
/* and let thread sleep until this time is elapsed */
if ((ev = pth_event(PTH_EVENT_RTIME|PTH_MODE_STATIC, &ev_key, offset)) == NULL)
return pth_error(-1, errno);
pth_wait(ev);
return 0;
}
/* Pth variant of sleep(3) */
unsigned int pth_sleep(unsigned int sec)
{
pth_itime_t offset;
pth_event_t ev;
static pth_key_t ev_key = PTH_KEY_INIT;
/* consistency check */
if (sec == 0)
return 0;
/* calculate asleep time */
offset = pth_itime(sec, 0);
/* and let thread sleep until this time is elapsed */
if ((ev = pth_event(PTH_EVENT_RTIME|PTH_MODE_STATIC, &ev_key, offset)) == NULL)
return sec;
pth_wait(ev);
return 0;
}
/* Pth variant of POSIX pthread_sigmask(3) */
int pth_sigmask(int how, const sigset_t *set, sigset_t *oset)
{
int rv;
/* change the explicitly remembered signal mask copy for the scheduler */
if (set != NULL)
pth_sc(sigprocmask)(how, &(pth_current->mctx.sigs), NULL);
/* change the real (per-thread saved/restored) signal mask */
rv = pth_sc(sigprocmask)(how, set, oset);
return rv;
}
/* Pth variant of POSIX sigwait(3) */
int pth_sigwait(const sigset_t *set, int *sigp)
{
return pth_sigwait_ev(set, sigp, NULL);
}
/* Pth variant of POSIX sigwait(3) with extra events */
int pth_sigwait_ev(const sigset_t *set, int *sigp, pth_event_t ev_extra)
{
pth_event_t ev;
static pth_key_t ev_key = PTH_KEY_INIT;
sigset_t pending;
int sig;
if (set == NULL || sigp == NULL)
return pth_error(EINVAL, EINVAL);
/* check whether signal is already pending */
if (sigpending(&pending) < 0)
sigemptyset(&pending);
for (sig = 1; sig < PTH_NSIG; sig++) {
if (sigismember(set, sig) && sigismember(&pending, sig)) {
pth_util_sigdelete(sig);
*sigp = sig;
return 0;
}
}
/* create event and wait on it */
if ((ev = pth_event(PTH_EVENT_SIGS|PTH_MODE_STATIC, &ev_key, set, sigp)) == NULL)
return pth_error(errno, errno);
if (ev_extra != NULL)
pth_event_concat(ev, ev_extra, NULL);
pth_wait(ev);
if (ev_extra != NULL) {
pth_event_isolate(ev);
if (pth_event_status(ev) != PTH_STATUS_OCCURRED)
return pth_error(EINTR, EINTR);
}
/* nothing to do, scheduler has already set *sigp for us */
return 0;
}
/* Pth variant of waitpid(2) */
pid_t pth_waitpid(pid_t wpid, int *status, int options)
{
pth_event_t ev;
static pth_key_t ev_key = PTH_KEY_INIT;
pid_t pid;
pth_debug2("pth_waitpid: called from thread \"%s\"", pth_current->name);
for (;;) {
/* do a non-blocking poll for the pid */
while ( (pid = pth_sc(waitpid)(wpid, status, options|WNOHANG)) < 0
&& errno == EINTR) ;
/* if pid was found or caller requested a polling return immediately */
if (pid == -1 || pid > 0 || (pid == 0 && (options & WNOHANG)))
break;
/* else wait a little bit */
ev = pth_event(PTH_EVENT_RTIME|PTH_MODE_STATIC, &ev_key, pth_time(0,250000));
pth_wait(ev);
}
pth_debug2("pth_waitpid: leave to thread \"%s\"", pth_current->name);
return pid;
}
/* Pth variant of system(3) */
int pth_system(const char *cmd)
{
struct sigaction sa_ign, sa_int, sa_quit;
sigset_t ss_block, ss_old;
struct stat sb;
pid_t pid;
int pstat;
/* POSIX calling convention: determine whether the
Bourne Shell ("sh") is available on this platform */
if (cmd == NULL) {
if (stat(PTH_PATH_BINSH, &sb) == -1)
return 0;
return 1;
}
/* temporarily ignore SIGINT and SIGQUIT actions */
sa_ign.sa_handler = SIG_IGN;
sigemptyset(&sa_ign.sa_mask);
sa_ign.sa_flags = 0;
sigaction(SIGINT, &sa_ign, &sa_int);
sigaction(SIGQUIT, &sa_ign, &sa_quit);
/* block SIGCHLD signal */
sigemptyset(&ss_block);
sigaddset(&ss_block, SIGCHLD);
pth_sc(sigprocmask)(SIG_BLOCK, &ss_block, &ss_old);
/* fork the current process */
pstat = -1;
switch (pid = pth_fork()) {
case -1: /* error */
break;
case 0: /* child */
/* restore original signal dispositions and execute the command */
sigaction(SIGINT, &sa_int, NULL);
sigaction(SIGQUIT, &sa_quit, NULL);
pth_sc(sigprocmask)(SIG_SETMASK, &ss_old, NULL);
/* stop the Pth scheduling */
pth_scheduler_kill();
/* execute the command through Bourne Shell */
execl(PTH_PATH_BINSH, "sh", "-c", cmd, (char *)NULL);
/* POSIX compliant return in case execution failed */
exit(127);
default: /* parent */
/* wait until child process terminates */
pid = pth_waitpid(pid, &pstat, 0);
break;
}
/* restore original signal dispositions and execute the command */
sigaction(SIGINT, &sa_int, NULL);
sigaction(SIGQUIT, &sa_quit, NULL);
pth_sc(sigprocmask)(SIG_SETMASK, &ss_old, NULL);
/* return error or child process result code */
return (pid == -1 ? -1 : pstat);
}
/* Pth variant of select(2) */
int pth_select(int nfds, fd_set *rfds, fd_set *wfds,
fd_set *efds, struct timeval *timeout)
{
return pth_select_ev(nfds, rfds, wfds, efds, timeout, NULL);
}
/* Pth variant of select(2) with extra events */
int pth_select_ev(int nfd, fd_set *rfds, fd_set *wfds,
fd_set *efds, struct timeval *timeout, pth_event_t ev_extra)
{
struct timeval delay;
pth_event_t ev;
pth_event_t ev_select;
pth_event_t ev_timeout;
static pth_key_t ev_key_select = PTH_KEY_INIT;
static pth_key_t ev_key_timeout = PTH_KEY_INIT;
fd_set rspare, wspare, espare;
fd_set *rtmp, *wtmp, *etmp;
int selected;
int rc;
pth_implicit_init();
pth_debug2("pth_select_ev: called from thread \"%s\"", pth_current->name);
/* POSIX.1-2001/SUSv3 compliance */
if (nfd < 0 || nfd > FD_SETSIZE)
return pth_error(-1, EINVAL);
if (timeout != NULL) {
if ( timeout->tv_sec < 0
|| timeout->tv_usec < 0
|| timeout->tv_usec >= 1000000 /* a full second */)
return pth_error(-1, EINVAL);
if (timeout->tv_sec > 31*24*60*60)
timeout->tv_sec = 31*24*60*60;
}
/* first deal with the special situation of a plain microsecond delay */
if (nfd == 0 && rfds == NULL && wfds == NULL && efds == NULL && timeout != NULL) {
if (timeout->tv_sec == 0 && timeout->tv_usec <= 10000 /* 1/100 second */) {
/* very small delays are acceptable to be performed directly */
while ( pth_sc(select)(0, NULL, NULL, NULL, timeout) < 0
&& errno == EINTR) ;
}
else {
/* larger delays have to go through the scheduler */
ev = pth_event(PTH_EVENT_RTIME|PTH_MODE_STATIC, &ev_key_timeout,
pth_time(timeout->tv_sec, timeout->tv_usec));
if (ev_extra != NULL)
pth_event_concat(ev, ev_extra, NULL);
pth_wait(ev);
if (ev_extra != NULL) {
pth_event_isolate(ev);
if (pth_event_status(ev) != PTH_STATUS_OCCURRED)
return pth_error(-1, EINTR);
}
}
/* POSIX.1-2001/SUSv3 compliance */
if (rfds != NULL) FD_ZERO(rfds);
if (wfds != NULL) FD_ZERO(wfds);
if (efds != NULL) FD_ZERO(efds);
return 0;
}
/* now directly poll filedescriptor sets to avoid unnecessary
(and resource consuming because of context switches, etc) event
handling through the scheduler. We've to be carefully here, because not
all platforms guaranty us that the sets are unmodified if an error
or timeout occurred. */
delay.tv_sec = 0;
delay.tv_usec = 0;
rtmp = NULL;
if (rfds != NULL) {
memcpy(&rspare, rfds, sizeof(fd_set));
rtmp = &rspare;
}
wtmp = NULL;
if (wfds != NULL) {
memcpy(&wspare, wfds, sizeof(fd_set));
wtmp = &wspare;
}
etmp = NULL;
if (efds != NULL) {
memcpy(&espare, efds, sizeof(fd_set));
etmp = &espare;
}
while ((rc = pth_sc(select)(nfd, rtmp, wtmp, etmp, &delay)) < 0
&& errno == EINTR)
;
if (rc < 0)
/* pass-through immediate error */
return pth_error(-1, errno);
else if ( rc > 0
|| ( rc == 0
&& timeout != NULL
&& timeout->tv_sec == 0
&& timeout->tv_usec == 0)) {
/* pass-through immediate success */
if (rfds != NULL)
memcpy(rfds, &rspare, sizeof(fd_set));
if (wfds != NULL)
memcpy(wfds, &wspare, sizeof(fd_set));
if (efds != NULL)
memcpy(efds, &espare, sizeof(fd_set));
return rc;
}
/* suspend current thread until one filedescriptor
is ready or the timeout occurred */
rc = -1;
ev = ev_select = pth_event(PTH_EVENT_SELECT|PTH_MODE_STATIC,
&ev_key_select, &rc, nfd, rfds, wfds, efds);
ev_timeout = NULL;
if (timeout != NULL) {
ev_timeout = pth_event(PTH_EVENT_RTIME|PTH_MODE_STATIC, &ev_key_timeout,
pth_time(timeout->tv_sec, timeout->tv_usec));
pth_event_concat(ev, ev_timeout, NULL);
}
if (ev_extra != NULL)
pth_event_concat(ev, ev_extra, NULL);
pth_wait(ev);
if (ev_extra != NULL)
pth_event_isolate(ev_extra);
if (timeout != NULL)
pth_event_isolate(ev_timeout);
/* select return code semantics */
if (pth_event_status(ev_select) == PTH_STATUS_FAILED)
return pth_error(-1, EBADF);
selected = FALSE;
if (pth_event_status(ev_select) == PTH_STATUS_OCCURRED)
selected = TRUE;
if ( timeout != NULL
&& pth_event_status(ev_timeout) == PTH_STATUS_OCCURRED) {
selected = TRUE;
/* POSIX.1-2001/SUSv3 compliance */
if (rfds != NULL) FD_ZERO(rfds);
if (wfds != NULL) FD_ZERO(wfds);
if (efds != NULL) FD_ZERO(efds);
rc = 0;
}
if (ev_extra != NULL && !selected)
return pth_error(-1, EINTR);
return rc;
}
/* Pth variant of pth_pselect(2) */
int pth_pselect(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds,
const struct timespec *ts, const sigset_t *mask)
{
sigset_t omask;
struct timeval tv;
struct timeval *tvp;
int rv;
/* convert timeout */
if (ts != NULL) {
tv.tv_sec = ts->tv_sec;
tv.tv_usec = ts->tv_nsec / 1000;
tvp = &tv;
}
else
tvp = NULL;
/* optionally set signal mask */
if (mask != NULL)
if (pth_sc(sigprocmask)(SIG_SETMASK, mask, &omask) < 0)
return pth_error(-1, errno);
rv = pth_select(nfds, rfds, wfds, efds, tvp);
/* optionally set signal mask */
if (mask != NULL)
pth_shield { pth_sc(sigprocmask)(SIG_SETMASK, &omask, NULL); }
return rv;
}
/* Pth variant of poll(2) */
int pth_poll(struct pollfd *pfd, nfds_t nfd, int timeout)
{
return pth_poll_ev(pfd, nfd, timeout, NULL);
}
/* Pth variant of poll(2) with extra events:
NOTICE: THIS HAS TO BE BASED ON pth_select(2) BECAUSE
INTERNALLY THE SCHEDULER IS ONLY select(2) BASED!! */
int pth_poll_ev(struct pollfd *pfd, nfds_t nfd, int timeout, pth_event_t ev_extra)
{
fd_set rfds, wfds, efds, xfds;
struct timeval tv, *ptv;
int maxfd, rc, n;
unsigned int i;
char data[64];
pth_implicit_init();
pth_debug2("pth_poll_ev: called from thread \"%s\"", pth_current->name);
/* argument sanity checks */
if (pfd == NULL)
return pth_error(-1, EFAULT);
if (nfd < 0 || nfd > FD_SETSIZE)
return pth_error(-1, EINVAL);
/* convert timeout number into a timeval structure */
ptv = &tv;
if (timeout == 0) {
/* return immediately */
ptv->tv_sec = 0;
ptv->tv_usec = 0;
}
else if (timeout == INFTIM /* (-1) */) {
/* wait forever */
ptv = NULL;
}
else if (timeout > 0) {
/* return after timeout */
ptv->tv_sec = (timeout / 1000);
ptv->tv_usec = (timeout % 1000) * 1000;
}
else
return pth_error(-1, EINVAL);
/* create fd sets and determine max fd */
maxfd = -1;
FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_ZERO(&efds);
FD_ZERO(&xfds);
for (i = 0; i < nfd; i++) {
/* convert into fd_sets but remember that BSD select(2) says
"the only exceptional condition detectable is out-of-band
data received on a socket", hence we push POLLWRBAND events
onto wfds instead of efds. Additionally, remember invalid
filedescriptors in an extra fd_set xfds. */
if (!pth_util_fd_valid(pfd[i].fd)) {
FD_SET(pfd[i].fd, &xfds);
continue;
}
if (pfd[i].events & (POLLIN|POLLRDNORM))
FD_SET(pfd[i].fd, &rfds);
if (pfd[i].events & (POLLOUT|POLLWRNORM|POLLWRBAND))
FD_SET(pfd[i].fd, &wfds);
if (pfd[i].events & (POLLPRI|POLLRDBAND))
FD_SET(pfd[i].fd, &efds);
if ( pfd[i].fd >= maxfd
&& (pfd[i].events & (POLLIN|POLLOUT|POLLPRI|
POLLRDNORM|POLLRDBAND|
POLLWRNORM|POLLWRBAND)))
maxfd = pfd[i].fd;
}
/* examine fd sets with pth_select(3) */
rc = -1;
if (maxfd != -1) {
rc = pth_select_ev(maxfd+1, &rfds, &wfds, &efds, ptv, ev_extra);
if (rc < 0)
return pth_error(-1, errno);
else if (rc == 0)
return 0;
}
/* POSIX.1-2001/SUSv3 compliant result establishment */
n = 0;
for (i = 0; i < nfd; i++) {
pfd[i].revents = 0;
if (FD_ISSET(pfd[i].fd, &xfds)) {
if (pfd[i].fd >= 0) {
pfd[i].revents |= POLLNVAL;
n++;
}
continue;
}
if (maxfd == -1)
continue;
if (FD_ISSET(pfd[i].fd, &rfds)) {
if (pfd[i].events & POLLIN)
pfd[i].revents |= POLLIN;
if (pfd[i].events & POLLRDNORM)
pfd[i].revents |= POLLRDNORM;
n++;
/* support for POLLHUP */
if ( recv(pfd[i].fd, data, sizeof(data), MSG_PEEK) == -1
&& ( errno == ESHUTDOWN || errno == ECONNRESET
|| errno == ECONNABORTED || errno == ENETRESET )) {
pfd[i].revents &= ~(POLLIN);
pfd[i].revents &= ~(POLLRDNORM);
pfd[i].revents |= POLLHUP;
}
}
else if (FD_ISSET(pfd[i].fd, &wfds)) {
if (pfd[i].events & POLLOUT)
pfd[i].revents |= POLLOUT;
if (pfd[i].events & POLLWRNORM)
pfd[i].revents |= POLLWRNORM;
if (pfd[i].events & POLLWRBAND)
pfd[i].revents |= POLLWRBAND;
n++;
}
else if (FD_ISSET(pfd[i].fd, &efds)) {
if (pfd[i].events & POLLPRI)
pfd[i].revents |= POLLPRI;
if (pfd[i].events & POLLRDBAND)
pfd[i].revents |= POLLRDBAND;
n++;
}
}
return n;
}
/* Pth variant of connect(2) */
int pth_connect(int s, const struct sockaddr *addr, socklen_t addrlen)
{
return pth_connect_ev(s, addr, addrlen, NULL);
}
/* Pth variant of connect(2) with extra events */
int pth_connect_ev(int s, const struct sockaddr *addr, socklen_t addrlen, pth_event_t ev_extra)
{
pth_event_t ev;
static pth_key_t ev_key = PTH_KEY_INIT;
int rv, err;
socklen_t errlen;
int fdmode;
pth_implicit_init();
pth_debug2("pth_connect_ev: enter from thread \"%s\"", pth_current->name);
/* POSIX compliance */
if (!pth_util_fd_valid(s))
return pth_error(-1, EBADF);
/* force filedescriptor into non-blocking mode */
if ((fdmode = pth_fdmode(s, PTH_FDMODE_NONBLOCK)) == PTH_FDMODE_ERROR)
return pth_error(-1, EBADF);
/* try to connect */
while ( (rv = pth_sc(connect)(s, (struct sockaddr *)addr, addrlen)) == -1
&& errno == EINTR)
;
/* restore filedescriptor mode */
pth_shield { pth_fdmode(s, fdmode); }
/* if it is still on progress wait until socket is really writeable */
if (rv == -1 && errno == EINPROGRESS && fdmode != PTH_FDMODE_NONBLOCK) {
if ((ev = pth_event(PTH_EVENT_FD|PTH_UNTIL_FD_WRITEABLE|PTH_MODE_STATIC, &ev_key, s)) == NULL)
return pth_error(-1, errno);
if (ev_extra != NULL)
pth_event_concat(ev, ev_extra, NULL);
pth_wait(ev);
if (ev_extra != NULL) {
pth_event_isolate(ev);
if (pth_event_status(ev) != PTH_STATUS_OCCURRED)
return pth_error(-1, EINTR);
}
errlen = sizeof(err);
if (getsockopt(s, SOL_SOCKET, SO_ERROR, (void *)&err, &errlen) == -1)
return -1;
if (err == 0)
return 0;
return pth_error(rv, err);
}
pth_debug2("pth_connect_ev: leave to thread \"%s\"", pth_current->name);
return rv;
}
/* Pth variant of accept(2) */
int pth_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
{
return pth_accept_ev(s, addr, addrlen, NULL);
}
/* Pth variant of accept(2) with extra events */
int pth_accept_ev(int s, struct sockaddr *addr, socklen_t *addrlen, pth_event_t ev_extra)
{
pth_event_t ev;
static pth_key_t ev_key = PTH_KEY_INIT;
int fdmode;
int rv;
pth_implicit_init();
pth_debug2("pth_accept_ev: enter from thread \"%s\"", pth_current->name);
/* POSIX compliance */
if (!pth_util_fd_valid(s))
return pth_error(-1, EBADF);
/* force filedescriptor into non-blocking mode */
if ((fdmode = pth_fdmode(s, PTH_FDMODE_NONBLOCK)) == PTH_FDMODE_ERROR)
return pth_error(-1, EBADF);
/* poll socket via accept */
ev = NULL;
while ((rv = pth_sc(accept)(s, addr, addrlen)) == -1
&& (errno == EAGAIN || errno == EWOULDBLOCK)
&& fdmode != PTH_FDMODE_NONBLOCK) {
/* do lazy event allocation */
if (ev == NULL) {
if ((ev = pth_event(PTH_EVENT_FD|PTH_UNTIL_FD_READABLE|PTH_MODE_STATIC, &ev_key, s)) == NULL)
return pth_error(-1, errno);
if (ev_extra != NULL)
pth_event_concat(ev, ev_extra, NULL);
}
/* wait until accept has a chance */
pth_wait(ev);
/* check for the extra events */
if (ev_extra != NULL) {
pth_event_isolate(ev);
if (pth_event_status(ev) != PTH_STATUS_OCCURRED) {
pth_fdmode(s, fdmode);
return pth_error(-1, EINTR);
}
}
}
/* restore filedescriptor mode */
pth_shield {
pth_fdmode(s, fdmode);
if (rv != -1)
pth_fdmode(rv, fdmode);
}
pth_debug2("pth_accept_ev: leave to thread \"%s\"", pth_current->name);
return rv;
}
/* Pth variant of read(2) */
ssize_t pth_read(int fd, void *buf, size_t nbytes)
{
return pth_read_ev(fd, buf, nbytes, NULL);
}
/* Pth variant of read(2) with extra event(s) */
ssize_t pth_read_ev(int fd, void *buf, size_t nbytes, pth_event_t ev_extra)
{
struct timeval delay;
pth_event_t ev;
static pth_key_t ev_key = PTH_KEY_INIT;
fd_set fds;
int fdmode;
int n;
pth_implicit_init();
pth_debug2("pth_read_ev: enter from thread \"%s\"", pth_current->name);
/* POSIX compliance */
if (nbytes == 0)
return 0;
if (!pth_util_fd_valid(fd))
return pth_error(-1, EBADF);
/* check mode of filedescriptor */
if ((fdmode = pth_fdmode(fd, PTH_FDMODE_POLL)) == PTH_FDMODE_ERROR)
return pth_error(-1, EBADF);
/* poll filedescriptor if not already in non-blocking operation */
if (fdmode == PTH_FDMODE_BLOCK) {
/* now directly poll filedescriptor for readability
to avoid unneccessary (and resource consuming because of context
switches, etc) event handling through the scheduler */
FD_ZERO(&fds);
FD_SET(fd, &fds);
delay.tv_sec = 0;
delay.tv_usec = 0;
while ((n = pth_sc(select)(fd+1, &fds, NULL, NULL, &delay)) < 0
&& errno == EINTR) ;
if (n < 0 && (errno == EINVAL || errno == EBADF))
return pth_error(-1, errno);
/* if filedescriptor is still not readable,
let thread sleep until it is or the extra event occurs */
if (n == 0) {
ev = pth_event(PTH_EVENT_FD|PTH_UNTIL_FD_READABLE|PTH_MODE_STATIC, &ev_key, fd);
if (ev_extra != NULL)
pth_event_concat(ev, ev_extra, NULL);
n = pth_wait(ev);
if (ev_extra != NULL) {
pth_event_isolate(ev);
if (pth_event_status(ev) != PTH_STATUS_OCCURRED)
return pth_error(-1, EINTR);
}
}
}
/* Now perform the actual read. We're now guarrantied to not block,
either because we were already in non-blocking mode or we determined
above by polling that the next read(2) call will not block. But keep
in mind, that only 1 next read(2) call is guarrantied to not block
(except for the EINTR situation). */
while ((n = pth_sc(read)(fd, buf, nbytes)) < 0
&& errno == EINTR) ;
pth_debug2("pth_read_ev: leave to thread \"%s\"", pth_current->name);
return n;
}
/* Pth variant of write(2) */
ssize_t pth_write(int fd, const void *buf, size_t nbytes)
{
return pth_write_ev(fd, buf, nbytes, NULL);
}
/* Pth variant of write(2) with extra event(s) */
ssize_t pth_write_ev(int fd, const void *buf, size_t nbytes, pth_event_t ev_extra)
{
struct timeval delay;
pth_event_t ev;
static pth_key_t ev_key = PTH_KEY_INIT;
fd_set fds;
int fdmode;
ssize_t rv;
ssize_t s;
int n;
pth_implicit_init();
pth_debug2("pth_write_ev: enter from thread \"%s\"", pth_current->name);
/* POSIX compliance */
if (nbytes == 0)
return 0;
if (!pth_util_fd_valid(fd))
return pth_error(-1, EBADF);
/* force filedescriptor into non-blocking mode */
if ((fdmode = pth_fdmode(fd, PTH_FDMODE_NONBLOCK)) == PTH_FDMODE_ERROR)
return pth_error(-1, EBADF);
/* poll filedescriptor if not already in non-blocking operation */
if (fdmode != PTH_FDMODE_NONBLOCK) {
/* now directly poll filedescriptor for writeability
to avoid unneccessary (and resource consuming because of context
switches, etc) event handling through the scheduler */
FD_ZERO(&fds);
FD_SET(fd, &fds);
delay.tv_sec = 0;
delay.tv_usec = 0;
while ((n = pth_sc(select)(fd+1, NULL, &fds, NULL, &delay)) < 0
&& errno == EINTR) ;
if (n < 0 && (errno == EINVAL || errno == EBADF))
return pth_error(-1, errno);
rv = 0;
for (;;) {
/* if filedescriptor is still not writeable,
let thread sleep until it is or event occurs */
if (n < 1) {
ev = pth_event(PTH_EVENT_FD|PTH_UNTIL_FD_WRITEABLE|PTH_MODE_STATIC, &ev_key, fd);
if (ev_extra != NULL)
pth_event_concat(ev, ev_extra, NULL);
pth_wait(ev);
if (ev_extra != NULL) {
pth_event_isolate(ev);
if (pth_event_status(ev) != PTH_STATUS_OCCURRED) {
pth_fdmode(fd, fdmode);
return pth_error(-1, EINTR);
}
}
}
/* now perform the actual write operation */
while ((s = pth_sc(write)(fd, buf, nbytes)) < 0
&& errno == EINTR) ;
if (s > 0)
rv += s;
/* although we're physically now in non-blocking mode,
iterate unless all data is written or an error occurs, because
we've to mimic the usual blocking I/O behaviour of write(2). */
if (s > 0 && s < (ssize_t)nbytes) {
nbytes -= s;
buf = (void *)((char *)buf + s);
n = 0;
continue;
}
/* pass error to caller, but not for partial writes (rv > 0) */
if (s < 0 && rv == 0)
rv = -1;
/* stop looping */
break;
}
}
else {
/* just perform the actual write operation */
while ((rv = pth_sc(write)(fd, buf, nbytes)) < 0
&& errno == EINTR) ;
}
/* restore filedescriptor mode */
pth_shield { pth_fdmode(fd, fdmode); }
pth_debug2("pth_write_ev: leave to thread \"%s\"", pth_current->name);
return rv;
}
/* Pth variant of readv(2) */
ssize_t pth_readv(int fd, const struct iovec *iov, int iovcnt)
{
return pth_readv_ev(fd, iov, iovcnt, NULL);
}
/* Pth variant of readv(2) with extra event(s) */
ssize_t pth_readv_ev(int fd, const struct iovec *iov, int iovcnt, pth_event_t ev_extra)
{
struct timeval delay;
pth_event_t ev;
static pth_key_t ev_key = PTH_KEY_INIT;
fd_set fds;
int fdmode;
int n;
pth_implicit_init();
pth_debug2("pth_readv_ev: enter from thread \"%s\"", pth_current->name);
/* POSIX compliance */
if (iovcnt <= 0 || iovcnt > UIO_MAXIOV)
return pth_error(-1, EINVAL);
if (!pth_util_fd_valid(fd))
return pth_error(-1, EBADF);
/* check mode of filedescriptor */
if ((fdmode = pth_fdmode(fd, PTH_FDMODE_POLL)) == PTH_FDMODE_ERROR)
return pth_error(-1, EBADF);
/* poll filedescriptor if not already in non-blocking operation */
if (fdmode == PTH_FDMODE_BLOCK) {
/* first directly poll filedescriptor for readability
to avoid unneccessary (and resource consuming because of context
switches, etc) event handling through the scheduler */
FD_ZERO(&fds);
FD_SET(fd, &fds);
delay.tv_sec = 0;
delay.tv_usec = 0;
while ((n = pth_sc(select)(fd+1, &fds, NULL, NULL, &delay)) < 0
&& errno == EINTR) ;
/* if filedescriptor is still not readable,
let thread sleep until it is or event occurs */
if (n < 1) {
ev = pth_event(PTH_EVENT_FD|PTH_UNTIL_FD_READABLE|PTH_MODE_STATIC, &ev_key, fd);
if (ev_extra != NULL)
pth_event_concat(ev, ev_extra, NULL);
n = pth_wait(ev);
if (ev_extra != NULL) {
pth_event_isolate(ev);
if (pth_event_status(ev) != PTH_STATUS_OCCURRED)
return pth_error(-1, EINTR);
}
}
}
/* Now perform the actual read. We're now guarrantied to not block,
either because we were already in non-blocking mode or we determined
above by polling that the next read(2) call will not block. But keep
in mind, that only 1 next read(2) call is guarrantied to not block
(except for the EINTR situation). */
#if PTH_FAKE_RWV
while ((n = pth_readv_faked(fd, iov, iovcnt)) < 0
&& errno == EINTR) ;
#else
while ((n = pth_sc(readv)(fd, iov, iovcnt)) < 0
&& errno == EINTR) ;
#endif
pth_debug2("pth_readv_ev: leave to thread \"%s\"", pth_current->name);
return n;
}
/* A faked version of readv(2) */
intern ssize_t pth_readv_faked(int fd, const struct iovec *iov, int iovcnt)
{
char *buffer;
size_t bytes, copy, rv;
int i;
/* determine total number of bytes to read */
bytes = 0;
for (i = 0; i < iovcnt; i++) {
if (iov[i].iov_len <= 0)
return pth_error((ssize_t)(-1), EINVAL);
bytes += iov[i].iov_len;
}
if (bytes <= 0)
return pth_error((ssize_t)(-1), EINVAL);
/* allocate a temporary buffer */
if ((buffer = (char *)malloc(bytes)) == NULL)
return (ssize_t)(-1);
/* read data into temporary buffer (caller guarrantied us to not block) */
rv = pth_sc(read)(fd, buffer, bytes);
/* scatter read data into callers vector */
if (rv > 0) {
bytes = rv;
for (i = 0; i < iovcnt; i++) {
copy = pth_util_min(iov[i].iov_len, bytes);
memcpy(iov[i].iov_base, buffer, copy);
buffer += copy;
bytes -= copy;
if (bytes <= 0)
break;
}
}
/* remove the temporary buffer */
pth_shield { free(buffer); }
/* return number of read bytes */
return(rv);
}
/* Pth variant of writev(2) */
ssize_t pth_writev(int fd, const struct iovec *iov, int iovcnt)
{
return pth_writev_ev(fd, iov, iovcnt, NULL);
}
/* Pth variant of writev(2) with extra event(s) */
ssize_t pth_writev_ev(int fd, const struct iovec *iov, int iovcnt, pth_event_t ev_extra)
{