… | |
… | |
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 |
|
|
138 | |
|
|
139 | struct etp_worker; |
127 | |
140 | |
128 | #define ETP_REQ eio_req |
141 | #define ETP_REQ eio_req |
129 | #define ETP_DESTROY(req) eio_destroy (req) |
142 | #define ETP_DESTROY(req) eio_destroy (req) |
130 | static int eio_finish (eio_req *req); |
143 | static int eio_finish (eio_req *req); |
131 | #define ETP_FINISH(req) eio_finish (req) |
144 | #define ETP_FINISH(req) eio_finish (req) |
|
|
145 | static void eio_execute (struct etp_worker *self, eio_req *req); |
|
|
146 | #define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) |
132 | |
147 | |
133 | #define ETP_WORKER_CLEAR(req) \ |
148 | #define ETP_WORKER_CLEAR(req) \ |
134 | if (wrk->dbuf) \ |
149 | if (wrk->dbuf) \ |
135 | { \ |
150 | { \ |
136 | free (wrk->dbuf); \ |
151 | free (wrk->dbuf); \ |
… | |
… | |
173 | static mutex_t wrklock = X_MUTEX_INIT; |
188 | static mutex_t wrklock = X_MUTEX_INIT; |
174 | static mutex_t reslock = X_MUTEX_INIT; |
189 | static mutex_t reslock = X_MUTEX_INIT; |
175 | static mutex_t reqlock = X_MUTEX_INIT; |
190 | static mutex_t reqlock = X_MUTEX_INIT; |
176 | static cond_t reqwait = X_COND_INIT; |
191 | static cond_t reqwait = X_COND_INIT; |
177 | |
192 | |
|
|
193 | #if !HAVE_PREADWRITE |
|
|
194 | /* |
|
|
195 | * make our pread/pwrite emulation safe against themselves, but not against |
|
|
196 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
197 | * but that's your problem, not mine. |
|
|
198 | */ |
|
|
199 | static mutex_t preadwritelock = X_MUTEX_INIT; |
|
|
200 | #endif |
|
|
201 | |
178 | typedef struct etp_worker |
202 | typedef struct etp_worker |
179 | { |
203 | { |
180 | /* locked by wrklock */ |
204 | /* locked by wrklock */ |
181 | struct etp_worker *prev, *next; |
205 | struct etp_worker *prev, *next; |
182 | |
206 | |
… | |
… | |
310 | X_LOCK (reqlock); |
334 | X_LOCK (reqlock); |
311 | X_LOCK (reslock); |
335 | X_LOCK (reslock); |
312 | #if !HAVE_PREADWRITE |
336 | #if !HAVE_PREADWRITE |
313 | X_LOCK (preadwritelock); |
337 | X_LOCK (preadwritelock); |
314 | #endif |
338 | #endif |
315 | #if !HAVE_READDIR_R |
|
|
316 | X_LOCK (readdirlock); |
|
|
317 | #endif |
|
|
318 | } |
339 | } |
319 | |
340 | |
320 | static void etp_atfork_parent (void) |
341 | static void etp_atfork_parent (void) |
321 | { |
342 | { |
322 | #if !HAVE_READDIR_R |
|
|
323 | X_UNLOCK (readdirlock); |
|
|
324 | #endif |
|
|
325 | #if !HAVE_PREADWRITE |
343 | #if !HAVE_PREADWRITE |
326 | X_UNLOCK (preadwritelock); |
344 | X_UNLOCK (preadwritelock); |
327 | #endif |
345 | #endif |
328 | X_UNLOCK (reslock); |
346 | X_UNLOCK (reslock); |
329 | X_UNLOCK (reqlock); |
347 | X_UNLOCK (reqlock); |
… | |
… | |
332 | |
350 | |
333 | static void etp_atfork_child (void) |
351 | static void etp_atfork_child (void) |
334 | { |
352 | { |
335 | ETP_REQ *prv; |
353 | ETP_REQ *prv; |
336 | |
354 | |
337 | while (prv = reqq_shift (&req_queue)) |
355 | while ((prv = reqq_shift (&req_queue))) |
338 | ETP_DESTROY (prv); |
356 | ETP_DESTROY (prv); |
339 | |
357 | |
340 | while (prv = reqq_shift (&res_queue)) |
358 | while ((prv = reqq_shift (&res_queue))) |
341 | ETP_DESTROY (prv); |
359 | ETP_DESTROY (prv); |
342 | |
360 | |
343 | while (wrk_first.next != &wrk_first) |
361 | while (wrk_first.next != &wrk_first) |
344 | { |
362 | { |
345 | etp_worker *wrk = wrk_first.next; |
363 | etp_worker *wrk = wrk_first.next; |
… | |
… | |
373 | |
391 | |
374 | pthread_once (&doinit, etp_once_init); |
392 | pthread_once (&doinit, etp_once_init); |
375 | |
393 | |
376 | want_poll_cb = want_poll; |
394 | want_poll_cb = want_poll; |
377 | done_poll_cb = done_poll; |
395 | done_poll_cb = done_poll; |
|
|
396 | |
|
|
397 | return 0; |
378 | } |
398 | } |
379 | |
399 | |
380 | X_THREAD_PROC (etp_proc); |
400 | X_THREAD_PROC (etp_proc); |
381 | |
401 | |
382 | static void etp_start_thread (void) |
402 | static void etp_start_thread (void) |
… | |
… | |
402 | X_UNLOCK (wrklock); |
422 | X_UNLOCK (wrklock); |
403 | } |
423 | } |
404 | |
424 | |
405 | static void etp_maybe_start_thread (void) |
425 | static void etp_maybe_start_thread (void) |
406 | { |
426 | { |
407 | if (etp_nthreads () >= wanted) |
427 | if (expect_true (etp_nthreads () >= wanted)) |
408 | return; |
428 | return; |
409 | |
429 | |
410 | /* todo: maybe use idle here, but might be less exact */ |
430 | /* todo: maybe use idle here, but might be less exact */ |
411 | 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 ())) |
412 | return; |
432 | return; |
413 | |
433 | |
414 | etp_start_thread (); |
434 | etp_start_thread (); |
415 | } |
435 | } |
416 | |
436 | |
… | |
… | |
469 | |
489 | |
470 | X_LOCK (reqlock); |
490 | X_LOCK (reqlock); |
471 | --nreqs; |
491 | --nreqs; |
472 | X_UNLOCK (reqlock); |
492 | X_UNLOCK (reqlock); |
473 | |
493 | |
474 | if (req->type == EIO_GROUP && req->size) |
494 | if (expect_false (req->type == EIO_GROUP && req->size)) |
475 | { |
495 | { |
476 | req->int1 = 1; /* mark request as delayed */ |
496 | req->int1 = 1; /* mark request as delayed */ |
477 | continue; |
497 | continue; |
478 | } |
498 | } |
479 | else |
499 | else |
480 | { |
500 | { |
481 | int res = ETP_FINISH (req); |
501 | int res = ETP_FINISH (req); |
482 | if (res) |
502 | if (expect_false (res)) |
483 | return res; |
503 | return res; |
484 | } |
504 | } |
485 | |
505 | |
486 | if (maxreqs && !--maxreqs) |
506 | if (expect_false (maxreqs && !--maxreqs)) |
487 | break; |
507 | break; |
488 | |
508 | |
489 | if (maxtime) |
509 | if (maxtime) |
490 | { |
510 | { |
491 | gettimeofday (&tv_now, 0); |
511 | gettimeofday (&tv_now, 0); |
… | |
… | |
510 | |
530 | |
511 | static void etp_submit (ETP_REQ *req) |
531 | static void etp_submit (ETP_REQ *req) |
512 | { |
532 | { |
513 | req->pri -= ETP_PRI_MIN; |
533 | req->pri -= ETP_PRI_MIN; |
514 | |
534 | |
515 | 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; |
516 | 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; |
517 | |
537 | |
|
|
538 | if (expect_false (req->type == EIO_GROUP)) |
|
|
539 | { |
|
|
540 | /* I hope this is worth it :/ */ |
518 | X_LOCK (reqlock); |
541 | X_LOCK (reqlock); |
519 | ++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; |
520 | ++nready; |
558 | ++nready; |
521 | reqq_push (&req_queue, req); |
559 | reqq_push (&req_queue, req); |
522 | X_COND_SIGNAL (reqwait); |
560 | X_COND_SIGNAL (reqwait); |
523 | X_UNLOCK (reqlock); |
561 | X_UNLOCK (reqlock); |
524 | |
562 | |
525 | etp_maybe_start_thread (); |
563 | etp_maybe_start_thread (); |
|
|
564 | } |
526 | } |
565 | } |
527 | |
566 | |
528 | static void etp_set_max_poll_time (double nseconds) |
567 | static void etp_set_max_poll_time (double nseconds) |
529 | { |
568 | { |
530 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
569 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
… | |
… | |
565 | |
604 | |
566 | static void grp_try_feed (eio_req *grp) |
605 | static void grp_try_feed (eio_req *grp) |
567 | { |
606 | { |
568 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
607 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
569 | { |
608 | { |
570 | int old_len = grp->size; |
609 | grp->flags &= ~EIO_FLAG_GROUPADD; |
571 | |
610 | |
572 | EIO_FEED (grp); |
611 | EIO_FEED (grp); |
573 | |
612 | |
574 | /* stop if no progress has been made */ |
613 | /* stop if no progress has been made */ |
575 | if (old_len == grp->size) |
614 | if (!(grp->flags & EIO_FLAG_GROUPADD)) |
576 | { |
615 | { |
577 | grp->feed = 0; |
616 | grp->feed = 0; |
578 | break; |
617 | break; |
579 | } |
618 | } |
580 | } |
619 | } |
… | |
… | |
697 | |
736 | |
698 | /*****************************************************************************/ |
737 | /*****************************************************************************/ |
699 | /* work around various missing functions */ |
738 | /* work around various missing functions */ |
700 | |
739 | |
701 | #if !HAVE_PREADWRITE |
740 | #if !HAVE_PREADWRITE |
|
|
741 | # undef pread |
|
|
742 | # undef pwrite |
702 | # define pread eio__pread |
743 | # define pread eio__pread |
703 | # define pwrite eio__pwrite |
744 | # define pwrite eio__pwrite |
704 | |
|
|
705 | /* |
|
|
706 | * make our pread/pwrite safe against themselves, but not against |
|
|
707 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
708 | * but that's your problem, not mine. |
|
|
709 | */ |
|
|
710 | static mutex_t preadwritelock = X_MUTEX_INIT; |
|
|
711 | |
745 | |
712 | static ssize_t |
746 | static ssize_t |
713 | 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) |
714 | { |
748 | { |
715 | ssize_t res; |
749 | ssize_t res; |
… | |
… | |
742 | } |
776 | } |
743 | #endif |
777 | #endif |
744 | |
778 | |
745 | #ifndef HAVE_FUTIMES |
779 | #ifndef HAVE_FUTIMES |
746 | |
780 | |
|
|
781 | # undef utimes |
|
|
782 | # undef futimes |
747 | # define utimes(path,times) eio__utimes (path, times) |
783 | # define utimes(path,times) eio__utimes (path, times) |
748 | # define futimes(fd,times) eio__futimes (fd, times) |
784 | # define futimes(fd,times) eio__futimes (fd, times) |
749 | |
785 | |
750 | static int |
786 | static int |
751 | eio__utimes (const char *filename, const struct timeval times[2]) |
787 | eio__utimes (const char *filename, const struct timeval times[2]) |
… | |
… | |
770 | } |
806 | } |
771 | |
807 | |
772 | #endif |
808 | #endif |
773 | |
809 | |
774 | #if !HAVE_FDATASYNC |
810 | #if !HAVE_FDATASYNC |
|
|
811 | # undef fdatasync |
775 | # define fdatasync fsync |
812 | # define fdatasync(fd) fsync (fd) |
776 | #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 != 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 | } |
777 | |
842 | |
778 | #if !HAVE_READAHEAD |
843 | #if !HAVE_READAHEAD |
|
|
844 | # undef readahead |
779 | # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) |
845 | # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) |
780 | |
846 | |
781 | static ssize_t |
847 | static ssize_t |
782 | eio__readahead (int fd, off_t offset, size_t count, worker *self) |
848 | eio__readahead (int fd, off_t offset, size_t count, etp_worker *self) |
783 | { |
849 | { |
784 | size_t todo = count; |
850 | size_t todo = count; |
785 | dBUF; |
851 | dBUF; |
786 | |
852 | |
787 | while (todo > 0) |
853 | while (todo > 0) |
… | |
… | |
795 | |
861 | |
796 | errno = 0; |
862 | errno = 0; |
797 | return count; |
863 | return count; |
798 | } |
864 | } |
799 | |
865 | |
800 | #endif |
|
|
801 | |
|
|
802 | #if !HAVE_READDIR_R |
|
|
803 | # define readdir_r eio__readdir_r |
|
|
804 | |
|
|
805 | static mutex_t readdirlock = X_MUTEX_INIT; |
|
|
806 | |
|
|
807 | static int |
|
|
808 | eio__readdir_r (DIR *dirp, EIO_STRUCT_DIRENT *ent, EIO_STRUCT_DIRENT **res) |
|
|
809 | { |
|
|
810 | EIO_STRUCT_DIRENT *e; |
|
|
811 | int errorno; |
|
|
812 | |
|
|
813 | X_LOCK (readdirlock); |
|
|
814 | |
|
|
815 | e = readdir (dirp); |
|
|
816 | errorno = errno; |
|
|
817 | |
|
|
818 | if (e) |
|
|
819 | { |
|
|
820 | *res = ent; |
|
|
821 | strcpy (ent->d_name, e->d_name); |
|
|
822 | } |
|
|
823 | else |
|
|
824 | *res = 0; |
|
|
825 | |
|
|
826 | X_UNLOCK (readdirlock); |
|
|
827 | |
|
|
828 | errno = errorno; |
|
|
829 | return e ? 0 : -1; |
|
|
830 | } |
|
|
831 | #endif |
866 | #endif |
832 | |
867 | |
833 | /* sendfile always needs emulation */ |
868 | /* sendfile always needs emulation */ |
834 | static ssize_t |
869 | static ssize_t |
835 | eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) |
870 | eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) |
… | |
… | |
927 | /* read a full directory */ |
962 | /* read a full directory */ |
928 | static void |
963 | static void |
929 | eio__scandir (eio_req *req, etp_worker *self) |
964 | eio__scandir (eio_req *req, etp_worker *self) |
930 | { |
965 | { |
931 | DIR *dirp; |
966 | DIR *dirp; |
932 | union |
|
|
933 | { |
|
|
934 | EIO_STRUCT_DIRENT d; |
|
|
935 | char b [offsetof (EIO_STRUCT_DIRENT, d_name) + NAME_MAX + 1]; |
|
|
936 | } *u; |
|
|
937 | EIO_STRUCT_DIRENT *entp; |
967 | EIO_STRUCT_DIRENT *entp; |
938 | char *name, *names; |
968 | char *name, *names; |
939 | int memlen = 4096; |
969 | int memlen = 4096; |
940 | int memofs = 0; |
970 | int memofs = 0; |
941 | int res = 0; |
971 | int res = 0; |
942 | |
972 | |
943 | X_LOCK (wrklock); |
973 | X_LOCK (wrklock); |
|
|
974 | /* the corresponding closedir is in ETP_WORKER_CLEAR */ |
944 | self->dirp = dirp = opendir (req->ptr1); |
975 | self->dirp = dirp = opendir (req->ptr1); |
945 | self->dbuf = u = malloc (sizeof (*u)); |
|
|
946 | req->flags |= EIO_FLAG_PTR2_FREE; |
976 | req->flags |= EIO_FLAG_PTR2_FREE; |
947 | req->ptr2 = names = malloc (memlen); |
977 | req->ptr2 = names = malloc (memlen); |
948 | X_UNLOCK (wrklock); |
978 | X_UNLOCK (wrklock); |
949 | |
979 | |
950 | if (dirp && u && names) |
980 | if (dirp && names) |
951 | for (;;) |
981 | for (;;) |
952 | { |
982 | { |
953 | errno = 0; |
983 | errno = 0; |
954 | readdir_r (dirp, &u->d, &entp); |
984 | entp = readdir (dirp); |
955 | |
985 | |
956 | if (!entp) |
986 | if (!entp) |
957 | break; |
987 | break; |
958 | |
988 | |
959 | name = entp->d_name; |
989 | name = entp->d_name; |
… | |
… | |
982 | |
1012 | |
983 | if (errno) |
1013 | if (errno) |
984 | res = -1; |
1014 | res = -1; |
985 | |
1015 | |
986 | 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) ENOSYS |
|
|
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; |
987 | } |
1047 | } |
988 | |
1048 | |
989 | /*****************************************************************************/ |
1049 | /*****************************************************************************/ |
990 | |
1050 | |
991 | #define ALLOC(len) \ |
1051 | #define ALLOC(len) \ |
… | |
… | |
1047 | |
1107 | |
1048 | --nready; |
1108 | --nready; |
1049 | |
1109 | |
1050 | X_UNLOCK (reqlock); |
1110 | X_UNLOCK (reqlock); |
1051 | |
1111 | |
1052 | errno = 0; /* strictly unnecessary */ |
1112 | if (req->type < 0) |
|
|
1113 | goto quit; |
1053 | |
1114 | |
1054 | if (!EIO_CANCELLED (req)) |
1115 | if (!EIO_CANCELLED (req)) |
1055 | switch (req->type) |
1116 | ETP_EXECUTE (self, req); |
1056 | { |
|
|
1057 | case EIO_READ: ALLOC (req->size); |
|
|
1058 | req->result = req->offs >= 0 |
|
|
1059 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
|
|
1060 | : read (req->int1, req->ptr2, req->size); break; |
|
|
1061 | case EIO_WRITE: req->result = req->offs >= 0 |
|
|
1062 | ? pwrite (req->int1, req->ptr2, req->size, req->offs) |
|
|
1063 | : write (req->int1, req->ptr2, req->size); break; |
|
|
1064 | |
|
|
1065 | case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
|
|
1066 | case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size, self); break; |
|
|
1067 | |
|
|
1068 | case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
|
|
1069 | req->result = stat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; |
|
|
1070 | case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
|
|
1071 | req->result = lstat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; |
|
|
1072 | case EIO_FSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
|
|
1073 | req->result = fstat (req->int1, (EIO_STRUCT_STAT *)req->ptr2); break; |
|
|
1074 | |
|
|
1075 | case EIO_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; |
|
|
1076 | case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; |
|
|
1077 | case EIO_CHMOD: req->result = chmod (req->ptr1, (mode_t)req->int2); break; |
|
|
1078 | case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break; |
|
|
1079 | case EIO_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break; |
|
|
1080 | case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; |
|
|
1081 | |
|
|
1082 | case EIO_OPEN: req->result = open (req->ptr1, req->int1, (mode_t)req->int2); break; |
|
|
1083 | case EIO_CLOSE: req->result = close (req->int1); break; |
|
|
1084 | case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; |
|
|
1085 | case EIO_UNLINK: req->result = unlink (req->ptr1); break; |
|
|
1086 | case EIO_RMDIR: req->result = rmdir (req->ptr1); break; |
|
|
1087 | case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break; |
|
|
1088 | case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; |
|
|
1089 | case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; |
|
|
1090 | case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; |
|
|
1091 | case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
|
|
1092 | |
|
|
1093 | case EIO_READLINK: ALLOC (NAME_MAX); |
|
|
1094 | req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break; |
|
|
1095 | |
|
|
1096 | case EIO_SYNC: req->result = 0; sync (); break; |
|
|
1097 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
|
|
1098 | case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; |
|
|
1099 | |
|
|
1100 | case EIO_READDIR: eio__scandir (req, self); break; |
|
|
1101 | |
|
|
1102 | case EIO_BUSY: |
|
|
1103 | #ifdef _WIN32 |
|
|
1104 | Sleep (req->nv1 * 1000.); |
|
|
1105 | #else |
|
|
1106 | { |
|
|
1107 | struct timeval tv; |
|
|
1108 | |
|
|
1109 | tv.tv_sec = req->nv1; |
|
|
1110 | tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; |
|
|
1111 | |
|
|
1112 | req->result = select (0, 0, 0, 0, &tv); |
|
|
1113 | } |
|
|
1114 | #endif |
|
|
1115 | break; |
|
|
1116 | |
|
|
1117 | case EIO_UTIME: |
|
|
1118 | case EIO_FUTIME: |
|
|
1119 | { |
|
|
1120 | struct timeval tv[2]; |
|
|
1121 | struct timeval *times; |
|
|
1122 | |
|
|
1123 | if (req->nv1 != -1. || req->nv2 != -1.) |
|
|
1124 | { |
|
|
1125 | tv[0].tv_sec = req->nv1; |
|
|
1126 | tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.; |
|
|
1127 | tv[1].tv_sec = req->nv2; |
|
|
1128 | tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.; |
|
|
1129 | |
|
|
1130 | times = tv; |
|
|
1131 | } |
|
|
1132 | else |
|
|
1133 | times = 0; |
|
|
1134 | |
|
|
1135 | |
|
|
1136 | req->result = req->type == EIO_FUTIME |
|
|
1137 | ? futimes (req->int1, times) |
|
|
1138 | : utimes (req->ptr1, times); |
|
|
1139 | } |
|
|
1140 | |
|
|
1141 | case EIO_GROUP: |
|
|
1142 | case EIO_NOP: |
|
|
1143 | req->result = 0; |
|
|
1144 | break; |
|
|
1145 | |
|
|
1146 | case -1: |
|
|
1147 | goto quit; |
|
|
1148 | |
|
|
1149 | default: |
|
|
1150 | req->result = -1; |
|
|
1151 | break; |
|
|
1152 | } |
|
|
1153 | |
|
|
1154 | req->errorno = errno; |
|
|
1155 | |
1117 | |
1156 | X_LOCK (reslock); |
1118 | X_LOCK (reslock); |
1157 | |
1119 | |
1158 | ++npending; |
1120 | ++npending; |
1159 | |
1121 | |
… | |
… | |
1176 | |
1138 | |
1177 | /*****************************************************************************/ |
1139 | /*****************************************************************************/ |
1178 | |
1140 | |
1179 | int eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1141 | int eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1180 | { |
1142 | { |
1181 | etp_init (want_poll, done_poll); |
1143 | return etp_init (want_poll, done_poll); |
1182 | } |
1144 | } |
1183 | |
1145 | |
1184 | static void eio_api_destroy (eio_req *req) |
1146 | static void eio_api_destroy (eio_req *req) |
1185 | { |
1147 | { |
1186 | free (req); |
1148 | free (req); |
… | |
… | |
1208 | { \ |
1170 | { \ |
1209 | eio_api_destroy (req); \ |
1171 | eio_api_destroy (req); \ |
1210 | return 0; \ |
1172 | return 0; \ |
1211 | } |
1173 | } |
1212 | |
1174 | |
|
|
1175 | static void eio_execute (etp_worker *self, eio_req *req) |
|
|
1176 | { |
|
|
1177 | errno = 0; |
|
|
1178 | |
|
|
1179 | switch (req->type) |
|
|
1180 | { |
|
|
1181 | case EIO_READ: ALLOC (req->size); |
|
|
1182 | req->result = req->offs >= 0 |
|
|
1183 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
|
|
1184 | : read (req->int1, req->ptr2, req->size); break; |
|
|
1185 | case EIO_WRITE: req->result = req->offs >= 0 |
|
|
1186 | ? pwrite (req->int1, req->ptr2, req->size, req->offs) |
|
|
1187 | : write (req->int1, req->ptr2, req->size); break; |
|
|
1188 | |
|
|
1189 | case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
|
|
1190 | case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size, self); break; |
|
|
1191 | |
|
|
1192 | case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
|
|
1193 | req->result = stat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; |
|
|
1194 | case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
|
|
1195 | req->result = lstat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; |
|
|
1196 | case EIO_FSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
|
|
1197 | req->result = fstat (req->int1, (EIO_STRUCT_STAT *)req->ptr2); break; |
|
|
1198 | |
|
|
1199 | case EIO_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; |
|
|
1200 | case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; |
|
|
1201 | case EIO_CHMOD: req->result = chmod (req->ptr1, (mode_t)req->int2); break; |
|
|
1202 | case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break; |
|
|
1203 | case EIO_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break; |
|
|
1204 | case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; |
|
|
1205 | |
|
|
1206 | case EIO_OPEN: req->result = open (req->ptr1, req->int1, (mode_t)req->int2); break; |
|
|
1207 | case EIO_CLOSE: req->result = close (req->int1); break; |
|
|
1208 | case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; |
|
|
1209 | case EIO_UNLINK: req->result = unlink (req->ptr1); break; |
|
|
1210 | case EIO_RMDIR: req->result = rmdir (req->ptr1); break; |
|
|
1211 | case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break; |
|
|
1212 | case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; |
|
|
1213 | case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; |
|
|
1214 | case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; |
|
|
1215 | case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->int3); break; |
|
|
1216 | |
|
|
1217 | case EIO_READLINK: ALLOC (NAME_MAX); |
|
|
1218 | req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break; |
|
|
1219 | |
|
|
1220 | case EIO_SYNC: req->result = 0; sync (); break; |
|
|
1221 | case EIO_FSYNC: req->result = fsync (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; |
|
|
1226 | |
|
|
1227 | case EIO_READDIR: eio__scandir (req, self); break; |
|
|
1228 | |
|
|
1229 | case EIO_BUSY: |
|
|
1230 | #ifdef _WIN32 |
|
|
1231 | Sleep (req->nv1 * 1000.); |
|
|
1232 | #else |
|
|
1233 | { |
|
|
1234 | struct timeval tv; |
|
|
1235 | |
|
|
1236 | tv.tv_sec = req->nv1; |
|
|
1237 | tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; |
|
|
1238 | |
|
|
1239 | req->result = select (0, 0, 0, 0, &tv); |
|
|
1240 | } |
|
|
1241 | #endif |
|
|
1242 | break; |
|
|
1243 | |
|
|
1244 | case EIO_UTIME: |
|
|
1245 | case EIO_FUTIME: |
|
|
1246 | { |
|
|
1247 | struct timeval tv[2]; |
|
|
1248 | struct timeval *times; |
|
|
1249 | |
|
|
1250 | if (req->nv1 != -1. || req->nv2 != -1.) |
|
|
1251 | { |
|
|
1252 | tv[0].tv_sec = req->nv1; |
|
|
1253 | tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.; |
|
|
1254 | tv[1].tv_sec = req->nv2; |
|
|
1255 | tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.; |
|
|
1256 | |
|
|
1257 | times = tv; |
|
|
1258 | } |
|
|
1259 | else |
|
|
1260 | times = 0; |
|
|
1261 | |
|
|
1262 | |
|
|
1263 | req->result = req->type == EIO_FUTIME |
|
|
1264 | ? futimes (req->int1, times) |
|
|
1265 | : utimes (req->ptr1, times); |
|
|
1266 | } |
|
|
1267 | break; |
|
|
1268 | |
|
|
1269 | case EIO_GROUP: |
|
|
1270 | abort (); /* handled in eio_request */ |
|
|
1271 | |
|
|
1272 | case EIO_NOP: |
|
|
1273 | req->result = 0; |
|
|
1274 | break; |
|
|
1275 | |
|
|
1276 | case EIO_CUSTOM: |
|
|
1277 | ((void (*)(eio_req *))req->feed) (req); |
|
|
1278 | break; |
|
|
1279 | |
|
|
1280 | default: |
|
|
1281 | req->result = -1; |
|
|
1282 | break; |
|
|
1283 | } |
|
|
1284 | |
|
|
1285 | req->errorno = errno; |
|
|
1286 | } |
|
|
1287 | |
1213 | #ifndef EIO_NO_WRAPPERS |
1288 | #ifndef EIO_NO_WRAPPERS |
1214 | |
1289 | |
1215 | eio_req *eio_nop (int pri, eio_cb cb, void *data) |
1290 | eio_req *eio_nop (int pri, eio_cb cb, void *data) |
1216 | { |
1291 | { |
1217 | REQ (EIO_NOP); SEND; |
1292 | REQ (EIO_NOP); SEND; |
… | |
… | |
1228 | } |
1303 | } |
1229 | |
1304 | |
1230 | 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) |
1231 | { |
1306 | { |
1232 | 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; |
1233 | } |
1323 | } |
1234 | |
1324 | |
1235 | 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) |
1236 | { |
1326 | { |
1237 | REQ (EIO_FDATASYNC); req->int1 = fd; SEND; |
1327 | REQ (EIO_FDATASYNC); req->int1 = fd; SEND; |
… | |
… | |
1358 | return eio__1path (EIO_READDIR, path, pri, cb, data); |
1448 | return eio__1path (EIO_READDIR, path, pri, cb, data); |
1359 | } |
1449 | } |
1360 | |
1450 | |
1361 | eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) |
1451 | eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) |
1362 | { |
1452 | { |
1363 | REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int2 = (long)dev; SEND; |
1453 | REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND; |
1364 | } |
1454 | } |
1365 | |
1455 | |
1366 | static eio_req * |
1456 | static eio_req * |
1367 | eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1457 | eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1368 | { |
1458 | { |
… | |
… | |
1392 | eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1482 | eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1393 | { |
1483 | { |
1394 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
1484 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
1395 | } |
1485 | } |
1396 | |
1486 | |
|
|
1487 | eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) |
|
|
1488 | { |
|
|
1489 | REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND; |
|
|
1490 | } |
|
|
1491 | |
1397 | #endif |
1492 | #endif |
1398 | |
1493 | |
1399 | eio_req *eio_grp (eio_cb cb, void *data) |
1494 | eio_req *eio_grp (eio_cb cb, void *data) |
1400 | { |
1495 | { |
1401 | const int pri = EIO_PRI_MAX; |
1496 | const int pri = EIO_PRI_MAX; |
… | |
… | |
1427 | |
1522 | |
1428 | void eio_grp_add (eio_req *grp, eio_req *req) |
1523 | void eio_grp_add (eio_req *grp, eio_req *req) |
1429 | { |
1524 | { |
1430 | 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)); |
1431 | |
1526 | |
|
|
1527 | grp->flags |= EIO_FLAG_GROUPADD; |
|
|
1528 | |
1432 | ++grp->size; |
1529 | ++grp->size; |
1433 | req->grp = grp; |
1530 | req->grp = grp; |
1434 | |
1531 | |
1435 | req->grp_prev = 0; |
1532 | req->grp_prev = 0; |
1436 | req->grp_next = grp->grp_first; |
1533 | req->grp_next = grp->grp_first; |