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

Comparing libeio/eio.c (file contents):
Revision 1.121 by root, Sat Jun 2 20:13:26 2012 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 *
10 * 1. Redistributions of source code must retain the above copyright notice, 10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer. 11 * this list of conditions and the following disclaimer.
12 * 12 *
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution. 15 * documentation and/or other materials provided with the distribution.
16 * 16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
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 */
131 #define mknod(path,mode,dev) EIO_ENOSYS () 132 #define mknod(path,mode,dev) EIO_ENOSYS ()
132 #define sync() EIO_ENOSYS () 133 #define sync() EIO_ENOSYS ()
133 #define readlink(path,buf,s) EIO_ENOSYS () 134 #define readlink(path,buf,s) EIO_ENOSYS ()
134 #define statvfs(path,buf) EIO_ENOSYS () 135 #define statvfs(path,buf) EIO_ENOSYS ()
135 #define fstatvfs(fd,buf) EIO_ENOSYS () 136 #define fstatvfs(fd,buf) EIO_ENOSYS ()
137
138 #define pread(fd,buf,count,offset) eio__pread (fd, buf, count, offset)
139 #define pwrite(fd,buf,count,offset) eio__pwrite (fd, buf, count, offset)
140
141 #if __GNUC__
142 typedef long long eio_off_t; /* signed for compatibility to msvc */
143 #else
144 typedef __int64 eio_off_t; /* unsigned not supported by msvc */
145 #endif
146
147 static eio_ssize_t
148 eio__pread (int fd, void *buf, eio_ssize_t count, eio_off_t offset)
149 {
150 OVERLAPPED o = { 0 };
151 DWORD got;
152
153 o.Offset = offset;
154 o.OffsetHigh = offset >> 32;
155
156 return ReadFile ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd), buf, count, &got, &o)
157 ? got : -1;
158 }
159
160 static eio_ssize_t
161 eio__pwrite (int fd, void *buf, eio_ssize_t count, eio_off_t offset)
162 {
163 OVERLAPPED o = { 0 };
164 DWORD got;
165
166 o.Offset = offset;
167 o.OffsetHigh = offset >> 32;
168
169 return WriteFile ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd), buf, count, &got, &o)
170 ? got : -1;
171 }
136 172
137 /* rename() uses MoveFile, which fails to overwrite */ 173 /* rename() uses MoveFile, which fails to overwrite */
138 #define rename(old,neu) eio__rename (old, neu) 174 #define rename(old,neu) eio__rename (old, neu)
139 175
140 static int 176 static int
178 #endif 214 #endif
179 215
180 return EIO_ERRNO (ENOENT, -1); 216 return EIO_ERRNO (ENOENT, -1);
181 } 217 }
182 218
183 /* POSIX API only */ 219 /* POSIX API only, causing trouble for win32 apps */
184 #define CreateHardLink(neu,old,flags) 0 220 #define CreateHardLink(neu,old,flags) 0 /* not really creating hardlink, still using relative paths? */
185 #define CreateSymbolicLink(neu,old,flags) 0 221 #define CreateSymbolicLink(neu,old,flags) 0 /* vista+ only */
186 222
187 struct statvfs 223 struct statvfs
188 { 224 {
189 int dummy; 225 int dummy;
190 }; 226 };
196 232
197#else 233#else
198 234
199 #include <sys/time.h> 235 #include <sys/time.h>
200 #include <sys/select.h> 236 #include <sys/select.h>
201 #include <sys/statvfs.h>
202 #include <unistd.h> 237 #include <unistd.h>
203 #include <signal.h> 238 #include <signal.h>
204 #include <dirent.h> 239 #include <dirent.h>
240
241 #ifdef ANDROID
242 #include <sys/vfs.h>
243 #define statvfs statfs
244 #define fstatvfs fstatfs
245 #include <asm/page.h> /* supposedly limits.h does #define PAGESIZE PAGESIZE */
246 #else
247 #include <sys/statvfs.h>
248 #endif
205 249
206 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES 250 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
207 #include <sys/mman.h> 251 #include <sys/mman.h>
208 #endif 252 #endif
209 253
281#endif 325#endif
282 326
283/* buffer size for various temporary buffers */ 327/* buffer size for various temporary buffers */
284#define EIO_BUFSIZE 65536 328#define EIO_BUFSIZE 65536
285 329
286#define dBUF \ 330#define dBUF \
287 char *eio_buf = malloc (EIO_BUFSIZE); \ 331 char *eio_buf = malloc (EIO_BUFSIZE); \
288 errno = ENOMEM; \ 332 errno = ENOMEM; \
289 if (!eio_buf) \ 333 if (!eio_buf) \
290 return -1 334 return -1
291 335
292#define FUBd \ 336#define FUBd \
293 free (eio_buf) 337 free (eio_buf)
294 338
295#define EIO_TICKS ((1000000 + 1023) >> 10)
296
297/*****************************************************************************/ 339/*****************************************************************************/
298 340
299struct tmpbuf
300{
301 void *ptr;
302 int len;
303};
304
305static void *
306tmpbuf_get (struct tmpbuf *buf, int len)
307{
308 if (buf->len < len)
309 {
310 free (buf->ptr);
311 buf->ptr = malloc (buf->len = len);
312 }
313
314 return buf->ptr;
315}
316
317struct tmpbuf; 341struct etp_tmpbuf;
318 342
319#if _POSIX_VERSION >= 200809L 343#if _POSIX_VERSION >= 200809L
320 #define HAVE_AT 1 344 #define HAVE_AT 1
321 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) 345 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD)
322 #ifndef O_SEARCH 346 #ifndef O_SEARCH
323 #define O_SEARCH O_RDONLY 347 #define O_SEARCH O_RDONLY
324 #endif 348 #endif
325#else 349#else
326 #define HAVE_AT 0 350 #define HAVE_AT 0
327 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);
328#endif 352#endif
329 353
330struct eio_pwd 354struct eio_pwd
331{ 355{
332#if HAVE_AT 356#if HAVE_AT
339/*****************************************************************************/ 363/*****************************************************************************/
340 364
341#define ETP_PRI_MIN EIO_PRI_MIN 365#define ETP_PRI_MIN EIO_PRI_MIN
342#define ETP_PRI_MAX EIO_PRI_MAX 366#define ETP_PRI_MAX EIO_PRI_MAX
343 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
344struct etp_worker; 378struct etp_worker;
345
346#define ETP_REQ eio_req 379#define ETP_REQ eio_req
347#define ETP_DESTROY(req) eio_destroy (req) 380#define ETP_DESTROY(req) eio_destroy (req)
348static int eio_finish (eio_req *req); 381static int eio_finish (eio_req *req);
349#define ETP_FINISH(req) eio_finish (req) 382#define ETP_FINISH(req) eio_finish (req)
350static void eio_execute (struct etp_worker *self, eio_req *req); 383static void eio_execute (struct etp_worker *self, eio_req *req);
351#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) 384#define ETP_EXECUTE(wrk,req) eio_execute (wrk, req)
352 385
353/*****************************************************************************/ 386#include "etp.c"
354 387
355#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 388static struct etp_pool eio_pool;
356 389#define EIO_POOL (&eio_pool)
357/* calculate time difference in ~1/EIO_TICKS of a second */
358ecb_inline int
359tvdiff (struct timeval *tv1, struct timeval *tv2)
360{
361 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
362 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
363}
364
365static unsigned int started, idle, wanted = 4;
366
367static void (*want_poll_cb) (void);
368static void (*done_poll_cb) (void);
369
370static unsigned int max_poll_time; /* reslock */
371static unsigned int max_poll_reqs; /* reslock */
372
373static unsigned int nreqs; /* reqlock */
374static unsigned int nready; /* reqlock */
375static unsigned int npending; /* reqlock */
376static unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */
377static unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */
378
379static xmutex_t wrklock;
380static xmutex_t reslock;
381static xmutex_t reqlock;
382static xcond_t reqwait;
383
384typedef struct etp_worker
385{
386 struct tmpbuf tmpbuf;
387
388 /* locked by wrklock */
389 struct etp_worker *prev, *next;
390
391 xthread_t tid;
392
393#ifdef ETP_WORKER_COMMON
394 ETP_WORKER_COMMON
395#endif
396} etp_worker;
397
398static etp_worker wrk_first; /* NOT etp */
399
400#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
401#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
402
403/* worker threads management */
404
405static void
406etp_worker_clear (etp_worker *wrk)
407{
408}
409
410static void ecb_cold
411etp_worker_free (etp_worker *wrk)
412{
413 free (wrk->tmpbuf.ptr);
414
415 wrk->next->prev = wrk->prev;
416 wrk->prev->next = wrk->next;
417
418 free (wrk);
419}
420
421static unsigned int
422etp_nreqs (void)
423{
424 int retval;
425 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
426 retval = nreqs;
427 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
428 return retval;
429}
430
431static unsigned int
432etp_nready (void)
433{
434 unsigned int retval;
435
436 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
437 retval = nready;
438 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
439
440 return retval;
441}
442
443static unsigned int
444etp_npending (void)
445{
446 unsigned int retval;
447
448 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
449 retval = npending;
450 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
451
452 return retval;
453}
454
455static unsigned int
456etp_nthreads (void)
457{
458 unsigned int retval;
459
460 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
461 retval = started;
462 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
463
464 return retval;
465}
466
467/*
468 * a somewhat faster data structure might be nice, but
469 * with 8 priorities this actually needs <20 insns
470 * per shift, the most expensive operation.
471 */
472typedef struct {
473 ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */
474 int size;
475} etp_reqq;
476
477static etp_reqq req_queue;
478static etp_reqq res_queue;
479
480static void ecb_noinline ecb_cold
481reqq_init (etp_reqq *q)
482{
483 int pri;
484
485 for (pri = 0; pri < ETP_NUM_PRI; ++pri)
486 q->qs[pri] = q->qe[pri] = 0;
487
488 q->size = 0;
489}
490
491static int ecb_noinline
492reqq_push (etp_reqq *q, ETP_REQ *req)
493{
494 int pri = req->pri;
495 req->next = 0;
496
497 if (q->qe[pri])
498 {
499 q->qe[pri]->next = req;
500 q->qe[pri] = req;
501 }
502 else
503 q->qe[pri] = q->qs[pri] = req;
504
505 return q->size++;
506}
507
508static ETP_REQ * ecb_noinline
509reqq_shift (etp_reqq *q)
510{
511 int pri;
512
513 if (!q->size)
514 return 0;
515
516 --q->size;
517
518 for (pri = ETP_NUM_PRI; pri--; )
519 {
520 eio_req *req = q->qs[pri];
521
522 if (req)
523 {
524 if (!(q->qs[pri] = (eio_req *)req->next))
525 q->qe[pri] = 0;
526
527 return req;
528 }
529 }
530
531 abort ();
532}
533
534static int ecb_cold
535etp_init (void (*want_poll)(void), void (*done_poll)(void))
536{
537 X_MUTEX_CREATE (wrklock);
538 X_MUTEX_CREATE (reslock);
539 X_MUTEX_CREATE (reqlock);
540 X_COND_CREATE (reqwait);
541
542 reqq_init (&req_queue);
543 reqq_init (&res_queue);
544
545 wrk_first.next =
546 wrk_first.prev = &wrk_first;
547
548 started = 0;
549 idle = 0;
550 nreqs = 0;
551 nready = 0;
552 npending = 0;
553
554 want_poll_cb = want_poll;
555 done_poll_cb = done_poll;
556
557 return 0;
558}
559
560X_THREAD_PROC (etp_proc);
561
562static void ecb_cold
563etp_start_thread (void)
564{
565 etp_worker *wrk = calloc (1, sizeof (etp_worker));
566
567 /*TODO*/
568 assert (("unable to allocate worker thread data", wrk));
569
570 X_LOCK (wrklock);
571
572 if (thread_create (&wrk->tid, etp_proc, (void *)wrk))
573 {
574 wrk->prev = &wrk_first;
575 wrk->next = wrk_first.next;
576 wrk_first.next->prev = wrk;
577 wrk_first.next = wrk;
578 ++started;
579 }
580 else
581 free (wrk);
582
583 X_UNLOCK (wrklock);
584}
585
586static void
587etp_maybe_start_thread (void)
588{
589 if (ecb_expect_true (etp_nthreads () >= wanted))
590 return;
591
592 /* todo: maybe use idle here, but might be less exact */
593 if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()))
594 return;
595
596 etp_start_thread ();
597}
598
599static void ecb_cold
600etp_end_thread (void)
601{
602 eio_req *req = calloc (1, sizeof (eio_req)); /* will be freed by worker */
603
604 req->type = -1;
605 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
606
607 X_LOCK (reqlock);
608 reqq_push (&req_queue, req);
609 X_COND_SIGNAL (reqwait);
610 X_UNLOCK (reqlock);
611
612 X_LOCK (wrklock);
613 --started;
614 X_UNLOCK (wrklock);
615}
616
617static int
618etp_poll (void)
619{
620 unsigned int maxreqs;
621 unsigned int maxtime;
622 struct timeval tv_start, tv_now;
623
624 X_LOCK (reslock);
625 maxreqs = max_poll_reqs;
626 maxtime = max_poll_time;
627 X_UNLOCK (reslock);
628
629 if (maxtime)
630 gettimeofday (&tv_start, 0);
631
632 for (;;)
633 {
634 ETP_REQ *req;
635
636 etp_maybe_start_thread ();
637
638 X_LOCK (reslock);
639 req = reqq_shift (&res_queue);
640
641 if (req)
642 {
643 --npending;
644
645 if (!res_queue.size && done_poll_cb)
646 done_poll_cb ();
647 }
648
649 X_UNLOCK (reslock);
650
651 if (!req)
652 return 0;
653
654 X_LOCK (reqlock);
655 --nreqs;
656 X_UNLOCK (reqlock);
657
658 if (ecb_expect_false (req->type == EIO_GROUP && req->size))
659 {
660 req->int1 = 1; /* mark request as delayed */
661 continue;
662 }
663 else
664 {
665 int res = ETP_FINISH (req);
666 if (ecb_expect_false (res))
667 return res;
668 }
669
670 if (ecb_expect_false (maxreqs && !--maxreqs))
671 break;
672
673 if (maxtime)
674 {
675 gettimeofday (&tv_now, 0);
676
677 if (tvdiff (&tv_start, &tv_now) >= maxtime)
678 break;
679 }
680 }
681
682 errno = EAGAIN;
683 return -1;
684}
685
686static void
687etp_cancel (ETP_REQ *req)
688{
689 req->cancelled = 1;
690
691 eio_grp_cancel (req);
692}
693
694static void
695etp_submit (ETP_REQ *req)
696{
697 req->pri -= ETP_PRI_MIN;
698
699 if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN;
700 if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
701
702 if (ecb_expect_false (req->type == EIO_GROUP))
703 {
704 /* I hope this is worth it :/ */
705 X_LOCK (reqlock);
706 ++nreqs;
707 X_UNLOCK (reqlock);
708
709 X_LOCK (reslock);
710
711 ++npending;
712
713 if (!reqq_push (&res_queue, req) && want_poll_cb)
714 want_poll_cb ();
715
716 X_UNLOCK (reslock);
717 }
718 else
719 {
720 X_LOCK (reqlock);
721 ++nreqs;
722 ++nready;
723 reqq_push (&req_queue, req);
724 X_COND_SIGNAL (reqwait);
725 X_UNLOCK (reqlock);
726
727 etp_maybe_start_thread ();
728 }
729}
730
731static void ecb_cold
732etp_set_max_poll_time (double nseconds)
733{
734 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
735 max_poll_time = nseconds * EIO_TICKS;
736 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
737}
738
739static void ecb_cold
740etp_set_max_poll_reqs (unsigned int maxreqs)
741{
742 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
743 max_poll_reqs = maxreqs;
744 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
745}
746
747static void ecb_cold
748etp_set_max_idle (unsigned int nthreads)
749{
750 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
751 max_idle = nthreads;
752 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
753}
754
755static void ecb_cold
756etp_set_idle_timeout (unsigned int seconds)
757{
758 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
759 idle_timeout = seconds;
760 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
761}
762
763static void ecb_cold
764etp_set_min_parallel (unsigned int nthreads)
765{
766 if (wanted < nthreads)
767 wanted = nthreads;
768}
769
770static void ecb_cold
771etp_set_max_parallel (unsigned int nthreads)
772{
773 if (wanted > nthreads)
774 wanted = nthreads;
775
776 while (started > wanted)
777 etp_end_thread ();
778}
779 390
780/*****************************************************************************/ 391/*****************************************************************************/
781 392
782static void 393static void
783grp_try_feed (eio_req *grp) 394grp_try_feed (eio_req *grp)
784{ 395{
785 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 396 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
786 { 397 {
787 grp->flags &= ~EIO_FLAG_GROUPADD; 398 grp->flags &= ~ETP_FLAG_GROUPADD;
788 399
789 EIO_FEED (grp); 400 EIO_FEED (grp);
790 401
791 /* stop if no progress has been made */ 402 /* stop if no progress has been made */
792 if (!(grp->flags & EIO_FLAG_GROUPADD)) 403 if (!(grp->flags & ETP_FLAG_GROUPADD))
793 { 404 {
794 grp->feed = 0; 405 grp->feed = 0;
795 break; 406 break;
796 } 407 }
797 } 408 }
804 415
805 /* call feeder, if applicable */ 416 /* call feeder, if applicable */
806 grp_try_feed (grp); 417 grp_try_feed (grp);
807 418
808 /* finish, if done */ 419 /* finish, if done */
809 if (!grp->size && grp->int1) 420 if (!grp->size && grp->flags & ETP_FLAG_DELAYED)
810 return eio_finish (grp); 421 return eio_finish (grp);
811 else 422 else
812 return 0; 423 return 0;
813} 424}
814 425
850} 461}
851 462
852void 463void
853eio_grp_cancel (eio_req *grp) 464eio_grp_cancel (eio_req *grp)
854{ 465{
855 for (grp = grp->grp_first; grp; grp = grp->grp_next) 466 etp_grp_cancel (EIO_POOL, grp);
856 eio_cancel (grp);
857} 467}
858 468
859void 469void
860eio_cancel (eio_req *req) 470eio_cancel (eio_req *req)
861{ 471{
862 etp_cancel (req); 472 etp_cancel (EIO_POOL, req);
863} 473}
864 474
865void 475void
866eio_submit (eio_req *req) 476eio_submit (eio_req *req)
867{ 477{
868 etp_submit (req); 478 etp_submit (EIO_POOL, req);
869} 479}
870 480
871unsigned int 481unsigned int
872eio_nreqs (void) 482eio_nreqs (void)
873{ 483{
874 return etp_nreqs (); 484 return etp_nreqs (EIO_POOL);
875} 485}
876 486
877unsigned int 487unsigned int
878eio_nready (void) 488eio_nready (void)
879{ 489{
880 return etp_nready (); 490 return etp_nready (EIO_POOL);
881} 491}
882 492
883unsigned int 493unsigned int
884eio_npending (void) 494eio_npending (void)
885{ 495{
886 return etp_npending (); 496 return etp_npending (EIO_POOL);
887} 497}
888 498
889unsigned int ecb_cold 499unsigned int ecb_cold
890eio_nthreads (void) 500eio_nthreads (void)
891{ 501{
892 return etp_nthreads (); 502 return etp_nthreads (EIO_POOL);
893} 503}
894 504
895void ecb_cold 505void ecb_cold
896eio_set_max_poll_time (double nseconds) 506eio_set_max_poll_time (double nseconds)
897{ 507{
898 etp_set_max_poll_time (nseconds); 508 etp_set_max_poll_time (EIO_POOL, nseconds);
899} 509}
900 510
901void ecb_cold 511void ecb_cold
902eio_set_max_poll_reqs (unsigned int maxreqs) 512eio_set_max_poll_reqs (unsigned int maxreqs)
903{ 513{
904 etp_set_max_poll_reqs (maxreqs); 514 etp_set_max_poll_reqs (EIO_POOL, maxreqs);
905} 515}
906 516
907void ecb_cold 517void ecb_cold
908eio_set_max_idle (unsigned int nthreads) 518eio_set_max_idle (unsigned int nthreads)
909{ 519{
910 etp_set_max_idle (nthreads); 520 etp_set_max_idle (EIO_POOL, nthreads);
911} 521}
912 522
913void ecb_cold 523void ecb_cold
914eio_set_idle_timeout (unsigned int seconds) 524eio_set_idle_timeout (unsigned int seconds)
915{ 525{
916 etp_set_idle_timeout (seconds); 526 etp_set_idle_timeout (EIO_POOL, seconds);
917} 527}
918 528
919void ecb_cold 529void ecb_cold
920eio_set_min_parallel (unsigned int nthreads) 530eio_set_min_parallel (unsigned int nthreads)
921{ 531{
922 etp_set_min_parallel (nthreads); 532 etp_set_min_parallel (EIO_POOL, nthreads);
923} 533}
924 534
925void ecb_cold 535void ecb_cold
926eio_set_max_parallel (unsigned int nthreads) 536eio_set_max_parallel (unsigned int nthreads)
927{ 537{
928 etp_set_max_parallel (nthreads); 538 etp_set_max_parallel (EIO_POOL, nthreads);
929} 539}
930 540
931int eio_poll (void) 541int eio_poll (void)
932{ 542{
933 return etp_poll (); 543 return etp_poll (EIO_POOL);
934} 544}
935 545
936/*****************************************************************************/ 546/*****************************************************************************/
937/* work around various missing functions */ 547/* work around various missing functions */
938 548
1348 req->result = req->offs == (off_t)-1 ? -1 : 0; 958 req->result = req->offs == (off_t)-1 ? -1 : 0;
1349} 959}
1350 960
1351/* 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 */
1352static int 962static int
1353eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 963eio__realpath (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
1354{ 964{
965 char *res;
1355 const char *rel = path; 966 const char *rel = path;
1356 char *res;
1357 char *tmp1, *tmp2; 967 char *tmp1, *tmp2;
1358#if SYMLOOP_MAX > 32 968#if SYMLOOP_MAX > 32
1359 int symlinks = SYMLOOP_MAX; 969 int symlinks = SYMLOOP_MAX;
1360#else 970#else
1361 int symlinks = 32; 971 int symlinks = 32;
1367 977
1368 errno = ENOENT; 978 errno = ENOENT;
1369 if (!*rel) 979 if (!*rel)
1370 return -1; 980 return -1;
1371 981
1372 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
1373 tmp1 = res + PATH_MAX; 1000 tmp1 = res + PATH_MAX;
1374 tmp2 = tmp1 + PATH_MAX; 1001 tmp2 = tmp1 + PATH_MAX;
1375 1002
1376#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 */
1377#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) 1004#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1381 1008
1382 if (fd >= 0) 1009 if (fd >= 0)
1383 { 1010 {
1384 sprintf (tmp1, "/proc/self/fd/%d", fd); 1011 sprintf (tmp1, "/proc/self/fd/%d", fd);
1385 req->result = readlink (tmp1, res, PATH_MAX); 1012 req->result = readlink (tmp1, res, PATH_MAX);
1013 /* here we should probably stat the open file and the disk file, to make sure they still match */
1386 close (fd); 1014 close (fd);
1387
1388 /* here we should probably stat the open file and the disk file, to make sure they still match */
1389 1015
1390 if (req->result > 0) 1016 if (req->result > 0)
1391 goto done; 1017 goto done;
1392 } 1018 }
1393 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) 1019 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO)
1394 return; 1020 return -1;
1395 } 1021 }
1396#endif 1022#endif
1397#endif 1023#endif
1398 1024
1399 if (*rel != '/') 1025 if (*rel != '/')
1501 /* special case for the lone root path */ 1127 /* special case for the lone root path */
1502 if (res == tmpbuf->ptr) 1128 if (res == tmpbuf->ptr)
1503 *res++ = '/'; 1129 *res++ = '/';
1504 1130
1505 return res - (char *)tmpbuf->ptr; 1131 return res - (char *)tmpbuf->ptr;
1132#endif
1506} 1133}
1507 1134
1508static signed char 1135static signed char
1509eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1136eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1510{ 1137{
1883 #ifdef DT_FIFO 1510 #ifdef DT_FIFO
1884 case DT_FIFO: ent->type = EIO_DT_FIFO; break; 1511 case DT_FIFO: ent->type = EIO_DT_FIFO; break;
1885 #endif 1512 #endif
1886 #ifdef DT_CHR 1513 #ifdef DT_CHR
1887 case DT_CHR: ent->type = EIO_DT_CHR; break; 1514 case DT_CHR: ent->type = EIO_DT_CHR; break;
1888 #endif 1515 #endif
1889 #ifdef DT_MPC 1516 #ifdef DT_MPC
1890 case DT_MPC: ent->type = EIO_DT_MPC; break; 1517 case DT_MPC: ent->type = EIO_DT_MPC; break;
1891 #endif 1518 #endif
1892 #ifdef DT_DIR 1519 #ifdef DT_DIR
1893 case DT_DIR: ent->type = EIO_DT_DIR; break; 1520 case DT_DIR: ent->type = EIO_DT_DIR; break;
1894 #endif 1521 #endif
1895 #ifdef DT_NAM 1522 #ifdef DT_NAM
1896 case DT_NAM: ent->type = EIO_DT_NAM; break; 1523 case DT_NAM: ent->type = EIO_DT_NAM; break;
1897 #endif 1524 #endif
1898 #ifdef DT_BLK 1525 #ifdef DT_BLK
1899 case DT_BLK: ent->type = EIO_DT_BLK; break; 1526 case DT_BLK: ent->type = EIO_DT_BLK; break;
1900 #endif 1527 #endif
1901 #ifdef DT_MPB 1528 #ifdef DT_MPB
1902 case DT_MPB: ent->type = EIO_DT_MPB; break; 1529 case DT_MPB: ent->type = EIO_DT_MPB; break;
1903 #endif 1530 #endif
1904 #ifdef DT_REG 1531 #ifdef DT_REG
1905 case DT_REG: ent->type = EIO_DT_REG; break; 1532 case DT_REG: ent->type = EIO_DT_REG; break;
1906 #endif 1533 #endif
1907 #ifdef DT_NWK 1534 #ifdef DT_NWK
1908 case DT_NWK: ent->type = EIO_DT_NWK; break; 1535 case DT_NWK: ent->type = EIO_DT_NWK; break;
1909 #endif 1536 #endif
1910 #ifdef DT_CMP 1537 #ifdef DT_CMP
1911 case DT_CMP: ent->type = EIO_DT_CMP; break; 1538 case DT_CMP: ent->type = EIO_DT_CMP; break;
1912 #endif 1539 #endif
1913 #ifdef DT_LNK 1540 #ifdef DT_LNK
1914 case DT_LNK: ent->type = EIO_DT_LNK; break; 1541 case DT_LNK: ent->type = EIO_DT_LNK; break;
1915 #endif 1542 #endif
1916 #ifdef DT_SOCK 1543 #ifdef DT_SOCK
1917 case DT_SOCK: ent->type = EIO_DT_SOCK; break; 1544 case DT_SOCK: ent->type = EIO_DT_SOCK; break;
1969#if !HAVE_AT 1596#if !HAVE_AT
1970 1597
1971/* 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 */
1972/* an absolute or canonical path */ 1599/* an absolute or canonical path */
1973static const char * 1600static const char *
1974wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1601wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
1975{ 1602{
1976 if (!wd || *path == '/') 1603 if (!wd || *path == '/')
1977 return path; 1604 return path;
1978 1605
1979 if (path [0] == '.' && !path [1]) 1606 if (path [0] == '.' && !path [1])
1981 1608
1982 { 1609 {
1983 int l1 = wd->len; 1610 int l1 = wd->len;
1984 int l2 = strlen (path); 1611 int l2 = strlen (path);
1985 1612
1986 char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2); 1613 char *res = etp_tmpbuf_get (tmpbuf, l1 + l2 + 2);
1987 1614
1988 memcpy (res, wd->str, l1); 1615 memcpy (res, wd->str, l1);
1989 res [l1] = '/'; 1616 res [l1] = '/';
1990 memcpy (res + l1 + 1, path, l2 + 1); 1617 memcpy (res + l1 + 1, path, l2 + 1);
1991 1618
1994} 1621}
1995 1622
1996#endif 1623#endif
1997 1624
1998static eio_wd 1625static eio_wd
1999eio__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)
2000{ 1627{
2001 int fd; 1628 int fd;
2002 eio_wd res; 1629 eio_wd res;
2003 int len = eio__realpath (tmpbuf, wd, path); 1630 int len = eio__realpath (tmpbuf, wd, path);
2004 1631
2026} 1653}
2027 1654
2028eio_wd 1655eio_wd
2029eio_wd_open_sync (eio_wd wd, const char *path) 1656eio_wd_open_sync (eio_wd wd, const char *path)
2030{ 1657{
2031 struct tmpbuf tmpbuf = { 0 }; 1658 struct etp_tmpbuf tmpbuf = { };
2032 wd = eio__wd_open_sync (&tmpbuf, wd, path); 1659 wd = eio__wd_open_sync (&tmpbuf, wd, path);
2033 free (tmpbuf.ptr); 1660 free (tmpbuf.ptr);
2034 1661
2035 return wd; 1662 return wd;
2036} 1663}
2085/*****************************************************************************/ 1712/*****************************************************************************/
2086 1713
2087#define ALLOC(len) \ 1714#define ALLOC(len) \
2088 if (!req->ptr2) \ 1715 if (!req->ptr2) \
2089 { \ 1716 { \
2090 X_LOCK (wrklock); \ 1717 X_LOCK (EIO_POOL->wrklock); \
2091 req->flags |= EIO_FLAG_PTR2_FREE; \ 1718 req->flags |= EIO_FLAG_PTR2_FREE; \
2092 X_UNLOCK (wrklock); \ 1719 X_UNLOCK (EIO_POOL->wrklock); \
2093 req->ptr2 = malloc (len); \ 1720 req->ptr2 = malloc (len); \
2094 if (!req->ptr2) \ 1721 if (!req->ptr2) \
2095 { \ 1722 { \
2096 errno = ENOMEM; \ 1723 errno = ENOMEM; \
2097 req->result = -1; \ 1724 req->result = -1; \
2098 break; \ 1725 break; \
2099 } \ 1726 } \
2100 } 1727 }
2101 1728
2102static void ecb_noinline ecb_cold
2103etp_proc_init (void)
2104{
2105#if HAVE_PRCTL_SET_NAME
2106 /* provide a more sensible "thread name" */
2107 char name[16 + 1];
2108 const int namelen = sizeof (name) - 1;
2109 int len;
2110
2111 prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0);
2112 name [namelen] = 0;
2113 len = strlen (name);
2114 strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio");
2115 prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0);
2116#endif
2117}
2118
2119X_THREAD_PROC (etp_proc)
2120{
2121 ETP_REQ *req;
2122 struct timespec ts;
2123 etp_worker *self = (etp_worker *)thr_arg;
2124
2125 etp_proc_init ();
2126
2127 /* try to distribute timeouts somewhat evenly */
2128 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
2129
2130 for (;;)
2131 {
2132 ts.tv_sec = 0;
2133
2134 X_LOCK (reqlock);
2135
2136 for (;;)
2137 {
2138 req = reqq_shift (&req_queue);
2139
2140 if (req)
2141 break;
2142
2143 if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */
2144 {
2145 X_UNLOCK (reqlock);
2146 X_LOCK (wrklock);
2147 --started;
2148 X_UNLOCK (wrklock);
2149 goto quit;
2150 }
2151
2152 ++idle;
2153
2154 if (idle <= max_idle)
2155 /* we are allowed to idle, so do so without any timeout */
2156 X_COND_WAIT (reqwait, reqlock);
2157 else
2158 {
2159 /* initialise timeout once */
2160 if (!ts.tv_sec)
2161 ts.tv_sec = time (0) + idle_timeout;
2162
2163 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
2164 ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */
2165 }
2166
2167 --idle;
2168 }
2169
2170 --nready;
2171
2172 X_UNLOCK (reqlock);
2173
2174 if (req->type < 0)
2175 goto quit;
2176
2177 ETP_EXECUTE (self, req);
2178
2179 X_LOCK (reslock);
2180
2181 ++npending;
2182
2183 if (!reqq_push (&res_queue, req) && want_poll_cb)
2184 want_poll_cb ();
2185
2186 etp_worker_clear (self);
2187
2188 X_UNLOCK (reslock);
2189 }
2190
2191quit:
2192 free (req);
2193
2194 X_LOCK (wrklock);
2195 etp_worker_free (self);
2196 X_UNLOCK (wrklock);
2197}
2198
2199/*****************************************************************************/ 1729/*****************************************************************************/
2200 1730
2201int ecb_cold 1731int ecb_cold
2202eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1732eio_init (void (*want_poll)(void), void (*done_poll)(void))
2203{ 1733{
2204 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);
2205} 1738}
2206 1739
2207ecb_inline void 1740ecb_inline void
2208eio_api_destroy (eio_req *req) 1741eio_api_destroy (eio_req *req)
2209{ 1742{
2232 { \ 1765 { \
2233 eio_api_destroy (req); \ 1766 eio_api_destroy (req); \
2234 return 0; \ 1767 return 0; \
2235 } 1768 }
2236 1769
1770#define SINGLEDOT(ptr) (0[(char *)(ptr)] == '.' && !1[(char *)(ptr)])
1771
2237static void 1772static void
2238eio_execute (etp_worker *self, eio_req *req) 1773eio_execute (etp_worker *self, eio_req *req)
2239{ 1774{
2240#if HAVE_AT 1775#if HAVE_AT
2241 int dirfd; 1776 int dirfd;
2296 case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break; 1831 case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break;
2297 case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break; 1832 case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break;
2298 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break; 1833 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break;
2299 1834
2300 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; 1835 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break;
2301 case EIO_RMDIR: req->result = unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; 1836 case EIO_RMDIR: /* complications arise because "." cannot be removed, so we might have to expand */
1837 req->result = req->wd && SINGLEDOT (req->ptr1)
1838 ? rmdir (req->wd->str)
1839 : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break;
2302 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; 1840 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break;
2303 case EIO_RENAME: req->result = renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; 1841 case EIO_RENAME: /* complications arise because "." cannot be renamed, so we might have to expand */
1842 req->result = req->wd && SINGLEDOT (req->ptr1)
1843 ? rename (req->wd->str, req->ptr2)
1844 : renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break;
2304 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; 1845 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break;
2305 case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; 1846 case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break;
2306 case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; 1847 case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
2307 case EIO_READLINK: ALLOC (PATH_MAX); 1848 case EIO_READLINK: ALLOC (PATH_MAX);
2308 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break; 1849 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break;
2426 req->result = select (0, 0, 0, 0, &tv); 1967 req->result = select (0, 0, 0, 0, &tv);
2427 } 1968 }
2428#endif 1969#endif
2429 break; 1970 break;
2430 1971
1972#if 0
2431 case EIO_GROUP: 1973 case EIO_GROUP:
2432 abort (); /* handled in eio_request */ 1974 abort (); /* handled in eio_request */
1975#endif
2433 1976
2434 case EIO_NOP: 1977 case EIO_NOP:
2435 req->result = 0; 1978 req->result = 0;
2436 break; 1979 break;
2437 1980
2737void 2280void
2738eio_grp_add (eio_req *grp, eio_req *req) 2281eio_grp_add (eio_req *grp, eio_req *req)
2739{ 2282{
2740 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));
2741 2284
2742 grp->flags |= EIO_FLAG_GROUPADD; 2285 grp->flags |= ETP_FLAG_GROUPADD;
2743 2286
2744 ++grp->size; 2287 ++grp->size;
2745 req->grp = grp; 2288 req->grp = grp;
2746 2289
2747 req->grp_prev = 0; 2290 req->grp_prev = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines