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

Comparing libeio/eio.c (file contents):
Revision 1.127 by root, Mon Feb 18 03:15:19 2013 UTC vs.
Revision 1.139 by root, Thu Jun 25 18:14:19 2015 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 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 *
120 #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1)) 120 #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1))
121 121
122 #define chmod(path,mode) _chmod (path, mode) 122 #define chmod(path,mode) _chmod (path, mode)
123 #define dup(fd) _dup (fd) 123 #define dup(fd) _dup (fd)
124 #define dup2(fd1,fd2) _dup2 (fd1, fd2) 124 #define dup2(fd1,fd2) _dup2 (fd1, fd2)
125 #define pipe(fds) _pipe (fds, 4096, O_BINARY)
125 126
126 #define fchmod(fd,mode) EIO_ENOSYS () 127 #define fchmod(fd,mode) EIO_ENOSYS ()
127 #define chown(path,uid,gid) EIO_ENOSYS () 128 #define chown(path,uid,gid) EIO_ENOSYS ()
128 #define fchown(fd,uid,gid) EIO_ENOSYS () 129 #define fchown(fd,uid,gid) EIO_ENOSYS ()
129 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ 130 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */
213 #endif 214 #endif
214 215
215 return EIO_ERRNO (ENOENT, -1); 216 return EIO_ERRNO (ENOENT, -1);
216 } 217 }
217 218
218 /* POSIX API only */ 219 /* POSIX API only, causing trouble for win32 apps */
219 #define CreateHardLink(neu,old,flags) 0 220 #define CreateHardLink(neu,old,flags) 0 /* not really creating hardlink, still using relative paths? */
220 #define CreateSymbolicLink(neu,old,flags) 0 221 #define CreateSymbolicLink(neu,old,flags) 0 /* vista+ only */
221 222
222 struct statvfs 223 struct statvfs
223 { 224 {
224 int dummy; 225 int dummy;
225 }; 226 };
235 #include <sys/select.h> 236 #include <sys/select.h>
236 #include <unistd.h> 237 #include <unistd.h>
237 #include <signal.h> 238 #include <signal.h>
238 #include <dirent.h> 239 #include <dirent.h>
239 240
240 #if ANDROID 241 #ifdef ANDROID
241 #include <sys/vfs.h> 242 #include <sys/vfs.h>
242 #define statvfs statfs 243 #define statvfs statfs
243 #define fstatvfs fstatfs 244 #define fstatvfs fstatfs
244 #include <asm/page.h> /* supposedly limits.h does #define PAGESIZE PAGESIZE */ 245 #include <asm/page.h> /* supposedly limits.h does #define PAGESIZE PAGESIZE */
245 #else 246 #else
333 return -1 334 return -1
334 335
335#define FUBd \ 336#define FUBd \
336 free (eio_buf) 337 free (eio_buf)
337 338
338#define EIO_TICKS ((1000000 + 1023) >> 10)
339
340/*****************************************************************************/ 339/*****************************************************************************/
341 340
342struct tmpbuf
343{
344 void *ptr;
345 int len;
346};
347
348static void *
349tmpbuf_get (struct tmpbuf *buf, int len)
350{
351 if (buf->len < len)
352 {
353 free (buf->ptr);
354 buf->ptr = malloc (buf->len = len);
355 }
356
357 return buf->ptr;
358}
359
360struct tmpbuf; 341struct etp_tmpbuf;
361 342
362#if _POSIX_VERSION >= 200809L 343#if _POSIX_VERSION >= 200809L
363 #define HAVE_AT 1 344 #define HAVE_AT 1
364 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) 345 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD)
365 #ifndef O_SEARCH 346 #ifndef O_SEARCH
366 #define O_SEARCH O_RDONLY 347 #define O_SEARCH O_RDONLY
367 #endif 348 #endif
368#else 349#else
369 #define HAVE_AT 0 350 #define HAVE_AT 0
370 static const char *wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path); 351 static const char *wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path);
371#endif 352#endif
372 353
373struct eio_pwd 354struct eio_pwd
374{ 355{
375#if HAVE_AT 356#if HAVE_AT
382/*****************************************************************************/ 363/*****************************************************************************/
383 364
384#define ETP_PRI_MIN EIO_PRI_MIN 365#define ETP_PRI_MIN EIO_PRI_MIN
385#define ETP_PRI_MAX EIO_PRI_MAX 366#define ETP_PRI_MAX EIO_PRI_MAX
386 367
368#define ETP_TYPE_QUIT -1
369#define ETP_TYPE_GROUP EIO_GROUP
370
371static void eio_nop_callback (void) { }
372static void (*eio_want_poll_cb)(void) = eio_nop_callback;
373static void (*eio_done_poll_cb)(void) = eio_nop_callback;
374
375#define ETP_WANT_POLL(pool) eio_want_poll_cb ()
376#define ETP_DONE_POLL(pool) eio_done_poll_cb ()
377
387struct etp_worker; 378struct etp_worker;
388
389#define ETP_REQ eio_req 379#define ETP_REQ eio_req
390#define ETP_DESTROY(req) eio_destroy (req) 380#define ETP_DESTROY(req) eio_destroy (req)
391static int eio_finish (eio_req *req); 381static int eio_finish (eio_req *req);
392#define ETP_FINISH(req) eio_finish (req) 382#define ETP_FINISH(req) eio_finish (req)
393static void eio_execute (struct etp_worker *self, eio_req *req); 383static void eio_execute (struct etp_worker *self, eio_req *req);
394#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) 384#define ETP_EXECUTE(wrk,req) eio_execute (wrk, req)
395 385
396/*****************************************************************************/ 386#include "etp.c"
397 387
398#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 388static struct etp_pool eio_pool;
399 389#define EIO_POOL (&eio_pool)
400/* calculate time difference in ~1/EIO_TICKS of a second */
401ecb_inline int
402tvdiff (struct timeval *tv1, struct timeval *tv2)
403{
404 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
405 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
406}
407
408static unsigned int started, idle, wanted = 4;
409
410static void (*want_poll_cb) (void);
411static void (*done_poll_cb) (void);
412
413static unsigned int max_poll_time; /* reslock */
414static unsigned int max_poll_reqs; /* reslock */
415
416static unsigned int nreqs; /* reqlock */
417static unsigned int nready; /* reqlock */
418static unsigned int npending; /* reqlock */
419static unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */
420static unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */
421
422static xmutex_t wrklock;
423static xmutex_t reslock;
424static xmutex_t reqlock;
425static xcond_t reqwait;
426
427typedef struct etp_worker
428{
429 struct tmpbuf tmpbuf;
430
431 /* locked by wrklock */
432 struct etp_worker *prev, *next;
433
434 xthread_t tid;
435
436#ifdef ETP_WORKER_COMMON
437 ETP_WORKER_COMMON
438#endif
439} etp_worker;
440
441static etp_worker wrk_first; /* NOT etp */
442
443#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
444#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
445
446/* worker threads management */
447
448static void
449etp_worker_clear (etp_worker *wrk)
450{
451}
452
453static void ecb_cold
454etp_worker_free (etp_worker *wrk)
455{
456 free (wrk->tmpbuf.ptr);
457
458 wrk->next->prev = wrk->prev;
459 wrk->prev->next = wrk->next;
460
461 free (wrk);
462}
463
464static unsigned int
465etp_nreqs (void)
466{
467 int retval;
468 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
469 retval = nreqs;
470 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
471 return retval;
472}
473
474static unsigned int
475etp_nready (void)
476{
477 unsigned int retval;
478
479 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
480 retval = nready;
481 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
482
483 return retval;
484}
485
486static unsigned int
487etp_npending (void)
488{
489 unsigned int retval;
490
491 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
492 retval = npending;
493 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
494
495 return retval;
496}
497
498static unsigned int
499etp_nthreads (void)
500{
501 unsigned int retval;
502
503 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
504 retval = started;
505 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
506
507 return retval;
508}
509
510/*
511 * a somewhat faster data structure might be nice, but
512 * with 8 priorities this actually needs <20 insns
513 * per shift, the most expensive operation.
514 */
515typedef struct {
516 ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */
517 int size;
518} etp_reqq;
519
520static etp_reqq req_queue;
521static etp_reqq res_queue;
522
523static void ecb_noinline ecb_cold
524reqq_init (etp_reqq *q)
525{
526 int pri;
527
528 for (pri = 0; pri < ETP_NUM_PRI; ++pri)
529 q->qs[pri] = q->qe[pri] = 0;
530
531 q->size = 0;
532}
533
534static int ecb_noinline
535reqq_push (etp_reqq *q, ETP_REQ *req)
536{
537 int pri = req->pri;
538 req->next = 0;
539
540 if (q->qe[pri])
541 {
542 q->qe[pri]->next = req;
543 q->qe[pri] = req;
544 }
545 else
546 q->qe[pri] = q->qs[pri] = req;
547
548 return q->size++;
549}
550
551static ETP_REQ * ecb_noinline
552reqq_shift (etp_reqq *q)
553{
554 int pri;
555
556 if (!q->size)
557 return 0;
558
559 --q->size;
560
561 for (pri = ETP_NUM_PRI; pri--; )
562 {
563 eio_req *req = q->qs[pri];
564
565 if (req)
566 {
567 if (!(q->qs[pri] = (eio_req *)req->next))
568 q->qe[pri] = 0;
569
570 return req;
571 }
572 }
573
574 abort ();
575}
576
577static int ecb_cold
578etp_init (void (*want_poll)(void), void (*done_poll)(void))
579{
580 X_MUTEX_CREATE (wrklock);
581 X_MUTEX_CREATE (reslock);
582 X_MUTEX_CREATE (reqlock);
583 X_COND_CREATE (reqwait);
584
585 reqq_init (&req_queue);
586 reqq_init (&res_queue);
587
588 wrk_first.next =
589 wrk_first.prev = &wrk_first;
590
591 started = 0;
592 idle = 0;
593 nreqs = 0;
594 nready = 0;
595 npending = 0;
596
597 want_poll_cb = want_poll;
598 done_poll_cb = done_poll;
599
600 return 0;
601}
602
603X_THREAD_PROC (etp_proc);
604
605static void ecb_cold
606etp_start_thread (void)
607{
608 etp_worker *wrk = calloc (1, sizeof (etp_worker));
609
610 /*TODO*/
611 assert (("unable to allocate worker thread data", wrk));
612
613 X_LOCK (wrklock);
614
615 if (xthread_create (&wrk->tid, etp_proc, (void *)wrk))
616 {
617 wrk->prev = &wrk_first;
618 wrk->next = wrk_first.next;
619 wrk_first.next->prev = wrk;
620 wrk_first.next = wrk;
621 ++started;
622 }
623 else
624 free (wrk);
625
626 X_UNLOCK (wrklock);
627}
628
629static void
630etp_maybe_start_thread (void)
631{
632 if (ecb_expect_true (etp_nthreads () >= wanted))
633 return;
634
635 /* todo: maybe use idle here, but might be less exact */
636 if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()))
637 return;
638
639 etp_start_thread ();
640}
641
642static void ecb_cold
643etp_end_thread (void)
644{
645 eio_req *req = calloc (1, sizeof (eio_req)); /* will be freed by worker */
646
647 req->type = -1;
648 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
649
650 X_LOCK (reqlock);
651 reqq_push (&req_queue, req);
652 X_COND_SIGNAL (reqwait);
653 X_UNLOCK (reqlock);
654
655 X_LOCK (wrklock);
656 --started;
657 X_UNLOCK (wrklock);
658}
659
660static int
661etp_poll (void)
662{
663 unsigned int maxreqs;
664 unsigned int maxtime;
665 struct timeval tv_start, tv_now;
666
667 X_LOCK (reslock);
668 maxreqs = max_poll_reqs;
669 maxtime = max_poll_time;
670 X_UNLOCK (reslock);
671
672 if (maxtime)
673 gettimeofday (&tv_start, 0);
674
675 for (;;)
676 {
677 ETP_REQ *req;
678
679 etp_maybe_start_thread ();
680
681 X_LOCK (reslock);
682 req = reqq_shift (&res_queue);
683
684 if (req)
685 {
686 --npending;
687
688 if (!res_queue.size && done_poll_cb)
689 done_poll_cb ();
690 }
691
692 X_UNLOCK (reslock);
693
694 if (!req)
695 return 0;
696
697 X_LOCK (reqlock);
698 --nreqs;
699 X_UNLOCK (reqlock);
700
701 if (ecb_expect_false (req->type == EIO_GROUP && req->size))
702 {
703 req->int1 = 1; /* mark request as delayed */
704 continue;
705 }
706 else
707 {
708 int res = ETP_FINISH (req);
709 if (ecb_expect_false (res))
710 return res;
711 }
712
713 if (ecb_expect_false (maxreqs && !--maxreqs))
714 break;
715
716 if (maxtime)
717 {
718 gettimeofday (&tv_now, 0);
719
720 if (tvdiff (&tv_start, &tv_now) >= maxtime)
721 break;
722 }
723 }
724
725 errno = EAGAIN;
726 return -1;
727}
728
729static void
730etp_cancel (ETP_REQ *req)
731{
732 req->cancelled = 1;
733
734 eio_grp_cancel (req);
735}
736
737static void
738etp_submit (ETP_REQ *req)
739{
740 req->pri -= ETP_PRI_MIN;
741
742 if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN;
743 if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
744
745 if (ecb_expect_false (req->type == EIO_GROUP))
746 {
747 /* I hope this is worth it :/ */
748 X_LOCK (reqlock);
749 ++nreqs;
750 X_UNLOCK (reqlock);
751
752 X_LOCK (reslock);
753
754 ++npending;
755
756 if (!reqq_push (&res_queue, req) && want_poll_cb)
757 want_poll_cb ();
758
759 X_UNLOCK (reslock);
760 }
761 else
762 {
763 X_LOCK (reqlock);
764 ++nreqs;
765 ++nready;
766 reqq_push (&req_queue, req);
767 X_COND_SIGNAL (reqwait);
768 X_UNLOCK (reqlock);
769
770 etp_maybe_start_thread ();
771 }
772}
773
774static void ecb_cold
775etp_set_max_poll_time (double nseconds)
776{
777 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
778 max_poll_time = nseconds * EIO_TICKS;
779 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
780}
781
782static void ecb_cold
783etp_set_max_poll_reqs (unsigned int maxreqs)
784{
785 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
786 max_poll_reqs = maxreqs;
787 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
788}
789
790static void ecb_cold
791etp_set_max_idle (unsigned int nthreads)
792{
793 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
794 max_idle = nthreads;
795 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
796}
797
798static void ecb_cold
799etp_set_idle_timeout (unsigned int seconds)
800{
801 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
802 idle_timeout = seconds;
803 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
804}
805
806static void ecb_cold
807etp_set_min_parallel (unsigned int nthreads)
808{
809 if (wanted < nthreads)
810 wanted = nthreads;
811}
812
813static void ecb_cold
814etp_set_max_parallel (unsigned int nthreads)
815{
816 if (wanted > nthreads)
817 wanted = nthreads;
818
819 while (started > wanted)
820 etp_end_thread ();
821}
822 390
823/*****************************************************************************/ 391/*****************************************************************************/
824 392
825static void 393static void
826grp_try_feed (eio_req *grp) 394grp_try_feed (eio_req *grp)
827{ 395{
828 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 396 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
829 { 397 {
830 grp->flags &= ~EIO_FLAG_GROUPADD; 398 grp->flags &= ~ETP_FLAG_GROUPADD;
831 399
832 EIO_FEED (grp); 400 EIO_FEED (grp);
833 401
834 /* stop if no progress has been made */ 402 /* stop if no progress has been made */
835 if (!(grp->flags & EIO_FLAG_GROUPADD)) 403 if (!(grp->flags & ETP_FLAG_GROUPADD))
836 { 404 {
837 grp->feed = 0; 405 grp->feed = 0;
838 break; 406 break;
839 } 407 }
840 } 408 }
847 415
848 /* call feeder, if applicable */ 416 /* call feeder, if applicable */
849 grp_try_feed (grp); 417 grp_try_feed (grp);
850 418
851 /* finish, if done */ 419 /* finish, if done */
852 if (!grp->size && grp->int1) 420 if (!grp->size && grp->flags & ETP_FLAG_DELAYED)
853 return eio_finish (grp); 421 return eio_finish (grp);
854 else 422 else
855 return 0; 423 return 0;
856} 424}
857 425
893} 461}
894 462
895void 463void
896eio_grp_cancel (eio_req *grp) 464eio_grp_cancel (eio_req *grp)
897{ 465{
898 for (grp = grp->grp_first; grp; grp = grp->grp_next) 466 etp_grp_cancel (EIO_POOL, grp);
899 eio_cancel (grp);
900} 467}
901 468
902void 469void
903eio_cancel (eio_req *req) 470eio_cancel (eio_req *req)
904{ 471{
905 etp_cancel (req); 472 etp_cancel (EIO_POOL, req);
906} 473}
907 474
908void 475void
909eio_submit (eio_req *req) 476eio_submit (eio_req *req)
910{ 477{
911 etp_submit (req); 478 etp_submit (EIO_POOL, req);
912} 479}
913 480
914unsigned int 481unsigned int
915eio_nreqs (void) 482eio_nreqs (void)
916{ 483{
917 return etp_nreqs (); 484 return etp_nreqs (EIO_POOL);
918} 485}
919 486
920unsigned int 487unsigned int
921eio_nready (void) 488eio_nready (void)
922{ 489{
923 return etp_nready (); 490 return etp_nready (EIO_POOL);
924} 491}
925 492
926unsigned int 493unsigned int
927eio_npending (void) 494eio_npending (void)
928{ 495{
929 return etp_npending (); 496 return etp_npending (EIO_POOL);
930} 497}
931 498
932unsigned int ecb_cold 499unsigned int ecb_cold
933eio_nthreads (void) 500eio_nthreads (void)
934{ 501{
935 return etp_nthreads (); 502 return etp_nthreads (EIO_POOL);
936} 503}
937 504
938void ecb_cold 505void ecb_cold
939eio_set_max_poll_time (double nseconds) 506eio_set_max_poll_time (double nseconds)
940{ 507{
941 etp_set_max_poll_time (nseconds); 508 etp_set_max_poll_time (EIO_POOL, nseconds);
942} 509}
943 510
944void ecb_cold 511void ecb_cold
945eio_set_max_poll_reqs (unsigned int maxreqs) 512eio_set_max_poll_reqs (unsigned int maxreqs)
946{ 513{
947 etp_set_max_poll_reqs (maxreqs); 514 etp_set_max_poll_reqs (EIO_POOL, maxreqs);
948} 515}
949 516
950void ecb_cold 517void ecb_cold
951eio_set_max_idle (unsigned int nthreads) 518eio_set_max_idle (unsigned int nthreads)
952{ 519{
953 etp_set_max_idle (nthreads); 520 etp_set_max_idle (EIO_POOL, nthreads);
954} 521}
955 522
956void ecb_cold 523void ecb_cold
957eio_set_idle_timeout (unsigned int seconds) 524eio_set_idle_timeout (unsigned int seconds)
958{ 525{
959 etp_set_idle_timeout (seconds); 526 etp_set_idle_timeout (EIO_POOL, seconds);
960} 527}
961 528
962void ecb_cold 529void ecb_cold
963eio_set_min_parallel (unsigned int nthreads) 530eio_set_min_parallel (unsigned int nthreads)
964{ 531{
965 etp_set_min_parallel (nthreads); 532 etp_set_min_parallel (EIO_POOL, nthreads);
966} 533}
967 534
968void ecb_cold 535void ecb_cold
969eio_set_max_parallel (unsigned int nthreads) 536eio_set_max_parallel (unsigned int nthreads)
970{ 537{
971 etp_set_max_parallel (nthreads); 538 etp_set_max_parallel (EIO_POOL, nthreads);
972} 539}
973 540
974int eio_poll (void) 541int eio_poll (void)
975{ 542{
976 return etp_poll (); 543 return etp_poll (EIO_POOL);
977} 544}
978 545
979/*****************************************************************************/ 546/*****************************************************************************/
980/* work around various missing functions */ 547/* work around various missing functions */
981 548
1391 req->result = req->offs == (off_t)-1 ? -1 : 0; 958 req->result = req->offs == (off_t)-1 ? -1 : 0;
1392} 959}
1393 960
1394/* result will always end up in tmpbuf, there is always space for adding a 0-byte */ 961/* result will always end up in tmpbuf, there is always space for adding a 0-byte */
1395static int 962static int
1396eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 963eio__realpath (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
1397{ 964{
965 char *res;
1398 const char *rel = path; 966 const char *rel = path;
1399 char *res;
1400 char *tmp1, *tmp2; 967 char *tmp1, *tmp2;
1401#if SYMLOOP_MAX > 32 968#if SYMLOOP_MAX > 32
1402 int symlinks = SYMLOOP_MAX; 969 int symlinks = SYMLOOP_MAX;
1403#else 970#else
1404 int symlinks = 32; 971 int symlinks = 32;
1410 977
1411 errno = ENOENT; 978 errno = ENOENT;
1412 if (!*rel) 979 if (!*rel)
1413 return -1; 980 return -1;
1414 981
1415 res = tmpbuf_get (tmpbuf, PATH_MAX * 3); 982 res = etp_tmpbuf_get (tmpbuf, PATH_MAX * 3);
983#ifdef _WIN32
984 if (_access (rel, 4) != 0)
985 return -1;
986
987 symlinks = GetFullPathName (rel, PATH_MAX * 3, res, 0);
988
989 errno = ENAMETOOLONG;
990 if (symlinks >= PATH_MAX * 3)
991 return -1;
992
993 errno = EIO;
994 if (symlinks <= 0)
995 return -1;
996
997 return symlinks;
998
999#else
1416 tmp1 = res + PATH_MAX; 1000 tmp1 = res + PATH_MAX;
1417 tmp2 = tmp1 + PATH_MAX; 1001 tmp2 = tmp1 + PATH_MAX;
1418 1002
1419#if 0 /* disabled, the musl way to do things is just too racy */ 1003#if 0 /* disabled, the musl way to do things is just too racy */
1420#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) 1004#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1543 /* special case for the lone root path */ 1127 /* special case for the lone root path */
1544 if (res == tmpbuf->ptr) 1128 if (res == tmpbuf->ptr)
1545 *res++ = '/'; 1129 *res++ = '/';
1546 1130
1547 return res - (char *)tmpbuf->ptr; 1131 return res - (char *)tmpbuf->ptr;
1132#endif
1548} 1133}
1549 1134
1550static signed char 1135static signed char
1551eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1136eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1552{ 1137{
2011#if !HAVE_AT 1596#if !HAVE_AT
2012 1597
2013/* a bit like realpath, but usually faster because it doesn'T have to return */ 1598/* a bit like realpath, but usually faster because it doesn'T have to return */
2014/* an absolute or canonical path */ 1599/* an absolute or canonical path */
2015static const char * 1600static const char *
2016wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1601wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
2017{ 1602{
2018 if (!wd || *path == '/') 1603 if (!wd || *path == '/')
2019 return path; 1604 return path;
2020 1605
2021 if (path [0] == '.' && !path [1]) 1606 if (path [0] == '.' && !path [1])
2023 1608
2024 { 1609 {
2025 int l1 = wd->len; 1610 int l1 = wd->len;
2026 int l2 = strlen (path); 1611 int l2 = strlen (path);
2027 1612
2028 char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2); 1613 char *res = etp_tmpbuf_get (tmpbuf, l1 + l2 + 2);
2029 1614
2030 memcpy (res, wd->str, l1); 1615 memcpy (res, wd->str, l1);
2031 res [l1] = '/'; 1616 res [l1] = '/';
2032 memcpy (res + l1 + 1, path, l2 + 1); 1617 memcpy (res + l1 + 1, path, l2 + 1);
2033 1618
2036} 1621}
2037 1622
2038#endif 1623#endif
2039 1624
2040static eio_wd 1625static eio_wd
2041eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1626eio__wd_open_sync (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
2042{ 1627{
2043 int fd; 1628 int fd;
2044 eio_wd res; 1629 eio_wd res;
2045 int len = eio__realpath (tmpbuf, wd, path); 1630 int len = eio__realpath (tmpbuf, wd, path);
2046 1631
2068} 1653}
2069 1654
2070eio_wd 1655eio_wd
2071eio_wd_open_sync (eio_wd wd, const char *path) 1656eio_wd_open_sync (eio_wd wd, const char *path)
2072{ 1657{
2073 struct tmpbuf tmpbuf = { 0 }; 1658 struct etp_tmpbuf tmpbuf = { };
2074 wd = eio__wd_open_sync (&tmpbuf, wd, path); 1659 wd = eio__wd_open_sync (&tmpbuf, wd, path);
2075 free (tmpbuf.ptr); 1660 free (tmpbuf.ptr);
2076 1661
2077 return wd; 1662 return wd;
2078} 1663}
2127/*****************************************************************************/ 1712/*****************************************************************************/
2128 1713
2129#define ALLOC(len) \ 1714#define ALLOC(len) \
2130 if (!req->ptr2) \ 1715 if (!req->ptr2) \
2131 { \ 1716 { \
2132 X_LOCK (wrklock); \ 1717 X_LOCK (EIO_POOL->wrklock); \
2133 req->flags |= EIO_FLAG_PTR2_FREE; \ 1718 req->flags |= EIO_FLAG_PTR2_FREE; \
2134 X_UNLOCK (wrklock); \ 1719 X_UNLOCK (EIO_POOL->wrklock); \
2135 req->ptr2 = malloc (len); \ 1720 req->ptr2 = malloc (len); \
2136 if (!req->ptr2) \ 1721 if (!req->ptr2) \
2137 { \ 1722 { \
2138 errno = ENOMEM; \ 1723 errno = ENOMEM; \
2139 req->result = -1; \ 1724 req->result = -1; \
2140 break; \ 1725 break; \
2141 } \ 1726 } \
2142 } 1727 }
2143 1728
2144static void ecb_noinline ecb_cold
2145etp_proc_init (void)
2146{
2147#if HAVE_PRCTL_SET_NAME
2148 /* provide a more sensible "thread name" */
2149 char name[16 + 1];
2150 const int namelen = sizeof (name) - 1;
2151 int len;
2152
2153 prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0);
2154 name [namelen] = 0;
2155 len = strlen (name);
2156 strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio");
2157 prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0);
2158#endif
2159}
2160
2161X_THREAD_PROC (etp_proc)
2162{
2163 ETP_REQ *req;
2164 struct timespec ts;
2165 etp_worker *self = (etp_worker *)thr_arg;
2166
2167 etp_proc_init ();
2168
2169 /* try to distribute timeouts somewhat evenly */
2170 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
2171
2172 for (;;)
2173 {
2174 ts.tv_sec = 0;
2175
2176 X_LOCK (reqlock);
2177
2178 for (;;)
2179 {
2180 req = reqq_shift (&req_queue);
2181
2182 if (req)
2183 break;
2184
2185 if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */
2186 {
2187 X_UNLOCK (reqlock);
2188 X_LOCK (wrklock);
2189 --started;
2190 X_UNLOCK (wrklock);
2191 goto quit;
2192 }
2193
2194 ++idle;
2195
2196 if (idle <= max_idle)
2197 /* we are allowed to idle, so do so without any timeout */
2198 X_COND_WAIT (reqwait, reqlock);
2199 else
2200 {
2201 /* initialise timeout once */
2202 if (!ts.tv_sec)
2203 ts.tv_sec = time (0) + idle_timeout;
2204
2205 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
2206 ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */
2207 }
2208
2209 --idle;
2210 }
2211
2212 --nready;
2213
2214 X_UNLOCK (reqlock);
2215
2216 if (req->type < 0)
2217 goto quit;
2218
2219 ETP_EXECUTE (self, req);
2220
2221 X_LOCK (reslock);
2222
2223 ++npending;
2224
2225 if (!reqq_push (&res_queue, req) && want_poll_cb)
2226 want_poll_cb ();
2227
2228 etp_worker_clear (self);
2229
2230 X_UNLOCK (reslock);
2231 }
2232
2233quit:
2234 free (req);
2235
2236 X_LOCK (wrklock);
2237 etp_worker_free (self);
2238 X_UNLOCK (wrklock);
2239
2240 return 0;
2241}
2242
2243/*****************************************************************************/ 1729/*****************************************************************************/
2244 1730
2245int ecb_cold 1731int ecb_cold
2246eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1732eio_init (void (*want_poll)(void), void (*done_poll)(void))
2247{ 1733{
2248 return etp_init (want_poll, done_poll); 1734 eio_want_poll_cb = want_poll;
1735 eio_done_poll_cb = done_poll;
1736
1737 return etp_init (EIO_POOL, 0, 0, 0);
2249} 1738}
2250 1739
2251ecb_inline void 1740ecb_inline void
2252eio_api_destroy (eio_req *req) 1741eio_api_destroy (eio_req *req)
2253{ 1742{
2478 req->result = select (0, 0, 0, 0, &tv); 1967 req->result = select (0, 0, 0, 0, &tv);
2479 } 1968 }
2480#endif 1969#endif
2481 break; 1970 break;
2482 1971
1972#if 0
2483 case EIO_GROUP: 1973 case EIO_GROUP:
2484 abort (); /* handled in eio_request */ 1974 abort (); /* handled in eio_request */
1975#endif
2485 1976
2486 case EIO_NOP: 1977 case EIO_NOP:
2487 req->result = 0; 1978 req->result = 0;
2488 break; 1979 break;
2489 1980
2789void 2280void
2790eio_grp_add (eio_req *grp, eio_req *req) 2281eio_grp_add (eio_req *grp, eio_req *req)
2791{ 2282{
2792 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); 2283 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
2793 2284
2794 grp->flags |= EIO_FLAG_GROUPADD; 2285 grp->flags |= ETP_FLAG_GROUPADD;
2795 2286
2796 ++grp->size; 2287 ++grp->size;
2797 req->grp = grp; 2288 req->grp = grp;
2798 2289
2799 req->grp_prev = 0; 2290 req->grp_prev = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines