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

Comparing libeio/eio.c (file contents):
Revision 1.106 by root, Mon Sep 26 20:19:08 2011 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 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,
58#include <sys/stat.h> 58#include <sys/stat.h>
59#include <limits.h> 59#include <limits.h>
60#include <fcntl.h> 60#include <fcntl.h>
61#include <assert.h> 61#include <assert.h>
62 62
63#if _POSIX_VERSION >= 200809L
64# define HAVE_AT 1
65#else
66# define HAVE_AT 0
67#endif
68
69/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ 63/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */
70/* intptr_t only comes from stdint.h, says idiot openbsd coder */ 64/* intptr_t only comes from stdint.h, says idiot openbsd coder */
71#if HAVE_STDINT_H 65#if HAVE_STDINT_H
72# include <stdint.h> 66# include <stdint.h>
73#endif 67#endif
126 #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))
127 121
128 #define chmod(path,mode) _chmod (path, mode) 122 #define chmod(path,mode) _chmod (path, mode)
129 #define dup(fd) _dup (fd) 123 #define dup(fd) _dup (fd)
130 #define dup2(fd1,fd2) _dup2 (fd1, fd2) 124 #define dup2(fd1,fd2) _dup2 (fd1, fd2)
125 #define pipe(fds) _pipe (fds, 4096, O_BINARY)
131 126
132 #define fchmod(fd,mode) EIO_ENOSYS () 127 #define fchmod(fd,mode) EIO_ENOSYS ()
133 #define chown(path,uid,gid) EIO_ENOSYS () 128 #define chown(path,uid,gid) EIO_ENOSYS ()
134 #define fchown(fd,uid,gid) EIO_ENOSYS () 129 #define fchown(fd,uid,gid) EIO_ENOSYS ()
135 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ 130 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */
137 #define mknod(path,mode,dev) EIO_ENOSYS () 132 #define mknod(path,mode,dev) EIO_ENOSYS ()
138 #define sync() EIO_ENOSYS () 133 #define sync() EIO_ENOSYS ()
139 #define readlink(path,buf,s) EIO_ENOSYS () 134 #define readlink(path,buf,s) EIO_ENOSYS ()
140 #define statvfs(path,buf) EIO_ENOSYS () 135 #define statvfs(path,buf) EIO_ENOSYS ()
141 #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 }
142 172
143 /* rename() uses MoveFile, which fails to overwrite */ 173 /* rename() uses MoveFile, which fails to overwrite */
144 #define rename(old,neu) eio__rename (old, neu) 174 #define rename(old,neu) eio__rename (old, neu)
145 175
146 static int 176 static int
184 #endif 214 #endif
185 215
186 return EIO_ERRNO (ENOENT, -1); 216 return EIO_ERRNO (ENOENT, -1);
187 } 217 }
188 218
189 /* POSIX API only */ 219 /* POSIX API only, causing trouble for win32 apps */
190 #define CreateHardLink(neu,old,flags) 0 220 #define CreateHardLink(neu,old,flags) 0 /* not really creating hardlink, still using relative paths? */
191 #define CreateSymbolicLink(neu,old,flags) 0 221 #define CreateSymbolicLink(neu,old,flags) 0 /* vista+ only */
192 222
193 struct statvfs 223 struct statvfs
194 { 224 {
195 int dummy; 225 int dummy;
196 }; 226 };
202 232
203#else 233#else
204 234
205 #include <sys/time.h> 235 #include <sys/time.h>
206 #include <sys/select.h> 236 #include <sys/select.h>
207 #include <sys/statvfs.h>
208 #include <unistd.h> 237 #include <unistd.h>
209 #include <signal.h> 238 #include <signal.h>
210 #include <dirent.h> 239 #include <dirent.h>
211 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
249
212 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES 250 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
213 #include <sys/mman.h> 251 #include <sys/mman.h>
214 #endif 252 #endif
215 253
216 #define D_NAME(entp) entp->d_name 254 #define D_NAME(entp) entp->d_name
217 255
218 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ 256 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */
219 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ 257 #if __FreeBSD__ || __NetBSD__ || __OpenBSD__
220 #define _DIRENT_HAVE_D_TYPE /* sigh */ 258 #define _DIRENT_HAVE_D_TYPE /* sigh */
221 #define D_INO(de) (de)->d_fileno 259 #define D_INO(de) (de)->d_fileno
222 #define D_NAMLEN(de) (de)->d_namlen 260 #define D_NAMLEN(de) (de)->d_namlen
223 #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 261 #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600
224 #define D_INO(de) (de)->d_ino 262 #define D_INO(de) (de)->d_ino
287#endif 325#endif
288 326
289/* buffer size for various temporary buffers */ 327/* buffer size for various temporary buffers */
290#define EIO_BUFSIZE 65536 328#define EIO_BUFSIZE 65536
291 329
292#define dBUF \ 330#define dBUF \
293 char *eio_buf = malloc (EIO_BUFSIZE); \ 331 char *eio_buf = malloc (EIO_BUFSIZE); \
294 errno = ENOMEM; \ 332 errno = ENOMEM; \
295 if (!eio_buf) \ 333 if (!eio_buf) \
296 return -1 334 return -1
297 335
298#define FUBd \ 336#define FUBd \
299 free (eio_buf) 337 free (eio_buf)
300 338
301#define EIO_TICKS ((1000000 + 1023) >> 10)
302
303/*****************************************************************************/ 339/*****************************************************************************/
304 340
305struct tmpbuf 341struct etp_tmpbuf;
342
343#if _POSIX_VERSION >= 200809L
344 #define HAVE_AT 1
345 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD)
346 #ifndef O_SEARCH
347 #define O_SEARCH O_RDONLY
348 #endif
349#else
350 #define HAVE_AT 0
351 static const char *wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path);
352#endif
353
354struct eio_pwd
306{ 355{
307 void *ptr; 356#if HAVE_AT
357 int fd;
358#endif
308 int len; 359 int len;
360 char str[1]; /* actually, a 0-terminated canonical path */
309}; 361};
310
311static void *
312tmpbuf_get (struct tmpbuf *buf, int len)
313{
314 if (buf->len < len)
315 {
316 free (buf->ptr);
317 buf->ptr = malloc (buf->len = len);
318 }
319
320 return buf->ptr;
321}
322 362
323/*****************************************************************************/ 363/*****************************************************************************/
324 364
325#define ETP_PRI_MIN EIO_PRI_MIN 365#define ETP_PRI_MIN EIO_PRI_MIN
326#define ETP_PRI_MAX EIO_PRI_MAX 366#define ETP_PRI_MAX EIO_PRI_MAX
327 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
328struct etp_worker; 378struct etp_worker;
329
330#define ETP_REQ eio_req 379#define ETP_REQ eio_req
331#define ETP_DESTROY(req) eio_destroy (req) 380#define ETP_DESTROY(req) eio_destroy (req)
332static int eio_finish (eio_req *req); 381static int eio_finish (eio_req *req);
333#define ETP_FINISH(req) eio_finish (req) 382#define ETP_FINISH(req) eio_finish (req)
334static void eio_execute (struct etp_worker *self, eio_req *req); 383static void eio_execute (struct etp_worker *self, eio_req *req);
335#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) 384#define ETP_EXECUTE(wrk,req) eio_execute (wrk, req)
336 385
337/*****************************************************************************/ 386#include "etp.c"
338 387
339#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 388static struct etp_pool eio_pool;
340 389#define EIO_POOL (&eio_pool)
341/* calculate time difference in ~1/EIO_TICKS of a second */
342ecb_inline int
343tvdiff (struct timeval *tv1, struct timeval *tv2)
344{
345 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
346 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
347}
348
349static unsigned int started, idle, wanted = 4;
350
351static void (*want_poll_cb) (void);
352static void (*done_poll_cb) (void);
353
354static unsigned int max_poll_time; /* reslock */
355static unsigned int max_poll_reqs; /* reslock */
356
357static unsigned int nreqs; /* reqlock */
358static unsigned int nready; /* reqlock */
359static unsigned int npending; /* reqlock */
360static unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */
361static unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */
362
363static xmutex_t wrklock;
364static xmutex_t reslock;
365static xmutex_t reqlock;
366static xcond_t reqwait;
367
368#if !HAVE_PREADWRITE
369/*
370 * make our pread/pwrite emulation safe against themselves, but not against
371 * normal read/write by using a mutex. slows down execution a lot,
372 * but that's your problem, not mine.
373 */
374static xmutex_t preadwritelock;
375#endif
376
377typedef struct etp_worker
378{
379 struct tmpbuf tmpbuf;
380
381 /* locked by wrklock */
382 struct etp_worker *prev, *next;
383
384 xthread_t tid;
385
386#ifdef ETP_WORKER_COMMON
387 ETP_WORKER_COMMON
388#endif
389} etp_worker;
390
391static etp_worker wrk_first; /* NOT etp */
392
393#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
394#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
395
396/* worker threads management */
397
398static void ecb_cold
399etp_worker_clear (etp_worker *wrk)
400{
401}
402
403static void ecb_cold
404etp_worker_free (etp_worker *wrk)
405{
406 free (wrk->tmpbuf.ptr);
407
408 wrk->next->prev = wrk->prev;
409 wrk->prev->next = wrk->next;
410
411 free (wrk);
412}
413
414static unsigned int
415etp_nreqs (void)
416{
417 int retval;
418 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
419 retval = nreqs;
420 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
421 return retval;
422}
423
424static unsigned int
425etp_nready (void)
426{
427 unsigned int retval;
428
429 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
430 retval = nready;
431 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
432
433 return retval;
434}
435
436static unsigned int
437etp_npending (void)
438{
439 unsigned int retval;
440
441 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
442 retval = npending;
443 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
444
445 return retval;
446}
447
448static unsigned int
449etp_nthreads (void)
450{
451 unsigned int retval;
452
453 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
454 retval = started;
455 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
456
457 return retval;
458}
459
460/*
461 * a somewhat faster data structure might be nice, but
462 * with 8 priorities this actually needs <20 insns
463 * per shift, the most expensive operation.
464 */
465typedef struct {
466 ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */
467 int size;
468} etp_reqq;
469
470static etp_reqq req_queue;
471static etp_reqq res_queue;
472
473static void ecb_noinline ecb_cold
474reqq_init (etp_reqq *q)
475{
476 int pri;
477
478 for (pri = 0; pri < ETP_NUM_PRI; ++pri)
479 q->qs[pri] = q->qe[pri] = 0;
480
481 q->size = 0;
482}
483
484static int ecb_noinline
485reqq_push (etp_reqq *q, ETP_REQ *req)
486{
487 int pri = req->pri;
488 req->next = 0;
489
490 if (q->qe[pri])
491 {
492 q->qe[pri]->next = req;
493 q->qe[pri] = req;
494 }
495 else
496 q->qe[pri] = q->qs[pri] = req;
497
498 return q->size++;
499}
500
501static ETP_REQ * ecb_noinline
502reqq_shift (etp_reqq *q)
503{
504 int pri;
505
506 if (!q->size)
507 return 0;
508
509 --q->size;
510
511 for (pri = ETP_NUM_PRI; pri--; )
512 {
513 eio_req *req = q->qs[pri];
514
515 if (req)
516 {
517 if (!(q->qs[pri] = (eio_req *)req->next))
518 q->qe[pri] = 0;
519
520 return req;
521 }
522 }
523
524 abort ();
525}
526
527static int ecb_cold
528etp_init (void (*want_poll)(void), void (*done_poll)(void))
529{
530 X_MUTEX_CREATE (wrklock);
531 X_MUTEX_CREATE (reslock);
532 X_MUTEX_CREATE (reqlock);
533 X_COND_CREATE (reqwait);
534
535 reqq_init (&req_queue);
536 reqq_init (&res_queue);
537
538 wrk_first.next =
539 wrk_first.prev = &wrk_first;
540
541 started = 0;
542 idle = 0;
543 nreqs = 0;
544 nready = 0;
545 npending = 0;
546
547 want_poll_cb = want_poll;
548 done_poll_cb = done_poll;
549
550 return 0;
551}
552
553X_THREAD_PROC (etp_proc);
554
555static void ecb_cold
556etp_start_thread (void)
557{
558 etp_worker *wrk = calloc (1, sizeof (etp_worker));
559
560 /*TODO*/
561 assert (("unable to allocate worker thread data", wrk));
562
563 X_LOCK (wrklock);
564
565 if (thread_create (&wrk->tid, etp_proc, (void *)wrk))
566 {
567 wrk->prev = &wrk_first;
568 wrk->next = wrk_first.next;
569 wrk_first.next->prev = wrk;
570 wrk_first.next = wrk;
571 ++started;
572 }
573 else
574 free (wrk);
575
576 X_UNLOCK (wrklock);
577}
578
579static void
580etp_maybe_start_thread (void)
581{
582 if (ecb_expect_true (etp_nthreads () >= wanted))
583 return;
584
585 /* todo: maybe use idle here, but might be less exact */
586 if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()))
587 return;
588
589 etp_start_thread ();
590}
591
592static void ecb_cold
593etp_end_thread (void)
594{
595 eio_req *req = calloc (1, sizeof (eio_req)); /* will be freed by worker */
596
597 req->type = -1;
598 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
599
600 X_LOCK (reqlock);
601 reqq_push (&req_queue, req);
602 X_COND_SIGNAL (reqwait);
603 X_UNLOCK (reqlock);
604
605 X_LOCK (wrklock);
606 --started;
607 X_UNLOCK (wrklock);
608}
609
610static int
611etp_poll (void)
612{
613 unsigned int maxreqs;
614 unsigned int maxtime;
615 struct timeval tv_start, tv_now;
616
617 X_LOCK (reslock);
618 maxreqs = max_poll_reqs;
619 maxtime = max_poll_time;
620 X_UNLOCK (reslock);
621
622 if (maxtime)
623 gettimeofday (&tv_start, 0);
624
625 for (;;)
626 {
627 ETP_REQ *req;
628
629 etp_maybe_start_thread ();
630
631 X_LOCK (reslock);
632 req = reqq_shift (&res_queue);
633
634 if (req)
635 {
636 --npending;
637
638 if (!res_queue.size && done_poll_cb)
639 done_poll_cb ();
640 }
641
642 X_UNLOCK (reslock);
643
644 if (!req)
645 return 0;
646
647 X_LOCK (reqlock);
648 --nreqs;
649 X_UNLOCK (reqlock);
650
651 if (ecb_expect_false (req->type == EIO_GROUP && req->size))
652 {
653 req->int1 = 1; /* mark request as delayed */
654 continue;
655 }
656 else
657 {
658 int res = ETP_FINISH (req);
659 if (ecb_expect_false (res))
660 return res;
661 }
662
663 if (ecb_expect_false (maxreqs && !--maxreqs))
664 break;
665
666 if (maxtime)
667 {
668 gettimeofday (&tv_now, 0);
669
670 if (tvdiff (&tv_start, &tv_now) >= maxtime)
671 break;
672 }
673 }
674
675 errno = EAGAIN;
676 return -1;
677}
678
679static void
680etp_cancel (ETP_REQ *req)
681{
682 req->cancelled = 1;
683
684 eio_grp_cancel (req);
685}
686
687static void
688etp_submit (ETP_REQ *req)
689{
690 req->pri -= ETP_PRI_MIN;
691
692 if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN;
693 if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
694
695 if (ecb_expect_false (req->type == EIO_GROUP))
696 {
697 /* I hope this is worth it :/ */
698 X_LOCK (reqlock);
699 ++nreqs;
700 X_UNLOCK (reqlock);
701
702 X_LOCK (reslock);
703
704 ++npending;
705
706 if (!reqq_push (&res_queue, req) && want_poll_cb)
707 want_poll_cb ();
708
709 X_UNLOCK (reslock);
710 }
711 else
712 {
713 X_LOCK (reqlock);
714 ++nreqs;
715 ++nready;
716 reqq_push (&req_queue, req);
717 X_COND_SIGNAL (reqwait);
718 X_UNLOCK (reqlock);
719
720 etp_maybe_start_thread ();
721 }
722}
723
724static void ecb_cold
725etp_set_max_poll_time (double nseconds)
726{
727 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
728 max_poll_time = nseconds * EIO_TICKS;
729 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
730}
731
732static void ecb_cold
733etp_set_max_poll_reqs (unsigned int maxreqs)
734{
735 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
736 max_poll_reqs = maxreqs;
737 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
738}
739
740static void ecb_cold
741etp_set_max_idle (unsigned int nthreads)
742{
743 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
744 max_idle = nthreads;
745 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
746}
747
748static void ecb_cold
749etp_set_idle_timeout (unsigned int seconds)
750{
751 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
752 idle_timeout = seconds;
753 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
754}
755
756static void ecb_cold
757etp_set_min_parallel (unsigned int nthreads)
758{
759 if (wanted < nthreads)
760 wanted = nthreads;
761}
762
763static void ecb_cold
764etp_set_max_parallel (unsigned int nthreads)
765{
766 if (wanted > nthreads)
767 wanted = nthreads;
768
769 while (started > wanted)
770 etp_end_thread ();
771}
772 390
773/*****************************************************************************/ 391/*****************************************************************************/
774 392
775static void 393static void
776grp_try_feed (eio_req *grp) 394grp_try_feed (eio_req *grp)
777{ 395{
778 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 396 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
779 { 397 {
780 grp->flags &= ~EIO_FLAG_GROUPADD; 398 grp->flags &= ~ETP_FLAG_GROUPADD;
781 399
782 EIO_FEED (grp); 400 EIO_FEED (grp);
783 401
784 /* stop if no progress has been made */ 402 /* stop if no progress has been made */
785 if (!(grp->flags & EIO_FLAG_GROUPADD)) 403 if (!(grp->flags & ETP_FLAG_GROUPADD))
786 { 404 {
787 grp->feed = 0; 405 grp->feed = 0;
788 break; 406 break;
789 } 407 }
790 } 408 }
797 415
798 /* call feeder, if applicable */ 416 /* call feeder, if applicable */
799 grp_try_feed (grp); 417 grp_try_feed (grp);
800 418
801 /* finish, if done */ 419 /* finish, if done */
802 if (!grp->size && grp->int1) 420 if (!grp->size && grp->flags & ETP_FLAG_DELAYED)
803 return eio_finish (grp); 421 return eio_finish (grp);
804 else 422 else
805 return 0; 423 return 0;
806} 424}
807 425
843} 461}
844 462
845void 463void
846eio_grp_cancel (eio_req *grp) 464eio_grp_cancel (eio_req *grp)
847{ 465{
848 for (grp = grp->grp_first; grp; grp = grp->grp_next) 466 etp_grp_cancel (EIO_POOL, grp);
849 eio_cancel (grp);
850} 467}
851 468
852void 469void
853eio_cancel (eio_req *req) 470eio_cancel (eio_req *req)
854{ 471{
855 etp_cancel (req); 472 etp_cancel (EIO_POOL, req);
856} 473}
857 474
858void 475void
859eio_submit (eio_req *req) 476eio_submit (eio_req *req)
860{ 477{
861 etp_submit (req); 478 etp_submit (EIO_POOL, req);
862} 479}
863 480
864unsigned int 481unsigned int
865eio_nreqs (void) 482eio_nreqs (void)
866{ 483{
867 return etp_nreqs (); 484 return etp_nreqs (EIO_POOL);
868} 485}
869 486
870unsigned int 487unsigned int
871eio_nready (void) 488eio_nready (void)
872{ 489{
873 return etp_nready (); 490 return etp_nready (EIO_POOL);
874} 491}
875 492
876unsigned int 493unsigned int
877eio_npending (void) 494eio_npending (void)
878{ 495{
879 return etp_npending (); 496 return etp_npending (EIO_POOL);
880} 497}
881 498
882unsigned int ecb_cold 499unsigned int ecb_cold
883eio_nthreads (void) 500eio_nthreads (void)
884{ 501{
885 return etp_nthreads (); 502 return etp_nthreads (EIO_POOL);
886} 503}
887 504
888void ecb_cold 505void ecb_cold
889eio_set_max_poll_time (double nseconds) 506eio_set_max_poll_time (double nseconds)
890{ 507{
891 etp_set_max_poll_time (nseconds); 508 etp_set_max_poll_time (EIO_POOL, nseconds);
892} 509}
893 510
894void ecb_cold 511void ecb_cold
895eio_set_max_poll_reqs (unsigned int maxreqs) 512eio_set_max_poll_reqs (unsigned int maxreqs)
896{ 513{
897 etp_set_max_poll_reqs (maxreqs); 514 etp_set_max_poll_reqs (EIO_POOL, maxreqs);
898} 515}
899 516
900void ecb_cold 517void ecb_cold
901eio_set_max_idle (unsigned int nthreads) 518eio_set_max_idle (unsigned int nthreads)
902{ 519{
903 etp_set_max_idle (nthreads); 520 etp_set_max_idle (EIO_POOL, nthreads);
904} 521}
905 522
906void ecb_cold 523void ecb_cold
907eio_set_idle_timeout (unsigned int seconds) 524eio_set_idle_timeout (unsigned int seconds)
908{ 525{
909 etp_set_idle_timeout (seconds); 526 etp_set_idle_timeout (EIO_POOL, seconds);
910} 527}
911 528
912void ecb_cold 529void ecb_cold
913eio_set_min_parallel (unsigned int nthreads) 530eio_set_min_parallel (unsigned int nthreads)
914{ 531{
915 etp_set_min_parallel (nthreads); 532 etp_set_min_parallel (EIO_POOL, nthreads);
916} 533}
917 534
918void ecb_cold 535void ecb_cold
919eio_set_max_parallel (unsigned int nthreads) 536eio_set_max_parallel (unsigned int nthreads)
920{ 537{
921 etp_set_max_parallel (nthreads); 538 etp_set_max_parallel (EIO_POOL, nthreads);
922} 539}
923 540
924int eio_poll (void) 541int eio_poll (void)
925{ 542{
926 return etp_poll (); 543 return etp_poll (EIO_POOL);
927} 544}
928 545
929/*****************************************************************************/ 546/*****************************************************************************/
930/* work around various missing functions */ 547/* work around various missing functions */
931
932#if !HAVE_PREADWRITE
933# undef pread
934# undef pwrite
935# define pread eio__pread
936# define pwrite eio__pwrite
937
938static eio_ssize_t
939eio__pread (int fd, void *buf, size_t count, off_t offset)
940{
941 eio_ssize_t res;
942 off_t ooffset;
943
944 X_LOCK (preadwritelock);
945 ooffset = lseek (fd, 0, SEEK_CUR);
946 lseek (fd, offset, SEEK_SET);
947 res = read (fd, buf, count);
948 lseek (fd, ooffset, SEEK_SET);
949 X_UNLOCK (preadwritelock);
950
951 return res;
952}
953
954static eio_ssize_t
955eio__pwrite (int fd, void *buf, size_t count, off_t offset)
956{
957 eio_ssize_t res;
958 off_t ooffset;
959
960 X_LOCK (preadwritelock);
961 ooffset = lseek (fd, 0, SEEK_CUR);
962 lseek (fd, offset, SEEK_SET);
963 res = write (fd, buf, count);
964 lseek (fd, ooffset, SEEK_SET);
965 X_UNLOCK (preadwritelock);
966
967 return res;
968}
969#endif
970 548
971#ifndef HAVE_UTIMES 549#ifndef HAVE_UTIMES
972 550
973# undef utimes 551# undef utimes
974# define utimes(path,times) eio__utimes (path, times) 552# define utimes(path,times) eio__utimes (path, times)
1016 int res; 594 int res;
1017 595
1018#if HAVE_SYS_SYNCFS 596#if HAVE_SYS_SYNCFS
1019 res = (int)syscall (__NR_syncfs, (int)(fd)); 597 res = (int)syscall (__NR_syncfs, (int)(fd));
1020#else 598#else
1021 res = -1; 599 res = EIO_ENOSYS ();
1022 errno = ENOSYS;
1023#endif 600#endif
1024 601
1025 if (res < 0 && errno == ENOSYS && fd >= 0) 602 if (res < 0 && errno == ENOSYS && fd >= 0)
1026 sync (); 603 sync ();
1027 604
1057} 634}
1058 635
1059static int 636static int
1060eio__fallocate (int fd, int mode, off_t offset, size_t len) 637eio__fallocate (int fd, int mode, off_t offset, size_t len)
1061{ 638{
1062#if HAVE_FALLOCATE 639#if HAVE_LINUX_FALLOCATE
1063 return fallocate (fd, mode, offset, len); 640 return fallocate (fd, mode, offset, len);
1064#else 641#else
1065 errno = ENOSYS; 642 return EIO_ENOSYS ();
1066 return -1;
1067#endif 643#endif
1068} 644}
1069 645
1070#if !HAVE_READAHEAD 646#if !HAVE_READAHEAD
1071# undef readahead 647# undef readahead
1086 todo -= len; 662 todo -= len;
1087 } 663 }
1088 664
1089 FUBd; 665 FUBd;
1090 666
1091 errno = 0; 667 /* linux's readahead basically only fails for EBADF or EINVAL (not mmappable) */
668 /* but not for e.g. EIO or eof, so we also never fail */
1092 return count; 669 return 0;
1093} 670}
1094 671
1095#endif 672#endif
1096 673
1097/* sendfile always needs emulation */ 674/* sendfile always needs emulation */
1132 709
1133 /* according to source inspection, this is correct, and useful behaviour */ 710 /* according to source inspection, this is correct, and useful behaviour */
1134 if (sbytes) 711 if (sbytes)
1135 res = sbytes; 712 res = sbytes;
1136 713
1137# elif defined (__APPLE__) 714# elif defined __APPLE__
1138 off_t sbytes = count; 715 off_t sbytes = count;
1139 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); 716 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0);
1140 717
1141 /* according to the manpage, sbytes is always valid */ 718 /* according to the manpage, sbytes is always valid */
1142 if (sbytes) 719 if (sbytes)
1169 HANDLE h = TO_SOCKET (ifd); 746 HANDLE h = TO_SOCKET (ifd);
1170 SetFilePointer (h, offset, 0, FILE_BEGIN); 747 SetFilePointer (h, offset, 0, FILE_BEGIN);
1171 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); 748 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0);
1172 749
1173#else 750#else
1174 res = -1; 751 res = EIO_ENOSYS ();
1175 errno = ENOSYS;
1176#endif 752#endif
1177 753
1178 /* we assume sendfile can copy at least 128mb in one go */ 754 /* we assume sendfile can copy at least 128mb in one go */
1179 if (res <= 128 * 1024 * 1024) 755 if (res <= 128 * 1024 * 1024)
1180 { 756 {
1366} 942}
1367 943
1368/*****************************************************************************/ 944/*****************************************************************************/
1369/* requests implemented outside eio_execute, because they are so large */ 945/* requests implemented outside eio_execute, because they are so large */
1370 946
1371/* copies some absolute path to tmpbuf */ 947static void
1372static char * 948eio__lseek (eio_req *req)
1373eio__getwd (struct tmpbuf *tmpbuf, eio_wd wd)
1374{ 949{
1375 if (wd == EIO_CWD) 950 /* this usually gets optimised away completely, or your compiler sucks, */
1376 return getcwd (tmpbuf->ptr, PATH_MAX); 951 /* or the whence constants really are not 0, 1, 2 */
952 int whence = req->int2 == EIO_SEEK_SET ? SEEK_SET
953 : req->int2 == EIO_SEEK_CUR ? SEEK_CUR
954 : req->int2 == EIO_SEEK_END ? SEEK_END
955 : req->int2;
1377 956
1378#if HAVE_AT 957 req->offs = lseek (req->int1, req->offs, whence);
1379 abort (); /*TODO*/ 958 req->result = req->offs == (off_t)-1 ? -1 : 0;
1380#else
1381 strcpy (tmpbuf->ptr, wd);
1382#endif
1383 return tmpbuf->ptr;
1384} 959}
1385 960
1386/* 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 */
1387static int 962static int
1388eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 963eio__realpath (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
1389{ 964{
965 char *res;
1390 const char *rel = path; 966 const char *rel = path;
1391 char *res;
1392 char *tmp1, *tmp2; 967 char *tmp1, *tmp2;
1393#if SYMLOOP_MAX > 32 968#if SYMLOOP_MAX > 32
1394 int symlinks = SYMLOOP_MAX; 969 int symlinks = SYMLOOP_MAX;
1395#else 970#else
1396 int symlinks = 32; 971 int symlinks = 32;
1397#endif 972#endif
1398 973
1399 /*D*/ /*TODO: wd ignored */
1400
1401 errno = EINVAL; 974 errno = EINVAL;
1402 if (!rel) 975 if (!rel)
1403 return -1; 976 return -1;
1404 977
1405 errno = ENOENT; 978 errno = ENOENT;
1406 if (!*rel) 979 if (!*rel)
1407 return -1; 980 return -1;
1408 981
1409 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
1410 tmp1 = res + PATH_MAX; 1000 tmp1 = res + PATH_MAX;
1411 tmp2 = tmp1 + PATH_MAX; 1001 tmp2 = tmp1 + PATH_MAX;
1412 1002
1413#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 */
1414#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) 1004#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1418 1008
1419 if (fd >= 0) 1009 if (fd >= 0)
1420 { 1010 {
1421 sprintf (tmp1, "/proc/self/fd/%d", fd); 1011 sprintf (tmp1, "/proc/self/fd/%d", fd);
1422 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 */
1423 close (fd); 1014 close (fd);
1424
1425 /* here we should probably stat the open file and the disk file, to make sure they still match */
1426 1015
1427 if (req->result > 0) 1016 if (req->result > 0)
1428 goto done; 1017 goto done;
1429 } 1018 }
1430 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)
1431 return; 1020 return -1;
1432 } 1021 }
1433#endif 1022#endif
1434#endif 1023#endif
1435 1024
1436 if (*rel != '/') 1025 if (*rel != '/')
1437 { 1026 {
1438 if (!eio__getwd (tmpbuf, wd)) 1027 int len;
1028
1029 errno = ENOENT;
1030 if (wd == EIO_INVALID_WD)
1439 return -1; 1031 return -1;
1032
1033 if (wd == EIO_CWD)
1034 {
1035 if (!getcwd (res, PATH_MAX))
1036 return -1;
1037
1038 len = strlen (res);
1039 }
1040 else
1041 memcpy (res, wd->str, len = wd->len);
1440 1042
1441 if (res [1]) /* only use if not / */ 1043 if (res [1]) /* only use if not / */
1442 res += strlen (res); 1044 res += len;
1443 } 1045 }
1444 1046
1445 while (*rel) 1047 while (*rel)
1446 { 1048 {
1447 eio_ssize_t len, linklen; 1049 eio_ssize_t len, linklen;
1475 } 1077 }
1476 } 1078 }
1477 1079
1478 errno = ENAMETOOLONG; 1080 errno = ENAMETOOLONG;
1479 if (res + 1 + len + 1 >= tmp1) 1081 if (res + 1 + len + 1 >= tmp1)
1480 return; 1082 return -1;
1481 1083
1482 /* copy one component */ 1084 /* copy one component */
1483 *res = '/'; 1085 *res = '/';
1484 memcpy (res + 1, beg, len); 1086 memcpy (res + 1, beg, len);
1485 1087
1525 /* special case for the lone root path */ 1127 /* special case for the lone root path */
1526 if (res == tmpbuf->ptr) 1128 if (res == tmpbuf->ptr)
1527 *res++ = '/'; 1129 *res++ = '/';
1528 1130
1529 return res - (char *)tmpbuf->ptr; 1131 return res - (char *)tmpbuf->ptr;
1132#endif
1530} 1133}
1531 1134
1532static signed char 1135static signed char
1533eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1136eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1534{ 1137{
1694 eio_dent_insertion_sort (dents, size); 1297 eio_dent_insertion_sort (dents, size);
1695} 1298}
1696 1299
1697/* read a full directory */ 1300/* read a full directory */
1698static void 1301static void
1699eio__scandir (eio_req *req) 1302eio__scandir (eio_req *req, etp_worker *self)
1700{ 1303{
1701 char *name, *names; 1304 char *name, *names;
1702 int namesalloc = 4096 - sizeof (void *) * 4; 1305 int namesalloc = 4096 - sizeof (void *) * 4;
1703 int namesoffs = 0; 1306 int namesoffs = 0;
1704 int flags = req->int1; 1307 int flags = req->int1;
1722#ifdef _WIN32 1325#ifdef _WIN32
1723 { 1326 {
1724 int len = strlen ((const char *)req->ptr1); 1327 int len = strlen ((const char *)req->ptr1);
1725 char *path = malloc (MAX_PATH); 1328 char *path = malloc (MAX_PATH);
1726 const char *fmt; 1329 const char *fmt;
1330 const char *reqpath = wd_expand (&self->tmpbuf, req->wd, req->ptr1);
1727 1331
1728 if (!len) 1332 if (!len)
1729 fmt = "./*"; 1333 fmt = "./*";
1730 else if (((const char *)req->ptr1)[len - 1] == '/' || ((const char *)req->ptr1)[len - 1] == '\\') 1334 else if (reqpath[len - 1] == '/' || reqpath[len - 1] == '\\')
1731 fmt = "%s*"; 1335 fmt = "%s*";
1732 else 1336 else
1733 fmt = "%s/*"; 1337 fmt = "%s/*";
1734 1338
1735 _snprintf (path, MAX_PATH, fmt, (const char *)req->ptr1); 1339 _snprintf (path, MAX_PATH, fmt, reqpath);
1736 dirp = FindFirstFile (path, &entp); 1340 dirp = FindFirstFile (path, &entp);
1737 free (path); 1341 free (path);
1738 1342
1739 if (dirp == INVALID_HANDLE_VALUE) 1343 if (dirp == INVALID_HANDLE_VALUE)
1740 { 1344 {
1762 1366
1763 return; 1367 return;
1764 } 1368 }
1765 } 1369 }
1766#else 1370#else
1371 #if HAVE_AT
1372 if (req->wd)
1373 {
1374 int fd = openat (WD2FD (req->wd), req->ptr1, O_CLOEXEC | O_SEARCH | O_DIRECTORY);
1375
1376 if (fd < 0)
1377 return;
1378
1379 dirp = fdopendir (fd);
1380
1381 if (!dirp)
1382 close (fd);
1383 }
1384 else
1767 dirp = opendir (req->ptr1); 1385 dirp = opendir (req->ptr1);
1386 #else
1387 dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1));
1388 #endif
1768 1389
1769 if (!dirp) 1390 if (!dirp)
1770 return; 1391 return;
1771#endif 1392#endif
1772 1393
1889 #ifdef DT_FIFO 1510 #ifdef DT_FIFO
1890 case DT_FIFO: ent->type = EIO_DT_FIFO; break; 1511 case DT_FIFO: ent->type = EIO_DT_FIFO; break;
1891 #endif 1512 #endif
1892 #ifdef DT_CHR 1513 #ifdef DT_CHR
1893 case DT_CHR: ent->type = EIO_DT_CHR; break; 1514 case DT_CHR: ent->type = EIO_DT_CHR; break;
1894 #endif 1515 #endif
1895 #ifdef DT_MPC 1516 #ifdef DT_MPC
1896 case DT_MPC: ent->type = EIO_DT_MPC; break; 1517 case DT_MPC: ent->type = EIO_DT_MPC; break;
1897 #endif 1518 #endif
1898 #ifdef DT_DIR 1519 #ifdef DT_DIR
1899 case DT_DIR: ent->type = EIO_DT_DIR; break; 1520 case DT_DIR: ent->type = EIO_DT_DIR; break;
1900 #endif 1521 #endif
1901 #ifdef DT_NAM 1522 #ifdef DT_NAM
1902 case DT_NAM: ent->type = EIO_DT_NAM; break; 1523 case DT_NAM: ent->type = EIO_DT_NAM; break;
1903 #endif 1524 #endif
1904 #ifdef DT_BLK 1525 #ifdef DT_BLK
1905 case DT_BLK: ent->type = EIO_DT_BLK; break; 1526 case DT_BLK: ent->type = EIO_DT_BLK; break;
1906 #endif 1527 #endif
1907 #ifdef DT_MPB 1528 #ifdef DT_MPB
1908 case DT_MPB: ent->type = EIO_DT_MPB; break; 1529 case DT_MPB: ent->type = EIO_DT_MPB; break;
1909 #endif 1530 #endif
1910 #ifdef DT_REG 1531 #ifdef DT_REG
1911 case DT_REG: ent->type = EIO_DT_REG; break; 1532 case DT_REG: ent->type = EIO_DT_REG; break;
1912 #endif 1533 #endif
1913 #ifdef DT_NWK 1534 #ifdef DT_NWK
1914 case DT_NWK: ent->type = EIO_DT_NWK; break; 1535 case DT_NWK: ent->type = EIO_DT_NWK; break;
1915 #endif 1536 #endif
1916 #ifdef DT_CMP 1537 #ifdef DT_CMP
1917 case DT_CMP: ent->type = EIO_DT_CMP; break; 1538 case DT_CMP: ent->type = EIO_DT_CMP; break;
1918 #endif 1539 #endif
1919 #ifdef DT_LNK 1540 #ifdef DT_LNK
1920 case DT_LNK: ent->type = EIO_DT_LNK; break; 1541 case DT_LNK: ent->type = EIO_DT_LNK; break;
1921 #endif 1542 #endif
1922 #ifdef DT_SOCK 1543 #ifdef DT_SOCK
1923 case DT_SOCK: ent->type = EIO_DT_SOCK; break; 1544 case DT_SOCK: ent->type = EIO_DT_SOCK; break;
1936 { 1557 {
1937 if (ent->type == EIO_DT_UNKNOWN) 1558 if (ent->type == EIO_DT_UNKNOWN)
1938 { 1559 {
1939 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ 1560 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */
1940 ent->score = 1; 1561 ent->score = 1;
1941 else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */ 1562 else if (!strchr (name, '.')) /* absence of dots indicate likely dirs */
1942 ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */ 1563 ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */
1943 } 1564 }
1944 else if (ent->type == EIO_DT_DIR) 1565 else if (ent->type == EIO_DT_DIR)
1945 ent->score = 0; 1566 ent->score = 0;
1946 } 1567 }
1966 } 1587 }
1967} 1588}
1968 1589
1969/*****************************************************************************/ 1590/*****************************************************************************/
1970/* working directory stuff */ 1591/* working directory stuff */
1592/* various deficiencies in the posix 2008 api force us to */
1593/* keep the absolute path in string form at all times */
1594/* fuck yeah. */
1595
1596#if !HAVE_AT
1597
1598/* a bit like realpath, but usually faster because it doesn'T have to return */
1599/* an absolute or canonical path */
1600static const char *
1601wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
1602{
1603 if (!wd || *path == '/')
1604 return path;
1605
1606 if (path [0] == '.' && !path [1])
1607 return wd->str;
1608
1609 {
1610 int l1 = wd->len;
1611 int l2 = strlen (path);
1612
1613 char *res = etp_tmpbuf_get (tmpbuf, l1 + l2 + 2);
1614
1615 memcpy (res, wd->str, l1);
1616 res [l1] = '/';
1617 memcpy (res + l1 + 1, path, l2 + 1);
1618
1619 return res;
1620 }
1621}
1622
1623#endif
1624
1625static eio_wd
1626eio__wd_open_sync (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
1627{
1628 int fd;
1629 eio_wd res;
1630 int len = eio__realpath (tmpbuf, wd, path);
1631
1632 if (len < 0)
1633 return EIO_INVALID_WD;
1971 1634
1972#if HAVE_AT 1635#if HAVE_AT
1636 fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY);
1973 1637
1974#define WD2FD(wd) ((wd) ? ((int)(long)(wd)) - 1 : AT_FDCWD) 1638 if (fd < 0)
1975 1639 return EIO_INVALID_WD;
1976#ifndef O_SEARCH
1977# define O_SEARCH O_RDONLY
1978#endif 1640#endif
1641
1642 res = malloc (sizeof (*res) + len); /* one extra 0-byte */
1643
1644#if HAVE_AT
1645 res->fd = fd;
1646#endif
1647
1648 res->len = len;
1649 memcpy (res->str, tmpbuf->ptr, len);
1650 res->str [len] = 0;
1651
1652 return res;
1653}
1979 1654
1980eio_wd 1655eio_wd
1981eio_wd_open_sync (eio_wd wd, const char *path) 1656eio_wd_open_sync (eio_wd wd, const char *path)
1982{ 1657{
1983 int fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY); 1658 struct etp_tmpbuf tmpbuf = { };
1984
1985 return fd >= 0 ? (eio_wd)(long)(fd + 1) : EIO_INVALID_WD;
1986}
1987
1988static eio_wd
1989eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
1990{
1991 return eio_wd_open_sync (wd, path); 1659 wd = eio__wd_open_sync (&tmpbuf, wd, path);
1660 free (tmpbuf.ptr);
1661
1662 return wd;
1992} 1663}
1993 1664
1994void 1665void
1995eio_wd_close_sync (eio_wd wd) 1666eio_wd_close_sync (eio_wd wd)
1996{ 1667{
1997 int fd = WD2FD (wd); 1668 if (wd != EIO_INVALID_WD && wd != EIO_CWD)
1998 1669 {
1999 if (fd >= 0) 1670 #if HAVE_AT
2000 close (fd); 1671 close (wd->fd);
1672 #endif
1673 free (wd);
1674 }
2001} 1675}
1676
1677#if HAVE_AT
1678
1679/* they forgot these */
2002 1680
2003static int 1681static int
2004eio__truncateat (int dirfd, const char *path, off_t length) 1682eio__truncateat (int dirfd, const char *path, off_t length)
2005{ 1683{
2006 int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC); 1684 int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC);
2027 close (fd); 1705 close (fd);
2028 return res; 1706 return res;
2029 1707
2030} 1708}
2031 1709
2032#else
2033
2034/* on legacy systems, we represent the working directories simply by their path strings */
2035
2036static const char *
2037wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
2038{
2039 if (!wd || *path == '/')
2040 return path;
2041
2042 {
2043 int l1 = strlen ((const char *)wd);
2044 int l2 = strlen (path);
2045
2046 char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2);
2047
2048 memcpy (res, wd, l1);
2049 res [l1] = '/';
2050 memcpy (res + l1 + 1, path, l2 + 1);
2051
2052 return res;
2053 }
2054}
2055
2056static eio_wd
2057eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
2058{
2059 if (*path == '/') /* absolute paths ignore wd */
2060 path = strdup (path);
2061 else if (path [0] == '.' && !path [1]) /* special case '.', as it is common */
2062 return wd;
2063 else
2064 {
2065 int len = eio__realpath (tmpbuf, wd, path);
2066
2067 path = EIO_INVALID_WD;
2068
2069 if (len >= 0)
2070 {
2071 ((char *)tmpbuf->ptr)[len] = 0;
2072 path = strdup (tmpbuf->ptr);
2073 }
2074 }
2075
2076 if (!path)
2077 path = EIO_INVALID_WD;
2078
2079 return (eio_wd)path;
2080}
2081
2082eio_wd
2083eio_wd_open_sync (eio_wd wd, const char *path)
2084{
2085 struct tmpbuf tmpbuf = { 0 };
2086 wd = eio__wd_open_sync (&tmpbuf, wd, path);
2087 free (tmpbuf.ptr);
2088
2089 return wd;
2090}
2091
2092void
2093eio_wd_close_sync (eio_wd wd)
2094{
2095 if (wd != EIO_INVALID_WD)
2096 free (wd);
2097}
2098
2099#endif 1710#endif
2100 1711
2101/*****************************************************************************/ 1712/*****************************************************************************/
2102 1713
2103#define ALLOC(len) \ 1714#define ALLOC(len) \
2104 if (!req->ptr2) \ 1715 if (!req->ptr2) \
2105 { \ 1716 { \
2106 X_LOCK (wrklock); \ 1717 X_LOCK (EIO_POOL->wrklock); \
2107 req->flags |= EIO_FLAG_PTR2_FREE; \ 1718 req->flags |= EIO_FLAG_PTR2_FREE; \
2108 X_UNLOCK (wrklock); \ 1719 X_UNLOCK (EIO_POOL->wrklock); \
2109 req->ptr2 = malloc (len); \ 1720 req->ptr2 = malloc (len); \
2110 if (!req->ptr2) \ 1721 if (!req->ptr2) \
2111 { \ 1722 { \
2112 errno = ENOMEM; \ 1723 errno = ENOMEM; \
2113 req->result = -1; \ 1724 req->result = -1; \
2114 break; \ 1725 break; \
2115 } \ 1726 } \
2116 } 1727 }
2117 1728
2118X_THREAD_PROC (etp_proc)
2119{
2120 ETP_REQ *req;
2121 struct timespec ts;
2122 etp_worker *self = (etp_worker *)thr_arg;
2123
2124#if HAVE_PRCTL_SET_NAME
2125 prctl (PR_SET_NAME, (unsigned long)"eio_thread", 0, 0, 0);
2126#endif
2127
2128 /* try to distribute timeouts somewhat evenly */
2129 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
2130
2131 for (;;)
2132 {
2133 ts.tv_sec = 0;
2134
2135 X_LOCK (reqlock);
2136
2137 for (;;)
2138 {
2139 req = reqq_shift (&req_queue);
2140
2141 if (req)
2142 break;
2143
2144 if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */
2145 {
2146 X_UNLOCK (reqlock);
2147 X_LOCK (wrklock);
2148 --started;
2149 X_UNLOCK (wrklock);
2150 goto quit;
2151 }
2152
2153 ++idle;
2154
2155 if (idle <= max_idle)
2156 /* we are allowed to idle, so do so without any timeout */
2157 X_COND_WAIT (reqwait, reqlock);
2158 else
2159 {
2160 /* initialise timeout once */
2161 if (!ts.tv_sec)
2162 ts.tv_sec = time (0) + idle_timeout;
2163
2164 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
2165 ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */
2166 }
2167
2168 --idle;
2169 }
2170
2171 --nready;
2172
2173 X_UNLOCK (reqlock);
2174
2175 if (req->type < 0)
2176 goto quit;
2177
2178 ETP_EXECUTE (self, req);
2179
2180 X_LOCK (reslock);
2181
2182 ++npending;
2183
2184 if (!reqq_push (&res_queue, req) && want_poll_cb)
2185 want_poll_cb ();
2186
2187 etp_worker_clear (self);
2188
2189 X_UNLOCK (reslock);
2190 }
2191
2192quit:
2193 free (req);
2194
2195 X_LOCK (wrklock);
2196 etp_worker_free (self);
2197 X_UNLOCK (wrklock);
2198
2199 return 0;
2200}
2201
2202/*****************************************************************************/ 1729/*****************************************************************************/
2203 1730
2204int ecb_cold 1731int ecb_cold
2205eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1732eio_init (void (*want_poll)(void), void (*done_poll)(void))
2206{ 1733{
2207#if !HAVE_PREADWRITE 1734 eio_want_poll_cb = want_poll;
2208 X_MUTEX_CREATE (preadwritelock); 1735 eio_done_poll_cb = done_poll;
2209#endif
2210 1736
2211 return etp_init (want_poll, done_poll); 1737 return etp_init (EIO_POOL, 0, 0, 0);
2212} 1738}
2213 1739
2214ecb_inline void 1740ecb_inline void
2215eio_api_destroy (eio_req *req) 1741eio_api_destroy (eio_req *req)
2216{ 1742{
2217 free (req); 1743 free (req);
2218} 1744}
2219 1745
2220#define REQ(rtype) \ 1746#define REQ(rtype) \
2221 eio_req *req; \ 1747 eio_req *req; \
2222 \ 1748 \
2223 req = (eio_req *)calloc (1, sizeof *req); \ 1749 req = (eio_req *)calloc (1, sizeof *req); \
2224 if (!req) \ 1750 if (!req) \
2225 return 0; \ 1751 return 0; \
2239 { \ 1765 { \
2240 eio_api_destroy (req); \ 1766 eio_api_destroy (req); \
2241 return 0; \ 1767 return 0; \
2242 } 1768 }
2243 1769
1770#define SINGLEDOT(ptr) (0[(char *)(ptr)] == '.' && !1[(char *)(ptr)])
1771
2244static void 1772static void
2245eio_execute (etp_worker *self, eio_req *req) 1773eio_execute (etp_worker *self, eio_req *req)
2246{ 1774{
2247#if HAVE_AT 1775#if HAVE_AT
2248 int dirfd; 1776 int dirfd;
2279 req->result = req->wd == EIO_INVALID_WD ? -1 : 0; 1807 req->result = req->wd == EIO_INVALID_WD ? -1 : 0;
2280 break; 1808 break;
2281 case EIO_WD_CLOSE: req->result = 0; 1809 case EIO_WD_CLOSE: req->result = 0;
2282 eio_wd_close_sync (req->wd); break; 1810 eio_wd_close_sync (req->wd); break;
2283 1811
1812 case EIO_SEEK: eio__lseek (req); break;
2284 case EIO_READ: ALLOC (req->size); 1813 case EIO_READ: ALLOC (req->size);
2285 req->result = req->offs >= 0 1814 req->result = req->offs >= 0
2286 ? pread (req->int1, req->ptr2, req->size, req->offs) 1815 ? pread (req->int1, req->ptr2, req->size, req->offs)
2287 : read (req->int1, req->ptr2, req->size); break; 1816 : read (req->int1, req->ptr2, req->size); break;
2288 case EIO_WRITE: req->result = req->offs >= 0 1817 case EIO_WRITE: req->result = req->offs >= 0
2302 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;
2303 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;
2304 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;
2305 1834
2306 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; 1835 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break;
2307 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;
2308 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;
2309 case EIO_RENAME: req->result = renameat (dirfd, req->ptr1, WD2FD (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;
2310 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD (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;
2311 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;
2312 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;
2313 case EIO_READLINK: ALLOC (PATH_MAX); 1848 case EIO_READLINK: ALLOC (PATH_MAX);
2314 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break; 1849 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break;
2315 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); 1850 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2415 case EIO_MTOUCH: req->result = eio__mtouch (req); break; 1950 case EIO_MTOUCH: req->result = eio__mtouch (req); break;
2416 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; 1951 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break;
2417 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; 1952 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break;
2418 case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break; 1953 case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break;
2419 1954
2420 case EIO_READDIR: eio__scandir (req); break; 1955 case EIO_READDIR: eio__scandir (req, self); break;
2421 1956
2422 case EIO_BUSY: 1957 case EIO_BUSY:
2423#ifdef _WIN32 1958#ifdef _WIN32
2424 Sleep (req->nv1 * 1e3); 1959 Sleep (req->nv1 * 1e3);
2425#else 1960#else
2432 req->result = select (0, 0, 0, 0, &tv); 1967 req->result = select (0, 0, 0, 0, &tv);
2433 } 1968 }
2434#endif 1969#endif
2435 break; 1970 break;
2436 1971
1972#if 0
2437 case EIO_GROUP: 1973 case EIO_GROUP:
2438 abort (); /* handled in eio_request */ 1974 abort (); /* handled in eio_request */
1975#endif
2439 1976
2440 case EIO_NOP: 1977 case EIO_NOP:
2441 req->result = 0; 1978 req->result = 0;
2442 break; 1979 break;
2443 1980
2444 case EIO_CUSTOM: 1981 case EIO_CUSTOM:
2445 req->feed (req); 1982 req->feed (req);
2446 break; 1983 break;
2447 1984
2448 default: 1985 default:
2449 errno = ENOSYS;
2450 req->result = -1; 1986 req->result = EIO_ENOSYS ();
2451 break; 1987 break;
2452 } 1988 }
2453 1989
2454 req->errorno = errno; 1990 req->errorno = errno;
2455} 1991}
2456 1992
2457#ifndef EIO_NO_WRAPPERS 1993#ifndef EIO_NO_WRAPPERS
2458 1994
1995eio_req *eio_wd_open (const char *path, int pri, eio_cb cb, void *data)
1996{
1997 REQ (EIO_WD_OPEN); PATH; SEND;
1998}
1999
2000eio_req *eio_wd_close (eio_wd wd, int pri, eio_cb cb, void *data)
2001{
2002 REQ (EIO_WD_CLOSE); req->wd = wd; SEND;
2003}
2004
2459eio_req *eio_nop (int pri, eio_cb cb, void *data) 2005eio_req *eio_nop (int pri, eio_cb cb, void *data)
2460{ 2006{
2461 REQ (EIO_NOP); SEND; 2007 REQ (EIO_NOP); SEND;
2462} 2008}
2463 2009
2522} 2068}
2523 2069
2524eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) 2070eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data)
2525{ 2071{
2526 REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; 2072 REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND;
2073}
2074
2075eio_req *eio_seek (int fd, off_t offset, int whence, int pri, eio_cb cb, void *data)
2076{
2077 REQ (EIO_SEEK); req->int1 = fd; req->offs = offset; req->int2 = whence; SEND;
2527} 2078}
2528 2079
2529eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) 2080eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2530{ 2081{
2531 REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; 2082 REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
2729void 2280void
2730eio_grp_add (eio_req *grp, eio_req *req) 2281eio_grp_add (eio_req *grp, eio_req *req)
2731{ 2282{
2732 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));
2733 2284
2734 grp->flags |= EIO_FLAG_GROUPADD; 2285 grp->flags |= ETP_FLAG_GROUPADD;
2735 2286
2736 ++grp->size; 2287 ++grp->size;
2737 req->grp = grp; 2288 req->grp = grp;
2738 2289
2739 req->grp_prev = 0; 2290 req->grp_prev = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines