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.161 by root, Fri Feb 16 21:20:52 2024 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#if defined(O_PATH)
351# define EIO_O_PATH O_PATH
352#elif defined(O_SEARCH)
353# define EIO_O_PATH O_SEARCH
354#else
355# define EIO_O_PATH 0
356#endif
357
358#define EIO_PATH_MAX (PATH_MAX <= EIO_PATH_MIN ? EIO_PATH_MIN : PATH_MAX)
317 359
318/* buffer size for various temporary buffers */ 360/* buffer size for various temporary buffers */
319#define EIO_BUFSIZE 65536 361#define EIO_BUFSIZE 65536
320 362
321#define dBUF \ 363#define dBUF \
325 return -1 367 return -1
326 368
327#define FUBd \ 369#define FUBd \
328 free (eio_buf) 370 free (eio_buf)
329 371
330#define EIO_TICKS ((1000000 + 1023) >> 10)
331
332/*****************************************************************************/ 372/*****************************************************************************/
333 373
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; 374struct etp_tmpbuf;
353 375
354#if _POSIX_VERSION >= 200809L 376#if _POSIX_VERSION >= 200809L
355 #define HAVE_AT 1 377 #define HAVE_AT 1
356 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) 378 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD)
357 #ifndef O_SEARCH
358 #define O_SEARCH O_RDONLY
359 #endif
360#else 379#else
361 #define HAVE_AT 0 380 #define HAVE_AT 0
362 static const char *wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path); 381 static const char *wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path);
363#endif 382#endif
364 383
365struct eio_pwd 384struct eio_pwd
366{ 385{
367#if HAVE_AT 386#if HAVE_AT
374/*****************************************************************************/ 393/*****************************************************************************/
375 394
376#define ETP_PRI_MIN EIO_PRI_MIN 395#define ETP_PRI_MIN EIO_PRI_MIN
377#define ETP_PRI_MAX EIO_PRI_MAX 396#define ETP_PRI_MAX EIO_PRI_MAX
378 397
398#define ETP_TYPE_QUIT -1
399#define ETP_TYPE_GROUP EIO_GROUP
400
401static void eio_nop_callback (void) { }
402static void (*eio_want_poll_cb)(void) = eio_nop_callback;
403static void (*eio_done_poll_cb)(void) = eio_nop_callback;
404
405#define ETP_WANT_POLL(pool) eio_want_poll_cb ()
406#define ETP_DONE_POLL(pool) eio_done_poll_cb ()
407
379struct etp_worker; 408struct etp_worker;
380
381#define ETP_REQ eio_req 409#define ETP_REQ eio_req
382#define ETP_DESTROY(req) eio_destroy (req) 410#define ETP_DESTROY(req) eio_destroy (req)
383static int eio_finish (eio_req *req); 411static int eio_finish (eio_req *req);
384#define ETP_FINISH(req) eio_finish (req) 412#define ETP_FINISH(req) eio_finish (req)
385static void eio_execute (struct etp_worker *self, eio_req *req); 413static void eio_execute (struct etp_worker *self, eio_req *req);
386#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) 414#define ETP_EXECUTE(wrk,req) eio_execute (wrk, req)
387 415
388/*****************************************************************************/ 416#include "etp.c"
389 417
390#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 418static struct etp_pool eio_pool;
391 419#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 420
815/*****************************************************************************/ 421/*****************************************************************************/
816 422
817static void 423static void
818grp_try_feed (eio_req *grp) 424grp_try_feed (eio_req *grp)
819{ 425{
820 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 426 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
821 { 427 {
822 grp->flags &= ~EIO_FLAG_GROUPADD; 428 grp->flags &= ~ETP_FLAG_GROUPADD;
823 429
824 EIO_FEED (grp); 430 EIO_FEED (grp);
825 431
826 /* stop if no progress has been made */ 432 /* stop if no progress has been made */
827 if (!(grp->flags & EIO_FLAG_GROUPADD)) 433 if (!(grp->flags & ETP_FLAG_GROUPADD))
828 { 434 {
829 grp->feed = 0; 435 grp->feed = 0;
830 break; 436 break;
831 } 437 }
832 } 438 }
839 445
840 /* call feeder, if applicable */ 446 /* call feeder, if applicable */
841 grp_try_feed (grp); 447 grp_try_feed (grp);
842 448
843 /* finish, if done */ 449 /* finish, if done */
844 if (!grp->size && grp->int1) 450 if (!grp->size && grp->flags & ETP_FLAG_DELAYED)
845 return eio_finish (grp); 451 return eio_finish (grp);
846 else 452 else
847 return 0; 453 return 0;
848} 454}
849 455
885} 491}
886 492
887void 493void
888eio_grp_cancel (eio_req *grp) 494eio_grp_cancel (eio_req *grp)
889{ 495{
890 for (grp = grp->grp_first; grp; grp = grp->grp_next) 496 etp_grp_cancel (EIO_POOL, grp);
891 eio_cancel (grp);
892} 497}
893 498
894void 499void
895eio_cancel (eio_req *req) 500eio_cancel (eio_req *req)
896{ 501{
897 etp_cancel (req); 502 etp_cancel (EIO_POOL, req);
898} 503}
899 504
900void 505void
901eio_submit (eio_req *req) 506eio_submit (eio_req *req)
902{ 507{
903 etp_submit (req); 508 etp_submit (EIO_POOL, req);
904} 509}
905 510
906unsigned int 511unsigned int
907eio_nreqs (void) 512eio_nreqs (void)
908{ 513{
909 return etp_nreqs (); 514 return etp_nreqs (EIO_POOL);
910} 515}
911 516
912unsigned int 517unsigned int
913eio_nready (void) 518eio_nready (void)
914{ 519{
915 return etp_nready (); 520 return etp_nready (EIO_POOL);
916} 521}
917 522
918unsigned int 523unsigned int
919eio_npending (void) 524eio_npending (void)
920{ 525{
921 return etp_npending (); 526 return etp_npending (EIO_POOL);
922} 527}
923 528
924unsigned int ecb_cold 529unsigned int ecb_cold
925eio_nthreads (void) 530eio_nthreads (void)
926{ 531{
927 return etp_nthreads (); 532 return etp_nthreads (EIO_POOL);
928} 533}
929 534
930void ecb_cold 535void ecb_cold
931eio_set_max_poll_time (double nseconds) 536eio_set_max_poll_time (double nseconds)
932{ 537{
933 etp_set_max_poll_time (nseconds); 538 etp_set_max_poll_time (EIO_POOL, nseconds);
934} 539}
935 540
936void ecb_cold 541void ecb_cold
937eio_set_max_poll_reqs (unsigned int maxreqs) 542eio_set_max_poll_reqs (unsigned int maxreqs)
938{ 543{
939 etp_set_max_poll_reqs (maxreqs); 544 etp_set_max_poll_reqs (EIO_POOL, maxreqs);
940} 545}
941 546
942void ecb_cold 547void ecb_cold
943eio_set_max_idle (unsigned int nthreads) 548eio_set_max_idle (unsigned int nthreads)
944{ 549{
945 etp_set_max_idle (nthreads); 550 etp_set_max_idle (EIO_POOL, nthreads);
946} 551}
947 552
948void ecb_cold 553void ecb_cold
949eio_set_idle_timeout (unsigned int seconds) 554eio_set_idle_timeout (unsigned int seconds)
950{ 555{
951 etp_set_idle_timeout (seconds); 556 etp_set_idle_timeout (EIO_POOL, seconds);
952} 557}
953 558
954void ecb_cold 559void ecb_cold
955eio_set_min_parallel (unsigned int nthreads) 560eio_set_min_parallel (unsigned int nthreads)
956{ 561{
957 etp_set_min_parallel (nthreads); 562 etp_set_min_parallel (EIO_POOL, nthreads);
958} 563}
959 564
960void ecb_cold 565void ecb_cold
961eio_set_max_parallel (unsigned int nthreads) 566eio_set_max_parallel (unsigned int nthreads)
962{ 567{
963 etp_set_max_parallel (nthreads); 568 etp_set_max_parallel (EIO_POOL, nthreads);
964} 569}
965 570
966int eio_poll (void) 571int eio_poll (void)
967{ 572{
968 return etp_poll (); 573 return etp_poll (EIO_POOL);
969} 574}
970 575
971/*****************************************************************************/ 576/*****************************************************************************/
972/* work around various missing functions */ 577/* work around various missing functions */
578
579#if HAVE_POSIX_CLOSE && !__linux
580# define eio__close(fd) posix_close (fd, 0)
581#else
582# define eio__close(fd) close (fd)
583#endif
584
585/* close() without disturbing errno */
586static void
587silent_close (int fd)
588{
589 int saved_errno = errno;
590 eio__close (fd);
591 errno = saved_errno;
592}
973 593
974#ifndef HAVE_UTIMES 594#ifndef HAVE_UTIMES
975 595
976# undef utimes 596# undef utimes
977# define utimes(path,times) eio__utimes (path, times) 597# define utimes(path,times) eio__utimes (path, times)
1290 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 910 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1291 extern int mallopt (int, int); 911 extern int mallopt (int, int);
1292 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ 912 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1293 #endif 913 #endif
1294 914
915 #ifndef MCL_ONFAULT
916 if (flags & EIO_MCL_ONFAULT)
917 return EIO_ERRNO (EINVAL, -1);
918 #define MCL_ONFAULT 4
919 #endif
920
1295 if (EIO_MCL_CURRENT != MCL_CURRENT 921 if (EIO_MCL_CURRENT != MCL_CURRENT
1296 || EIO_MCL_FUTURE != MCL_FUTURE) 922 || EIO_MCL_FUTURE != MCL_FUTURE
923 || EIO_MCL_ONFAULT != MCL_ONFAULT)
1297 { 924 {
1298 flags = 0 925 flags = 0
1299 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0) 926 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0)
1300 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0); 927 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0)
928 | (flags & EIO_MCL_ONFAULT ? MCL_ONFAULT : 0)
929 ;
1301 } 930 }
1302 931
1303 return mlockall (flags); 932 return mlockall (flags);
1304} 933}
1305#endif 934#endif
1356 intptr_t end = addr + len; 985 intptr_t end = addr + len;
1357 intptr_t page = eio_pagesize (); 986 intptr_t page = eio_pagesize ();
1358 987
1359 if (addr < end) 988 if (addr < end)
1360 if (flags & EIO_MT_MODIFY) /* modify */ 989 if (flags & EIO_MT_MODIFY) /* modify */
1361 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req)); 990 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < end && !EIO_CANCELLED (req));
1362 else 991 else
1363 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req)); 992 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < end && !EIO_CANCELLED (req));
1364 } 993 }
1365 994
1366 return 0; 995 return 0;
1367} 996}
1368 997
1383 req->result = req->offs == (off_t)-1 ? -1 : 0; 1012 req->result = req->offs == (off_t)-1 ? -1 : 0;
1384} 1013}
1385 1014
1386/* result will always end up in tmpbuf, there is always space for adding a 0-byte */ 1015/* result will always end up in tmpbuf, there is always space for adding a 0-byte */
1387static int 1016static int
1388eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1017eio__realpath (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
1389{ 1018{
1019 char *res;
1390 const char *rel = path; 1020 const char *rel = path;
1391 char *res;
1392 char *tmp1, *tmp2; 1021 char *tmp1, *tmp2;
1393#if SYMLOOP_MAX > 32 1022#if SYMLOOP_MAX > 32
1394 int symlinks = SYMLOOP_MAX; 1023 int symlinks = SYMLOOP_MAX;
1395#else 1024#else
1396 int symlinks = 32; 1025 int symlinks = 32;
1402 1031
1403 errno = ENOENT; 1032 errno = ENOENT;
1404 if (!*rel) 1033 if (!*rel)
1405 return -1; 1034 return -1;
1406 1035
1407 res = tmpbuf_get (tmpbuf, PATH_MAX * 3); 1036 res = etp_tmpbuf_get (tmpbuf, EIO_PATH_MAX * 3);
1037#ifdef _WIN32
1038 if (_access (rel, 4) != 0)
1039 return -1;
1040
1041 symlinks = GetFullPathName (rel, EIO_PATH_MAX * 3, res, 0);
1042
1043 errno = ENAMETOOLONG;
1044 if (symlinks >= EIO_PATH_MAX * 3)
1045 return -1;
1046
1047 errno = EIO;
1048 if (symlinks <= 0)
1049 return -1;
1050
1051 return symlinks;
1052
1053#else
1408 tmp1 = res + PATH_MAX; 1054 tmp1 = res + EIO_PATH_MAX;
1409 tmp2 = tmp1 + PATH_MAX; 1055 tmp2 = tmp1 + EIO_PATH_MAX;
1410 1056
1411#if 0 /* disabled, the musl way to do things is just too racy */ 1057#if 0 /* disabled, the musl way to do things is just too racy */
1412#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) 1058#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1413 /* on linux we may be able to ask the kernel */ 1059 /* on linux we may be able to ask the kernel */
1414 { 1060 {
1415 int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); 1061 int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME);
1416 1062
1417 if (fd >= 0) 1063 if (fd >= 0)
1418 { 1064 {
1419 sprintf (tmp1, "/proc/self/fd/%d", fd); 1065 sprintf (tmp1, "/proc/self/fd/%d", fd);
1420 req->result = readlink (tmp1, res, PATH_MAX); 1066 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 */ 1067 /* here we should probably stat the open file and the disk file, to make sure they still match */
1422 close (fd); 1068 eio__close (fd);
1423 1069
1424 if (req->result > 0) 1070 if (req->result > 0)
1425 goto done; 1071 goto done;
1426 } 1072 }
1427 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) 1073 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO)
1438 if (wd == EIO_INVALID_WD) 1084 if (wd == EIO_INVALID_WD)
1439 return -1; 1085 return -1;
1440 1086
1441 if (wd == EIO_CWD) 1087 if (wd == EIO_CWD)
1442 { 1088 {
1443 if (!getcwd (res, PATH_MAX)) 1089 if (!getcwd (res, EIO_PATH_MAX))
1444 return -1; 1090 return -1;
1445 1091
1446 len = strlen (res); 1092 len = strlen (res);
1447 } 1093 }
1448 else 1094 else
1495 1141
1496 /* zero-terminate, for readlink */ 1142 /* zero-terminate, for readlink */
1497 res [len + 1] = 0; 1143 res [len + 1] = 0;
1498 1144
1499 /* now check if it's a symlink */ 1145 /* now check if it's a symlink */
1500 linklen = readlink (tmpbuf->ptr, tmp1, PATH_MAX); 1146 linklen = readlink (tmpbuf->ptr, tmp1, EIO_PATH_MAX);
1501 1147
1502 if (linklen < 0) 1148 if (linklen < 0)
1503 { 1149 {
1504 if (errno != EINVAL) 1150 if (errno != EINVAL)
1505 return -1; 1151 return -1;
1511 { 1157 {
1512 /* yay, it was a symlink - build new path in tmp2 */ 1158 /* yay, it was a symlink - build new path in tmp2 */
1513 int rellen = strlen (rel); 1159 int rellen = strlen (rel);
1514 1160
1515 errno = ENAMETOOLONG; 1161 errno = ENAMETOOLONG;
1516 if (linklen + 1 + rellen >= PATH_MAX) 1162 if (linklen + 1 + rellen >= EIO_PATH_MAX) /* also catch linklen >= EIO_PATH_MAX */
1517 return -1; 1163 return -1;
1518 1164
1519 errno = ELOOP; 1165 errno = ELOOP;
1520 if (!--symlinks) 1166 if (!--symlinks)
1521 return -1; 1167 return -1;
1535 /* special case for the lone root path */ 1181 /* special case for the lone root path */
1536 if (res == tmpbuf->ptr) 1182 if (res == tmpbuf->ptr)
1537 *res++ = '/'; 1183 *res++ = '/';
1538 1184
1539 return res - (char *)tmpbuf->ptr; 1185 return res - (char *)tmpbuf->ptr;
1186#endif
1540} 1187}
1541 1188
1542static signed char 1189static signed char
1543eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1190eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1544{ 1191{
1774 return; 1421 return;
1775 } 1422 }
1776 } 1423 }
1777#else 1424#else
1778 #if HAVE_AT 1425 #if HAVE_AT
1779 if (req->wd)
1780 { 1426 {
1781 int fd = openat (WD2FD (req->wd), req->ptr1, O_CLOEXEC | O_SEARCH | O_DIRECTORY); 1427 int fd = openat (WD2FD (req->wd), req->ptr1, O_CLOEXEC | O_RDONLY | O_DIRECTORY | O_NONBLOCK);
1782 1428
1783 if (fd < 0) 1429 if (fd < 0)
1430 return;
1431
1432 dirp = fdopendir (fd);
1433
1434 if (!dirp)
1435 {
1436 silent_close (fd);
1784 return; 1437 return;
1785
1786 dirp = fdopendir (fd);
1787
1788 if (!dirp)
1789 close (fd);
1790 } 1438 }
1791 else 1439 }
1792 dirp = opendir (req->ptr1);
1793 #else 1440 #else
1794 dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1)); 1441 dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1));
1442
1443 if (!dirp)
1444 return;
1795 #endif 1445 #endif
1796
1797 if (!dirp)
1798 return;
1799#endif 1446#endif
1800 1447
1801 if (req->flags & EIO_FLAG_PTR1_FREE) 1448 if (req->flags & EIO_FLAG_PTR1_FREE)
1802 free (req->ptr1); 1449 free (req->ptr1);
1803 1450
2000/* keep the absolute path in string form at all times */ 1647/* keep the absolute path in string form at all times */
2001/* fuck yeah. */ 1648/* fuck yeah. */
2002 1649
2003#if !HAVE_AT 1650#if !HAVE_AT
2004 1651
2005/* a bit like realpath, but usually faster because it doesn'T have to return */ 1652/* a bit like realpath, but usually faster because it doesn't have to return */
2006/* an absolute or canonical path */ 1653/* an absolute or canonical path */
2007static const char * 1654static const char *
2008wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1655wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
2009{ 1656{
2010 if (!wd || *path == '/') 1657 if (!wd || *path == '/')
2011 return path; 1658 return path;
2012 1659
2013 if (path [0] == '.' && !path [1]) 1660 if (path [0] == '.' && !path [1])
2015 1662
2016 { 1663 {
2017 int l1 = wd->len; 1664 int l1 = wd->len;
2018 int l2 = strlen (path); 1665 int l2 = strlen (path);
2019 1666
2020 char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2); 1667 char *res = etp_tmpbuf_get (tmpbuf, l1 + l2 + 2);
2021 1668
2022 memcpy (res, wd->str, l1); 1669 memcpy (res, wd->str, l1);
2023 res [l1] = '/'; 1670 res [l1] = '/';
2024 memcpy (res + l1 + 1, path, l2 + 1); 1671 memcpy (res + l1 + 1, path, l2 + 1);
2025 1672
2028} 1675}
2029 1676
2030#endif 1677#endif
2031 1678
2032static eio_wd 1679static eio_wd
2033eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1680eio__wd_open_sync (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
2034{ 1681{
2035 int fd; 1682 int fd;
2036 eio_wd res; 1683 eio_wd res;
2037 int len = eio__realpath (tmpbuf, wd, path); 1684 int len = eio__realpath (tmpbuf, wd, path);
2038 1685
2039 if (len < 0) 1686 if (len < 0)
2040 return EIO_INVALID_WD; 1687 return EIO_INVALID_WD;
2041 1688
2042#if HAVE_AT 1689#if HAVE_AT
2043 fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY); 1690 fd = openat (WD2FD (wd), path, EIO_O_PATH | O_DIRECTORY | O_NONBLOCK | O_CLOEXEC);
1691
1692 /* 0 is a valid fd, but we use it for EIO_CWD, so in the very unlikely */
1693 /* case of fd 0 being available (almost certainly an a pplication bug) */
1694 /* make sure we use another fd value */
1695 #if EIO_CWD
1696 error EIO_CWD must be 0
1697 #endif
1698 if (ecb_expect_false (fd == 0))
1699 {
1700 int fd2 = fcntl (fd, F_DUPFD_CLOEXEC ? F_DUPFD_CLOEXEC : F_DUPFD);
1701 fcntl (fd2, F_SETFD, FD_CLOEXEC);
1702 eio__close (fd);
1703 fd = fd2;
1704 }
2044 1705
2045 if (fd < 0) 1706 if (fd < 0)
2046 return EIO_INVALID_WD; 1707 return EIO_INVALID_WD;
2047#endif 1708#endif
2048 1709
2060} 1721}
2061 1722
2062eio_wd 1723eio_wd
2063eio_wd_open_sync (eio_wd wd, const char *path) 1724eio_wd_open_sync (eio_wd wd, const char *path)
2064{ 1725{
2065 struct tmpbuf tmpbuf = { 0 }; 1726 struct etp_tmpbuf tmpbuf = { 0 };
2066 wd = eio__wd_open_sync (&tmpbuf, wd, path); 1727 wd = eio__wd_open_sync (&tmpbuf, wd, path);
2067 free (tmpbuf.ptr); 1728 free (tmpbuf.ptr);
2068 1729
2069 return wd; 1730 return wd;
2070} 1731}
2073eio_wd_close_sync (eio_wd wd) 1734eio_wd_close_sync (eio_wd wd)
2074{ 1735{
2075 if (wd != EIO_INVALID_WD && wd != EIO_CWD) 1736 if (wd != EIO_INVALID_WD && wd != EIO_CWD)
2076 { 1737 {
2077 #if HAVE_AT 1738 #if HAVE_AT
2078 close (wd->fd); 1739 eio__close (wd->fd);
2079 #endif 1740 #endif
2080 free (wd); 1741 free (wd);
2081 } 1742 }
2082} 1743}
2083 1744
2084#if HAVE_AT 1745#if HAVE_AT
2085 1746
1747static int
1748eio__renameat2 (int olddirfd, const char *oldpath, int newdirfd, const char *newpath, unsigned int flags)
1749{
1750#if HAVE_RENAMEAT2
1751 return syscall (SYS_renameat2, olddirfd, oldpath, newdirfd, newpath, flags);
1752#else
1753 if (flags)
1754 return EIO_ENOSYS ();
1755
1756 return renameat (olddirfd, oldpath, newdirfd, newpath);
1757#endif
1758}
1759
2086/* they forgot these */ 1760/* they forgot these */
2087 1761
2088static int 1762static int
2089eio__truncateat (int dirfd, const char *path, off_t length) 1763eio__truncateat (int dirfd, const char *path, off_t length)
2090{ 1764{
2091 int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC); 1765 int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC | O_NONBLOCK);
2092 int res; 1766 int res;
2093 1767
2094 if (fd < 0) 1768 if (fd < 0)
2095 return fd; 1769 return fd;
2096 1770
2097 res = ftruncate (fd, length); 1771 res = ftruncate (fd, length);
2098 close (fd); 1772 silent_close (fd);
2099 return res; 1773 return res;
2100} 1774}
2101 1775
2102static int 1776static int
2103eio__statvfsat (int dirfd, const char *path, struct statvfs *buf) 1777eio__statvfsat (int dirfd, const char *path, struct statvfs *buf)
2104{ 1778{
2105 int fd = openat (dirfd, path, O_SEARCH | O_CLOEXEC); 1779 int fd = openat (dirfd, path, EIO_O_PATH | O_CLOEXEC | O_NONBLOCK);
2106 int res; 1780 int res;
2107 1781
2108 if (fd < 0) 1782 if (fd < 0)
2109 return fd; 1783 return fd;
2110 1784
2111 res = fstatvfs (fd, buf); 1785 res = fstatvfs (fd, buf);
2112 close (fd); 1786 silent_close (fd);
2113 return res; 1787 return res;
2114
2115} 1788}
2116 1789
2117#endif 1790#endif
2118 1791
2119/*****************************************************************************/ 1792/*****************************************************************************/
2120 1793
2121#define ALLOC(len) \ 1794#define ALLOC(len) \
2122 if (!req->ptr2) \ 1795 if (!req->ptr2) \
2123 { \ 1796 { \
2124 X_LOCK (wrklock); \ 1797 X_LOCK (EIO_POOL->wrklock); \
2125 req->flags |= EIO_FLAG_PTR2_FREE; \ 1798 req->flags |= EIO_FLAG_PTR2_FREE; \
2126 X_UNLOCK (wrklock); \ 1799 X_UNLOCK (EIO_POOL->wrklock); \
2127 req->ptr2 = malloc (len); \ 1800 req->ptr2 = malloc (len); \
2128 if (!req->ptr2) \ 1801 if (!req->ptr2) \
2129 { \ 1802 { \
2130 errno = ENOMEM; \ 1803 errno = ENOMEM; \
2131 req->result = -1; \ 1804 req->result = -1; \
2132 break; \ 1805 goto alloc_fail; \
2133 } \ 1806 } \
2134 } 1807 }
2135 1808
2136static void ecb_noinline ecb_cold 1809/*****************************************************************************/
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 1810
2145 prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0); 1811static void
2146 name [namelen] = 0; 1812eio__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{ 1813{
2155 ETP_REQ *req; 1814 req->result = fd;
2156 struct timespec ts;
2157 etp_worker *self = (etp_worker *)thr_arg;
2158 1815
2159 etp_proc_init (); 1816 if (fd < 0)
1817 return;
2160 1818
2161 /* try to distribute timeouts somewhat evenly */ 1819 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 { 1820 {
2166 ts.tv_sec = 0; 1821 off_t size = lseek (fd, 0, SEEK_END);
2167 1822
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) 1823 if (req->offs < 0)
2209 goto quit; 1824 req->offs += size;
2210 1825
2211 ETP_EXECUTE (self, req); 1826 if (!req->size)
2212 1827 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 } 1828 }
2224 1829
2225quit: 1830 ALLOC (req->size);
2226 free (req); 1831 req->result = pread (fd, req->ptr2, req->size, req->offs);
2227 1832
2228 X_LOCK (wrklock); 1833 silent_close (fd);
2229 etp_worker_free (self);
2230 X_UNLOCK (wrklock);
2231 1834
2232 return 0; 1835alloc_fail:
1836 ;
2233} 1837}
2234
2235/*****************************************************************************/
2236 1838
2237int ecb_cold 1839int ecb_cold
2238eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1840eio_init (void (*want_poll)(void), void (*done_poll)(void))
2239{ 1841{
2240 return etp_init (want_poll, done_poll); 1842 eio_want_poll_cb = want_poll;
1843 eio_done_poll_cb = done_poll;
1844
1845 return etp_init (EIO_POOL, 0, 0, 0);
2241} 1846}
2242 1847
2243ecb_inline void 1848ecb_inline void
2244eio_api_destroy (eio_req *req) 1849eio_api_destroy (eio_req *req)
2245{ 1850{
2319 : read (req->int1, req->ptr2, req->size); break; 1924 : read (req->int1, req->ptr2, req->size); break;
2320 case EIO_WRITE: req->result = req->offs >= 0 1925 case EIO_WRITE: req->result = req->offs >= 0
2321 ? pwrite (req->int1, req->ptr2, req->size, req->offs) 1926 ? pwrite (req->int1, req->ptr2, req->size, req->offs)
2322 : write (req->int1, req->ptr2, req->size); break; 1927 : write (req->int1, req->ptr2, req->size); break;
2323 1928
1929 case EIO_FCNTL: req->result = fcntl (req->int1, (int) req->int2, req->ptr2); break;
1930 case EIO_IOCTL: req->result = ioctl (req->int1, (unsigned long)req->int2, req->ptr2); break;
1931
2324 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; 1932 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; 1933 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break;
2326 1934
2327#if HAVE_AT 1935#if HAVE_AT
2328 1936
2332 req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, AT_SYMLINK_NOFOLLOW); break; 1940 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; 1941 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; 1942 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; 1943 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; 1944 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break;
1945 case EIO_SLURP: eio__slurp ( openat (dirfd, req->ptr1, O_RDONLY | O_CLOEXEC), req); break;
2337 1946
2338 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; 1947 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 */ 1948 case EIO_RMDIR: /* complications arise because "." cannot be removed, so we might have to expand */
2340 req->result = req->wd && SINGLEDOT (req->ptr1) 1949 req->result = req->wd && SINGLEDOT (req->ptr1)
2341 ? rmdir (req->wd->str) 1950 ? rmdir (req->wd->str)
2342 : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; 1951 : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break;
2343 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; 1952 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break;
1953 case EIO_RENAME: req->result = eio__renameat2 (
1954 dirfd,
2344 case EIO_RENAME: /* complications arise because "." cannot be renamed, so we might have to expand */ 1955 /* complications arise because "." cannot be renamed, so we might have to expand */
2345 req->result = req->wd && SINGLEDOT (req->ptr1) 1956 req->wd && SINGLEDOT (req->ptr1) ? req->wd->str : req->ptr1,
2346 ? rename (req->wd->str, req->ptr2) 1957 WD2FD ((eio_wd)req->int3),
2347 : renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; 1958 req->ptr2,
1959 req->int2
1960 );
1961 break;
2348 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; 1962 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; 1963 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; 1964 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)); 1965 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2354 req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; 1966 req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break;
1967 case EIO_READLINK: ALLOC (EIO_PATH_MAX);
1968 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, EIO_PATH_MAX);
1969 if (req->result == EIO_PATH_MAX)
1970 {
1971 req->result = -1;
1972 errno = ENAMETOOLONG;
1973 }
1974 break;
2355 case EIO_UTIME: 1975 case EIO_UTIME:
2356 case EIO_FUTIME: 1976 case EIO_FUTIME:
2357 { 1977 {
2358 struct timespec ts[2]; 1978 struct timespec ts[2];
2359 struct timespec *times; 1979 struct timespec *times;
2384 req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break; 2004 req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break;
2385 case EIO_CHOWN: req->result = chown (path , req->int2, req->int3); break; 2005 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; 2006 case EIO_CHMOD: req->result = chmod (path , (mode_t)req->int2); break;
2387 case EIO_TRUNCATE: req->result = truncate (path , req->offs); break; 2007 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; 2008 case EIO_OPEN: req->result = open (path , req->int1, (mode_t)req->int2); break;
2009 case EIO_SLURP: eio__slurp ( open (path , O_RDONLY | O_CLOEXEC), req); break;
2389 2010
2390 case EIO_UNLINK: req->result = unlink (path ); break; 2011 case EIO_UNLINK: req->result = unlink (path ); break;
2391 case EIO_RMDIR: req->result = rmdir (path ); break; 2012 case EIO_RMDIR: req->result = rmdir (path ); break;
2392 case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; 2013 case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break;
2393 case EIO_RENAME: req->result = rename (path , req->ptr2); break; 2014 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; 2015 case EIO_LINK: req->result = link (path , req->ptr2); break;
2395 case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; 2016 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; 2017 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)); 2018 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2400 req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; 2019 req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break;
2020 case EIO_READLINK: ALLOC (EIO_PATH_MAX);
2021 req->result = readlink (path, req->ptr2, EIO_PATH_MAX);
2022 if (req->result == EIO_PATH_MAX)
2023 {
2024 req->result = -1;
2025 errno = ENAMETOOLONG;
2026 }
2027 break;
2401 2028
2402 case EIO_UTIME: 2029 case EIO_UTIME:
2403 case EIO_FUTIME: 2030 case EIO_FUTIME:
2404 { 2031 {
2405 struct timeval tv[2]; 2032 struct timeval tv[2];
2440 2067
2441 case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; 2068 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; 2069 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; 2070 case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break;
2444 2071
2445 case EIO_CLOSE: req->result = close (req->int1); break; 2072 case EIO_CLOSE: req->result = eio__close (req->int1); break;
2446 case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; 2073 case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break;
2447 case EIO_SYNC: req->result = 0; sync (); break; 2074 case EIO_SYNC: req->result = 0; sync (); break;
2448 case EIO_FSYNC: req->result = fsync (req->int1); break; 2075 case EIO_FSYNC: req->result = fsync (req->int1); break;
2449 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; 2076 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
2450 case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break; 2077 case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break;
2470 req->result = select (0, 0, 0, 0, &tv); 2097 req->result = select (0, 0, 0, 0, &tv);
2471 } 2098 }
2472#endif 2099#endif
2473 break; 2100 break;
2474 2101
2102#if 0
2475 case EIO_GROUP: 2103 case EIO_GROUP:
2476 abort (); /* handled in eio_request */ 2104 abort (); /* handled in eio_request */
2105#endif
2477 2106
2478 case EIO_NOP: 2107 case EIO_NOP:
2479 req->result = 0; 2108 req->result = 0;
2480 break; 2109 break;
2481 2110
2486 default: 2115 default:
2487 req->result = EIO_ENOSYS (); 2116 req->result = EIO_ENOSYS ();
2488 break; 2117 break;
2489 } 2118 }
2490 2119
2120alloc_fail:
2491 req->errorno = errno; 2121 req->errorno = errno;
2492} 2122}
2493 2123
2494#ifndef EIO_NO_WRAPPERS 2124#ifndef EIO_NO_WRAPPERS
2495 2125
2584} 2214}
2585 2215
2586eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) 2216eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2587{ 2217{
2588 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; 2218 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
2219}
2220
2221eio_req *eio_fcntl (int fd, int cmd, void *arg, int pri, eio_cb cb, void *data)
2222{
2223 REQ (EIO_IOCTL); req->int1 = fd; req->int2 = cmd; req->ptr2 = arg; SEND;
2224}
2225
2226eio_req *eio_ioctl (int fd, unsigned long request, void *buf, int pri, eio_cb cb, void *data)
2227{
2228 REQ (EIO_IOCTL); req->int1 = fd; req->int2 = request; req->ptr2 = buf; SEND;
2589} 2229}
2590 2230
2591eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) 2231eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data)
2592{ 2232{
2593 REQ (EIO_FSTAT); req->int1 = fd; SEND; 2233 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) 2378eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
2739{ 2379{
2740 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); 2380 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
2741} 2381}
2742 2382
2383eio_req *eio_slurp (const char *path, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2384{
2385 REQ (EIO_SLURP); PATH; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
2386}
2387
2743eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data) 2388eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data)
2744{ 2389{
2745 REQ (EIO_CUSTOM); req->feed = execute; SEND; 2390 REQ (EIO_CUSTOM); req->feed = execute; SEND;
2746} 2391}
2747 2392
2781void 2426void
2782eio_grp_add (eio_req *grp, eio_req *req) 2427eio_grp_add (eio_req *grp, eio_req *req)
2783{ 2428{
2784 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); 2429 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
2785 2430
2786 grp->flags |= EIO_FLAG_GROUPADD; 2431 grp->flags |= ETP_FLAG_GROUPADD;
2787 2432
2788 ++grp->size; 2433 ++grp->size;
2789 req->grp = grp; 2434 req->grp = grp;
2790 2435
2791 req->grp_prev = 0; 2436 req->grp_prev = 0;
2804eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) 2449eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
2805{ 2450{
2806 return eio__sendfile (ofd, ifd, offset, count); 2451 return eio__sendfile (ofd, ifd, offset, count);
2807} 2452}
2808 2453
2454int eio_mlockall_sync (int flags)
2455{
2456 return eio__mlockall (flags);
2457}
2458

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines