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