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

Comparing cvsroot/libeio/eio.c (file contents):
Revision 1.66 by root, Sun Jun 5 19:58:37 2011 UTC vs.
Revision 1.84 by root, Thu Jul 7 22:39:56 2011 UTC

40#ifndef _WIN32 40#ifndef _WIN32
41# include "config.h" 41# include "config.h"
42#endif 42#endif
43 43
44#include "eio.h" 44#include "eio.h"
45#include "ecb.h"
45 46
46#ifdef EIO_STACKSIZE 47#ifdef EIO_STACKSIZE
47# define XTHREAD_STACKSIZE EIO_STACKSIZE 48# define XTHREAD_STACKSIZE EIO_STACKSIZE
48#endif 49#endif
49#include "xthread.h" 50#include "xthread.h"
59#include <limits.h> 60#include <limits.h>
60#include <fcntl.h> 61#include <fcntl.h>
61#include <assert.h> 62#include <assert.h>
62 63
63/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ 64/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */
64/* intptr_t only comes form stdint.h, says idiot openbsd coder */ 65/* intptr_t only comes from stdint.h, says idiot openbsd coder */
65#if HAVE_STDINT_H 66#if HAVE_STDINT_H
66# include <stdint.h> 67# include <stdint.h>
67#endif 68#endif
69
70#ifndef ECANCELED
71# define ECANCELED EDOM
72#endif
73
74static void eio_destroy (eio_req *req);
68 75
69#ifndef EIO_FINISH 76#ifndef EIO_FINISH
70# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 77# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0
71#endif 78#endif
72 79
165 if (!eio_buf) \ 172 if (!eio_buf) \
166 return -1; 173 return -1;
167 174
168#define EIO_TICKS ((1000000 + 1023) >> 10) 175#define EIO_TICKS ((1000000 + 1023) >> 10)
169 176
170/*****************************************************************************/
171
172#if __GNUC__ >= 3
173# define expect(expr,value) __builtin_expect ((expr),(value))
174#else
175# define expect(expr,value) (expr)
176#endif
177
178#define expect_false(expr) expect ((expr) != 0, 0)
179#define expect_true(expr) expect ((expr) != 0, 1)
180
181/*****************************************************************************/
182
183#define ETP_PRI_MIN EIO_PRI_MIN 177#define ETP_PRI_MIN EIO_PRI_MIN
184#define ETP_PRI_MAX EIO_PRI_MAX 178#define ETP_PRI_MAX EIO_PRI_MAX
185 179
186struct etp_worker; 180struct etp_worker;
187 181
212/*****************************************************************************/ 206/*****************************************************************************/
213 207
214#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 208#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
215 209
216/* calculate time difference in ~1/EIO_TICKS of a second */ 210/* calculate time difference in ~1/EIO_TICKS of a second */
211ecb_inline int
217static int tvdiff (struct timeval *tv1, struct timeval *tv2) 212tvdiff (struct timeval *tv1, struct timeval *tv2)
218{ 213{
219 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS 214 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
220 + ((tv2->tv_usec - tv1->tv_usec) >> 10); 215 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
221} 216}
222 217
266#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) 261#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
267#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) 262#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
268 263
269/* worker threads management */ 264/* worker threads management */
270 265
266static void ecb_cold
271static void etp_worker_clear (etp_worker *wrk) 267etp_worker_clear (etp_worker *wrk)
272{ 268{
273 ETP_WORKER_CLEAR (wrk); 269 ETP_WORKER_CLEAR (wrk);
274} 270}
275 271
272static void ecb_cold
276static void etp_worker_free (etp_worker *wrk) 273etp_worker_free (etp_worker *wrk)
277{ 274{
278 wrk->next->prev = wrk->prev; 275 wrk->next->prev = wrk->prev;
279 wrk->prev->next = wrk->next; 276 wrk->prev->next = wrk->next;
280 277
281 free (wrk); 278 free (wrk);
282} 279}
283 280
284static unsigned int etp_nreqs (void) 281static unsigned int
282etp_nreqs (void)
285{ 283{
286 int retval; 284 int retval;
287 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 285 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
288 retval = nreqs; 286 retval = nreqs;
289 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 287 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
290 return retval; 288 return retval;
291} 289}
292 290
293static unsigned int etp_nready (void) 291static unsigned int
292etp_nready (void)
294{ 293{
295 unsigned int retval; 294 unsigned int retval;
296 295
297 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 296 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
298 retval = nready; 297 retval = nready;
299 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 298 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
300 299
301 return retval; 300 return retval;
302} 301}
303 302
304static unsigned int etp_npending (void) 303static unsigned int
304etp_npending (void)
305{ 305{
306 unsigned int retval; 306 unsigned int retval;
307 307
308 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 308 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
309 retval = npending; 309 retval = npending;
310 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 310 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
311 311
312 return retval; 312 return retval;
313} 313}
314 314
315static unsigned int etp_nthreads (void) 315static unsigned int
316etp_nthreads (void)
316{ 317{
317 unsigned int retval; 318 unsigned int retval;
318 319
319 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 320 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
320 retval = started; 321 retval = started;
334} etp_reqq; 335} etp_reqq;
335 336
336static etp_reqq req_queue; 337static etp_reqq req_queue;
337static etp_reqq res_queue; 338static etp_reqq res_queue;
338 339
340static int ecb_noinline
339static int reqq_push (etp_reqq *q, ETP_REQ *req) 341reqq_push (etp_reqq *q, ETP_REQ *req)
340{ 342{
341 int pri = req->pri; 343 int pri = req->pri;
342 req->next = 0; 344 req->next = 0;
343 345
344 if (q->qe[pri]) 346 if (q->qe[pri])
350 q->qe[pri] = q->qs[pri] = req; 352 q->qe[pri] = q->qs[pri] = req;
351 353
352 return q->size++; 354 return q->size++;
353} 355}
354 356
357static ETP_REQ * ecb_noinline
355static ETP_REQ *reqq_shift (etp_reqq *q) 358reqq_shift (etp_reqq *q)
356{ 359{
357 int pri; 360 int pri;
358 361
359 if (!q->size) 362 if (!q->size)
360 return 0; 363 return 0;
375 } 378 }
376 379
377 abort (); 380 abort ();
378} 381}
379 382
383static void ecb_cold
380static void etp_thread_init (void) 384etp_thread_init (void)
381{ 385{
382 X_MUTEX_CREATE (wrklock); 386 X_MUTEX_CREATE (wrklock);
383 X_MUTEX_CREATE (reslock); 387 X_MUTEX_CREATE (reslock);
384 X_MUTEX_CREATE (reqlock); 388 X_MUTEX_CREATE (reqlock);
385 X_COND_CREATE (reqwait); 389 X_COND_CREATE (reqwait);
386} 390}
387 391
392static void ecb_cold
388static void etp_atfork_prepare (void) 393etp_atfork_prepare (void)
389{ 394{
390 X_LOCK (wrklock); 395 X_LOCK (wrklock);
391 X_LOCK (reqlock); 396 X_LOCK (reqlock);
392 X_LOCK (reslock); 397 X_LOCK (reslock);
393#if !HAVE_PREADWRITE 398#if !HAVE_PREADWRITE
394 X_LOCK (preadwritelock); 399 X_LOCK (preadwritelock);
395#endif 400#endif
396} 401}
397 402
403static void ecb_cold
398static void etp_atfork_parent (void) 404etp_atfork_parent (void)
399{ 405{
400#if !HAVE_PREADWRITE 406#if !HAVE_PREADWRITE
401 X_UNLOCK (preadwritelock); 407 X_UNLOCK (preadwritelock);
402#endif 408#endif
403 X_UNLOCK (reslock); 409 X_UNLOCK (reslock);
404 X_UNLOCK (reqlock); 410 X_UNLOCK (reqlock);
405 X_UNLOCK (wrklock); 411 X_UNLOCK (wrklock);
406} 412}
407 413
414static void ecb_cold
408static void etp_atfork_child (void) 415etp_atfork_child (void)
409{ 416{
410 ETP_REQ *prv; 417 ETP_REQ *prv;
411 418
412 while ((prv = reqq_shift (&req_queue))) 419 while ((prv = reqq_shift (&req_queue)))
413 ETP_DESTROY (prv); 420 ETP_DESTROY (prv);
433 npending = 0; 440 npending = 0;
434 441
435 etp_thread_init (); 442 etp_thread_init ();
436} 443}
437 444
438static void 445static void ecb_cold
439etp_once_init (void) 446etp_once_init (void)
440{ 447{
441 etp_thread_init (); 448 etp_thread_init ();
442 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child); 449 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child);
443} 450}
444 451
445static int 452static int ecb_cold
446etp_init (void (*want_poll)(void), void (*done_poll)(void)) 453etp_init (void (*want_poll)(void), void (*done_poll)(void))
447{ 454{
448 static pthread_once_t doinit = PTHREAD_ONCE_INIT; 455 static pthread_once_t doinit = PTHREAD_ONCE_INIT;
449 456
450 pthread_once (&doinit, etp_once_init); 457 pthread_once (&doinit, etp_once_init);
455 return 0; 462 return 0;
456} 463}
457 464
458X_THREAD_PROC (etp_proc); 465X_THREAD_PROC (etp_proc);
459 466
467static void ecb_cold
460static void etp_start_thread (void) 468etp_start_thread (void)
461{ 469{
462 etp_worker *wrk = calloc (1, sizeof (etp_worker)); 470 etp_worker *wrk = calloc (1, sizeof (etp_worker));
463 471
464 /*TODO*/ 472 /*TODO*/
465 assert (("unable to allocate worker thread data", wrk)); 473 assert (("unable to allocate worker thread data", wrk));
478 free (wrk); 486 free (wrk);
479 487
480 X_UNLOCK (wrklock); 488 X_UNLOCK (wrklock);
481} 489}
482 490
491static void
483static void etp_maybe_start_thread (void) 492etp_maybe_start_thread (void)
484{ 493{
485 if (expect_true (etp_nthreads () >= wanted)) 494 if (ecb_expect_true (etp_nthreads () >= wanted))
486 return; 495 return;
487 496
488 /* todo: maybe use idle here, but might be less exact */ 497 /* todo: maybe use idle here, but might be less exact */
489 if (expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) 498 if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()))
490 return; 499 return;
491 500
492 etp_start_thread (); 501 etp_start_thread ();
493} 502}
494 503
504static void ecb_cold
495static void etp_end_thread (void) 505etp_end_thread (void)
496{ 506{
497 eio_req *req = calloc (1, sizeof (eio_req)); 507 eio_req *req = calloc (1, sizeof (eio_req));
498 508
499 req->type = -1; 509 req->type = -1;
500 req->pri = ETP_PRI_MAX - ETP_PRI_MIN; 510 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
507 X_LOCK (wrklock); 517 X_LOCK (wrklock);
508 --started; 518 --started;
509 X_UNLOCK (wrklock); 519 X_UNLOCK (wrklock);
510} 520}
511 521
512static int etp_poll (void) 522static int
523etp_poll (void)
513{ 524{
514 unsigned int maxreqs; 525 unsigned int maxreqs;
515 unsigned int maxtime; 526 unsigned int maxtime;
516 struct timeval tv_start, tv_now; 527 struct timeval tv_start, tv_now;
517 528
547 558
548 X_LOCK (reqlock); 559 X_LOCK (reqlock);
549 --nreqs; 560 --nreqs;
550 X_UNLOCK (reqlock); 561 X_UNLOCK (reqlock);
551 562
552 if (expect_false (req->type == EIO_GROUP && req->size)) 563 if (ecb_expect_false (req->type == EIO_GROUP && req->size))
553 { 564 {
554 req->int1 = 1; /* mark request as delayed */ 565 req->int1 = 1; /* mark request as delayed */
555 continue; 566 continue;
556 } 567 }
557 else 568 else
558 { 569 {
559 int res = ETP_FINISH (req); 570 int res = ETP_FINISH (req);
560 if (expect_false (res)) 571 if (ecb_expect_false (res))
561 return res; 572 return res;
562 } 573 }
563 574
564 if (expect_false (maxreqs && !--maxreqs)) 575 if (ecb_expect_false (maxreqs && !--maxreqs))
565 break; 576 break;
566 577
567 if (maxtime) 578 if (maxtime)
568 { 579 {
569 gettimeofday (&tv_now, 0); 580 gettimeofday (&tv_now, 0);
575 586
576 errno = EAGAIN; 587 errno = EAGAIN;
577 return -1; 588 return -1;
578} 589}
579 590
591static void
580static void etp_cancel (ETP_REQ *req) 592etp_cancel (ETP_REQ *req)
581{ 593{
582 X_LOCK (wrklock); 594 req->cancelled = 1;
583 req->flags |= EIO_FLAG_CANCELLED;
584 X_UNLOCK (wrklock);
585 595
586 eio_grp_cancel (req); 596 eio_grp_cancel (req);
587} 597}
588 598
599static void
589static void etp_submit (ETP_REQ *req) 600etp_submit (ETP_REQ *req)
590{ 601{
591 req->pri -= ETP_PRI_MIN; 602 req->pri -= ETP_PRI_MIN;
592 603
593 if (expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; 604 if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN;
594 if (expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; 605 if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
595 606
596 if (expect_false (req->type == EIO_GROUP)) 607 if (ecb_expect_false (req->type == EIO_GROUP))
597 { 608 {
598 /* I hope this is worth it :/ */ 609 /* I hope this is worth it :/ */
599 X_LOCK (reqlock); 610 X_LOCK (reqlock);
600 ++nreqs; 611 ++nreqs;
601 X_UNLOCK (reqlock); 612 X_UNLOCK (reqlock);
620 631
621 etp_maybe_start_thread (); 632 etp_maybe_start_thread ();
622 } 633 }
623} 634}
624 635
636static void ecb_cold
625static void etp_set_max_poll_time (double nseconds) 637etp_set_max_poll_time (double nseconds)
626{ 638{
627 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 639 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
628 max_poll_time = nseconds * EIO_TICKS; 640 max_poll_time = nseconds * EIO_TICKS;
629 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); 641 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
630} 642}
631 643
644static void ecb_cold
632static void etp_set_max_poll_reqs (unsigned int maxreqs) 645etp_set_max_poll_reqs (unsigned int maxreqs)
633{ 646{
634 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 647 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
635 max_poll_reqs = maxreqs; 648 max_poll_reqs = maxreqs;
636 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); 649 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
637} 650}
638 651
652static void ecb_cold
639static void etp_set_max_idle (unsigned int nthreads) 653etp_set_max_idle (unsigned int nthreads)
640{ 654{
641 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 655 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
642 max_idle = nthreads; 656 max_idle = nthreads;
643 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 657 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
644} 658}
645 659
660static void ecb_cold
646static void etp_set_idle_timeout (unsigned int seconds) 661etp_set_idle_timeout (unsigned int seconds)
647{ 662{
648 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 663 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
649 idle_timeout = seconds; 664 idle_timeout = seconds;
650 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 665 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
651} 666}
652 667
668static void ecb_cold
653static void etp_set_min_parallel (unsigned int nthreads) 669etp_set_min_parallel (unsigned int nthreads)
654{ 670{
655 if (wanted < nthreads) 671 if (wanted < nthreads)
656 wanted = nthreads; 672 wanted = nthreads;
657} 673}
658 674
675static void ecb_cold
659static void etp_set_max_parallel (unsigned int nthreads) 676etp_set_max_parallel (unsigned int nthreads)
660{ 677{
661 if (wanted > nthreads) 678 if (wanted > nthreads)
662 wanted = nthreads; 679 wanted = nthreads;
663 680
664 while (started > wanted) 681 while (started > wanted)
665 etp_end_thread (); 682 etp_end_thread ();
666} 683}
667 684
668/*****************************************************************************/ 685/*****************************************************************************/
669 686
687static void
670static void grp_try_feed (eio_req *grp) 688grp_try_feed (eio_req *grp)
671{ 689{
672 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 690 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
673 { 691 {
674 grp->flags &= ~EIO_FLAG_GROUPADD; 692 grp->flags &= ~EIO_FLAG_GROUPADD;
675 693
682 break; 700 break;
683 } 701 }
684 } 702 }
685} 703}
686 704
705static int
687static int grp_dec (eio_req *grp) 706grp_dec (eio_req *grp)
688{ 707{
689 --grp->size; 708 --grp->size;
690 709
691 /* call feeder, if applicable */ 710 /* call feeder, if applicable */
692 grp_try_feed (grp); 711 grp_try_feed (grp);
696 return eio_finish (grp); 715 return eio_finish (grp);
697 else 716 else
698 return 0; 717 return 0;
699} 718}
700 719
720static void
701void eio_destroy (eio_req *req) 721eio_destroy (eio_req *req)
702{ 722{
703 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); 723 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1);
704 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); 724 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2);
705 725
706 EIO_DESTROY (req); 726 EIO_DESTROY (req);
707} 727}
708 728
729static int
709static int eio_finish (eio_req *req) 730eio_finish (eio_req *req)
710{ 731{
711 int res = EIO_FINISH (req); 732 int res = EIO_FINISH (req);
712 733
713 if (req->grp) 734 if (req->grp)
714 { 735 {
722 if (grp->grp_first == req) 743 if (grp->grp_first == req)
723 grp->grp_first = req->grp_next; 744 grp->grp_first = req->grp_next;
724 745
725 res2 = grp_dec (grp); 746 res2 = grp_dec (grp);
726 747
727 if (!res && res2) 748 if (!res)
728 res = res2; 749 res = res2;
729 } 750 }
730 751
731 eio_destroy (req); 752 eio_destroy (req);
732 753
733 return res; 754 return res;
734} 755}
735 756
757void
736void eio_grp_cancel (eio_req *grp) 758eio_grp_cancel (eio_req *grp)
737{ 759{
738 for (grp = grp->grp_first; grp; grp = grp->grp_next) 760 for (grp = grp->grp_first; grp; grp = grp->grp_next)
739 eio_cancel (grp); 761 eio_cancel (grp);
740} 762}
741 763
764void
742void eio_cancel (eio_req *req) 765eio_cancel (eio_req *req)
743{ 766{
744 etp_cancel (req); 767 etp_cancel (req);
745} 768}
746 769
770void
747void eio_submit (eio_req *req) 771eio_submit (eio_req *req)
748{ 772{
749 etp_submit (req); 773 etp_submit (req);
750} 774}
751 775
752unsigned int eio_nreqs (void) 776unsigned int
777eio_nreqs (void)
753{ 778{
754 return etp_nreqs (); 779 return etp_nreqs ();
755} 780}
756 781
757unsigned int eio_nready (void) 782unsigned int
783eio_nready (void)
758{ 784{
759 return etp_nready (); 785 return etp_nready ();
760} 786}
761 787
762unsigned int eio_npending (void) 788unsigned int
789eio_npending (void)
763{ 790{
764 return etp_npending (); 791 return etp_npending ();
765} 792}
766 793
767unsigned int eio_nthreads (void) 794unsigned int ecb_cold
795eio_nthreads (void)
768{ 796{
769 return etp_nthreads (); 797 return etp_nthreads ();
770} 798}
771 799
800void ecb_cold
772void eio_set_max_poll_time (double nseconds) 801eio_set_max_poll_time (double nseconds)
773{ 802{
774 etp_set_max_poll_time (nseconds); 803 etp_set_max_poll_time (nseconds);
775} 804}
776 805
806void ecb_cold
777void eio_set_max_poll_reqs (unsigned int maxreqs) 807eio_set_max_poll_reqs (unsigned int maxreqs)
778{ 808{
779 etp_set_max_poll_reqs (maxreqs); 809 etp_set_max_poll_reqs (maxreqs);
780} 810}
781 811
812void ecb_cold
782void eio_set_max_idle (unsigned int nthreads) 813eio_set_max_idle (unsigned int nthreads)
783{ 814{
784 etp_set_max_idle (nthreads); 815 etp_set_max_idle (nthreads);
785} 816}
786 817
818void ecb_cold
787void eio_set_idle_timeout (unsigned int seconds) 819eio_set_idle_timeout (unsigned int seconds)
788{ 820{
789 etp_set_idle_timeout (seconds); 821 etp_set_idle_timeout (seconds);
790} 822}
791 823
824void ecb_cold
792void eio_set_min_parallel (unsigned int nthreads) 825eio_set_min_parallel (unsigned int nthreads)
793{ 826{
794 etp_set_min_parallel (nthreads); 827 etp_set_min_parallel (nthreads);
795} 828}
796 829
830void ecb_cold
797void eio_set_max_parallel (unsigned int nthreads) 831eio_set_max_parallel (unsigned int nthreads)
798{ 832{
799 etp_set_max_parallel (nthreads); 833 etp_set_max_parallel (nthreads);
800} 834}
801 835
802int eio_poll (void) 836int eio_poll (void)
872#ifndef HAVE_FUTIMES 906#ifndef HAVE_FUTIMES
873 907
874# undef futimes 908# undef futimes
875# define futimes(fd,times) eio__futimes (fd, times) 909# define futimes(fd,times) eio__futimes (fd, times)
876 910
911static int
877static int eio__futimes (int fd, const struct timeval tv[2]) 912eio__futimes (int fd, const struct timeval tv[2])
878{ 913{
879 errno = ENOSYS; 914 errno = ENOSYS;
880 return -1; 915 return -1;
881} 916}
882 917
886# undef fdatasync 921# undef fdatasync
887# define fdatasync(fd) fsync (fd) 922# define fdatasync(fd) fsync (fd)
888#endif 923#endif
889 924
890/* sync_file_range always needs emulation */ 925/* sync_file_range always needs emulation */
891int 926static int
892eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) 927eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags)
893{ 928{
894#if HAVE_SYNC_FILE_RANGE 929#if HAVE_SYNC_FILE_RANGE
895 int res; 930 int res;
896 931
913 /* even though we could play tricks with the flags, it's better to always 948 /* even though we could play tricks with the flags, it's better to always
914 * call fdatasync, as that matches the expectation of its users best */ 949 * call fdatasync, as that matches the expectation of its users best */
915 return fdatasync (fd); 950 return fdatasync (fd);
916} 951}
917 952
953static int
954eio__fallocate (int fd, int mode, off_t offset, size_t len)
955{
956#if HAVE_FALLOCATE
957 return fallocate (fd, mode, offset, len);
958#else
959 errno = ENOSYS;
960 return -1;
961#endif
962}
963
918#if !HAVE_READAHEAD 964#if !HAVE_READAHEAD
919# undef readahead 965# undef readahead
920# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) 966# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self)
921 967
922static ssize_t 968static ssize_t
942 988
943/* sendfile always needs emulation */ 989/* sendfile always needs emulation */
944static ssize_t 990static ssize_t
945eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) 991eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self)
946{ 992{
993 ssize_t written = 0;
947 ssize_t res; 994 ssize_t res;
948 995
949 if (!count) 996 if (!count)
950 return 0; 997 return 0;
951 998
999 for (;;)
1000 {
1001#ifdef __APPLE__
1002# undef HAVE_SENDFILE /* broken, as everything on os x */
1003#endif
952#if HAVE_SENDFILE 1004#if HAVE_SENDFILE
953# if __linux 1005# if __linux
1006 off_t soffset = offset;
954 res = sendfile (ofd, ifd, &offset, count); 1007 res = sendfile (ofd, ifd, &soffset, count);
955 1008
956# elif __FreeBSD__ 1009# elif __FreeBSD__
957 /* 1010 /*
958 * Of course, the freebsd sendfile is a dire hack with no thoughts 1011 * Of course, the freebsd sendfile is a dire hack with no thoughts
959 * wasted on making it similar to other I/O functions. 1012 * wasted on making it similar to other I/O functions.
960 */ 1013 */
961 {
962 off_t sbytes; 1014 off_t sbytes;
963 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 1015 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
964 1016
965 #if 0 /* according to the manpage, this is correct, but broken behaviour */ 1017 #if 0 /* according to the manpage, this is correct, but broken behaviour */
966 /* freebsd' sendfile will return 0 on success */ 1018 /* freebsd' sendfile will return 0 on success */
967 /* freebsd 8 documents it as only setting *sbytes on EINTR and EAGAIN, but */ 1019 /* freebsd 8 documents it as only setting *sbytes on EINTR and EAGAIN, but */
968 /* not on e.g. EIO or EPIPE - sounds broken */ 1020 /* not on e.g. EIO or EPIPE - sounds broken */
969 if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0) 1021 if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0)
970 res = sbytes; 1022 res = sbytes;
971 #endif 1023 #endif
972 1024
973 /* according to source inspection, this is correct, and useful behaviour */ 1025 /* according to source inspection, this is correct, and useful behaviour */
974 if (sbytes) 1026 if (sbytes)
975 res = sbytes; 1027 res = sbytes;
976 }
977 1028
978# elif defined (__APPLE__) 1029# elif defined (__APPLE__)
979
980 {
981 off_t sbytes = count; 1030 off_t sbytes = count;
982 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); 1031 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0);
983 1032
984 /* according to the manpage, sbytes is always valid */ 1033 /* according to the manpage, sbytes is always valid */
985 if (sbytes) 1034 if (sbytes)
986 res = sbytes; 1035 res = sbytes;
987 }
988 1036
989# elif __hpux 1037# elif __hpux
990 res = sendfile (ofd, ifd, offset, count, 0, 0); 1038 res = sendfile (ofd, ifd, offset, count, 0, 0);
991 1039
992# elif __solaris 1040# elif __solaris
993 {
994 struct sendfilevec vec; 1041 struct sendfilevec vec;
995 size_t sbytes; 1042 size_t sbytes;
996 1043
997 vec.sfv_fd = ifd; 1044 vec.sfv_fd = ifd;
998 vec.sfv_flag = 0; 1045 vec.sfv_flag = 0;
999 vec.sfv_off = offset; 1046 vec.sfv_off = offset;
1000 vec.sfv_len = count; 1047 vec.sfv_len = count;
1001 1048
1002 res = sendfilev (ofd, &vec, 1, &sbytes); 1049 res = sendfilev (ofd, &vec, 1, &sbytes);
1003 1050
1004 if (res < 0 && sbytes) 1051 if (res < 0 && sbytes)
1005 res = sbytes; 1052 res = sbytes;
1006 }
1007 1053
1008# endif 1054# endif
1009 1055
1010#elif defined (_WIN32) 1056#elif defined (_WIN32)
1011
1012 /* does not work, just for documentation of what would need to be done */ 1057 /* does not work, just for documentation of what would need to be done */
1013 { 1058 /* actually, cannot be done like this, as TransmitFile changes the file offset, */
1059 /* libeio guarantees that the file offset does not change, and windows */
1060 /* has no way to get an independent handle to the same file description */
1014 HANDLE h = TO_SOCKET (ifd); 1061 HANDLE h = TO_SOCKET (ifd);
1015 SetFilePointer (h, offset, 0, FILE_BEGIN); 1062 SetFilePointer (h, offset, 0, FILE_BEGIN);
1016 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); 1063 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0);
1017 }
1018 1064
1019#else 1065#else
1020 res = -1; 1066 res = -1;
1021 errno = ENOSYS; 1067 errno = ENOSYS;
1022#endif 1068#endif
1023 1069
1070 /* we assume sendfile can copy at least 128mb in one go */
1071 if (res <= 128 * 1024 * 1024)
1072 {
1073 if (res > 0)
1074 written += res;
1075
1076 if (written)
1077 return written;
1078
1079 break;
1080 }
1081 else
1082 {
1083 /* if we requested more, then probably the kernel was lazy */
1084 written += res;
1085 offset += res;
1086 count -= res;
1087
1088 if (!count)
1089 return written;
1090 }
1091 }
1092
1024 if (res < 0 1093 if (res < 0
1025 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK 1094 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
1026 /* BSDs */ 1095 /* BSDs */
1027#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */ 1096#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */
1028 || errno == ENOTSUP 1097 || errno == ENOTSUP
1029#endif 1098#endif
1064 count -= cnt; 1133 count -= cnt;
1065 } 1134 }
1066 } 1135 }
1067 1136
1068 return res; 1137 return res;
1138}
1139
1140#ifdef PAGESIZE
1141# define eio_pagesize() PAGESIZE
1142#else
1143static intptr_t
1144eio_pagesize (void)
1145{
1146 static intptr_t page;
1147
1148 if (!page)
1149 page = sysconf (_SC_PAGESIZE);
1150
1151 return page;
1152}
1153#endif
1154
1155static void
1156eio_page_align (void **addr, size_t *length)
1157{
1158 intptr_t mask = eio_pagesize () - 1;
1159
1160 /* round down addr */
1161 intptr_t adj = mask & (intptr_t)*addr;
1162
1163 *addr = (void *)((intptr_t)*addr - adj);
1164 *length += adj;
1165
1166 /* round up length */
1167 *length = (*length + mask) & ~mask;
1168}
1169
1170#if !_POSIX_MEMLOCK
1171# define eio__mlockall(a) ((errno = ENOSYS), -1)
1172#else
1173
1174static int
1175eio__mlockall (int flags)
1176{
1177 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1178 extern int mallopt (int, int);
1179 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1180 #endif
1181
1182 if (EIO_MCL_CURRENT != MCL_CURRENT
1183 || EIO_MCL_FUTURE != MCL_FUTURE)
1184 {
1185 flags = 0
1186 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0)
1187 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0);
1188 }
1189
1190 return mlockall (flags);
1191}
1192#endif
1193
1194#if !_POSIX_MEMLOCK_RANGE
1195# define eio__mlock(a,b) ((errno = ENOSYS), -1)
1196#else
1197
1198static int
1199eio__mlock (void *addr, size_t length)
1200{
1201 eio_page_align (&addr, &length);
1202
1203 return mlock (addr, length);
1204}
1205
1206#endif
1207
1208#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1209# define eio__msync(a,b,c) ((errno = ENOSYS), -1)
1210#else
1211
1212static int
1213eio__msync (void *mem, size_t len, int flags)
1214{
1215 eio_page_align (&mem, &len);
1216
1217 if (EIO_MS_ASYNC != MS_SYNC
1218 || EIO_MS_INVALIDATE != MS_INVALIDATE
1219 || EIO_MS_SYNC != MS_SYNC)
1220 {
1221 flags = 0
1222 | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0)
1223 | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0)
1224 | (flags & EIO_MS_SYNC ? MS_SYNC : 0);
1225 }
1226
1227 return msync (mem, len, flags);
1228}
1229
1230#endif
1231
1232static int
1233eio__mtouch (eio_req *req)
1234{
1235 void *mem = req->ptr2;
1236 size_t len = req->size;
1237 int flags = req->int1;
1238
1239 eio_page_align (&mem, &len);
1240
1241 {
1242 intptr_t addr = (intptr_t)mem;
1243 intptr_t end = addr + len;
1244 intptr_t page = eio_pagesize ();
1245
1246 if (addr < end)
1247 if (flags & EIO_MT_MODIFY) /* modify */
1248 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req));
1249 else
1250 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req));
1251 }
1252
1253 return 0;
1254}
1255
1256/*****************************************************************************/
1257/* requests implemented outside eio_execute, because they are so large */
1258
1259static void
1260eio__realpath (eio_req *req, etp_worker *self)
1261{
1262 char *rel = req->ptr1;
1263 char *res;
1264 char *tmp1, *tmp2;
1265#if SYMLOOP_MAX > 32
1266 int symlinks = SYMLOOP_MAX;
1267#else
1268 int symlinks = 32;
1269#endif
1270
1271 req->result = -1;
1272
1273 errno = EINVAL;
1274 if (!rel)
1275 return;
1276
1277 errno = ENOENT;
1278 if (!*rel)
1279 return;
1280
1281 if (!req->ptr2)
1282 {
1283 X_LOCK (wrklock);
1284 req->flags |= EIO_FLAG_PTR2_FREE;
1285 X_UNLOCK (wrklock);
1286 req->ptr2 = malloc (PATH_MAX * 3);
1287
1288 errno = ENOMEM;
1289 if (!req->ptr2)
1290 return;
1291 }
1292
1293 res = req->ptr2;
1294 tmp1 = res + PATH_MAX;
1295 tmp2 = tmp1 + PATH_MAX;
1296
1297#if 0 /* disabled, the musl way to do things is just too racy */
1298#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1299 /* on linux we may be able to ask the kernel */
1300 {
1301 int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME);
1302
1303 if (fd >= 0)
1304 {
1305 sprintf (tmp1, "/proc/self/fd/%d", fd);
1306 req->result = readlink (tmp1, res, PATH_MAX);
1307 close (fd);
1308
1309 /* here we should probably stat the open file and the disk file, to make sure they still match */
1310
1311 if (req->result > 0)
1312 goto done;
1313 }
1314 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO)
1315 return;
1316 }
1317#endif
1318#endif
1319
1320 if (*rel != '/')
1321 {
1322 if (!getcwd (res, PATH_MAX))
1323 return;
1324
1325 if (res [1]) /* only use if not / */
1326 res += strlen (res);
1327 }
1328
1329 while (*rel)
1330 {
1331 ssize_t len, linklen;
1332 char *beg = rel;
1333
1334 while (*rel && *rel != '/')
1335 ++rel;
1336
1337 len = rel - beg;
1338
1339 if (!len) /* skip slashes */
1340 {
1341 ++rel;
1342 continue;
1343 }
1344
1345 if (beg [0] == '.')
1346 {
1347 if (len == 1)
1348 continue; /* . - nop */
1349
1350 if (beg [1] == '.' && len == 2)
1351 {
1352 /* .. - back up one component, if possible */
1353
1354 while (res != req->ptr2)
1355 if (*--res == '/')
1356 break;
1357
1358 continue;
1359 }
1360 }
1361
1362 errno = ENAMETOOLONG;
1363 if (res + 1 + len + 1 >= tmp1)
1364 return;
1365
1366 /* copy one component */
1367 *res = '/';
1368 memcpy (res + 1, beg, len);
1369
1370 /* zero-terminate, for readlink */
1371 res [len + 1] = 0;
1372
1373 /* now check if it's a symlink */
1374 linklen = readlink (req->ptr2, tmp1, PATH_MAX);
1375
1376 if (linklen < 0)
1377 {
1378 if (errno != EINVAL)
1379 return;
1380
1381 /* it's a normal directory. hopefully */
1382 res += len + 1;
1383 }
1384 else
1385 {
1386 /* yay, it was a symlink - build new path in tmp2 */
1387 int rellen = strlen (rel);
1388
1389 errno = ENAMETOOLONG;
1390 if (linklen + 1 + rellen >= PATH_MAX)
1391 return;
1392
1393 errno = ELOOP;
1394 if (!--symlinks)
1395 return;
1396
1397 if (*tmp1 == '/')
1398 res = req->ptr2; /* symlink resolves to an absolute path */
1399
1400 /* we need to be careful, as rel might point into tmp2 already */
1401 memmove (tmp2 + linklen + 1, rel, rellen + 1);
1402 tmp2 [linklen] = '/';
1403 memcpy (tmp2, tmp1, linklen);
1404
1405 rel = tmp2;
1406 }
1407 }
1408
1409 /* special case for the lone root path */
1410 if (res == req->ptr2)
1411 *res++ = '/';
1412
1413 req->result = res - (char *)req->ptr2;
1414
1415done:
1416 req->ptr2 = realloc (req->ptr2, req->result); /* trade time for space savings */
1069} 1417}
1070 1418
1071static signed char 1419static signed char
1072eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1420eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1073{ 1421{
1255 1603
1256 X_LOCK (wrklock); 1604 X_LOCK (wrklock);
1257 /* the corresponding closedir is in ETP_WORKER_CLEAR */ 1605 /* the corresponding closedir is in ETP_WORKER_CLEAR */
1258 self->dirp = dirp = opendir (req->ptr1); 1606 self->dirp = dirp = opendir (req->ptr1);
1259 1607
1608 if (req->flags & EIO_FLAG_PTR1_FREE)
1609 free (req->ptr1);
1610
1260 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; 1611 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE;
1261 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; 1612 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0;
1262 req->ptr2 = names = malloc (namesalloc); 1613 req->ptr2 = names = malloc (namesalloc);
1263 X_UNLOCK (wrklock); 1614 X_UNLOCK (wrklock);
1264 1615
1315 /* skip . and .. entries */ 1666 /* skip . and .. entries */
1316 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 1667 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
1317 { 1668 {
1318 int len = D_NAMLEN (entp) + 1; 1669 int len = D_NAMLEN (entp) + 1;
1319 1670
1320 while (expect_false (namesoffs + len > namesalloc)) 1671 while (ecb_expect_false (namesoffs + len > namesalloc))
1321 { 1672 {
1322 namesalloc *= 2; 1673 namesalloc *= 2;
1323 X_LOCK (wrklock); 1674 X_LOCK (wrklock);
1324 req->ptr2 = names = realloc (names, namesalloc); 1675 req->ptr2 = names = realloc (names, namesalloc);
1325 X_UNLOCK (wrklock); 1676 X_UNLOCK (wrklock);
1332 1683
1333 if (dents) 1684 if (dents)
1334 { 1685 {
1335 struct eio_dirent *ent; 1686 struct eio_dirent *ent;
1336 1687
1337 if (expect_false (dentoffs == dentalloc)) 1688 if (ecb_expect_false (dentoffs == dentalloc))
1338 { 1689 {
1339 dentalloc *= 2; 1690 dentalloc *= 2;
1340 X_LOCK (wrklock); 1691 X_LOCK (wrklock);
1341 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); 1692 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent));
1342 X_UNLOCK (wrklock); 1693 X_UNLOCK (wrklock);
1430 break; 1781 break;
1431 } 1782 }
1432 } 1783 }
1433} 1784}
1434 1785
1435#ifdef PAGESIZE
1436# define eio_pagesize() PAGESIZE
1437#else
1438static intptr_t
1439eio_pagesize (void)
1440{
1441 static intptr_t page;
1442
1443 if (!page)
1444 page = sysconf (_SC_PAGESIZE);
1445
1446 return page;
1447}
1448#endif
1449
1450static void
1451eio_page_align (void **addr, size_t *length)
1452{
1453 intptr_t mask = eio_pagesize () - 1;
1454
1455 /* round down addr */
1456 intptr_t adj = mask & (intptr_t)*addr;
1457
1458 *addr = (void *)((intptr_t)*addr - adj);
1459 *length += adj;
1460
1461 /* round up length */
1462 *length = (*length + mask) & ~mask;
1463}
1464
1465#if !_POSIX_MEMLOCK
1466# define eio__mlockall(a) ((errno = ENOSYS), -1)
1467#else
1468
1469static int
1470eio__mlockall (int flags)
1471{
1472 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1473 extern int mallopt (int, int);
1474 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1475 #endif
1476
1477 if (EIO_MCL_CURRENT != MCL_CURRENT
1478 || EIO_MCL_FUTURE != MCL_FUTURE)
1479 {
1480 flags = 0
1481 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0)
1482 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0);
1483 }
1484
1485 return mlockall (flags);
1486}
1487#endif
1488
1489#if !_POSIX_MEMLOCK_RANGE
1490# define eio__mlock(a,b) ((errno = ENOSYS), -1)
1491#else
1492
1493static int
1494eio__mlock (void *addr, size_t length)
1495{
1496 eio_page_align (&addr, &length);
1497
1498 return mlock (addr, length);
1499}
1500
1501#endif
1502
1503#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1504# define eio__msync(a,b,c) ((errno = ENOSYS), -1)
1505#else
1506
1507int
1508eio__msync (void *mem, size_t len, int flags)
1509{
1510 eio_page_align (&mem, &len);
1511
1512 if (EIO_MS_ASYNC != MS_SYNC
1513 || EIO_MS_INVALIDATE != MS_INVALIDATE
1514 || EIO_MS_SYNC != MS_SYNC)
1515 {
1516 flags = 0
1517 | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0)
1518 | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0)
1519 | (flags & EIO_MS_SYNC ? MS_SYNC : 0);
1520 }
1521
1522 return msync (mem, len, flags);
1523}
1524
1525#endif
1526
1527int
1528eio__mtouch (void *mem, size_t len, int flags)
1529{
1530 eio_page_align (&mem, &len);
1531
1532 {
1533 intptr_t addr = (intptr_t)mem;
1534 intptr_t end = addr + len;
1535 intptr_t page = eio_pagesize ();
1536
1537 if (addr < end)
1538 if (flags & EIO_MT_MODIFY) /* modify */
1539 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len);
1540 else
1541 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len);
1542 }
1543
1544 return 0;
1545}
1546
1547/*****************************************************************************/ 1786/*****************************************************************************/
1548 1787
1549#define ALLOC(len) \ 1788#define ALLOC(len) \
1550 if (!req->ptr2) \ 1789 if (!req->ptr2) \
1551 { \ 1790 { \
1608 X_UNLOCK (reqlock); 1847 X_UNLOCK (reqlock);
1609 1848
1610 if (req->type < 0) 1849 if (req->type < 0)
1611 goto quit; 1850 goto quit;
1612 1851
1613 if (!EIO_CANCELLED (req))
1614 ETP_EXECUTE (self, req); 1852 ETP_EXECUTE (self, req);
1615 1853
1616 X_LOCK (reslock); 1854 X_LOCK (reslock);
1617 1855
1618 ++npending; 1856 ++npending;
1619 1857
1634 return 0; 1872 return 0;
1635} 1873}
1636 1874
1637/*****************************************************************************/ 1875/*****************************************************************************/
1638 1876
1877int ecb_cold
1639int eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1878eio_init (void (*want_poll)(void), void (*done_poll)(void))
1640{ 1879{
1641 return etp_init (want_poll, done_poll); 1880 return etp_init (want_poll, done_poll);
1642} 1881}
1643 1882
1883ecb_inline void
1644static void eio_api_destroy (eio_req *req) 1884eio_api_destroy (eio_req *req)
1645{ 1885{
1646 free (req); 1886 free (req);
1647} 1887}
1648 1888
1649#define REQ(rtype) \ 1889#define REQ(rtype) \
1668 { \ 1908 { \
1669 eio_api_destroy (req); \ 1909 eio_api_destroy (req); \
1670 return 0; \ 1910 return 0; \
1671 } 1911 }
1672 1912
1913static void
1673static void eio_execute (etp_worker *self, eio_req *req) 1914eio_execute (etp_worker *self, eio_req *req)
1674{ 1915{
1916 if (ecb_expect_false (EIO_CANCELLED (req)))
1917 {
1918 req->result = -1;
1919 req->errorno = ECANCELED;
1920 return;
1921 }
1922
1675 switch (req->type) 1923 switch (req->type)
1676 { 1924 {
1677 case EIO_READ: ALLOC (req->size); 1925 case EIO_READ: ALLOC (req->size);
1678 req->result = req->offs >= 0 1926 req->result = req->offs >= 0
1679 ? pread (req->int1, req->ptr2, req->size, req->offs) 1927 ? pread (req->int1, req->ptr2, req->size, req->offs)
1713 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; 1961 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break;
1714 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; 1962 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break;
1715 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; 1963 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break;
1716 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; 1964 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
1717 1965
1966 case EIO_REALPATH: eio__realpath (req, self); break;
1967
1718 case EIO_READLINK: ALLOC (PATH_MAX); 1968 case EIO_READLINK: ALLOC (PATH_MAX);
1719 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break; 1969 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break;
1720 1970
1721 case EIO_SYNC: req->result = 0; sync (); break; 1971 case EIO_SYNC: req->result = 0; sync (); break;
1722 case EIO_FSYNC: req->result = fsync (req->int1); break; 1972 case EIO_FSYNC: req->result = fsync (req->int1); break;
1723 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; 1973 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
1724 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; 1974 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break;
1725 case EIO_MTOUCH: req->result = eio__mtouch (req->ptr2, req->size, req->int1); break; 1975 case EIO_MTOUCH: req->result = eio__mtouch (req); break;
1726 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; 1976 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break;
1727 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; 1977 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break;
1728 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; 1978 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break;
1979 case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break;
1729 1980
1730 case EIO_READDIR: eio__scandir (req, self); break; 1981 case EIO_READDIR: eio__scandir (req, self); break;
1731 1982
1732 case EIO_BUSY: 1983 case EIO_BUSY:
1733#ifdef _WIN32 1984#ifdef _WIN32
1774 case EIO_NOP: 2025 case EIO_NOP:
1775 req->result = 0; 2026 req->result = 0;
1776 break; 2027 break;
1777 2028
1778 case EIO_CUSTOM: 2029 case EIO_CUSTOM:
1779 ((void (*)(eio_req *))req->feed) (req); 2030 req->feed (req);
1780 break; 2031 break;
1781 2032
1782 default: 2033 default:
1783 errno = ENOSYS; 2034 errno = ENOSYS;
1784 req->result = -1; 2035 req->result = -1;
1833eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) 2084eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data)
1834{ 2085{
1835 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; 2086 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND;
1836} 2087}
1837 2088
2089eio_req *eio_fallocate (int fd, int mode, off_t offset, size_t len, int pri, eio_cb cb, void *data)
2090{
2091 REQ (EIO_FALLOCATE); req->int1 = fd; req->int2 = mode; req->offs = offset; req->size = len; SEND;
2092}
2093
1838eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) 2094eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data)
1839{ 2095{
1840 REQ (EIO_FDATASYNC); req->int1 = fd; SEND; 2096 REQ (EIO_FDATASYNC); req->int1 = fd; SEND;
1841} 2097}
1842 2098
1937} 2193}
1938 2194
1939eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data) 2195eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data)
1940{ 2196{
1941 return eio__1path (EIO_READLINK, path, pri, cb, data); 2197 return eio__1path (EIO_READLINK, path, pri, cb, data);
2198}
2199
2200eio_req *eio_realpath (const char *path, int pri, eio_cb cb, void *data)
2201{
2202 return eio__1path (EIO_REALPATH, path, pri, cb, data);
1942} 2203}
1943 2204
1944eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data) 2205eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data)
1945{ 2206{
1946 return eio__1path (EIO_STAT, path, pri, cb, data); 2207 return eio__1path (EIO_STAT, path, pri, cb, data);
2005eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) 2266eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
2006{ 2267{
2007 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); 2268 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
2008} 2269}
2009 2270
2010eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) 2271eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data)
2011{ 2272{
2012 REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND; 2273 REQ (EIO_CUSTOM); req->feed = execute; SEND;
2013} 2274}
2014 2275
2015#endif 2276#endif
2016 2277
2017eio_req *eio_grp (eio_cb cb, void *data) 2278eio_req *eio_grp (eio_cb cb, void *data)
2026#undef SEND 2287#undef SEND
2027 2288
2028/*****************************************************************************/ 2289/*****************************************************************************/
2029/* grp functions */ 2290/* grp functions */
2030 2291
2292void
2031void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) 2293eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit)
2032{ 2294{
2033 grp->int2 = limit; 2295 grp->int2 = limit;
2034 grp->feed = feed; 2296 grp->feed = feed;
2035 2297
2036 grp_try_feed (grp); 2298 grp_try_feed (grp);
2037} 2299}
2038 2300
2301void
2039void eio_grp_limit (eio_req *grp, int limit) 2302eio_grp_limit (eio_req *grp, int limit)
2040{ 2303{
2041 grp->int2 = limit; 2304 grp->int2 = limit;
2042 2305
2043 grp_try_feed (grp); 2306 grp_try_feed (grp);
2044} 2307}
2045 2308
2309void
2046void eio_grp_add (eio_req *grp, eio_req *req) 2310eio_grp_add (eio_req *grp, eio_req *req)
2047{ 2311{
2048 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); 2312 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
2049 2313
2050 grp->flags |= EIO_FLAG_GROUPADD; 2314 grp->flags |= EIO_FLAG_GROUPADD;
2051 2315
2062} 2326}
2063 2327
2064/*****************************************************************************/ 2328/*****************************************************************************/
2065/* misc garbage */ 2329/* misc garbage */
2066 2330
2331ssize_t
2067ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) 2332eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
2068{ 2333{
2069 etp_worker wrk; 2334 etp_worker wrk;
2070 ssize_t ret; 2335 ssize_t ret;
2071 2336
2072 wrk.dbuf = 0; 2337 wrk.dbuf = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines