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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
|
/*
** GNU Pth - The GNU Portable Threads
** Copyright (c) 1999-2004 Ralf S. Engelschall <rse@engelschall.com>
**
** 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 <rse@engelschall.com>.
**
** 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_time_t until;
pth_time_t offset;
pth_time_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_time((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 */
ev = pth_event(PTH_EVENT_TIME|PTH_MODE_STATIC, &ev_key, until);
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.tv_sec;
rmtp->tv_nsec = until.tv_usec * 1000;
}
return 0;
}
/* Pth variant of usleep(3) */
int pth_usleep(unsigned int usec)
{
pth_time_t until;
pth_time_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_time((long)(usec / 1000000), (long)(usec % 1000000));
pth_time_set(&until, PTH_TIME_NOW);
pth_time_add(&until, &offset);
/* and let thread sleep until this time is elapsed */
ev = pth_event(PTH_EVENT_TIME|PTH_MODE_STATIC, &ev_key, until);
pth_wait(ev);
return 0;
}
/* Pth variant of sleep(3) */
unsigned int pth_sleep(unsigned int sec)
{
pth_time_t until;
pth_time_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_time(sec, 0);
pth_time_set(&until, PTH_TIME_NOW);
pth_time_add(&until, &offset);
/* and let thread sleep until this time is elapsed */
ev = pth_event(PTH_EVENT_TIME|PTH_MODE_STATIC, &ev_key, until);
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 */
ev = pth_event(PTH_EVENT_SIGS|PTH_MODE_STATIC, &ev_key, set, sigp);
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_TIME|PTH_MODE_STATIC, &ev_key, pth_timeout(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, 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_TIME|PTH_MODE_STATIC, &ev_key_timeout,
pth_timeout(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
&& pth_time_cmp(timeout, PTH_TIME_ZERO) == 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_TIME|PTH_MODE_STATIC, &ev_key_timeout,
pth_timeout(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) {
ev = pth_event(PTH_EVENT_FD|PTH_UNTIL_FD_WRITEABLE|PTH_MODE_STATIC, &ev_key, s);
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) {
ev = pth_event(PTH_EVENT_FD|PTH_UNTIL_FD_READABLE|PTH_MODE_STATIC, &ev_key, s);
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)
{
struct timeval delay;
pth_event_t ev;
static pth_key_t ev_key = PTH_KEY_INIT;
fd_set fds;
int fdmode;
struct iovec *liov;
int liovcnt;
size_t nbytes;
ssize_t rv;
ssize_t s;
int n;
struct iovec tiov_stack[32];
struct iovec *tiov;
int tiovcnt;
pth_implicit_init();
pth_debug2("pth_writev_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);
/* 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) {
/* provide temporary iovec structure */
if (iovcnt > sizeof(tiov_stack)) {
tiovcnt = (sizeof(struct iovec) * UIO_MAXIOV);
if ((tiov = (struct iovec *)malloc(tiovcnt)) == NULL)
return pth_error(-1, errno);
}
else {
tiovcnt = sizeof(tiov_stack);
tiov = tiov_stack;
}
/* init return value and number of bytes to write */
rv = 0;
nbytes = pth_writev_iov_bytes(iov, iovcnt);
/* init local iovec structure */
liov = NULL;
liovcnt = 0;
pth_writev_iov_advance(iov, iovcnt, 0, &liov, &liovcnt, tiov, tiovcnt);
/* first 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) ;
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);
if (iovcnt > sizeof(tiov_stack))
free(tiov);
return pth_error(-1, EINTR);
}
}
}
/* now perform the actual write operation */
#if PTH_FAKE_RWV
while ((s = pth_writev_faked(fd, liov, liovcnt)) < 0
&& errno == EINTR) ;
#else
while ((s = pth_sc(writev)(fd, liov, liovcnt)) < 0
&& errno == EINTR) ;
#endif
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 writev(2) */
if (s > 0 && s < (ssize_t)nbytes) {
nbytes -= s;
pth_writev_iov_advance(iov, iovcnt, s, &liov, &liovcnt, tiov, tiovcnt);
n = 0;
continue;
}
/* pass error to caller, but not for partial writes (rv > 0) */
if (s < 0 && rv == 0)
rv = -1;
/* stop looping */
break;
}
/* cleanup */
if (iovcnt > sizeof(tiov_stack))
free(tiov);
}
else {
/* just perform the actual write operation */
#if PTH_FAKE_RWV
while ((rv = pth_writev_faked(fd, iov, iovcnt)) < 0
&& errno == EINTR) ;
#else
while ((rv = pth_sc(writev)(fd, iov, iovcnt)) < 0
&& errno == EINTR) ;
#endif
}
/* restore filedescriptor mode */
pth_shield { pth_fdmode(fd, fdmode); }
pth_debug2("pth_writev_ev: leave to thread \"%s\"", pth_current->name);
return rv;
}
/* calculate number of bytes in a struct iovec */
intern ssize_t pth_writev_iov_bytes(const struct iovec *iov, int iovcnt)
{
ssize_t bytes;
int i;
bytes = 0;
for (i = 0; i < iovcnt; i++) {
if (iov[i].iov_len <= 0)
continue;
bytes += iov[i].iov_len;
}
return bytes;
}
/* advance the virtual pointer of a struct iov */
intern void pth_writev_iov_advance(const struct iovec *riov, int riovcnt, size_t advance,
struct iovec **liov, int *liovcnt,
struct iovec *tiov, int tiovcnt)
{
int i;
if (*liov == NULL && *liovcnt == 0) {
/* initialize with real (const) structure on first step */
*liov = (struct iovec *)riov;
*liovcnt = riovcnt;
}
if (advance > 0) {
if (*liov == riov && *liovcnt == riovcnt) {
/* reinitialize with a copy to be able to adjust it */
*liov = &tiov[0];
for (i = 0; i < riovcnt; i++) {
tiov[i].iov_base = riov[i].iov_base;
tiov[i].iov_len = riov[i].iov_len;
}
}
/* advance the virtual pointer */
while (*liovcnt > 0 && advance > 0) {
if ((*liov)->iov_len > advance) {
(*liov)->iov_base = (char *)((*liov)->iov_base) + advance;
(*liov)->iov_len -= advance;
break;
}
else {
advance -= (*liov)->iov_len;
(*liovcnt)--;
(*liov)++;
}
}
}
return;
}
/* A faked version of writev(2) */
intern ssize_t pth_writev_faked(int fd, const struct iovec *iov, int iovcnt)
{
char *buffer, *cp;
size_t bytes, to_copy, copy, rv;
int i;
/* determine total number of bytes to write */
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 to hold the data */
if ((buffer = (char *)malloc(bytes)) == NULL)
return (ssize_t)(-1);
/* concatenate the data from callers vector into buffer */
to_copy = bytes;
cp = buffer;
for (i = 0; i < iovcnt; i++) {
copy = pth_util_min(iov[i].iov_len, to_copy);
memcpy(cp, iov[i].iov_base, copy);
to_copy -= copy;
if (to_copy <= 0)
break;
}
/* write continuous chunck of data (caller guarrantied us to not block) */
rv = pth_sc(write)(fd, buffer, bytes);
/* remove the temporary buffer */
pth_shield { free(buffer); }
return(rv);
}
/* Pth variant of POSIX pread(3) */
ssize_t pth_pread(int fd, void *buf, size_t nbytes, off_t offset)
{
static pth_mutex_t mutex = PTH_MUTEX_INIT;
off_t old_offset;
ssize_t rc;
/* protect us: pth_read can yield! */
if (!pth_mutex_acquire(&mutex, FALSE, NULL))
return (-1);
/* remember current offset */
if ((old_offset = lseek(fd, 0, SEEK_CUR)) == (off_t)(-1)) {
pth_mutex_release(&mutex);
return (-1);
}
/* seek to requested offset */
if (lseek(fd, offset, SEEK_SET) == (off_t)(-1)) {
pth_mutex_release(&mutex);
return (-1);
}
/* perform the read operation */
rc = pth_read(fd, buf, nbytes);
/* restore the old offset situation */
pth_shield { lseek(fd, old_offset, SEEK_SET); }
/* unprotect and return result of read */
pth_mutex_release(&mutex);
return rc;
}
/* Pth variant of POSIX pwrite(3) */
ssize_t pth_pwrite(int fd, const void *buf, size_t nbytes, off_t offset)
{
static pth_mutex_t mutex = PTH_MUTEX_INIT;
off_t old_offset;
ssize_t rc;
/* protect us: pth_write can yield! */
if (!pth_mutex_acquire(&mutex, FALSE, NULL))
return (-1);
/* remember current offset */
if ((old_offset = lseek(fd, 0, SEEK_CUR)) == (off_t)(-1)) {
pth_mutex_release(&mutex);
return (-1);
}
/* seek to requested offset */
if (lseek(fd, offset, SEEK_SET) == (off_t)(-1)) {
pth_mutex_release(&mutex);
return (-1);
}
/* perform the write operation */
rc = pth_write(fd, buf, nbytes);
/* restore the old offset situation */
pth_shield { lseek(fd, old_offset, SEEK_SET); }
/* unprotect and return result of write */
pth_mutex_release(&mutex);
return rc;
}
/* Pth variant of SUSv2 recv(2) */
ssize_t pth_recv(int s, void *buf, size_t len, int flags)
{
return pth_recv_ev(s, buf, len, flags, NULL);
}
/* Pth variant of SUSv2 recv(2) with extra event(s) */
ssize_t pth_recv_ev(int s, void *buf, size_t len, int flags, pth_event_t ev)
{
return pth_recvfrom_ev(s, buf, len, flags, NULL, 0, ev);
}
/* Pth variant of SUSv2 recvfrom(2) */
ssize_t pth_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
{
return pth_recvfrom_ev(s, buf, len, flags, from, fromlen, NULL);
}
/* Pth variant of SUSv2 recvfrom(2) with extra event(s) */
ssize_t pth_recvfrom_ev(int fd, void *buf, size_t nbytes, int flags, struct sockaddr *from, socklen_t *fromlen, 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_recvfrom_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 */
if (!pth_util_fd_valid(fd))
return pth_error(-1, EBADF);
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 recvfrom(2) call will not block. But keep
in mind, that only 1 next recvfrom(2) call is guarrantied to not block
(except for the EINTR situation). */
while ((n = pth_sc(recvfrom)(fd, buf, nbytes, flags, from, fromlen)) < 0
&& errno == EINTR) ;
pth_debug2("pth_recvfrom_ev: leave to thread \"%s\"", pth_current->name);
return n;
}
/* Pth variant of SUSv2 send(2) */
ssize_t pth_send(int s, const void *buf, size_t len, int flags)
{
return pth_send_ev(s, buf, len, flags, NULL);
}
/* Pth variant of SUSv2 send(2) with extra event(s) */
ssize_t pth_send_ev(int s, const void *buf, size_t len, int flags, pth_event_t ev)
{
return pth_sendto_ev(s, buf, len, flags, NULL, 0, ev);
}
/* Pth variant of SUSv2 sendto(2) */
ssize_t pth_sendto(int s, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
{
return pth_sendto_ev(s, buf, len, flags, to, tolen, NULL);
}
/* Pth variant of SUSv2 sendto(2) with extra event(s) */
ssize_t pth_sendto_ev(int fd, const void *buf, size_t nbytes, int flags, const struct sockaddr *to, socklen_t tolen, 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_sendto_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 */
if (!pth_util_fd_valid(fd)) {
pth_fdmode(fd, fdmode);
return pth_error(-1, EBADF);
}
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 == 0) {
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 send operation */
while ((s = pth_sc(sendto)(fd, buf, nbytes, flags, to, tolen)) < 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 send operation */
while ((rv = pth_sc(sendto)(fd, buf, nbytes, flags, to, tolen)) < 0
&& errno == EINTR) ;
}
/* restore filedescriptor mode */
pth_shield { pth_fdmode(fd, fdmode); }
pth_debug2("pth_sendto_ev: leave to thread \"%s\"", pth_current->name);
return rv;
}
|