ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/eio.c
(Generate patch)

Comparing libeio/eio.c (file contents):
Revision 1.126 by root, Fri Dec 28 07:33:41 2012 UTC vs.
Revision 1.159 by root, Thu Oct 29 05:13:56 2020 UTC

1/* 1/*
2 * libeio implementation 2 * libeio implementation
3 * 3 *
4 * Copyright (c) 2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann <libeio@schmorp.de> 4 * Copyright (c) 2007,2008,2009,2010,2011,2012,2013,2016,2017,2018,2019 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>
120 #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1)) 115 #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1))
121 116
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)
120 #define pipe(fds) _pipe (fds, 4096, O_BINARY)
125 121
122 #define fcntl(fd,cmd,arg) EIO_ENOSYS ()
123 #define ioctl(fd,cmd,arg) EIO_ENOSYS ()
126 #define fchmod(fd,mode) EIO_ENOSYS () 124 #define fchmod(fd,mode) EIO_ENOSYS ()
127 #define chown(path,uid,gid) EIO_ENOSYS () 125 #define chown(path,uid,gid) EIO_ENOSYS ()
128 #define fchown(fd,uid,gid) EIO_ENOSYS () 126 #define fchown(fd,uid,gid) EIO_ENOSYS ()
129 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ 127 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */
130 #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ 128 #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */
203 /* we could even stat and see if it exists */ 201 /* we could even stat and see if it exists */
204 static int 202 static int
205 symlink (const char *old, const char *neu) 203 symlink (const char *old, const char *neu)
206 { 204 {
207 #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)
208 if (CreateSymbolicLink (neu, old, 1)) 213 if (CreateSymbolicLink (neu, old, flags))
209 return 0; 214 return 0;
210
211 if (CreateSymbolicLink (neu, old, 0))
212 return 0;
213 #endif 215 #endif
214 216
215 return EIO_ERRNO (ENOENT, -1); 217 return EIO_ERRNO (ENOENT, -1);
216 } 218 }
217 219
218 /* POSIX API only */ 220 /* POSIX API only, causing trouble for win32 apps */
219 #define CreateHardLink(neu,old,flags) 0 221 #define CreateHardLink(neu,old,flags) 0 /* not really creating hardlink, still using relative paths? */
220 #define CreateSymbolicLink(neu,old,flags) 0 222 #define CreateSymbolicLink(neu,old,flags) 0 /* vista+ only */
221 223
222 struct statvfs 224 struct statvfs
223 { 225 {
224 int dummy; 226 int dummy;
225 }; 227 };
229 #define D_NAME(entp) entp.cFileName 231 #define D_NAME(entp) entp.cFileName
230 #define D_TYPE(entp) (entp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? DT_DIR : DT_REG) 232 #define D_TYPE(entp) (entp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? DT_DIR : DT_REG)
231 233
232#else 234#else
233 235
236 #include <sys/ioctl.h>
234 #include <sys/time.h> 237 #include <sys/time.h>
235 #include <sys/select.h> 238 #include <sys/select.h>
236 #include <sys/statvfs.h>
237 #include <unistd.h> 239 #include <unistd.h>
238 #include <signal.h> 240 #include <signal.h>
239 #include <dirent.h> 241 #include <dirent.h>
242
243 #ifdef ANDROID
244 #include <sys/vfs.h>
245 #define statvfs statfs
246 #define fstatvfs fstatfs
247 #include <asm/page.h> /* supposedly limits.h does #define PAGESIZE PAGESIZE */
248 #else
249 #include <sys/statvfs.h>
250 #endif
240 251
241 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES 252 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
242 #include <sys/mman.h> 253 #include <sys/mman.h>
243 #endif 254 #endif
244 255
272# include <utime.h> 283# include <utime.h>
273#endif 284#endif
274 285
275#if HAVE_SYS_SYSCALL_H 286#if HAVE_SYS_SYSCALL_H
276# include <sys/syscall.h> 287# include <sys/syscall.h>
277#endif
278
279#if HAVE_SYS_PRCTL_H
280# include <sys/prctl.h>
281#endif 288#endif
282 289
283#if HAVE_SENDFILE 290#if HAVE_SENDFILE
284# if __linux 291# if __linux
285# include <sys/sendfile.h> 292# include <sys/sendfile.h>
293# else 300# else
294# error sendfile support requested but not available 301# error sendfile support requested but not available
295# endif 302# endif
296#endif 303#endif
297 304
305#if HAVE_RENAMEAT2
306# include <sys/syscall.h>
307# include <linux/fs.h>
308#endif
309
298#ifndef D_TYPE 310#ifndef D_TYPE
299# define D_TYPE(de) 0 311# define D_TYPE(de) 0
300#endif 312#endif
301#ifndef D_INO 313#ifndef D_INO
302# define D_INO(de) 0 314# define D_INO(de) 0
310# define NAME_MAX 4096 322# define NAME_MAX 4096
311#endif 323#endif
312 324
313/* used for readlink etc. */ 325/* used for readlink etc. */
314#ifndef PATH_MAX 326#ifndef PATH_MAX
315# define PATH_MAX 4096 327# define PATH_MAX 0
316#endif 328#endif
329
330#ifndef O_CLOEXEC
331 #define O_CLOEXEC 0
332#endif
333
334#ifndef O_NONBLOCK
335 #define O_NONBLOCK 0
336#endif
337
338#ifndef O_SEARCH
339 #define O_SEARCH O_RDONLY
340#endif
341
342#ifndef O_DIRECTORY
343 #define O_DIRECTORY 0
344#endif
345
346#ifndef EIO_PATH_MIN
347# define EIO_PATH_MIN 8160
348#endif
349
350#define EIO_PATH_MAX (PATH_MAX <= EIO_PATH_MIN ? EIO_PATH_MIN : PATH_MAX)
317 351
318/* buffer size for various temporary buffers */ 352/* buffer size for various temporary buffers */
319#define EIO_BUFSIZE 65536 353#define EIO_BUFSIZE 65536
320 354
321#define dBUF \ 355#define dBUF \
325 return -1 359 return -1
326 360
327#define FUBd \ 361#define FUBd \
328 free (eio_buf) 362 free (eio_buf)
329 363
330#define EIO_TICKS ((1000000 + 1023) >> 10)
331
332/*****************************************************************************/ 364/*****************************************************************************/
333 365
334struct tmpbuf
335{
336 void *ptr;
337 int len;
338};
339
340static void *
341tmpbuf_get (struct tmpbuf *buf, int len)
342{
343 if (buf->len < len)
344 {
345 free (buf->ptr);
346 buf->ptr = malloc (buf->len = len);
347 }
348
349 return buf->ptr;
350}
351
352struct tmpbuf; 366struct etp_tmpbuf;
353 367
354#if _POSIX_VERSION >= 200809L 368#if _POSIX_VERSION >= 200809L
355 #define HAVE_AT 1 369 #define HAVE_AT 1
356 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) 370 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD)
357 #ifndef O_SEARCH
358 #define O_SEARCH O_RDONLY
359 #endif
360#else 371#else
361 #define HAVE_AT 0 372 #define HAVE_AT 0
362 static const char *wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path); 373 static const char *wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path);
363#endif 374#endif
364 375
365struct eio_pwd 376struct eio_pwd
366{ 377{
367#if HAVE_AT 378#if HAVE_AT
374/*****************************************************************************/ 385/*****************************************************************************/
375 386
376#define ETP_PRI_MIN EIO_PRI_MIN 387#define ETP_PRI_MIN EIO_PRI_MIN
377#define ETP_PRI_MAX EIO_PRI_MAX 388#define ETP_PRI_MAX EIO_PRI_MAX
378 389
390#define ETP_TYPE_QUIT -1
391#define ETP_TYPE_GROUP EIO_GROUP
392
393static void eio_nop_callback (void) { }
394static void (*eio_want_poll_cb)(void) = eio_nop_callback;
395static void (*eio_done_poll_cb)(void) = eio_nop_callback;
396
397#define ETP_WANT_POLL(pool) eio_want_poll_cb ()
398#define ETP_DONE_POLL(pool) eio_done_poll_cb ()
399
379struct etp_worker; 400struct etp_worker;
380
381#define ETP_REQ eio_req 401#define ETP_REQ eio_req
382#define ETP_DESTROY(req) eio_destroy (req) 402#define ETP_DESTROY(req) eio_destroy (req)
383static int eio_finish (eio_req *req); 403static int eio_finish (eio_req *req);
384#define ETP_FINISH(req) eio_finish (req) 404#define ETP_FINISH(req) eio_finish (req)
385static void eio_execute (struct etp_worker *self, eio_req *req); 405static void eio_execute (struct etp_worker *self, eio_req *req);
386#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) 406#define ETP_EXECUTE(wrk,req) eio_execute (wrk, req)
387 407
388/*****************************************************************************/ 408#include "etp.c"
389 409
390#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 410static struct etp_pool eio_pool;
391 411#define EIO_POOL (&eio_pool)
392/* calculate time difference in ~1/EIO_TICKS of a second */
393ecb_inline int
394tvdiff (struct timeval *tv1, struct timeval *tv2)
395{
396 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
397 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
398}
399
400static unsigned int started, idle, wanted = 4;
401
402static void (*want_poll_cb) (void);
403static void (*done_poll_cb) (void);
404
405static unsigned int max_poll_time; /* reslock */
406static unsigned int max_poll_reqs; /* reslock */
407
408static unsigned int nreqs; /* reqlock */
409static unsigned int nready; /* reqlock */
410static unsigned int npending; /* reqlock */
411static unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */
412static unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */
413
414static xmutex_t wrklock;
415static xmutex_t reslock;
416static xmutex_t reqlock;
417static xcond_t reqwait;
418
419typedef struct etp_worker
420{
421 struct tmpbuf tmpbuf;
422
423 /* locked by wrklock */
424 struct etp_worker *prev, *next;
425
426 xthread_t tid;
427
428#ifdef ETP_WORKER_COMMON
429 ETP_WORKER_COMMON
430#endif
431} etp_worker;
432
433static etp_worker wrk_first; /* NOT etp */
434
435#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
436#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
437
438/* worker threads management */
439
440static void
441etp_worker_clear (etp_worker *wrk)
442{
443}
444
445static void ecb_cold
446etp_worker_free (etp_worker *wrk)
447{
448 free (wrk->tmpbuf.ptr);
449
450 wrk->next->prev = wrk->prev;
451 wrk->prev->next = wrk->next;
452
453 free (wrk);
454}
455
456static unsigned int
457etp_nreqs (void)
458{
459 int retval;
460 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
461 retval = nreqs;
462 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
463 return retval;
464}
465
466static unsigned int
467etp_nready (void)
468{
469 unsigned int retval;
470
471 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
472 retval = nready;
473 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
474
475 return retval;
476}
477
478static unsigned int
479etp_npending (void)
480{
481 unsigned int retval;
482
483 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
484 retval = npending;
485 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
486
487 return retval;
488}
489
490static unsigned int
491etp_nthreads (void)
492{
493 unsigned int retval;
494
495 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
496 retval = started;
497 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
498
499 return retval;
500}
501
502/*
503 * a somewhat faster data structure might be nice, but
504 * with 8 priorities this actually needs <20 insns
505 * per shift, the most expensive operation.
506 */
507typedef struct {
508 ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */
509 int size;
510} etp_reqq;
511
512static etp_reqq req_queue;
513static etp_reqq res_queue;
514
515static void ecb_noinline ecb_cold
516reqq_init (etp_reqq *q)
517{
518 int pri;
519
520 for (pri = 0; pri < ETP_NUM_PRI; ++pri)
521 q->qs[pri] = q->qe[pri] = 0;
522
523 q->size = 0;
524}
525
526static int ecb_noinline
527reqq_push (etp_reqq *q, ETP_REQ *req)
528{
529 int pri = req->pri;
530 req->next = 0;
531
532 if (q->qe[pri])
533 {
534 q->qe[pri]->next = req;
535 q->qe[pri] = req;
536 }
537 else
538 q->qe[pri] = q->qs[pri] = req;
539
540 return q->size++;
541}
542
543static ETP_REQ * ecb_noinline
544reqq_shift (etp_reqq *q)
545{
546 int pri;
547
548 if (!q->size)
549 return 0;
550
551 --q->size;
552
553 for (pri = ETP_NUM_PRI; pri--; )
554 {
555 eio_req *req = q->qs[pri];
556
557 if (req)
558 {
559 if (!(q->qs[pri] = (eio_req *)req->next))
560 q->qe[pri] = 0;
561
562 return req;
563 }
564 }
565
566 abort ();
567}
568
569static int ecb_cold
570etp_init (void (*want_poll)(void), void (*done_poll)(void))
571{
572 X_MUTEX_CREATE (wrklock);
573 X_MUTEX_CREATE (reslock);
574 X_MUTEX_CREATE (reqlock);
575 X_COND_CREATE (reqwait);
576
577 reqq_init (&req_queue);
578 reqq_init (&res_queue);
579
580 wrk_first.next =
581 wrk_first.prev = &wrk_first;
582
583 started = 0;
584 idle = 0;
585 nreqs = 0;
586 nready = 0;
587 npending = 0;
588
589 want_poll_cb = want_poll;
590 done_poll_cb = done_poll;
591
592 return 0;
593}
594
595X_THREAD_PROC (etp_proc);
596
597static void ecb_cold
598etp_start_thread (void)
599{
600 etp_worker *wrk = calloc (1, sizeof (etp_worker));
601
602 /*TODO*/
603 assert (("unable to allocate worker thread data", wrk));
604
605 X_LOCK (wrklock);
606
607 if (xthread_create (&wrk->tid, etp_proc, (void *)wrk))
608 {
609 wrk->prev = &wrk_first;
610 wrk->next = wrk_first.next;
611 wrk_first.next->prev = wrk;
612 wrk_first.next = wrk;
613 ++started;
614 }
615 else
616 free (wrk);
617
618 X_UNLOCK (wrklock);
619}
620
621static void
622etp_maybe_start_thread (void)
623{
624 if (ecb_expect_true (etp_nthreads () >= wanted))
625 return;
626
627 /* todo: maybe use idle here, but might be less exact */
628 if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()))
629 return;
630
631 etp_start_thread ();
632}
633
634static void ecb_cold
635etp_end_thread (void)
636{
637 eio_req *req = calloc (1, sizeof (eio_req)); /* will be freed by worker */
638
639 req->type = -1;
640 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
641
642 X_LOCK (reqlock);
643 reqq_push (&req_queue, req);
644 X_COND_SIGNAL (reqwait);
645 X_UNLOCK (reqlock);
646
647 X_LOCK (wrklock);
648 --started;
649 X_UNLOCK (wrklock);
650}
651
652static int
653etp_poll (void)
654{
655 unsigned int maxreqs;
656 unsigned int maxtime;
657 struct timeval tv_start, tv_now;
658
659 X_LOCK (reslock);
660 maxreqs = max_poll_reqs;
661 maxtime = max_poll_time;
662 X_UNLOCK (reslock);
663
664 if (maxtime)
665 gettimeofday (&tv_start, 0);
666
667 for (;;)
668 {
669 ETP_REQ *req;
670
671 etp_maybe_start_thread ();
672
673 X_LOCK (reslock);
674 req = reqq_shift (&res_queue);
675
676 if (req)
677 {
678 --npending;
679
680 if (!res_queue.size && done_poll_cb)
681 done_poll_cb ();
682 }
683
684 X_UNLOCK (reslock);
685
686 if (!req)
687 return 0;
688
689 X_LOCK (reqlock);
690 --nreqs;
691 X_UNLOCK (reqlock);
692
693 if (ecb_expect_false (req->type == EIO_GROUP && req->size))
694 {
695 req->int1 = 1; /* mark request as delayed */
696 continue;
697 }
698 else
699 {
700 int res = ETP_FINISH (req);
701 if (ecb_expect_false (res))
702 return res;
703 }
704
705 if (ecb_expect_false (maxreqs && !--maxreqs))
706 break;
707
708 if (maxtime)
709 {
710 gettimeofday (&tv_now, 0);
711
712 if (tvdiff (&tv_start, &tv_now) >= maxtime)
713 break;
714 }
715 }
716
717 errno = EAGAIN;
718 return -1;
719}
720
721static void
722etp_cancel (ETP_REQ *req)
723{
724 req->cancelled = 1;
725
726 eio_grp_cancel (req);
727}
728
729static void
730etp_submit (ETP_REQ *req)
731{
732 req->pri -= ETP_PRI_MIN;
733
734 if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN;
735 if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
736
737 if (ecb_expect_false (req->type == EIO_GROUP))
738 {
739 /* I hope this is worth it :/ */
740 X_LOCK (reqlock);
741 ++nreqs;
742 X_UNLOCK (reqlock);
743
744 X_LOCK (reslock);
745
746 ++npending;
747
748 if (!reqq_push (&res_queue, req) && want_poll_cb)
749 want_poll_cb ();
750
751 X_UNLOCK (reslock);
752 }
753 else
754 {
755 X_LOCK (reqlock);
756 ++nreqs;
757 ++nready;
758 reqq_push (&req_queue, req);
759 X_COND_SIGNAL (reqwait);
760 X_UNLOCK (reqlock);
761
762 etp_maybe_start_thread ();
763 }
764}
765
766static void ecb_cold
767etp_set_max_poll_time (double nseconds)
768{
769 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
770 max_poll_time = nseconds * EIO_TICKS;
771 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
772}
773
774static void ecb_cold
775etp_set_max_poll_reqs (unsigned int maxreqs)
776{
777 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
778 max_poll_reqs = maxreqs;
779 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
780}
781
782static void ecb_cold
783etp_set_max_idle (unsigned int nthreads)
784{
785 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
786 max_idle = nthreads;
787 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
788}
789
790static void ecb_cold
791etp_set_idle_timeout (unsigned int seconds)
792{
793 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
794 idle_timeout = seconds;
795 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
796}
797
798static void ecb_cold
799etp_set_min_parallel (unsigned int nthreads)
800{
801 if (wanted < nthreads)
802 wanted = nthreads;
803}
804
805static void ecb_cold
806etp_set_max_parallel (unsigned int nthreads)
807{
808 if (wanted > nthreads)
809 wanted = nthreads;
810
811 while (started > wanted)
812 etp_end_thread ();
813}
814 412
815/*****************************************************************************/ 413/*****************************************************************************/
816 414
817static void 415static void
818grp_try_feed (eio_req *grp) 416grp_try_feed (eio_req *grp)
819{ 417{
820 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 418 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
821 { 419 {
822 grp->flags &= ~EIO_FLAG_GROUPADD; 420 grp->flags &= ~ETP_FLAG_GROUPADD;
823 421
824 EIO_FEED (grp); 422 EIO_FEED (grp);
825 423
826 /* stop if no progress has been made */ 424 /* stop if no progress has been made */
827 if (!(grp->flags & EIO_FLAG_GROUPADD)) 425 if (!(grp->flags & ETP_FLAG_GROUPADD))
828 { 426 {
829 grp->feed = 0; 427 grp->feed = 0;
830 break; 428 break;
831 } 429 }
832 } 430 }
839 437
840 /* call feeder, if applicable */ 438 /* call feeder, if applicable */
841 grp_try_feed (grp); 439 grp_try_feed (grp);
842 440
843 /* finish, if done */ 441 /* finish, if done */
844 if (!grp->size && grp->int1) 442 if (!grp->size && grp->flags & ETP_FLAG_DELAYED)
845 return eio_finish (grp); 443 return eio_finish (grp);
846 else 444 else
847 return 0; 445 return 0;
848} 446}
849 447
885} 483}
886 484
887void 485void
888eio_grp_cancel (eio_req *grp) 486eio_grp_cancel (eio_req *grp)
889{ 487{
890 for (grp = grp->grp_first; grp; grp = grp->grp_next) 488 etp_grp_cancel (EIO_POOL, grp);
891 eio_cancel (grp);
892} 489}
893 490
894void 491void
895eio_cancel (eio_req *req) 492eio_cancel (eio_req *req)
896{ 493{
897 etp_cancel (req); 494 etp_cancel (EIO_POOL, req);
898} 495}
899 496
900void 497void
901eio_submit (eio_req *req) 498eio_submit (eio_req *req)
902{ 499{
903 etp_submit (req); 500 etp_submit (EIO_POOL, req);
904} 501}
905 502
906unsigned int 503unsigned int
907eio_nreqs (void) 504eio_nreqs (void)
908{ 505{
909 return etp_nreqs (); 506 return etp_nreqs (EIO_POOL);
910} 507}
911 508
912unsigned int 509unsigned int
913eio_nready (void) 510eio_nready (void)
914{ 511{
915 return etp_nready (); 512 return etp_nready (EIO_POOL);
916} 513}
917 514
918unsigned int 515unsigned int
919eio_npending (void) 516eio_npending (void)
920{ 517{
921 return etp_npending (); 518 return etp_npending (EIO_POOL);
922} 519}
923 520
924unsigned int ecb_cold 521unsigned int ecb_cold
925eio_nthreads (void) 522eio_nthreads (void)
926{ 523{
927 return etp_nthreads (); 524 return etp_nthreads (EIO_POOL);
928} 525}
929 526
930void ecb_cold 527void ecb_cold
931eio_set_max_poll_time (double nseconds) 528eio_set_max_poll_time (double nseconds)
932{ 529{
933 etp_set_max_poll_time (nseconds); 530 etp_set_max_poll_time (EIO_POOL, nseconds);
934} 531}
935 532
936void ecb_cold 533void ecb_cold
937eio_set_max_poll_reqs (unsigned int maxreqs) 534eio_set_max_poll_reqs (unsigned int maxreqs)
938{ 535{
939 etp_set_max_poll_reqs (maxreqs); 536 etp_set_max_poll_reqs (EIO_POOL, maxreqs);
940} 537}
941 538
942void ecb_cold 539void ecb_cold
943eio_set_max_idle (unsigned int nthreads) 540eio_set_max_idle (unsigned int nthreads)
944{ 541{
945 etp_set_max_idle (nthreads); 542 etp_set_max_idle (EIO_POOL, nthreads);
946} 543}
947 544
948void ecb_cold 545void ecb_cold
949eio_set_idle_timeout (unsigned int seconds) 546eio_set_idle_timeout (unsigned int seconds)
950{ 547{
951 etp_set_idle_timeout (seconds); 548 etp_set_idle_timeout (EIO_POOL, seconds);
952} 549}
953 550
954void ecb_cold 551void ecb_cold
955eio_set_min_parallel (unsigned int nthreads) 552eio_set_min_parallel (unsigned int nthreads)
956{ 553{
957 etp_set_min_parallel (nthreads); 554 etp_set_min_parallel (EIO_POOL, nthreads);
958} 555}
959 556
960void ecb_cold 557void ecb_cold
961eio_set_max_parallel (unsigned int nthreads) 558eio_set_max_parallel (unsigned int nthreads)
962{ 559{
963 etp_set_max_parallel (nthreads); 560 etp_set_max_parallel (EIO_POOL, nthreads);
964} 561}
965 562
966int eio_poll (void) 563int eio_poll (void)
967{ 564{
968 return etp_poll (); 565 return etp_poll (EIO_POOL);
969} 566}
970 567
971/*****************************************************************************/ 568/*****************************************************************************/
972/* work around various missing functions */ 569/* work around various missing functions */
570
571#if HAVE_POSIX_CLOSE && !__linux
572# define eio__close(fd) posix_close (fd, 0)
573#else
574# define eio__close(fd) close (fd)
575#endif
576
577/* close() without disturbing errno */
578static void
579silent_close (int fd)
580{
581 int saved_errno = errno;
582 eio__close (fd);
583 errno = saved_errno;
584}
973 585
974#ifndef HAVE_UTIMES 586#ifndef HAVE_UTIMES
975 587
976# undef utimes 588# undef utimes
977# define utimes(path,times) eio__utimes (path, times) 589# define utimes(path,times) eio__utimes (path, times)
1290 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 902 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1291 extern int mallopt (int, int); 903 extern int mallopt (int, int);
1292 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ 904 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1293 #endif 905 #endif
1294 906
907 #ifndef MCL_ONFAULT
908 if (flags & EIO_MCL_ONFAULT)
909 return EIO_ERRNO (EINVAL, -1);
910 #define MCL_ONFAULT 4
911 #endif
912
1295 if (EIO_MCL_CURRENT != MCL_CURRENT 913 if (EIO_MCL_CURRENT != MCL_CURRENT
1296 || EIO_MCL_FUTURE != MCL_FUTURE) 914 || EIO_MCL_FUTURE != MCL_FUTURE
915 || EIO_MCL_ONFAULT != MCL_ONFAULT)
1297 { 916 {
1298 flags = 0 917 flags = 0
1299 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0) 918 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0)
1300 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0); 919 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0)
920 | (flags & EIO_MCL_ONFAULT ? MCL_ONFAULT : 0)
921 ;
1301 } 922 }
1302 923
1303 return mlockall (flags); 924 return mlockall (flags);
1304} 925}
1305#endif 926#endif
1356 intptr_t end = addr + len; 977 intptr_t end = addr + len;
1357 intptr_t page = eio_pagesize (); 978 intptr_t page = eio_pagesize ();
1358 979
1359 if (addr < end) 980 if (addr < end)
1360 if (flags & EIO_MT_MODIFY) /* modify */ 981 if (flags & EIO_MT_MODIFY) /* modify */
1361 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req)); 982 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < end && !EIO_CANCELLED (req));
1362 else 983 else
1363 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req)); 984 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < end && !EIO_CANCELLED (req));
1364 } 985 }
1365 986
1366 return 0; 987 return 0;
1367} 988}
1368 989
1383 req->result = req->offs == (off_t)-1 ? -1 : 0; 1004 req->result = req->offs == (off_t)-1 ? -1 : 0;
1384} 1005}
1385 1006
1386/* result will always end up in tmpbuf, there is always space for adding a 0-byte */ 1007/* result will always end up in tmpbuf, there is always space for adding a 0-byte */
1387static int 1008static int
1388eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1009eio__realpath (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
1389{ 1010{
1011 char *res;
1390 const char *rel = path; 1012 const char *rel = path;
1391 char *res;
1392 char *tmp1, *tmp2; 1013 char *tmp1, *tmp2;
1393#if SYMLOOP_MAX > 32 1014#if SYMLOOP_MAX > 32
1394 int symlinks = SYMLOOP_MAX; 1015 int symlinks = SYMLOOP_MAX;
1395#else 1016#else
1396 int symlinks = 32; 1017 int symlinks = 32;
1402 1023
1403 errno = ENOENT; 1024 errno = ENOENT;
1404 if (!*rel) 1025 if (!*rel)
1405 return -1; 1026 return -1;
1406 1027
1407 res = tmpbuf_get (tmpbuf, PATH_MAX * 3); 1028 res = etp_tmpbuf_get (tmpbuf, EIO_PATH_MAX * 3);
1029#ifdef _WIN32
1030 if (_access (rel, 4) != 0)
1031 return -1;
1032
1033 symlinks = GetFullPathName (rel, EIO_PATH_MAX * 3, res, 0);
1034
1035 errno = ENAMETOOLONG;
1036 if (symlinks >= EIO_PATH_MAX * 3)
1037 return -1;
1038
1039 errno = EIO;
1040 if (symlinks <= 0)
1041 return -1;
1042
1043 return symlinks;
1044
1045#else
1408 tmp1 = res + PATH_MAX; 1046 tmp1 = res + EIO_PATH_MAX;
1409 tmp2 = tmp1 + PATH_MAX; 1047 tmp2 = tmp1 + EIO_PATH_MAX;
1410 1048
1411#if 0 /* disabled, the musl way to do things is just too racy */ 1049#if 0 /* disabled, the musl way to do things is just too racy */
1412#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) 1050#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1413 /* on linux we may be able to ask the kernel */ 1051 /* on linux we may be able to ask the kernel */
1414 { 1052 {
1415 int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); 1053 int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME);
1416 1054
1417 if (fd >= 0) 1055 if (fd >= 0)
1418 { 1056 {
1419 sprintf (tmp1, "/proc/self/fd/%d", fd); 1057 sprintf (tmp1, "/proc/self/fd/%d", fd);
1420 req->result = readlink (tmp1, res, PATH_MAX); 1058 req->result = readlink (tmp1, res, EIO_PATH_MAX);
1421 /* here we should probably stat the open file and the disk file, to make sure they still match */ 1059 /* here we should probably stat the open file and the disk file, to make sure they still match */
1422 close (fd); 1060 eio__close (fd);
1423 1061
1424 if (req->result > 0) 1062 if (req->result > 0)
1425 goto done; 1063 goto done;
1426 } 1064 }
1427 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) 1065 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO)
1438 if (wd == EIO_INVALID_WD) 1076 if (wd == EIO_INVALID_WD)
1439 return -1; 1077 return -1;
1440 1078
1441 if (wd == EIO_CWD) 1079 if (wd == EIO_CWD)
1442 { 1080 {
1443 if (!getcwd (res, PATH_MAX)) 1081 if (!getcwd (res, EIO_PATH_MAX))
1444 return -1; 1082 return -1;
1445 1083
1446 len = strlen (res); 1084 len = strlen (res);
1447 } 1085 }
1448 else 1086 else
1495 1133
1496 /* zero-terminate, for readlink */ 1134 /* zero-terminate, for readlink */
1497 res [len + 1] = 0; 1135 res [len + 1] = 0;
1498 1136
1499 /* now check if it's a symlink */ 1137 /* now check if it's a symlink */
1500 linklen = readlink (tmpbuf->ptr, tmp1, PATH_MAX); 1138 linklen = readlink (tmpbuf->ptr, tmp1, EIO_PATH_MAX);
1501 1139
1502 if (linklen < 0) 1140 if (linklen < 0)
1503 { 1141 {
1504 if (errno != EINVAL) 1142 if (errno != EINVAL)
1505 return -1; 1143 return -1;
1511 { 1149 {
1512 /* yay, it was a symlink - build new path in tmp2 */ 1150 /* yay, it was a symlink - build new path in tmp2 */
1513 int rellen = strlen (rel); 1151 int rellen = strlen (rel);
1514 1152
1515 errno = ENAMETOOLONG; 1153 errno = ENAMETOOLONG;
1516 if (linklen + 1 + rellen >= PATH_MAX) 1154 if (linklen + 1 + rellen >= EIO_PATH_MAX) /* also catch linklen >= EIO_PATH_MAX */
1517 return -1; 1155 return -1;
1518 1156
1519 errno = ELOOP; 1157 errno = ELOOP;
1520 if (!--symlinks) 1158 if (!--symlinks)
1521 return -1; 1159 return -1;
1535 /* special case for the lone root path */ 1173 /* special case for the lone root path */
1536 if (res == tmpbuf->ptr) 1174 if (res == tmpbuf->ptr)
1537 *res++ = '/'; 1175 *res++ = '/';
1538 1176
1539 return res - (char *)tmpbuf->ptr; 1177 return res - (char *)tmpbuf->ptr;
1178#endif
1540} 1179}
1541 1180
1542static signed char 1181static signed char
1543eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1182eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1544{ 1183{
1774 return; 1413 return;
1775 } 1414 }
1776 } 1415 }
1777#else 1416#else
1778 #if HAVE_AT 1417 #if HAVE_AT
1779 if (req->wd)
1780 { 1418 {
1781 int fd = openat (WD2FD (req->wd), req->ptr1, O_CLOEXEC | O_SEARCH | O_DIRECTORY); 1419 int fd = openat (WD2FD (req->wd), req->ptr1, O_CLOEXEC | O_SEARCH | O_DIRECTORY | O_NONBLOCK);
1782 1420
1783 if (fd < 0) 1421 if (fd < 0)
1422 return;
1423
1424 dirp = fdopendir (fd);
1425
1426 if (!dirp)
1427 {
1428 silent_close (fd);
1784 return; 1429 return;
1785
1786 dirp = fdopendir (fd);
1787
1788 if (!dirp)
1789 close (fd);
1790 } 1430 }
1791 else 1431 }
1792 dirp = opendir (req->ptr1);
1793 #else 1432 #else
1794 dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1)); 1433 dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1));
1434
1435 if (!dirp)
1436 return;
1795 #endif 1437 #endif
1796
1797 if (!dirp)
1798 return;
1799#endif 1438#endif
1800 1439
1801 if (req->flags & EIO_FLAG_PTR1_FREE) 1440 if (req->flags & EIO_FLAG_PTR1_FREE)
1802 free (req->ptr1); 1441 free (req->ptr1);
1803 1442
2000/* keep the absolute path in string form at all times */ 1639/* keep the absolute path in string form at all times */
2001/* fuck yeah. */ 1640/* fuck yeah. */
2002 1641
2003#if !HAVE_AT 1642#if !HAVE_AT
2004 1643
2005/* a bit like realpath, but usually faster because it doesn'T have to return */ 1644/* a bit like realpath, but usually faster because it doesn't have to return */
2006/* an absolute or canonical path */ 1645/* an absolute or canonical path */
2007static const char * 1646static const char *
2008wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1647wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
2009{ 1648{
2010 if (!wd || *path == '/') 1649 if (!wd || *path == '/')
2011 return path; 1650 return path;
2012 1651
2013 if (path [0] == '.' && !path [1]) 1652 if (path [0] == '.' && !path [1])
2015 1654
2016 { 1655 {
2017 int l1 = wd->len; 1656 int l1 = wd->len;
2018 int l2 = strlen (path); 1657 int l2 = strlen (path);
2019 1658
2020 char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2); 1659 char *res = etp_tmpbuf_get (tmpbuf, l1 + l2 + 2);
2021 1660
2022 memcpy (res, wd->str, l1); 1661 memcpy (res, wd->str, l1);
2023 res [l1] = '/'; 1662 res [l1] = '/';
2024 memcpy (res + l1 + 1, path, l2 + 1); 1663 memcpy (res + l1 + 1, path, l2 + 1);
2025 1664
2028} 1667}
2029 1668
2030#endif 1669#endif
2031 1670
2032static eio_wd 1671static eio_wd
2033eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1672eio__wd_open_sync (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
2034{ 1673{
2035 int fd; 1674 int fd;
2036 eio_wd res; 1675 eio_wd res;
2037 int len = eio__realpath (tmpbuf, wd, path); 1676 int len = eio__realpath (tmpbuf, wd, path);
2038 1677
2039 if (len < 0) 1678 if (len < 0)
2040 return EIO_INVALID_WD; 1679 return EIO_INVALID_WD;
2041 1680
2042#if HAVE_AT 1681#if HAVE_AT
2043 fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY); 1682 fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY | O_NONBLOCK);
1683
1684 /* 0 is a valid fd, but we use it for EIO_CWD, so in the very unlikely */
1685 /* case of fd 0 being available (almost certainly an a pplication bug) */
1686 /* make sure we use another fd value */
1687 #if EIO_CWD
1688 error EIO_CWD must be 0
1689 #endif
1690 if (ecb_expect_false (fd == 0))
1691 {
1692 int fd2 = fcntl (fd, F_DUPFD_CLOEXEC ? F_DUPFD_CLOEXEC : F_DUPFD);
1693 fcntl (fd2, F_SETFD, FD_CLOEXEC);
1694 eio__close (fd);
1695 fd = fd2;
1696 }
2044 1697
2045 if (fd < 0) 1698 if (fd < 0)
2046 return EIO_INVALID_WD; 1699 return EIO_INVALID_WD;
2047#endif 1700#endif
2048 1701
2060} 1713}
2061 1714
2062eio_wd 1715eio_wd
2063eio_wd_open_sync (eio_wd wd, const char *path) 1716eio_wd_open_sync (eio_wd wd, const char *path)
2064{ 1717{
2065 struct tmpbuf tmpbuf = { 0 }; 1718 struct etp_tmpbuf tmpbuf = { 0 };
2066 wd = eio__wd_open_sync (&tmpbuf, wd, path); 1719 wd = eio__wd_open_sync (&tmpbuf, wd, path);
2067 free (tmpbuf.ptr); 1720 free (tmpbuf.ptr);
2068 1721
2069 return wd; 1722 return wd;
2070} 1723}
2073eio_wd_close_sync (eio_wd wd) 1726eio_wd_close_sync (eio_wd wd)
2074{ 1727{
2075 if (wd != EIO_INVALID_WD && wd != EIO_CWD) 1728 if (wd != EIO_INVALID_WD && wd != EIO_CWD)
2076 { 1729 {
2077 #if HAVE_AT 1730 #if HAVE_AT
2078 close (wd->fd); 1731 eio__close (wd->fd);
2079 #endif 1732 #endif
2080 free (wd); 1733 free (wd);
2081 } 1734 }
2082} 1735}
2083 1736
2084#if HAVE_AT 1737#if HAVE_AT
2085 1738
1739static int
1740eio__renameat2 (int olddirfd, const char *oldpath, int newdirfd, const char *newpath, unsigned int flags)
1741{
1742#if HAVE_RENAMEAT2
1743 return syscall (SYS_renameat2, olddirfd, oldpath, newdirfd, newpath, flags);
1744#else
1745 if (flags)
1746 return EIO_ENOSYS ();
1747
1748 return renameat (olddirfd, oldpath, newdirfd, newpath);
1749#endif
1750}
1751
2086/* they forgot these */ 1752/* they forgot these */
2087 1753
2088static int 1754static int
2089eio__truncateat (int dirfd, const char *path, off_t length) 1755eio__truncateat (int dirfd, const char *path, off_t length)
2090{ 1756{
2091 int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC); 1757 int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC | O_NONBLOCK);
2092 int res; 1758 int res;
2093 1759
2094 if (fd < 0) 1760 if (fd < 0)
2095 return fd; 1761 return fd;
2096 1762
2097 res = ftruncate (fd, length); 1763 res = ftruncate (fd, length);
2098 close (fd); 1764 silent_close (fd);
2099 return res; 1765 return res;
2100} 1766}
2101 1767
2102static int 1768static int
2103eio__statvfsat (int dirfd, const char *path, struct statvfs *buf) 1769eio__statvfsat (int dirfd, const char *path, struct statvfs *buf)
2104{ 1770{
2105 int fd = openat (dirfd, path, O_SEARCH | O_CLOEXEC); 1771 int fd = openat (dirfd, path, O_SEARCH | O_CLOEXEC | O_NONBLOCK);
2106 int res; 1772 int res;
2107 1773
2108 if (fd < 0) 1774 if (fd < 0)
2109 return fd; 1775 return fd;
2110 1776
2111 res = fstatvfs (fd, buf); 1777 res = fstatvfs (fd, buf);
2112 close (fd); 1778 silent_close (fd);
2113 return res; 1779 return res;
2114
2115} 1780}
2116 1781
2117#endif 1782#endif
2118 1783
2119/*****************************************************************************/ 1784/*****************************************************************************/
2120 1785
2121#define ALLOC(len) \ 1786#define ALLOC(len) \
2122 if (!req->ptr2) \ 1787 if (!req->ptr2) \
2123 { \ 1788 { \
2124 X_LOCK (wrklock); \ 1789 X_LOCK (EIO_POOL->wrklock); \
2125 req->flags |= EIO_FLAG_PTR2_FREE; \ 1790 req->flags |= EIO_FLAG_PTR2_FREE; \
2126 X_UNLOCK (wrklock); \ 1791 X_UNLOCK (EIO_POOL->wrklock); \
2127 req->ptr2 = malloc (len); \ 1792 req->ptr2 = malloc (len); \
2128 if (!req->ptr2) \ 1793 if (!req->ptr2) \
2129 { \ 1794 { \
2130 errno = ENOMEM; \ 1795 errno = ENOMEM; \
2131 req->result = -1; \ 1796 req->result = -1; \
2132 break; \ 1797 goto alloc_fail; \
2133 } \ 1798 } \
2134 } 1799 }
2135 1800
2136static void ecb_noinline ecb_cold 1801/*****************************************************************************/
2137etp_proc_init (void)
2138{
2139#if HAVE_PRCTL_SET_NAME
2140 /* provide a more sensible "thread name" */
2141 char name[16 + 1];
2142 const int namelen = sizeof (name) - 1;
2143 int len;
2144 1802
2145 prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0); 1803static void
2146 name [namelen] = 0; 1804eio__slurp (int fd, eio_req *req)
2147 len = strlen (name);
2148 strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio");
2149 prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0);
2150#endif
2151}
2152
2153X_THREAD_PROC (etp_proc)
2154{ 1805{
2155 ETP_REQ *req; 1806 req->result = fd;
2156 struct timespec ts;
2157 etp_worker *self = (etp_worker *)thr_arg;
2158 1807
2159 etp_proc_init (); 1808 if (fd < 0)
1809 return;
2160 1810
2161 /* try to distribute timeouts somewhat evenly */ 1811 if (req->offs < 0 || !req->size) /* do we need the size? */
2162 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
2163
2164 for (;;)
2165 { 1812 {
2166 ts.tv_sec = 0; 1813 off_t size = lseek (fd, 0, SEEK_END);
2167 1814
2168 X_LOCK (reqlock);
2169
2170 for (;;)
2171 {
2172 req = reqq_shift (&req_queue);
2173
2174 if (req)
2175 break;
2176
2177 if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */
2178 {
2179 X_UNLOCK (reqlock);
2180 X_LOCK (wrklock);
2181 --started;
2182 X_UNLOCK (wrklock);
2183 goto quit;
2184 }
2185
2186 ++idle;
2187
2188 if (idle <= max_idle)
2189 /* we are allowed to idle, so do so without any timeout */
2190 X_COND_WAIT (reqwait, reqlock);
2191 else
2192 {
2193 /* initialise timeout once */
2194 if (!ts.tv_sec)
2195 ts.tv_sec = time (0) + idle_timeout;
2196
2197 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
2198 ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */
2199 }
2200
2201 --idle;
2202 }
2203
2204 --nready;
2205
2206 X_UNLOCK (reqlock);
2207
2208 if (req->type < 0) 1815 if (req->offs < 0)
2209 goto quit; 1816 req->offs += size;
2210 1817
2211 ETP_EXECUTE (self, req); 1818 if (!req->size)
2212 1819 req->size = size - req->offs;
2213 X_LOCK (reslock);
2214
2215 ++npending;
2216
2217 if (!reqq_push (&res_queue, req) && want_poll_cb)
2218 want_poll_cb ();
2219
2220 etp_worker_clear (self);
2221
2222 X_UNLOCK (reslock);
2223 } 1820 }
2224 1821
2225quit: 1822 ALLOC (req->size);
2226 free (req); 1823 req->result = pread (fd, req->ptr2, req->size, req->offs);
2227 1824
2228 X_LOCK (wrklock); 1825 silent_close (fd);
2229 etp_worker_free (self);
2230 X_UNLOCK (wrklock);
2231 1826
2232 return 0; 1827alloc_fail:
1828 ;
2233} 1829}
2234
2235/*****************************************************************************/
2236 1830
2237int ecb_cold 1831int ecb_cold
2238eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1832eio_init (void (*want_poll)(void), void (*done_poll)(void))
2239{ 1833{
2240 return etp_init (want_poll, done_poll); 1834 eio_want_poll_cb = want_poll;
1835 eio_done_poll_cb = done_poll;
1836
1837 return etp_init (EIO_POOL, 0, 0, 0);
2241} 1838}
2242 1839
2243ecb_inline void 1840ecb_inline void
2244eio_api_destroy (eio_req *req) 1841eio_api_destroy (eio_req *req)
2245{ 1842{
2319 : read (req->int1, req->ptr2, req->size); break; 1916 : read (req->int1, req->ptr2, req->size); break;
2320 case EIO_WRITE: req->result = req->offs >= 0 1917 case EIO_WRITE: req->result = req->offs >= 0
2321 ? pwrite (req->int1, req->ptr2, req->size, req->offs) 1918 ? pwrite (req->int1, req->ptr2, req->size, req->offs)
2322 : write (req->int1, req->ptr2, req->size); break; 1919 : write (req->int1, req->ptr2, req->size); break;
2323 1920
1921 case EIO_FCNTL: req->result = fcntl (req->int1, (int) req->int2, req->ptr2); break;
1922 case EIO_IOCTL: req->result = ioctl (req->int1, (unsigned long)req->int2, req->ptr2); break;
1923
2324 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; 1924 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
2325 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; 1925 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break;
2326 1926
2327#if HAVE_AT 1927#if HAVE_AT
2328 1928
2332 req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, AT_SYMLINK_NOFOLLOW); break; 1932 req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, AT_SYMLINK_NOFOLLOW); break;
2333 case EIO_CHOWN: req->result = fchownat (dirfd, req->ptr1, req->int2, req->int3, 0); break; 1933 case EIO_CHOWN: req->result = fchownat (dirfd, req->ptr1, req->int2, req->int3, 0); break;
2334 case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break; 1934 case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break;
2335 case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break; 1935 case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break;
2336 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break; 1936 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break;
1937 case EIO_SLURP: eio__slurp ( openat (dirfd, req->ptr1, O_RDONLY | O_CLOEXEC), req); break;
2337 1938
2338 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; 1939 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break;
2339 case EIO_RMDIR: /* complications arise because "." cannot be removed, so we might have to expand */ 1940 case EIO_RMDIR: /* complications arise because "." cannot be removed, so we might have to expand */
2340 req->result = req->wd && SINGLEDOT (req->ptr1) 1941 req->result = req->wd && SINGLEDOT (req->ptr1)
2341 ? rmdir (req->wd->str) 1942 ? rmdir (req->wd->str)
2342 : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; 1943 : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break;
2343 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; 1944 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break;
1945 case EIO_RENAME: req->result = eio__renameat2 (
1946 dirfd,
2344 case EIO_RENAME: /* complications arise because "." cannot be renamed, so we might have to expand */ 1947 /* complications arise because "." cannot be renamed, so we might have to expand */
2345 req->result = req->wd && SINGLEDOT (req->ptr1) 1948 req->wd && SINGLEDOT (req->ptr1) ? req->wd->str : req->ptr1,
2346 ? rename (req->wd->str, req->ptr2) 1949 WD2FD ((eio_wd)req->int3),
2347 : renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; 1950 req->ptr2,
1951 req->int2
1952 );
1953 break;
2348 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; 1954 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break;
2349 case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; 1955 case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break;
2350 case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; 1956 case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
2351 case EIO_READLINK: ALLOC (PATH_MAX);
2352 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break;
2353 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); 1957 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2354 req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; 1958 req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break;
1959 case EIO_READLINK: ALLOC (EIO_PATH_MAX);
1960 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, EIO_PATH_MAX);
1961 if (req->result == EIO_PATH_MAX)
1962 {
1963 req->result = -1;
1964 errno = ENAMETOOLONG;
1965 }
1966 break;
2355 case EIO_UTIME: 1967 case EIO_UTIME:
2356 case EIO_FUTIME: 1968 case EIO_FUTIME:
2357 { 1969 {
2358 struct timespec ts[2]; 1970 struct timespec ts[2];
2359 struct timespec *times; 1971 struct timespec *times;
2384 req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break; 1996 req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break;
2385 case EIO_CHOWN: req->result = chown (path , req->int2, req->int3); break; 1997 case EIO_CHOWN: req->result = chown (path , req->int2, req->int3); break;
2386 case EIO_CHMOD: req->result = chmod (path , (mode_t)req->int2); break; 1998 case EIO_CHMOD: req->result = chmod (path , (mode_t)req->int2); break;
2387 case EIO_TRUNCATE: req->result = truncate (path , req->offs); break; 1999 case EIO_TRUNCATE: req->result = truncate (path , req->offs); break;
2388 case EIO_OPEN: req->result = open (path , req->int1, (mode_t)req->int2); break; 2000 case EIO_OPEN: req->result = open (path , req->int1, (mode_t)req->int2); break;
2001 case EIO_SLURP: eio__slurp ( open (path , O_RDONLY | O_CLOEXEC), req); break;
2389 2002
2390 case EIO_UNLINK: req->result = unlink (path ); break; 2003 case EIO_UNLINK: req->result = unlink (path ); break;
2391 case EIO_RMDIR: req->result = rmdir (path ); break; 2004 case EIO_RMDIR: req->result = rmdir (path ); break;
2392 case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; 2005 case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break;
2393 case EIO_RENAME: req->result = rename (path , req->ptr2); break; 2006 case EIO_RENAME: req->result = req->int2 ? EIO_ENOSYS () : rename (path, req->ptr2); break;
2394 case EIO_LINK: req->result = link (path , req->ptr2); break; 2007 case EIO_LINK: req->result = link (path , req->ptr2); break;
2395 case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; 2008 case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break;
2396 case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break; 2009 case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break;
2397 case EIO_READLINK: ALLOC (PATH_MAX);
2398 req->result = readlink (path, req->ptr2, PATH_MAX); break;
2399 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); 2010 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2400 req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; 2011 req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break;
2012 case EIO_READLINK: ALLOC (EIO_PATH_MAX);
2013 req->result = readlink (path, req->ptr2, EIO_PATH_MAX);
2014 if (req->result == EIO_PATH_MAX)
2015 {
2016 req->result = -1;
2017 errno = ENAMETOOLONG;
2018 }
2019 break;
2401 2020
2402 case EIO_UTIME: 2021 case EIO_UTIME:
2403 case EIO_FUTIME: 2022 case EIO_FUTIME:
2404 { 2023 {
2405 struct timeval tv[2]; 2024 struct timeval tv[2];
2440 2059
2441 case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; 2060 case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break;
2442 case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break; 2061 case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break;
2443 case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; 2062 case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break;
2444 2063
2445 case EIO_CLOSE: req->result = close (req->int1); break; 2064 case EIO_CLOSE: req->result = eio__close (req->int1); break;
2446 case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; 2065 case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break;
2447 case EIO_SYNC: req->result = 0; sync (); break; 2066 case EIO_SYNC: req->result = 0; sync (); break;
2448 case EIO_FSYNC: req->result = fsync (req->int1); break; 2067 case EIO_FSYNC: req->result = fsync (req->int1); break;
2449 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; 2068 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
2450 case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break; 2069 case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break;
2470 req->result = select (0, 0, 0, 0, &tv); 2089 req->result = select (0, 0, 0, 0, &tv);
2471 } 2090 }
2472#endif 2091#endif
2473 break; 2092 break;
2474 2093
2094#if 0
2475 case EIO_GROUP: 2095 case EIO_GROUP:
2476 abort (); /* handled in eio_request */ 2096 abort (); /* handled in eio_request */
2097#endif
2477 2098
2478 case EIO_NOP: 2099 case EIO_NOP:
2479 req->result = 0; 2100 req->result = 0;
2480 break; 2101 break;
2481 2102
2486 default: 2107 default:
2487 req->result = EIO_ENOSYS (); 2108 req->result = EIO_ENOSYS ();
2488 break; 2109 break;
2489 } 2110 }
2490 2111
2112alloc_fail:
2491 req->errorno = errno; 2113 req->errorno = errno;
2492} 2114}
2493 2115
2494#ifndef EIO_NO_WRAPPERS 2116#ifndef EIO_NO_WRAPPERS
2495 2117
2584} 2206}
2585 2207
2586eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) 2208eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2587{ 2209{
2588 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; 2210 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
2211}
2212
2213eio_req *eio_fcntl (int fd, int cmd, void *arg, int pri, eio_cb cb, void *data)
2214{
2215 REQ (EIO_IOCTL); req->int1 = fd; req->int2 = cmd; req->ptr2 = arg; SEND;
2216}
2217
2218eio_req *eio_ioctl (int fd, unsigned long request, void *buf, int pri, eio_cb cb, void *data)
2219{
2220 REQ (EIO_IOCTL); req->int1 = fd; req->int2 = request; req->ptr2 = buf; SEND;
2589} 2221}
2590 2222
2591eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) 2223eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data)
2592{ 2224{
2593 REQ (EIO_FSTAT); req->int1 = fd; SEND; 2225 REQ (EIO_FSTAT); req->int1 = fd; SEND;
2738eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) 2370eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
2739{ 2371{
2740 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); 2372 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
2741} 2373}
2742 2374
2375eio_req *eio_slurp (const char *path, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2376{
2377 REQ (EIO_SLURP); PATH; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
2378}
2379
2743eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data) 2380eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data)
2744{ 2381{
2745 REQ (EIO_CUSTOM); req->feed = execute; SEND; 2382 REQ (EIO_CUSTOM); req->feed = execute; SEND;
2746} 2383}
2747 2384
2781void 2418void
2782eio_grp_add (eio_req *grp, eio_req *req) 2419eio_grp_add (eio_req *grp, eio_req *req)
2783{ 2420{
2784 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); 2421 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
2785 2422
2786 grp->flags |= EIO_FLAG_GROUPADD; 2423 grp->flags |= ETP_FLAG_GROUPADD;
2787 2424
2788 ++grp->size; 2425 ++grp->size;
2789 req->grp = grp; 2426 req->grp = grp;
2790 2427
2791 req->grp_prev = 0; 2428 req->grp_prev = 0;
2804eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) 2441eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
2805{ 2442{
2806 return eio__sendfile (ofd, ifd, offset, count); 2443 return eio__sendfile (ofd, ifd, offset, count);
2807} 2444}
2808 2445
2446int eio_mlockall_sync (int flags)
2447{
2448 return eio__mlockall (flags);
2449}
2450

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines