… | |
… | |
120 | |
120 | |
121 | #define EIO_TICKS ((1000000 + 1023) >> 10) |
121 | #define EIO_TICKS ((1000000 + 1023) >> 10) |
122 | |
122 | |
123 | /*****************************************************************************/ |
123 | /*****************************************************************************/ |
124 | |
124 | |
|
|
125 | #if __GNUC__ >= 3 |
|
|
126 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
127 | #else |
|
|
128 | # define expect(expr,value) (expr) |
|
|
129 | #endif |
|
|
130 | |
|
|
131 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
132 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
133 | |
|
|
134 | /*****************************************************************************/ |
|
|
135 | |
125 | #define ETP_PRI_MIN EIO_PRI_MIN |
136 | #define ETP_PRI_MIN EIO_PRI_MIN |
126 | #define ETP_PRI_MAX EIO_PRI_MAX |
137 | #define ETP_PRI_MAX EIO_PRI_MAX |
127 | |
138 | |
128 | struct etp_worker; |
139 | struct etp_worker; |
129 | |
140 | |
… | |
… | |
411 | X_UNLOCK (wrklock); |
422 | X_UNLOCK (wrklock); |
412 | } |
423 | } |
413 | |
424 | |
414 | static void etp_maybe_start_thread (void) |
425 | static void etp_maybe_start_thread (void) |
415 | { |
426 | { |
416 | if (etp_nthreads () >= wanted) |
427 | if (expect_true (etp_nthreads () >= wanted)) |
417 | return; |
428 | return; |
418 | |
429 | |
419 | /* todo: maybe use idle here, but might be less exact */ |
430 | /* todo: maybe use idle here, but might be less exact */ |
420 | if (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()) |
431 | if (expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) |
421 | return; |
432 | return; |
422 | |
433 | |
423 | etp_start_thread (); |
434 | etp_start_thread (); |
424 | } |
435 | } |
425 | |
436 | |
… | |
… | |
478 | |
489 | |
479 | X_LOCK (reqlock); |
490 | X_LOCK (reqlock); |
480 | --nreqs; |
491 | --nreqs; |
481 | X_UNLOCK (reqlock); |
492 | X_UNLOCK (reqlock); |
482 | |
493 | |
483 | if (req->type == EIO_GROUP && req->size) |
494 | if (expect_false (req->type == EIO_GROUP && req->size)) |
484 | { |
495 | { |
485 | req->int1 = 1; /* mark request as delayed */ |
496 | req->int1 = 1; /* mark request as delayed */ |
486 | continue; |
497 | continue; |
487 | } |
498 | } |
488 | else |
499 | else |
489 | { |
500 | { |
490 | int res = ETP_FINISH (req); |
501 | int res = ETP_FINISH (req); |
491 | if (res) |
502 | if (expect_false (res)) |
492 | return res; |
503 | return res; |
493 | } |
504 | } |
494 | |
505 | |
495 | if (maxreqs && !--maxreqs) |
506 | if (expect_false (maxreqs && !--maxreqs)) |
496 | break; |
507 | break; |
497 | |
508 | |
498 | if (maxtime) |
509 | if (maxtime) |
499 | { |
510 | { |
500 | gettimeofday (&tv_now, 0); |
511 | gettimeofday (&tv_now, 0); |
… | |
… | |
519 | |
530 | |
520 | static void etp_submit (ETP_REQ *req) |
531 | static void etp_submit (ETP_REQ *req) |
521 | { |
532 | { |
522 | req->pri -= ETP_PRI_MIN; |
533 | req->pri -= ETP_PRI_MIN; |
523 | |
534 | |
524 | if (req->pri < ETP_PRI_MIN - ETP_PRI_MIN) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; |
535 | if (expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; |
525 | if (req->pri > ETP_PRI_MAX - ETP_PRI_MIN) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
536 | if (expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
526 | |
537 | |
|
|
538 | if (expect_false (req->type == EIO_GROUP)) |
|
|
539 | { |
|
|
540 | /* I hope this is worth it :/ */ |
527 | X_LOCK (reqlock); |
541 | X_LOCK (reqlock); |
528 | ++nreqs; |
542 | ++nreqs; |
|
|
543 | X_UNLOCK (reqlock); |
|
|
544 | |
|
|
545 | X_LOCK (reslock); |
|
|
546 | |
|
|
547 | ++npending; |
|
|
548 | |
|
|
549 | if (!reqq_push (&res_queue, req) && want_poll_cb) |
|
|
550 | want_poll_cb (); |
|
|
551 | |
|
|
552 | X_UNLOCK (reslock); |
|
|
553 | } |
|
|
554 | else |
|
|
555 | { |
|
|
556 | X_LOCK (reqlock); |
|
|
557 | ++nreqs; |
529 | ++nready; |
558 | ++nready; |
530 | reqq_push (&req_queue, req); |
559 | reqq_push (&req_queue, req); |
531 | X_COND_SIGNAL (reqwait); |
560 | X_COND_SIGNAL (reqwait); |
532 | X_UNLOCK (reqlock); |
561 | X_UNLOCK (reqlock); |
533 | |
562 | |
534 | etp_maybe_start_thread (); |
563 | etp_maybe_start_thread (); |
|
|
564 | } |
535 | } |
565 | } |
536 | |
566 | |
537 | static void etp_set_max_poll_time (double nseconds) |
567 | static void etp_set_max_poll_time (double nseconds) |
538 | { |
568 | { |
539 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
569 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
… | |
… | |
574 | |
604 | |
575 | static void grp_try_feed (eio_req *grp) |
605 | static void grp_try_feed (eio_req *grp) |
576 | { |
606 | { |
577 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
607 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
578 | { |
608 | { |
579 | int old_len = grp->size; |
609 | grp->flags &= ~EIO_FLAG_GROUPADD; |
580 | |
610 | |
581 | EIO_FEED (grp); |
611 | EIO_FEED (grp); |
582 | |
612 | |
583 | /* stop if no progress has been made */ |
613 | /* stop if no progress has been made */ |
584 | if (old_len == grp->size) |
614 | if (!(grp->flags & EIO_FLAG_GROUPADD)) |
585 | { |
615 | { |
586 | grp->feed = 0; |
616 | grp->feed = 0; |
587 | break; |
617 | break; |
588 | } |
618 | } |
589 | } |
619 | } |
… | |
… | |
706 | |
736 | |
707 | /*****************************************************************************/ |
737 | /*****************************************************************************/ |
708 | /* work around various missing functions */ |
738 | /* work around various missing functions */ |
709 | |
739 | |
710 | #if !HAVE_PREADWRITE |
740 | #if !HAVE_PREADWRITE |
|
|
741 | # undef pread |
|
|
742 | # undef pwrite |
711 | # define pread eio__pread |
743 | # define pread eio__pread |
712 | # define pwrite eio__pwrite |
744 | # define pwrite eio__pwrite |
713 | |
745 | |
714 | static ssize_t |
746 | static ssize_t |
715 | eio__pread (int fd, void *buf, size_t count, off_t offset) |
747 | eio__pread (int fd, void *buf, size_t count, off_t offset) |
… | |
… | |
744 | } |
776 | } |
745 | #endif |
777 | #endif |
746 | |
778 | |
747 | #ifndef HAVE_FUTIMES |
779 | #ifndef HAVE_FUTIMES |
748 | |
780 | |
|
|
781 | # undef utimes |
|
|
782 | # undef futimes |
749 | # define utimes(path,times) eio__utimes (path, times) |
783 | # define utimes(path,times) eio__utimes (path, times) |
750 | # define futimes(fd,times) eio__futimes (fd, times) |
784 | # define futimes(fd,times) eio__futimes (fd, times) |
751 | |
785 | |
752 | static int |
786 | static int |
753 | eio__utimes (const char *filename, const struct timeval times[2]) |
787 | eio__utimes (const char *filename, const struct timeval times[2]) |
… | |
… | |
772 | } |
806 | } |
773 | |
807 | |
774 | #endif |
808 | #endif |
775 | |
809 | |
776 | #if !HAVE_FDATASYNC |
810 | #if !HAVE_FDATASYNC |
|
|
811 | # undef fdatasync |
777 | # define fdatasync fsync |
812 | # define fdatasync(fd) fsync (fd) |
778 | #endif |
813 | #endif |
|
|
814 | |
|
|
815 | /* sync_file_range always needs emulation */ |
|
|
816 | int |
|
|
817 | eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) |
|
|
818 | { |
|
|
819 | #if HAVE_SYNC_FILE_RANGE |
|
|
820 | int res; |
|
|
821 | |
|
|
822 | if (EIO_SYNC_FILE_RANGE_WAIT_BEFORE != SYNC_FILE_RANGE_WAIT_BEFORE |
|
|
823 | || EIO_SYNC_FILE_RANGE_WRITE != SYNC_FILE_RANGE_WRITE |
|
|
824 | || EIO_SYNC_FILE_RANGE_WAIT_AFTER != SYNC_FILE_RANGE_WAIT_AFTER) |
|
|
825 | { |
|
|
826 | flags = 0 |
|
|
827 | | (flags & EIO_SYNC_FILE_RANGE_WAIT_BEFORE ? SYNC_FILE_RANGE_WAIT_BEFORE : 0) |
|
|
828 | | (flags & EIO_SYNC_FILE_RANGE_WRITE ? SYNC_FILE_RANGE_WRITE : 0) |
|
|
829 | | (flags & EIO_SYNC_FILE_RANGE_WAIT_AFTER ? SYNC_FILE_RANGE_WAIT_AFTER : 0); |
|
|
830 | } |
|
|
831 | |
|
|
832 | res = sync_file_range (fd, offset, nbytes, flags); |
|
|
833 | |
|
|
834 | if (!res || errno != ENOSYS) |
|
|
835 | return res; |
|
|
836 | #endif |
|
|
837 | |
|
|
838 | /* even though we could play tricks with the flags, it's better to always |
|
|
839 | * call fdatasync, as thta matches the expectation of it's users best */ |
|
|
840 | return fdatasync (fd); |
|
|
841 | } |
779 | |
842 | |
780 | #if !HAVE_READAHEAD |
843 | #if !HAVE_READAHEAD |
|
|
844 | # undef readahead |
781 | # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) |
845 | # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) |
782 | |
846 | |
783 | static ssize_t |
847 | static ssize_t |
784 | eio__readahead (int fd, off_t offset, size_t count, etp_worker *self) |
848 | eio__readahead (int fd, off_t offset, size_t count, etp_worker *self) |
785 | { |
849 | { |
… | |
… | |
948 | |
1012 | |
949 | if (errno) |
1013 | if (errno) |
950 | res = -1; |
1014 | res = -1; |
951 | |
1015 | |
952 | req->result = res; |
1016 | req->result = res; |
|
|
1017 | } |
|
|
1018 | |
|
|
1019 | #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) |
|
|
1020 | # undef msync |
|
|
1021 | # define msync(a,b,c) ((errno = ENOSYS), -1) |
|
|
1022 | #endif |
|
|
1023 | |
|
|
1024 | int |
|
|
1025 | eio__mtouch (void *mem, size_t len, int flags) |
|
|
1026 | { |
|
|
1027 | intptr_t addr = (intptr_t)mem; |
|
|
1028 | intptr_t end = addr + len; |
|
|
1029 | #ifdef PAGESIZE |
|
|
1030 | const intptr_t page = PAGESIZE; |
|
|
1031 | #else |
|
|
1032 | static intptr_t page; |
|
|
1033 | |
|
|
1034 | if (!page) |
|
|
1035 | page = sysconf (_SC_PAGESIZE); |
|
|
1036 | #endif |
|
|
1037 | |
|
|
1038 | addr &= ~(page - 1); /* assume page size is always a power of two */ |
|
|
1039 | |
|
|
1040 | if (addr < end) |
|
|
1041 | if (flags) /* modify */ |
|
|
1042 | do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len); |
|
|
1043 | else |
|
|
1044 | do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len); |
|
|
1045 | |
|
|
1046 | return 0; |
953 | } |
1047 | } |
954 | |
1048 | |
955 | /*****************************************************************************/ |
1049 | /*****************************************************************************/ |
956 | |
1050 | |
957 | #define ALLOC(len) \ |
1051 | #define ALLOC(len) \ |
… | |
… | |
1124 | req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break; |
1218 | req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break; |
1125 | |
1219 | |
1126 | case EIO_SYNC: req->result = 0; sync (); break; |
1220 | case EIO_SYNC: req->result = 0; sync (); break; |
1127 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
1221 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
1128 | case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; |
1222 | case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; |
|
|
1223 | case EIO_MSYNC: req->result = msync (req->ptr2, req->size, req->int1); break; |
|
|
1224 | case EIO_MTOUCH: req->result = eio__mtouch (req->ptr2, req->size, req->int1); break; |
|
|
1225 | case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; |
1129 | |
1226 | |
1130 | case EIO_READDIR: eio__scandir (req, self); break; |
1227 | case EIO_READDIR: eio__scandir (req, self); break; |
1131 | |
1228 | |
1132 | case EIO_BUSY: |
1229 | case EIO_BUSY: |
1133 | #ifdef _WIN32 |
1230 | #ifdef _WIN32 |
… | |
… | |
1165 | |
1262 | |
1166 | req->result = req->type == EIO_FUTIME |
1263 | req->result = req->type == EIO_FUTIME |
1167 | ? futimes (req->int1, times) |
1264 | ? futimes (req->int1, times) |
1168 | : utimes (req->ptr1, times); |
1265 | : utimes (req->ptr1, times); |
1169 | } |
1266 | } |
|
|
1267 | break; |
1170 | |
1268 | |
1171 | case EIO_GROUP: |
1269 | case EIO_GROUP: |
|
|
1270 | abort (); /* handled in eio_request */ |
|
|
1271 | |
1172 | case EIO_NOP: |
1272 | case EIO_NOP: |
1173 | req->result = 0; |
1273 | req->result = 0; |
1174 | break; |
1274 | break; |
1175 | |
1275 | |
1176 | case EIO_CUSTOM: |
1276 | case EIO_CUSTOM: |
1177 | req->feed (req); |
1277 | ((void (*)(eio_req *))req->feed) (req); |
1178 | break; |
1278 | break; |
1179 | |
1279 | |
1180 | default: |
1280 | default: |
1181 | req->result = -1; |
1281 | req->result = -1; |
1182 | break; |
1282 | break; |
… | |
… | |
1203 | } |
1303 | } |
1204 | |
1304 | |
1205 | eio_req *eio_fsync (int fd, int pri, eio_cb cb, void *data) |
1305 | eio_req *eio_fsync (int fd, int pri, eio_cb cb, void *data) |
1206 | { |
1306 | { |
1207 | REQ (EIO_FSYNC); req->int1 = fd; SEND; |
1307 | REQ (EIO_FSYNC); req->int1 = fd; SEND; |
|
|
1308 | } |
|
|
1309 | |
|
|
1310 | eio_req *eio_msync (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) |
|
|
1311 | { |
|
|
1312 | REQ (EIO_MSYNC); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; |
|
|
1313 | } |
|
|
1314 | |
|
|
1315 | eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) |
|
|
1316 | { |
|
|
1317 | REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; |
|
|
1318 | } |
|
|
1319 | |
|
|
1320 | eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) |
|
|
1321 | { |
|
|
1322 | REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; |
1208 | } |
1323 | } |
1209 | |
1324 | |
1210 | eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) |
1325 | eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) |
1211 | { |
1326 | { |
1212 | REQ (EIO_FDATASYNC); req->int1 = fd; SEND; |
1327 | REQ (EIO_FDATASYNC); req->int1 = fd; SEND; |
… | |
… | |
1369 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
1484 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
1370 | } |
1485 | } |
1371 | |
1486 | |
1372 | eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) |
1487 | eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) |
1373 | { |
1488 | { |
1374 | REQ (EIO_CUSTOM); req->feed = execute; SEND; |
1489 | REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND; |
1375 | } |
1490 | } |
1376 | |
1491 | |
1377 | #endif |
1492 | #endif |
1378 | |
1493 | |
1379 | eio_req *eio_grp (eio_cb cb, void *data) |
1494 | eio_req *eio_grp (eio_cb cb, void *data) |
… | |
… | |
1407 | |
1522 | |
1408 | void eio_grp_add (eio_req *grp, eio_req *req) |
1523 | void eio_grp_add (eio_req *grp, eio_req *req) |
1409 | { |
1524 | { |
1410 | assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); |
1525 | assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); |
1411 | |
1526 | |
|
|
1527 | grp->flags |= EIO_FLAG_GROUPADD; |
|
|
1528 | |
1412 | ++grp->size; |
1529 | ++grp->size; |
1413 | req->grp = grp; |
1530 | req->grp = grp; |
1414 | |
1531 | |
1415 | req->grp_prev = 0; |
1532 | req->grp_prev = 0; |
1416 | req->grp_next = grp->grp_first; |
1533 | req->grp_next = grp->grp_first; |