… | |
… | |
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 |
|
|
142 | |
|
|
143 | struct etp_worker; |
127 | |
144 | |
128 | #define ETP_REQ eio_req |
145 | #define ETP_REQ eio_req |
129 | #define ETP_DESTROY(req) eio_destroy (req) |
146 | #define ETP_DESTROY(req) eio_destroy (req) |
130 | static int eio_finish (eio_req *req); |
147 | static int eio_finish (eio_req *req); |
131 | #define ETP_FINISH(req) eio_finish (req) |
148 | #define ETP_FINISH(req) eio_finish (req) |
|
|
149 | static void eio_execute (struct etp_worker *self, eio_req *req); |
|
|
150 | #define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) |
132 | |
151 | |
133 | #define ETP_WORKER_CLEAR(req) \ |
152 | #define ETP_WORKER_CLEAR(req) \ |
134 | if (wrk->dbuf) \ |
153 | if (wrk->dbuf) \ |
135 | { \ |
154 | { \ |
136 | free (wrk->dbuf); \ |
155 | free (wrk->dbuf); \ |
… | |
… | |
173 | static mutex_t wrklock = X_MUTEX_INIT; |
192 | static mutex_t wrklock = X_MUTEX_INIT; |
174 | static mutex_t reslock = X_MUTEX_INIT; |
193 | static mutex_t reslock = X_MUTEX_INIT; |
175 | static mutex_t reqlock = X_MUTEX_INIT; |
194 | static mutex_t reqlock = X_MUTEX_INIT; |
176 | static cond_t reqwait = X_COND_INIT; |
195 | static cond_t reqwait = X_COND_INIT; |
177 | |
196 | |
|
|
197 | #if !HAVE_PREADWRITE |
|
|
198 | /* |
|
|
199 | * make our pread/pwrite emulation safe against themselves, but not against |
|
|
200 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
201 | * but that's your problem, not mine. |
|
|
202 | */ |
|
|
203 | static mutex_t preadwritelock = X_MUTEX_INIT; |
|
|
204 | #endif |
|
|
205 | |
178 | typedef struct etp_worker |
206 | typedef struct etp_worker |
179 | { |
207 | { |
180 | /* locked by wrklock */ |
208 | /* locked by wrklock */ |
181 | struct etp_worker *prev, *next; |
209 | struct etp_worker *prev, *next; |
182 | |
210 | |
… | |
… | |
310 | X_LOCK (reqlock); |
338 | X_LOCK (reqlock); |
311 | X_LOCK (reslock); |
339 | X_LOCK (reslock); |
312 | #if !HAVE_PREADWRITE |
340 | #if !HAVE_PREADWRITE |
313 | X_LOCK (preadwritelock); |
341 | X_LOCK (preadwritelock); |
314 | #endif |
342 | #endif |
315 | #if !HAVE_READDIR_R |
|
|
316 | X_LOCK (readdirlock); |
|
|
317 | #endif |
|
|
318 | } |
343 | } |
319 | |
344 | |
320 | static void etp_atfork_parent (void) |
345 | static void etp_atfork_parent (void) |
321 | { |
346 | { |
322 | #if !HAVE_READDIR_R |
|
|
323 | X_UNLOCK (readdirlock); |
|
|
324 | #endif |
|
|
325 | #if !HAVE_PREADWRITE |
347 | #if !HAVE_PREADWRITE |
326 | X_UNLOCK (preadwritelock); |
348 | X_UNLOCK (preadwritelock); |
327 | #endif |
349 | #endif |
328 | X_UNLOCK (reslock); |
350 | X_UNLOCK (reslock); |
329 | X_UNLOCK (reqlock); |
351 | X_UNLOCK (reqlock); |
… | |
… | |
332 | |
354 | |
333 | static void etp_atfork_child (void) |
355 | static void etp_atfork_child (void) |
334 | { |
356 | { |
335 | ETP_REQ *prv; |
357 | ETP_REQ *prv; |
336 | |
358 | |
337 | while (prv = reqq_shift (&req_queue)) |
359 | while ((prv = reqq_shift (&req_queue))) |
338 | ETP_DESTROY (prv); |
360 | ETP_DESTROY (prv); |
339 | |
361 | |
340 | while (prv = reqq_shift (&res_queue)) |
362 | while ((prv = reqq_shift (&res_queue))) |
341 | ETP_DESTROY (prv); |
363 | ETP_DESTROY (prv); |
342 | |
364 | |
343 | while (wrk_first.next != &wrk_first) |
365 | while (wrk_first.next != &wrk_first) |
344 | { |
366 | { |
345 | etp_worker *wrk = wrk_first.next; |
367 | etp_worker *wrk = wrk_first.next; |
… | |
… | |
373 | |
395 | |
374 | pthread_once (&doinit, etp_once_init); |
396 | pthread_once (&doinit, etp_once_init); |
375 | |
397 | |
376 | want_poll_cb = want_poll; |
398 | want_poll_cb = want_poll; |
377 | done_poll_cb = done_poll; |
399 | done_poll_cb = done_poll; |
|
|
400 | |
|
|
401 | return 0; |
378 | } |
402 | } |
379 | |
403 | |
380 | X_THREAD_PROC (etp_proc); |
404 | X_THREAD_PROC (etp_proc); |
381 | |
405 | |
382 | static void etp_start_thread (void) |
406 | static void etp_start_thread (void) |
… | |
… | |
402 | X_UNLOCK (wrklock); |
426 | X_UNLOCK (wrklock); |
403 | } |
427 | } |
404 | |
428 | |
405 | static void etp_maybe_start_thread (void) |
429 | static void etp_maybe_start_thread (void) |
406 | { |
430 | { |
407 | if (etp_nthreads () >= wanted) |
431 | if (expect_true (etp_nthreads () >= wanted)) |
408 | return; |
432 | return; |
409 | |
433 | |
410 | /* todo: maybe use idle here, but might be less exact */ |
434 | /* todo: maybe use idle here, but might be less exact */ |
411 | 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 ())) |
412 | return; |
436 | return; |
413 | |
437 | |
414 | etp_start_thread (); |
438 | etp_start_thread (); |
415 | } |
439 | } |
416 | |
440 | |
… | |
… | |
469 | |
493 | |
470 | X_LOCK (reqlock); |
494 | X_LOCK (reqlock); |
471 | --nreqs; |
495 | --nreqs; |
472 | X_UNLOCK (reqlock); |
496 | X_UNLOCK (reqlock); |
473 | |
497 | |
474 | if (req->type == EIO_GROUP && req->size) |
498 | if (expect_false (req->type == EIO_GROUP && req->size)) |
475 | { |
499 | { |
476 | req->int1 = 1; /* mark request as delayed */ |
500 | req->int1 = 1; /* mark request as delayed */ |
477 | continue; |
501 | continue; |
478 | } |
502 | } |
479 | else |
503 | else |
480 | { |
504 | { |
481 | int res = ETP_FINISH (req); |
505 | int res = ETP_FINISH (req); |
482 | if (res) |
506 | if (expect_false (res)) |
483 | return res; |
507 | return res; |
484 | } |
508 | } |
485 | |
509 | |
486 | if (maxreqs && !--maxreqs) |
510 | if (expect_false (maxreqs && !--maxreqs)) |
487 | break; |
511 | break; |
488 | |
512 | |
489 | if (maxtime) |
513 | if (maxtime) |
490 | { |
514 | { |
491 | gettimeofday (&tv_now, 0); |
515 | gettimeofday (&tv_now, 0); |
… | |
… | |
510 | |
534 | |
511 | static void etp_submit (ETP_REQ *req) |
535 | static void etp_submit (ETP_REQ *req) |
512 | { |
536 | { |
513 | req->pri -= ETP_PRI_MIN; |
537 | req->pri -= ETP_PRI_MIN; |
514 | |
538 | |
515 | 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; |
516 | 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; |
517 | |
541 | |
|
|
542 | if (expect_false (req->type == EIO_GROUP)) |
|
|
543 | { |
|
|
544 | /* I hope this is worth it :/ */ |
518 | X_LOCK (reqlock); |
545 | X_LOCK (reqlock); |
519 | ++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; |
520 | ++nready; |
562 | ++nready; |
521 | reqq_push (&req_queue, req); |
563 | reqq_push (&req_queue, req); |
522 | X_COND_SIGNAL (reqwait); |
564 | X_COND_SIGNAL (reqwait); |
523 | X_UNLOCK (reqlock); |
565 | X_UNLOCK (reqlock); |
524 | |
566 | |
525 | etp_maybe_start_thread (); |
567 | etp_maybe_start_thread (); |
|
|
568 | } |
526 | } |
569 | } |
527 | |
570 | |
528 | static void etp_set_max_poll_time (double nseconds) |
571 | static void etp_set_max_poll_time (double nseconds) |
529 | { |
572 | { |
530 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
573 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
… | |
… | |
565 | |
608 | |
566 | static void grp_try_feed (eio_req *grp) |
609 | static void grp_try_feed (eio_req *grp) |
567 | { |
610 | { |
568 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
611 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
569 | { |
612 | { |
570 | int old_len = grp->size; |
613 | grp->flags &= ~EIO_FLAG_GROUPADD; |
571 | |
614 | |
572 | EIO_FEED (grp); |
615 | EIO_FEED (grp); |
573 | |
616 | |
574 | /* stop if no progress has been made */ |
617 | /* stop if no progress has been made */ |
575 | if (old_len == grp->size) |
618 | if (!(grp->flags & EIO_FLAG_GROUPADD)) |
576 | { |
619 | { |
577 | grp->feed = 0; |
620 | grp->feed = 0; |
578 | break; |
621 | break; |
579 | } |
622 | } |
580 | } |
623 | } |
… | |
… | |
697 | |
740 | |
698 | /*****************************************************************************/ |
741 | /*****************************************************************************/ |
699 | /* work around various missing functions */ |
742 | /* work around various missing functions */ |
700 | |
743 | |
701 | #if !HAVE_PREADWRITE |
744 | #if !HAVE_PREADWRITE |
|
|
745 | # undef pread |
|
|
746 | # undef pwrite |
702 | # define pread eio__pread |
747 | # define pread eio__pread |
703 | # define pwrite eio__pwrite |
748 | # 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 | |
749 | |
712 | static ssize_t |
750 | static ssize_t |
713 | 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) |
714 | { |
752 | { |
715 | ssize_t res; |
753 | ssize_t res; |
… | |
… | |
733 | |
771 | |
734 | X_LOCK (preadwritelock); |
772 | X_LOCK (preadwritelock); |
735 | ooffset = lseek (fd, 0, SEEK_CUR); |
773 | ooffset = lseek (fd, 0, SEEK_CUR); |
736 | lseek (fd, offset, SEEK_SET); |
774 | lseek (fd, offset, SEEK_SET); |
737 | res = write (fd, buf, count); |
775 | res = write (fd, buf, count); |
738 | lseek (fd, offset, SEEK_SET); |
776 | lseek (fd, ooffset, SEEK_SET); |
739 | X_UNLOCK (preadwritelock); |
777 | X_UNLOCK (preadwritelock); |
740 | |
778 | |
741 | return res; |
779 | return res; |
742 | } |
780 | } |
743 | #endif |
781 | #endif |
744 | |
782 | |
745 | #ifndef HAVE_FUTIMES |
783 | #ifndef HAVE_FUTIMES |
746 | |
784 | |
|
|
785 | # undef utimes |
|
|
786 | # undef futimes |
747 | # define utimes(path,times) eio__utimes (path, times) |
787 | # define utimes(path,times) eio__utimes (path, times) |
748 | # define futimes(fd,times) eio__futimes (fd, times) |
788 | # define futimes(fd,times) eio__futimes (fd, times) |
749 | |
789 | |
750 | static int |
790 | static int |
751 | eio__utimes (const char *filename, const struct timeval times[2]) |
791 | eio__utimes (const char *filename, const struct timeval times[2]) |
… | |
… | |
770 | } |
810 | } |
771 | |
811 | |
772 | #endif |
812 | #endif |
773 | |
813 | |
774 | #if !HAVE_FDATASYNC |
814 | #if !HAVE_FDATASYNC |
|
|
815 | # undef fdatasync |
775 | # define fdatasync fsync |
816 | # define fdatasync(fd) fsync (fd) |
776 | #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 | } |
777 | |
846 | |
778 | #if !HAVE_READAHEAD |
847 | #if !HAVE_READAHEAD |
|
|
848 | # undef readahead |
779 | # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) |
849 | # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) |
780 | |
850 | |
781 | static ssize_t |
851 | static ssize_t |
782 | eio__readahead (int fd, off_t offset, size_t count, worker *self) |
852 | eio__readahead (int fd, off_t offset, size_t count, etp_worker *self) |
783 | { |
853 | { |
784 | size_t todo = count; |
854 | size_t todo = count; |
785 | dBUF; |
855 | dBUF; |
786 | |
856 | |
787 | while (todo > 0) |
857 | while (todo > 0) |
… | |
… | |
795 | |
865 | |
796 | errno = 0; |
866 | errno = 0; |
797 | return count; |
867 | return count; |
798 | } |
868 | } |
799 | |
869 | |
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 |
870 | #endif |
832 | |
871 | |
833 | /* sendfile always needs emulation */ |
872 | /* sendfile always needs emulation */ |
834 | static ssize_t |
873 | static ssize_t |
835 | eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) |
874 | eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) |
… | |
… | |
927 | /* read a full directory */ |
966 | /* read a full directory */ |
928 | static void |
967 | static void |
929 | eio__scandir (eio_req *req, etp_worker *self) |
968 | eio__scandir (eio_req *req, etp_worker *self) |
930 | { |
969 | { |
931 | DIR *dirp; |
970 | 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; |
971 | EIO_STRUCT_DIRENT *entp; |
938 | char *name, *names; |
972 | char *name, *names; |
939 | int memlen = 4096; |
973 | int memlen = 4096; |
940 | int memofs = 0; |
974 | int memofs = 0; |
941 | int res = 0; |
975 | int res = 0; |
942 | |
976 | |
943 | X_LOCK (wrklock); |
977 | X_LOCK (wrklock); |
|
|
978 | /* the corresponding closedir is in ETP_WORKER_CLEAR */ |
944 | self->dirp = dirp = opendir (req->ptr1); |
979 | self->dirp = dirp = opendir (req->ptr1); |
945 | self->dbuf = u = malloc (sizeof (*u)); |
|
|
946 | req->flags |= EIO_FLAG_PTR2_FREE; |
980 | req->flags |= EIO_FLAG_PTR2_FREE; |
947 | req->ptr2 = names = malloc (memlen); |
981 | req->ptr2 = names = malloc (memlen); |
948 | X_UNLOCK (wrklock); |
982 | X_UNLOCK (wrklock); |
949 | |
983 | |
950 | if (dirp && u && names) |
984 | if (dirp && names) |
951 | for (;;) |
985 | for (;;) |
952 | { |
986 | { |
953 | errno = 0; |
987 | errno = 0; |
954 | readdir_r (dirp, &u->d, &entp); |
988 | entp = readdir (dirp); |
955 | |
989 | |
956 | if (!entp) |
990 | if (!entp) |
957 | break; |
991 | break; |
958 | |
992 | |
959 | name = entp->d_name; |
993 | name = entp->d_name; |
… | |
… | |
982 | |
1016 | |
983 | if (errno) |
1017 | if (errno) |
984 | res = -1; |
1018 | res = -1; |
985 | |
1019 | |
986 | 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; |
987 | } |
1051 | } |
988 | |
1052 | |
989 | /*****************************************************************************/ |
1053 | /*****************************************************************************/ |
990 | |
1054 | |
991 | #define ALLOC(len) \ |
1055 | #define ALLOC(len) \ |
… | |
… | |
1047 | |
1111 | |
1048 | --nready; |
1112 | --nready; |
1049 | |
1113 | |
1050 | X_UNLOCK (reqlock); |
1114 | X_UNLOCK (reqlock); |
1051 | |
1115 | |
1052 | errno = 0; /* strictly unnecessary */ |
1116 | if (req->type < 0) |
|
|
1117 | goto quit; |
1053 | |
1118 | |
1054 | if (!EIO_CANCELLED (req)) |
1119 | if (!EIO_CANCELLED (req)) |
1055 | switch (req->type) |
1120 | 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 | |
1121 | |
1156 | X_LOCK (reslock); |
1122 | X_LOCK (reslock); |
1157 | |
1123 | |
1158 | ++npending; |
1124 | ++npending; |
1159 | |
1125 | |
… | |
… | |
1176 | |
1142 | |
1177 | /*****************************************************************************/ |
1143 | /*****************************************************************************/ |
1178 | |
1144 | |
1179 | int eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1145 | int eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1180 | { |
1146 | { |
1181 | etp_init (want_poll, done_poll); |
1147 | return etp_init (want_poll, done_poll); |
1182 | } |
1148 | } |
1183 | |
1149 | |
1184 | static void eio_api_destroy (eio_req *req) |
1150 | static void eio_api_destroy (eio_req *req) |
1185 | { |
1151 | { |
1186 | free (req); |
1152 | free (req); |
… | |
… | |
1208 | { \ |
1174 | { \ |
1209 | eio_api_destroy (req); \ |
1175 | eio_api_destroy (req); \ |
1210 | return 0; \ |
1176 | return 0; \ |
1211 | } |
1177 | } |
1212 | |
1178 | |
|
|
1179 | static void eio_execute (etp_worker *self, eio_req *req) |
|
|
1180 | { |
|
|
1181 | errno = 0; |
|
|
1182 | |
|
|
1183 | switch (req->type) |
|
|
1184 | { |
|
|
1185 | case EIO_READ: ALLOC (req->size); |
|
|
1186 | req->result = req->offs >= 0 |
|
|
1187 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
|
|
1188 | : read (req->int1, req->ptr2, req->size); break; |
|
|
1189 | case EIO_WRITE: req->result = req->offs >= 0 |
|
|
1190 | ? pwrite (req->int1, req->ptr2, req->size, req->offs) |
|
|
1191 | : write (req->int1, req->ptr2, req->size); break; |
|
|
1192 | |
|
|
1193 | case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
|
|
1194 | case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size, self); break; |
|
|
1195 | |
|
|
1196 | case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
|
|
1197 | req->result = stat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; |
|
|
1198 | case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
|
|
1199 | req->result = lstat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; |
|
|
1200 | case EIO_FSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); |
|
|
1201 | req->result = fstat (req->int1, (EIO_STRUCT_STAT *)req->ptr2); break; |
|
|
1202 | |
|
|
1203 | case EIO_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; |
|
|
1204 | case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; |
|
|
1205 | case EIO_CHMOD: req->result = chmod (req->ptr1, (mode_t)req->int2); break; |
|
|
1206 | case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break; |
|
|
1207 | case EIO_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break; |
|
|
1208 | case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; |
|
|
1209 | |
|
|
1210 | case EIO_OPEN: req->result = open (req->ptr1, req->int1, (mode_t)req->int2); break; |
|
|
1211 | case EIO_CLOSE: req->result = close (req->int1); break; |
|
|
1212 | case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; |
|
|
1213 | case EIO_UNLINK: req->result = unlink (req->ptr1); break; |
|
|
1214 | case EIO_RMDIR: req->result = rmdir (req->ptr1); break; |
|
|
1215 | case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break; |
|
|
1216 | case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; |
|
|
1217 | case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; |
|
|
1218 | case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; |
|
|
1219 | case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->int3); break; |
|
|
1220 | |
|
|
1221 | case EIO_READLINK: ALLOC (NAME_MAX); |
|
|
1222 | req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break; |
|
|
1223 | |
|
|
1224 | case EIO_SYNC: req->result = 0; sync (); break; |
|
|
1225 | case EIO_FSYNC: req->result = fsync (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; |
|
|
1230 | |
|
|
1231 | case EIO_READDIR: eio__scandir (req, self); break; |
|
|
1232 | |
|
|
1233 | case EIO_BUSY: |
|
|
1234 | #ifdef _WIN32 |
|
|
1235 | Sleep (req->nv1 * 1000.); |
|
|
1236 | #else |
|
|
1237 | { |
|
|
1238 | struct timeval tv; |
|
|
1239 | |
|
|
1240 | tv.tv_sec = req->nv1; |
|
|
1241 | tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; |
|
|
1242 | |
|
|
1243 | req->result = select (0, 0, 0, 0, &tv); |
|
|
1244 | } |
|
|
1245 | #endif |
|
|
1246 | break; |
|
|
1247 | |
|
|
1248 | case EIO_UTIME: |
|
|
1249 | case EIO_FUTIME: |
|
|
1250 | { |
|
|
1251 | struct timeval tv[2]; |
|
|
1252 | struct timeval *times; |
|
|
1253 | |
|
|
1254 | if (req->nv1 != -1. || req->nv2 != -1.) |
|
|
1255 | { |
|
|
1256 | tv[0].tv_sec = req->nv1; |
|
|
1257 | tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.; |
|
|
1258 | tv[1].tv_sec = req->nv2; |
|
|
1259 | tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.; |
|
|
1260 | |
|
|
1261 | times = tv; |
|
|
1262 | } |
|
|
1263 | else |
|
|
1264 | times = 0; |
|
|
1265 | |
|
|
1266 | |
|
|
1267 | req->result = req->type == EIO_FUTIME |
|
|
1268 | ? futimes (req->int1, times) |
|
|
1269 | : utimes (req->ptr1, times); |
|
|
1270 | } |
|
|
1271 | break; |
|
|
1272 | |
|
|
1273 | case EIO_GROUP: |
|
|
1274 | abort (); /* handled in eio_request */ |
|
|
1275 | |
|
|
1276 | case EIO_NOP: |
|
|
1277 | req->result = 0; |
|
|
1278 | break; |
|
|
1279 | |
|
|
1280 | case EIO_CUSTOM: |
|
|
1281 | ((void (*)(eio_req *))req->feed) (req); |
|
|
1282 | break; |
|
|
1283 | |
|
|
1284 | default: |
|
|
1285 | req->result = -1; |
|
|
1286 | break; |
|
|
1287 | } |
|
|
1288 | |
|
|
1289 | req->errorno = errno; |
|
|
1290 | } |
|
|
1291 | |
1213 | #ifndef EIO_NO_WRAPPERS |
1292 | #ifndef EIO_NO_WRAPPERS |
1214 | |
1293 | |
1215 | eio_req *eio_nop (int pri, eio_cb cb, void *data) |
1294 | eio_req *eio_nop (int pri, eio_cb cb, void *data) |
1216 | { |
1295 | { |
1217 | REQ (EIO_NOP); SEND; |
1296 | REQ (EIO_NOP); SEND; |
… | |
… | |
1228 | } |
1307 | } |
1229 | |
1308 | |
1230 | 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) |
1231 | { |
1310 | { |
1232 | 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; |
1233 | } |
1327 | } |
1234 | |
1328 | |
1235 | 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) |
1236 | { |
1330 | { |
1237 | REQ (EIO_FDATASYNC); req->int1 = fd; SEND; |
1331 | REQ (EIO_FDATASYNC); req->int1 = fd; SEND; |
… | |
… | |
1358 | return eio__1path (EIO_READDIR, path, pri, cb, data); |
1452 | return eio__1path (EIO_READDIR, path, pri, cb, data); |
1359 | } |
1453 | } |
1360 | |
1454 | |
1361 | eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) |
1455 | eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) |
1362 | { |
1456 | { |
1363 | REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int2 = (long)dev; SEND; |
1457 | REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND; |
1364 | } |
1458 | } |
1365 | |
1459 | |
1366 | static eio_req * |
1460 | static eio_req * |
1367 | eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1461 | eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1368 | { |
1462 | { |
… | |
… | |
1392 | eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1486 | eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1393 | { |
1487 | { |
1394 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
1488 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
1395 | } |
1489 | } |
1396 | |
1490 | |
|
|
1491 | eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) |
|
|
1492 | { |
|
|
1493 | REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND; |
|
|
1494 | } |
|
|
1495 | |
1397 | #endif |
1496 | #endif |
1398 | |
1497 | |
1399 | eio_req *eio_grp (eio_cb cb, void *data) |
1498 | eio_req *eio_grp (eio_cb cb, void *data) |
1400 | { |
1499 | { |
1401 | const int pri = EIO_PRI_MAX; |
1500 | const int pri = EIO_PRI_MAX; |
… | |
… | |
1427 | |
1526 | |
1428 | void eio_grp_add (eio_req *grp, eio_req *req) |
1527 | void eio_grp_add (eio_req *grp, eio_req *req) |
1429 | { |
1528 | { |
1430 | 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)); |
1431 | |
1530 | |
|
|
1531 | grp->flags |= EIO_FLAG_GROUPADD; |
|
|
1532 | |
1432 | ++grp->size; |
1533 | ++grp->size; |
1433 | req->grp = grp; |
1534 | req->grp = grp; |
1434 | |
1535 | |
1435 | req->grp_prev = 0; |
1536 | req->grp_prev = 0; |
1436 | req->grp_next = grp->grp_first; |
1537 | req->grp_next = grp->grp_first; |