1 | /* |
1 | /* |
2 | * libeio implementation |
2 | * libeio implementation |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008,2009,2010,2011,2012,2013 Marc Alexander Lehmann <libeio@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010,2011,2012,2013,2016 Marc Alexander Lehmann <libeio@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
… | |
… | |
42 | #endif |
42 | #endif |
43 | |
43 | |
44 | #include "eio.h" |
44 | #include "eio.h" |
45 | #include "ecb.h" |
45 | #include "ecb.h" |
46 | |
46 | |
47 | #ifdef EIO_STACKSIZE |
|
|
48 | # define X_STACKSIZE EIO_STACKSIZE |
|
|
49 | #endif |
|
|
50 | #include "xthread.h" |
|
|
51 | |
|
|
52 | #include <errno.h> |
47 | #include <errno.h> |
53 | #include <stddef.h> |
48 | #include <stddef.h> |
54 | #include <stdlib.h> |
49 | #include <stdlib.h> |
55 | #include <string.h> |
50 | #include <string.h> |
56 | #include <errno.h> |
51 | #include <errno.h> |
… | |
… | |
122 | #define chmod(path,mode) _chmod (path, mode) |
117 | #define chmod(path,mode) _chmod (path, mode) |
123 | #define dup(fd) _dup (fd) |
118 | #define dup(fd) _dup (fd) |
124 | #define dup2(fd1,fd2) _dup2 (fd1, fd2) |
119 | #define dup2(fd1,fd2) _dup2 (fd1, fd2) |
125 | #define pipe(fds) _pipe (fds, 4096, O_BINARY) |
120 | #define pipe(fds) _pipe (fds, 4096, O_BINARY) |
126 | |
121 | |
|
|
122 | #define fcntl(fd,cmd,arg) EIO_ENOSYS () |
|
|
123 | #define ioctl(fd,cmd,arg) EIO_ENOSYS () |
127 | #define fchmod(fd,mode) EIO_ENOSYS () |
124 | #define fchmod(fd,mode) EIO_ENOSYS () |
128 | #define chown(path,uid,gid) EIO_ENOSYS () |
125 | #define chown(path,uid,gid) EIO_ENOSYS () |
129 | #define fchown(fd,uid,gid) EIO_ENOSYS () |
126 | #define fchown(fd,uid,gid) EIO_ENOSYS () |
130 | #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ |
127 | #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ |
131 | #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ |
128 | #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ |
… | |
… | |
204 | /* we could even stat and see if it exists */ |
201 | /* we could even stat and see if it exists */ |
205 | static int |
202 | static int |
206 | symlink (const char *old, const char *neu) |
203 | symlink (const char *old, const char *neu) |
207 | { |
204 | { |
208 | #if WINVER >= 0x0600 |
205 | #if WINVER >= 0x0600 |
|
|
206 | int flags; |
|
|
207 | |
|
|
208 | /* This tries out all combinations of SYMBOLIC_LINK_FLAG_DIRECTORY |
|
|
209 | * and SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE, |
|
|
210 | * with directory first. |
|
|
211 | */ |
|
|
212 | for (flags = 3; flags >= 0; --flags) |
209 | if (CreateSymbolicLink (neu, old, 1)) |
213 | if (CreateSymbolicLink (neu, old, flags)) |
210 | return 0; |
214 | return 0; |
211 | |
|
|
212 | if (CreateSymbolicLink (neu, old, 0)) |
|
|
213 | return 0; |
|
|
214 | #endif |
215 | #endif |
215 | |
216 | |
216 | return EIO_ERRNO (ENOENT, -1); |
217 | return EIO_ERRNO (ENOENT, -1); |
217 | } |
218 | } |
218 | |
219 | |
… | |
… | |
281 | # include <utime.h> |
282 | # include <utime.h> |
282 | #endif |
283 | #endif |
283 | |
284 | |
284 | #if HAVE_SYS_SYSCALL_H |
285 | #if HAVE_SYS_SYSCALL_H |
285 | # include <sys/syscall.h> |
286 | # include <sys/syscall.h> |
286 | #endif |
|
|
287 | |
|
|
288 | #if HAVE_SYS_PRCTL_H |
|
|
289 | # include <sys/prctl.h> |
|
|
290 | #endif |
287 | #endif |
291 | |
288 | |
292 | #if HAVE_SENDFILE |
289 | #if HAVE_SENDFILE |
293 | # if __linux |
290 | # if __linux |
294 | # include <sys/sendfile.h> |
291 | # include <sys/sendfile.h> |
… | |
… | |
336 | #define FUBd \ |
333 | #define FUBd \ |
337 | free (eio_buf) |
334 | free (eio_buf) |
338 | |
335 | |
339 | /*****************************************************************************/ |
336 | /*****************************************************************************/ |
340 | |
337 | |
341 | struct tmpbuf |
|
|
342 | { |
|
|
343 | void *ptr; |
|
|
344 | int len; |
|
|
345 | }; |
|
|
346 | |
|
|
347 | static void * |
|
|
348 | tmpbuf_get (struct tmpbuf *buf, int len) |
|
|
349 | { |
|
|
350 | if (buf->len < len) |
|
|
351 | { |
|
|
352 | free (buf->ptr); |
|
|
353 | buf->ptr = malloc (buf->len = len); |
|
|
354 | } |
|
|
355 | |
|
|
356 | return buf->ptr; |
|
|
357 | } |
|
|
358 | |
|
|
359 | struct tmpbuf; |
338 | struct etp_tmpbuf; |
360 | |
339 | |
361 | #if _POSIX_VERSION >= 200809L |
340 | #if _POSIX_VERSION >= 200809L |
362 | #define HAVE_AT 1 |
341 | #define HAVE_AT 1 |
363 | #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) |
342 | #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) |
364 | #ifndef O_SEARCH |
343 | #ifndef O_SEARCH |
365 | #define O_SEARCH O_RDONLY |
344 | #define O_SEARCH O_RDONLY |
366 | #endif |
345 | #endif |
367 | #else |
346 | #else |
368 | #define HAVE_AT 0 |
347 | #define HAVE_AT 0 |
369 | static const char *wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path); |
348 | static const char *wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path); |
370 | #endif |
349 | #endif |
371 | |
350 | |
372 | struct eio_pwd |
351 | struct eio_pwd |
373 | { |
352 | { |
374 | #if HAVE_AT |
353 | #if HAVE_AT |
… | |
… | |
384 | #define ETP_PRI_MAX EIO_PRI_MAX |
363 | #define ETP_PRI_MAX EIO_PRI_MAX |
385 | |
364 | |
386 | #define ETP_TYPE_QUIT -1 |
365 | #define ETP_TYPE_QUIT -1 |
387 | #define ETP_TYPE_GROUP EIO_GROUP |
366 | #define ETP_TYPE_GROUP EIO_GROUP |
388 | |
367 | |
|
|
368 | static void eio_nop_callback (void) { } |
|
|
369 | static void (*eio_want_poll_cb)(void) = eio_nop_callback; |
|
|
370 | static void (*eio_done_poll_cb)(void) = eio_nop_callback; |
|
|
371 | |
|
|
372 | #define ETP_WANT_POLL(pool) eio_want_poll_cb () |
|
|
373 | #define ETP_DONE_POLL(pool) eio_done_poll_cb () |
|
|
374 | |
389 | struct etp_worker; |
375 | struct etp_worker; |
390 | |
|
|
391 | #define ETP_REQ eio_req |
376 | #define ETP_REQ eio_req |
392 | #define ETP_DESTROY(req) eio_destroy (req) |
377 | #define ETP_DESTROY(req) eio_destroy (req) |
393 | static int eio_finish (eio_req *req); |
378 | static int eio_finish (eio_req *req); |
394 | #define ETP_FINISH(req) eio_finish (req) |
379 | #define ETP_FINISH(req) eio_finish (req) |
395 | static void eio_execute (struct etp_worker *self, eio_req *req); |
380 | static void eio_execute (struct etp_worker *self, eio_req *req); |
396 | #define ETP_EXECUTE(wrk,req) eio_execute (wrk, req) |
381 | #define ETP_EXECUTE(wrk,req) eio_execute (wrk, req) |
397 | |
382 | |
398 | #include "etp.c" |
383 | #include "etp.c" |
399 | |
384 | |
|
|
385 | static struct etp_pool eio_pool; |
|
|
386 | #define EIO_POOL (&eio_pool) |
|
|
387 | |
400 | /*****************************************************************************/ |
388 | /*****************************************************************************/ |
401 | |
389 | |
402 | static void |
390 | static void |
403 | grp_try_feed (eio_req *grp) |
391 | grp_try_feed (eio_req *grp) |
404 | { |
392 | { |
405 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
393 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
406 | { |
394 | { |
407 | grp->flags &= ~EIO_FLAG_GROUPADD; |
395 | grp->flags &= ~ETP_FLAG_GROUPADD; |
408 | |
396 | |
409 | EIO_FEED (grp); |
397 | EIO_FEED (grp); |
410 | |
398 | |
411 | /* stop if no progress has been made */ |
399 | /* stop if no progress has been made */ |
412 | if (!(grp->flags & EIO_FLAG_GROUPADD)) |
400 | if (!(grp->flags & ETP_FLAG_GROUPADD)) |
413 | { |
401 | { |
414 | grp->feed = 0; |
402 | grp->feed = 0; |
415 | break; |
403 | break; |
416 | } |
404 | } |
417 | } |
405 | } |
… | |
… | |
424 | |
412 | |
425 | /* call feeder, if applicable */ |
413 | /* call feeder, if applicable */ |
426 | grp_try_feed (grp); |
414 | grp_try_feed (grp); |
427 | |
415 | |
428 | /* finish, if done */ |
416 | /* finish, if done */ |
429 | if (!grp->size && grp->int1) |
417 | if (!grp->size && grp->flags & ETP_FLAG_DELAYED) |
430 | return eio_finish (grp); |
418 | return eio_finish (grp); |
431 | else |
419 | else |
432 | return 0; |
420 | return 0; |
433 | } |
421 | } |
434 | |
422 | |
… | |
… | |
470 | } |
458 | } |
471 | |
459 | |
472 | void |
460 | void |
473 | eio_grp_cancel (eio_req *grp) |
461 | eio_grp_cancel (eio_req *grp) |
474 | { |
462 | { |
475 | etp_grp_cancel (grp); |
463 | etp_grp_cancel (EIO_POOL, grp); |
476 | } |
464 | } |
477 | |
465 | |
478 | void |
466 | void |
479 | eio_cancel (eio_req *req) |
467 | eio_cancel (eio_req *req) |
480 | { |
468 | { |
481 | etp_cancel (req); |
469 | etp_cancel (EIO_POOL, req); |
482 | } |
470 | } |
483 | |
471 | |
484 | void |
472 | void |
485 | eio_submit (eio_req *req) |
473 | eio_submit (eio_req *req) |
486 | { |
474 | { |
487 | etp_submit (req); |
475 | etp_submit (EIO_POOL, req); |
488 | } |
476 | } |
489 | |
477 | |
490 | unsigned int |
478 | unsigned int |
491 | eio_nreqs (void) |
479 | eio_nreqs (void) |
492 | { |
480 | { |
493 | return etp_nreqs (); |
481 | return etp_nreqs (EIO_POOL); |
494 | } |
482 | } |
495 | |
483 | |
496 | unsigned int |
484 | unsigned int |
497 | eio_nready (void) |
485 | eio_nready (void) |
498 | { |
486 | { |
499 | return etp_nready (); |
487 | return etp_nready (EIO_POOL); |
500 | } |
488 | } |
501 | |
489 | |
502 | unsigned int |
490 | unsigned int |
503 | eio_npending (void) |
491 | eio_npending (void) |
504 | { |
492 | { |
505 | return etp_npending (); |
493 | return etp_npending (EIO_POOL); |
506 | } |
494 | } |
507 | |
495 | |
508 | unsigned int ecb_cold |
496 | unsigned int ecb_cold |
509 | eio_nthreads (void) |
497 | eio_nthreads (void) |
510 | { |
498 | { |
511 | return etp_nthreads (); |
499 | return etp_nthreads (EIO_POOL); |
512 | } |
500 | } |
513 | |
501 | |
514 | void ecb_cold |
502 | void ecb_cold |
515 | eio_set_max_poll_time (double nseconds) |
503 | eio_set_max_poll_time (double nseconds) |
516 | { |
504 | { |
517 | etp_set_max_poll_time (nseconds); |
505 | etp_set_max_poll_time (EIO_POOL, nseconds); |
518 | } |
506 | } |
519 | |
507 | |
520 | void ecb_cold |
508 | void ecb_cold |
521 | eio_set_max_poll_reqs (unsigned int maxreqs) |
509 | eio_set_max_poll_reqs (unsigned int maxreqs) |
522 | { |
510 | { |
523 | etp_set_max_poll_reqs (maxreqs); |
511 | etp_set_max_poll_reqs (EIO_POOL, maxreqs); |
524 | } |
512 | } |
525 | |
513 | |
526 | void ecb_cold |
514 | void ecb_cold |
527 | eio_set_max_idle (unsigned int nthreads) |
515 | eio_set_max_idle (unsigned int nthreads) |
528 | { |
516 | { |
529 | etp_set_max_idle (nthreads); |
517 | etp_set_max_idle (EIO_POOL, nthreads); |
530 | } |
518 | } |
531 | |
519 | |
532 | void ecb_cold |
520 | void ecb_cold |
533 | eio_set_idle_timeout (unsigned int seconds) |
521 | eio_set_idle_timeout (unsigned int seconds) |
534 | { |
522 | { |
535 | etp_set_idle_timeout (seconds); |
523 | etp_set_idle_timeout (EIO_POOL, seconds); |
536 | } |
524 | } |
537 | |
525 | |
538 | void ecb_cold |
526 | void ecb_cold |
539 | eio_set_min_parallel (unsigned int nthreads) |
527 | eio_set_min_parallel (unsigned int nthreads) |
540 | { |
528 | { |
541 | etp_set_min_parallel (nthreads); |
529 | etp_set_min_parallel (EIO_POOL, nthreads); |
542 | } |
530 | } |
543 | |
531 | |
544 | void ecb_cold |
532 | void ecb_cold |
545 | eio_set_max_parallel (unsigned int nthreads) |
533 | eio_set_max_parallel (unsigned int nthreads) |
546 | { |
534 | { |
547 | etp_set_max_parallel (nthreads); |
535 | etp_set_max_parallel (EIO_POOL, nthreads); |
548 | } |
536 | } |
549 | |
537 | |
550 | int eio_poll (void) |
538 | int eio_poll (void) |
551 | { |
539 | { |
552 | return etp_poll (); |
540 | return etp_poll (EIO_POOL); |
553 | } |
541 | } |
554 | |
542 | |
555 | /*****************************************************************************/ |
543 | /*****************************************************************************/ |
556 | /* work around various missing functions */ |
544 | /* work around various missing functions */ |
557 | |
545 | |
… | |
… | |
967 | req->result = req->offs == (off_t)-1 ? -1 : 0; |
955 | req->result = req->offs == (off_t)-1 ? -1 : 0; |
968 | } |
956 | } |
969 | |
957 | |
970 | /* result will always end up in tmpbuf, there is always space for adding a 0-byte */ |
958 | /* result will always end up in tmpbuf, there is always space for adding a 0-byte */ |
971 | static int |
959 | static int |
972 | eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
960 | eio__realpath (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path) |
973 | { |
961 | { |
974 | char *res; |
962 | char *res; |
975 | const char *rel = path; |
963 | const char *rel = path; |
976 | char *tmp1, *tmp2; |
964 | char *tmp1, *tmp2; |
977 | #if SYMLOOP_MAX > 32 |
965 | #if SYMLOOP_MAX > 32 |
… | |
… | |
986 | |
974 | |
987 | errno = ENOENT; |
975 | errno = ENOENT; |
988 | if (!*rel) |
976 | if (!*rel) |
989 | return -1; |
977 | return -1; |
990 | |
978 | |
991 | res = tmpbuf_get (tmpbuf, PATH_MAX * 3); |
979 | res = etp_tmpbuf_get (tmpbuf, PATH_MAX * 3); |
992 | #ifdef _WIN32 |
980 | #ifdef _WIN32 |
993 | if (_access (rel, 4) != 0) |
981 | if (_access (rel, 4) != 0) |
994 | return -1; |
982 | return -1; |
995 | |
983 | |
996 | symlinks = GetFullPathName (rel, PATH_MAX * 3, res, 0); |
984 | symlinks = GetFullPathName (rel, PATH_MAX * 3, res, 0); |
… | |
… | |
1605 | #if !HAVE_AT |
1593 | #if !HAVE_AT |
1606 | |
1594 | |
1607 | /* a bit like realpath, but usually faster because it doesn'T have to return */ |
1595 | /* a bit like realpath, but usually faster because it doesn'T have to return */ |
1608 | /* an absolute or canonical path */ |
1596 | /* an absolute or canonical path */ |
1609 | static const char * |
1597 | static const char * |
1610 | wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
1598 | wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path) |
1611 | { |
1599 | { |
1612 | if (!wd || *path == '/') |
1600 | if (!wd || *path == '/') |
1613 | return path; |
1601 | return path; |
1614 | |
1602 | |
1615 | if (path [0] == '.' && !path [1]) |
1603 | if (path [0] == '.' && !path [1]) |
… | |
… | |
1617 | |
1605 | |
1618 | { |
1606 | { |
1619 | int l1 = wd->len; |
1607 | int l1 = wd->len; |
1620 | int l2 = strlen (path); |
1608 | int l2 = strlen (path); |
1621 | |
1609 | |
1622 | char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2); |
1610 | char *res = etp_tmpbuf_get (tmpbuf, l1 + l2 + 2); |
1623 | |
1611 | |
1624 | memcpy (res, wd->str, l1); |
1612 | memcpy (res, wd->str, l1); |
1625 | res [l1] = '/'; |
1613 | res [l1] = '/'; |
1626 | memcpy (res + l1 + 1, path, l2 + 1); |
1614 | memcpy (res + l1 + 1, path, l2 + 1); |
1627 | |
1615 | |
… | |
… | |
1630 | } |
1618 | } |
1631 | |
1619 | |
1632 | #endif |
1620 | #endif |
1633 | |
1621 | |
1634 | static eio_wd |
1622 | static eio_wd |
1635 | eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
1623 | eio__wd_open_sync (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path) |
1636 | { |
1624 | { |
1637 | int fd; |
1625 | int fd; |
1638 | eio_wd res; |
1626 | eio_wd res; |
1639 | int len = eio__realpath (tmpbuf, wd, path); |
1627 | int len = eio__realpath (tmpbuf, wd, path); |
1640 | |
1628 | |
… | |
… | |
1662 | } |
1650 | } |
1663 | |
1651 | |
1664 | eio_wd |
1652 | eio_wd |
1665 | eio_wd_open_sync (eio_wd wd, const char *path) |
1653 | eio_wd_open_sync (eio_wd wd, const char *path) |
1666 | { |
1654 | { |
1667 | struct tmpbuf tmpbuf = { 0 }; |
1655 | struct etp_tmpbuf tmpbuf = { }; |
1668 | wd = eio__wd_open_sync (&tmpbuf, wd, path); |
1656 | wd = eio__wd_open_sync (&tmpbuf, wd, path); |
1669 | free (tmpbuf.ptr); |
1657 | free (tmpbuf.ptr); |
1670 | |
1658 | |
1671 | return wd; |
1659 | return wd; |
1672 | } |
1660 | } |
… | |
… | |
1721 | /*****************************************************************************/ |
1709 | /*****************************************************************************/ |
1722 | |
1710 | |
1723 | #define ALLOC(len) \ |
1711 | #define ALLOC(len) \ |
1724 | if (!req->ptr2) \ |
1712 | if (!req->ptr2) \ |
1725 | { \ |
1713 | { \ |
1726 | X_LOCK (wrklock); \ |
1714 | X_LOCK (EIO_POOL->wrklock); \ |
1727 | req->flags |= EIO_FLAG_PTR2_FREE; \ |
1715 | req->flags |= EIO_FLAG_PTR2_FREE; \ |
1728 | X_UNLOCK (wrklock); \ |
1716 | X_UNLOCK (EIO_POOL->wrklock); \ |
1729 | req->ptr2 = malloc (len); \ |
1717 | req->ptr2 = malloc (len); \ |
1730 | if (!req->ptr2) \ |
1718 | if (!req->ptr2) \ |
1731 | { \ |
1719 | { \ |
1732 | errno = ENOMEM; \ |
1720 | errno = ENOMEM; \ |
1733 | req->result = -1; \ |
1721 | req->result = -1; \ |
1734 | break; \ |
1722 | break; \ |
1735 | } \ |
1723 | } \ |
1736 | } |
1724 | } |
1737 | |
1725 | |
1738 | static void ecb_noinline ecb_cold |
|
|
1739 | etp_proc_init (void) |
|
|
1740 | { |
|
|
1741 | #if HAVE_PRCTL_SET_NAME |
|
|
1742 | /* provide a more sensible "thread name" */ |
|
|
1743 | char name[16 + 1]; |
|
|
1744 | const int namelen = sizeof (name) - 1; |
|
|
1745 | int len; |
|
|
1746 | |
|
|
1747 | prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0); |
|
|
1748 | name [namelen] = 0; |
|
|
1749 | len = strlen (name); |
|
|
1750 | strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio"); |
|
|
1751 | prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0); |
|
|
1752 | #endif |
|
|
1753 | } |
|
|
1754 | |
|
|
1755 | /* TODO: move somehow to etp.c */ |
|
|
1756 | X_THREAD_PROC (etp_proc) |
|
|
1757 | { |
|
|
1758 | ETP_REQ *req; |
|
|
1759 | struct timespec ts; |
|
|
1760 | etp_worker *self = (etp_worker *)thr_arg; |
|
|
1761 | |
|
|
1762 | etp_proc_init (); |
|
|
1763 | |
|
|
1764 | /* try to distribute timeouts somewhat evenly */ |
|
|
1765 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
|
|
1766 | |
|
|
1767 | for (;;) |
|
|
1768 | { |
|
|
1769 | ts.tv_sec = 0; |
|
|
1770 | |
|
|
1771 | X_LOCK (reqlock); |
|
|
1772 | |
|
|
1773 | for (;;) |
|
|
1774 | { |
|
|
1775 | req = reqq_shift (&req_queue); |
|
|
1776 | |
|
|
1777 | if (req) |
|
|
1778 | break; |
|
|
1779 | |
|
|
1780 | if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */ |
|
|
1781 | { |
|
|
1782 | X_UNLOCK (reqlock); |
|
|
1783 | X_LOCK (wrklock); |
|
|
1784 | --started; |
|
|
1785 | X_UNLOCK (wrklock); |
|
|
1786 | goto quit; |
|
|
1787 | } |
|
|
1788 | |
|
|
1789 | ++idle; |
|
|
1790 | |
|
|
1791 | if (idle <= max_idle) |
|
|
1792 | /* we are allowed to idle, so do so without any timeout */ |
|
|
1793 | X_COND_WAIT (reqwait, reqlock); |
|
|
1794 | else |
|
|
1795 | { |
|
|
1796 | /* initialise timeout once */ |
|
|
1797 | if (!ts.tv_sec) |
|
|
1798 | ts.tv_sec = time (0) + idle_timeout; |
|
|
1799 | |
|
|
1800 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) |
|
|
1801 | ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */ |
|
|
1802 | } |
|
|
1803 | |
|
|
1804 | --idle; |
|
|
1805 | } |
|
|
1806 | |
|
|
1807 | --nready; |
|
|
1808 | |
|
|
1809 | X_UNLOCK (reqlock); |
|
|
1810 | |
|
|
1811 | if (req->type == ETP_TYPE_QUIT) |
|
|
1812 | goto quit; |
|
|
1813 | |
|
|
1814 | ETP_EXECUTE (self, req); |
|
|
1815 | |
|
|
1816 | X_LOCK (reslock); |
|
|
1817 | |
|
|
1818 | ++npending; |
|
|
1819 | |
|
|
1820 | if (!reqq_push (&res_queue, req) && want_poll_cb) |
|
|
1821 | want_poll_cb (); |
|
|
1822 | |
|
|
1823 | etp_worker_clear (self); |
|
|
1824 | |
|
|
1825 | X_UNLOCK (reslock); |
|
|
1826 | } |
|
|
1827 | |
|
|
1828 | quit: |
|
|
1829 | free (req); |
|
|
1830 | |
|
|
1831 | X_LOCK (wrklock); |
|
|
1832 | etp_worker_free (self); |
|
|
1833 | X_UNLOCK (wrklock); |
|
|
1834 | |
|
|
1835 | return 0; |
|
|
1836 | } |
|
|
1837 | |
|
|
1838 | /*****************************************************************************/ |
1726 | /*****************************************************************************/ |
1839 | |
1727 | |
1840 | int ecb_cold |
1728 | int ecb_cold |
1841 | eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1729 | eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1842 | { |
1730 | { |
1843 | return etp_init (want_poll, done_poll); |
1731 | eio_want_poll_cb = want_poll; |
|
|
1732 | eio_done_poll_cb = done_poll; |
|
|
1733 | |
|
|
1734 | return etp_init (EIO_POOL, 0, 0, 0); |
1844 | } |
1735 | } |
1845 | |
1736 | |
1846 | ecb_inline void |
1737 | ecb_inline void |
1847 | eio_api_destroy (eio_req *req) |
1738 | eio_api_destroy (eio_req *req) |
1848 | { |
1739 | { |
… | |
… | |
1921 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
1812 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
1922 | : read (req->int1, req->ptr2, req->size); break; |
1813 | : read (req->int1, req->ptr2, req->size); break; |
1923 | case EIO_WRITE: req->result = req->offs >= 0 |
1814 | case EIO_WRITE: req->result = req->offs >= 0 |
1924 | ? pwrite (req->int1, req->ptr2, req->size, req->offs) |
1815 | ? pwrite (req->int1, req->ptr2, req->size, req->offs) |
1925 | : write (req->int1, req->ptr2, req->size); break; |
1816 | : write (req->int1, req->ptr2, req->size); break; |
|
|
1817 | |
|
|
1818 | case EIO_FCNTL: req->result = fcntl (req->int1, (int) req->int2, req->ptr2); break; |
|
|
1819 | case EIO_IOCTL: req->result = ioctl (req->int1, (unsigned long)req->int2, req->ptr2); break; |
1926 | |
1820 | |
1927 | case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
1821 | case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
1928 | case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; |
1822 | case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; |
1929 | |
1823 | |
1930 | #if HAVE_AT |
1824 | #if HAVE_AT |
… | |
… | |
2073 | req->result = select (0, 0, 0, 0, &tv); |
1967 | req->result = select (0, 0, 0, 0, &tv); |
2074 | } |
1968 | } |
2075 | #endif |
1969 | #endif |
2076 | break; |
1970 | break; |
2077 | |
1971 | |
|
|
1972 | #if 0 |
2078 | case EIO_GROUP: |
1973 | case EIO_GROUP: |
2079 | abort (); /* handled in eio_request */ |
1974 | abort (); /* handled in eio_request */ |
|
|
1975 | #endif |
2080 | |
1976 | |
2081 | case EIO_NOP: |
1977 | case EIO_NOP: |
2082 | req->result = 0; |
1978 | req->result = 0; |
2083 | break; |
1979 | break; |
2084 | |
1980 | |
… | |
… | |
2187 | } |
2083 | } |
2188 | |
2084 | |
2189 | eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2085 | eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2190 | { |
2086 | { |
2191 | REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
2087 | REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
|
|
2088 | } |
|
|
2089 | |
|
|
2090 | eio_req *eio_fcntl (int fd, int cmd, void *arg, int pri, eio_cb cb, void *data) |
|
|
2091 | { |
|
|
2092 | REQ (EIO_IOCTL); req->int1 = fd; req->int2 = cmd; req->ptr2 = arg; SEND; |
|
|
2093 | } |
|
|
2094 | |
|
|
2095 | eio_req *eio_ioctl (int fd, unsigned long request, void *buf, int pri, eio_cb cb, void *data) |
|
|
2096 | { |
|
|
2097 | REQ (EIO_IOCTL); req->int1 = fd; req->int2 = request; req->ptr2 = buf; SEND; |
2192 | } |
2098 | } |
2193 | |
2099 | |
2194 | eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) |
2100 | eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) |
2195 | { |
2101 | { |
2196 | REQ (EIO_FSTAT); req->int1 = fd; SEND; |
2102 | REQ (EIO_FSTAT); req->int1 = fd; SEND; |
… | |
… | |
2384 | void |
2290 | void |
2385 | eio_grp_add (eio_req *grp, eio_req *req) |
2291 | eio_grp_add (eio_req *grp, eio_req *req) |
2386 | { |
2292 | { |
2387 | assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); |
2293 | assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); |
2388 | |
2294 | |
2389 | grp->flags |= EIO_FLAG_GROUPADD; |
2295 | grp->flags |= ETP_FLAG_GROUPADD; |
2390 | |
2296 | |
2391 | ++grp->size; |
2297 | ++grp->size; |
2392 | req->grp = grp; |
2298 | req->grp = grp; |
2393 | |
2299 | |
2394 | req->grp_prev = 0; |
2300 | req->grp_prev = 0; |