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

Comparing libeio/eio.c (file contents):
Revision 1.116 by root, Tue Mar 27 19:27:35 2012 UTC vs.
Revision 1.144 by root, Tue Dec 27 09:58:44 2016 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,2016 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,
42#endif 42#endif
43 43
44#include "eio.h" 44#include "eio.h"
45#include "ecb.h" 45#include "ecb.h"
46 46
47#ifdef EIO_STACKSIZE
48# define X_STACKSIZE EIO_STACKSIZE
49#endif
50#include "xthread.h"
51
52#include <errno.h> 47#include <errno.h>
53#include <stddef.h> 48#include <stddef.h>
54#include <stdlib.h> 49#include <stdlib.h>
55#include <string.h> 50#include <string.h>
56#include <errno.h> 51#include <errno.h>
120 #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1)) 115 #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1))
121 116
122 #define chmod(path,mode) _chmod (path, mode) 117 #define chmod(path,mode) _chmod (path, mode)
123 #define dup(fd) _dup (fd) 118 #define dup(fd) _dup (fd)
124 #define dup2(fd1,fd2) _dup2 (fd1, fd2) 119 #define dup2(fd1,fd2) _dup2 (fd1, fd2)
120 #define pipe(fds) _pipe (fds, 4096, O_BINARY)
125 121
122 #define fcntl(fd,cmd,arg) EIO_ENOSYS ()
123 #define ioctl(fd,cmd,arg) EIO_ENOSYS ()
126 #define fchmod(fd,mode) EIO_ENOSYS () 124 #define fchmod(fd,mode) EIO_ENOSYS ()
127 #define chown(path,uid,gid) EIO_ENOSYS () 125 #define chown(path,uid,gid) EIO_ENOSYS ()
128 #define fchown(fd,uid,gid) EIO_ENOSYS () 126 #define fchown(fd,uid,gid) EIO_ENOSYS ()
129 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ 127 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */
130 #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ 128 #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */
131 #define mknod(path,mode,dev) EIO_ENOSYS () 129 #define mknod(path,mode,dev) EIO_ENOSYS ()
132 #define sync() EIO_ENOSYS () 130 #define sync() EIO_ENOSYS ()
133 #define readlink(path,buf,s) EIO_ENOSYS () 131 #define readlink(path,buf,s) EIO_ENOSYS ()
134 #define statvfs(path,buf) EIO_ENOSYS () 132 #define statvfs(path,buf) EIO_ENOSYS ()
135 #define fstatvfs(fd,buf) EIO_ENOSYS () 133 #define fstatvfs(fd,buf) EIO_ENOSYS ()
134
135 #define pread(fd,buf,count,offset) eio__pread (fd, buf, count, offset)
136 #define pwrite(fd,buf,count,offset) eio__pwrite (fd, buf, count, offset)
137
138 #if __GNUC__
139 typedef long long eio_off_t; /* signed for compatibility to msvc */
140 #else
141 typedef __int64 eio_off_t; /* unsigned not supported by msvc */
142 #endif
143
144 static eio_ssize_t
145 eio__pread (int fd, void *buf, eio_ssize_t count, eio_off_t offset)
146 {
147 OVERLAPPED o = { 0 };
148 DWORD got;
149
150 o.Offset = offset;
151 o.OffsetHigh = offset >> 32;
152
153 return ReadFile ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd), buf, count, &got, &o)
154 ? got : -1;
155 }
156
157 static eio_ssize_t
158 eio__pwrite (int fd, void *buf, eio_ssize_t count, eio_off_t offset)
159 {
160 OVERLAPPED o = { 0 };
161 DWORD got;
162
163 o.Offset = offset;
164 o.OffsetHigh = offset >> 32;
165
166 return WriteFile ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd), buf, count, &got, &o)
167 ? got : -1;
168 }
136 169
137 /* rename() uses MoveFile, which fails to overwrite */ 170 /* rename() uses MoveFile, which fails to overwrite */
138 #define rename(old,neu) eio__rename (old, neu) 171 #define rename(old,neu) eio__rename (old, neu)
139 172
140 static int 173 static int
168 /* we could even stat and see if it exists */ 201 /* we could even stat and see if it exists */
169 static int 202 static int
170 symlink (const char *old, const char *neu) 203 symlink (const char *old, const char *neu)
171 { 204 {
172 #if WINVER >= 0x0600 205 #if WINVER >= 0x0600
206 int flags;
207
208 /* This tries out all combinations of SYMBOLIC_LINK_FLAG_DIRECTORY
209 * and SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE,
210 * with directory first.
211 */
212 for (flags = 3; flags >= 0; --flags)
173 if (CreateSymbolicLink (neu, old, 1)) 213 if (CreateSymbolicLink (neu, old, flags))
174 return 0; 214 return 0;
175
176 if (CreateSymbolicLink (neu, old, 0))
177 return 0;
178 #endif 215 #endif
179 216
180 return EIO_ERRNO (ENOENT, -1); 217 return EIO_ERRNO (ENOENT, -1);
181 } 218 }
182 219
183 /* POSIX API only */ 220 /* POSIX API only, causing trouble for win32 apps */
184 #define CreateHardLink(neu,old,flags) 0 221 #define CreateHardLink(neu,old,flags) 0 /* not really creating hardlink, still using relative paths? */
185 #define CreateSymbolicLink(neu,old,flags) 0 222 #define CreateSymbolicLink(neu,old,flags) 0 /* vista+ only */
186 223
187 struct statvfs 224 struct statvfs
188 { 225 {
189 int dummy; 226 int dummy;
190 }; 227 };
196 233
197#else 234#else
198 235
199 #include <sys/time.h> 236 #include <sys/time.h>
200 #include <sys/select.h> 237 #include <sys/select.h>
201 #include <sys/statvfs.h>
202 #include <unistd.h> 238 #include <unistd.h>
203 #include <signal.h> 239 #include <signal.h>
204 #include <dirent.h> 240 #include <dirent.h>
205 241
242 #ifdef ANDROID
243 #include <sys/vfs.h>
244 #define statvfs statfs
245 #define fstatvfs fstatfs
246 #include <asm/page.h> /* supposedly limits.h does #define PAGESIZE PAGESIZE */
247 #else
248 #include <sys/statvfs.h>
249 #endif
250
206 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES 251 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
207 #include <sys/mman.h> 252 #include <sys/mman.h>
208 #endif 253 #endif
209 254
210 #define D_NAME(entp) entp->d_name 255 #define D_NAME(entp) entp->d_name
211 256
212 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ 257 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */
213 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ 258 #if __FreeBSD__ || __NetBSD__ || __OpenBSD__
214 #define _DIRENT_HAVE_D_TYPE /* sigh */ 259 #define _DIRENT_HAVE_D_TYPE /* sigh */
215 #define D_INO(de) (de)->d_fileno 260 #define D_INO(de) (de)->d_fileno
216 #define D_NAMLEN(de) (de)->d_namlen 261 #define D_NAMLEN(de) (de)->d_namlen
217 #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 262 #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600
218 #define D_INO(de) (de)->d_ino 263 #define D_INO(de) (de)->d_ino
237# include <utime.h> 282# include <utime.h>
238#endif 283#endif
239 284
240#if HAVE_SYS_SYSCALL_H 285#if HAVE_SYS_SYSCALL_H
241# include <sys/syscall.h> 286# include <sys/syscall.h>
242#endif
243
244#if HAVE_SYS_PRCTL_H
245# include <sys/prctl.h>
246#endif 287#endif
247 288
248#if HAVE_SENDFILE 289#if HAVE_SENDFILE
249# if __linux 290# if __linux
250# include <sys/sendfile.h> 291# include <sys/sendfile.h>
275# define NAME_MAX 4096 316# define NAME_MAX 4096
276#endif 317#endif
277 318
278/* used for readlink etc. */ 319/* used for readlink etc. */
279#ifndef PATH_MAX 320#ifndef PATH_MAX
280# define PATH_MAX 4096 321# define PATH_MAX 0
281#endif 322#endif
323
324#ifndef EIO_PATH_MIN
325# define EIO_PATH_MIN 8160
326#endif
327
328#define EIO_PATH_MAX (PATH_MAX <= EIO_PATH_MIN ? EIO_PATH_MIN : PATH_MAX)
282 329
283/* buffer size for various temporary buffers */ 330/* buffer size for various temporary buffers */
284#define EIO_BUFSIZE 65536 331#define EIO_BUFSIZE 65536
285 332
286#define dBUF \ 333#define dBUF \
287 char *eio_buf = malloc (EIO_BUFSIZE); \ 334 char *eio_buf = malloc (EIO_BUFSIZE); \
288 errno = ENOMEM; \ 335 errno = ENOMEM; \
289 if (!eio_buf) \ 336 if (!eio_buf) \
290 return -1 337 return -1
291 338
292#define FUBd \ 339#define FUBd \
293 free (eio_buf) 340 free (eio_buf)
294 341
295#define EIO_TICKS ((1000000 + 1023) >> 10)
296
297/*****************************************************************************/ 342/*****************************************************************************/
298 343
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; 344struct etp_tmpbuf;
318 345
319#if _POSIX_VERSION >= 200809L 346#if _POSIX_VERSION >= 200809L
320 #define HAVE_AT 1 347 #define HAVE_AT 1
321 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) 348 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD)
322 #ifndef O_SEARCH 349 #ifndef O_SEARCH
323 #define O_SEARCH O_RDONLY 350 #define O_SEARCH O_RDONLY
324 #endif 351 #endif
325#else 352#else
326 #define HAVE_AT 0 353 #define HAVE_AT 0
327 static const char *wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path); 354 static const char *wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path);
328#endif 355#endif
329 356
330struct eio_pwd 357struct eio_pwd
331{ 358{
332#if HAVE_AT 359#if HAVE_AT
339/*****************************************************************************/ 366/*****************************************************************************/
340 367
341#define ETP_PRI_MIN EIO_PRI_MIN 368#define ETP_PRI_MIN EIO_PRI_MIN
342#define ETP_PRI_MAX EIO_PRI_MAX 369#define ETP_PRI_MAX EIO_PRI_MAX
343 370
371#define ETP_TYPE_QUIT -1
372#define ETP_TYPE_GROUP EIO_GROUP
373
374static void eio_nop_callback (void) { }
375static void (*eio_want_poll_cb)(void) = eio_nop_callback;
376static void (*eio_done_poll_cb)(void) = eio_nop_callback;
377
378#define ETP_WANT_POLL(pool) eio_want_poll_cb ()
379#define ETP_DONE_POLL(pool) eio_done_poll_cb ()
380
344struct etp_worker; 381struct etp_worker;
345
346#define ETP_REQ eio_req 382#define ETP_REQ eio_req
347#define ETP_DESTROY(req) eio_destroy (req) 383#define ETP_DESTROY(req) eio_destroy (req)
348static int eio_finish (eio_req *req); 384static int eio_finish (eio_req *req);
349#define ETP_FINISH(req) eio_finish (req) 385#define ETP_FINISH(req) eio_finish (req)
350static void eio_execute (struct etp_worker *self, eio_req *req); 386static void eio_execute (struct etp_worker *self, eio_req *req);
351#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) 387#define ETP_EXECUTE(wrk,req) eio_execute (wrk, req)
352 388
353/*****************************************************************************/ 389#include "etp.c"
354 390
355#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 391static struct etp_pool eio_pool;
356 392#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
384#if !HAVE_PREADWRITE
385/*
386 * make our pread/pwrite emulation safe against themselves, but not against
387 * normal read/write by using a mutex. slows down execution a lot,
388 * but that's your problem, not mine.
389 */
390static xmutex_t preadwritelock;
391#endif
392
393typedef struct etp_worker
394{
395 struct tmpbuf tmpbuf;
396
397 /* locked by wrklock */
398 struct etp_worker *prev, *next;
399
400 xthread_t tid;
401
402#ifdef ETP_WORKER_COMMON
403 ETP_WORKER_COMMON
404#endif
405} etp_worker;
406
407static etp_worker wrk_first; /* NOT etp */
408
409#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
410#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
411
412/* worker threads management */
413
414static void
415etp_worker_clear (etp_worker *wrk)
416{
417}
418
419static void ecb_cold
420etp_worker_free (etp_worker *wrk)
421{
422 free (wrk->tmpbuf.ptr);
423
424 wrk->next->prev = wrk->prev;
425 wrk->prev->next = wrk->next;
426
427 free (wrk);
428}
429
430static unsigned int
431etp_nreqs (void)
432{
433 int retval;
434 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
435 retval = nreqs;
436 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
437 return retval;
438}
439
440static unsigned int
441etp_nready (void)
442{
443 unsigned int retval;
444
445 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
446 retval = nready;
447 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
448
449 return retval;
450}
451
452static unsigned int
453etp_npending (void)
454{
455 unsigned int retval;
456
457 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
458 retval = npending;
459 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
460
461 return retval;
462}
463
464static unsigned int
465etp_nthreads (void)
466{
467 unsigned int retval;
468
469 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
470 retval = started;
471 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
472
473 return retval;
474}
475
476/*
477 * a somewhat faster data structure might be nice, but
478 * with 8 priorities this actually needs <20 insns
479 * per shift, the most expensive operation.
480 */
481typedef struct {
482 ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */
483 int size;
484} etp_reqq;
485
486static etp_reqq req_queue;
487static etp_reqq res_queue;
488
489static void ecb_noinline ecb_cold
490reqq_init (etp_reqq *q)
491{
492 int pri;
493
494 for (pri = 0; pri < ETP_NUM_PRI; ++pri)
495 q->qs[pri] = q->qe[pri] = 0;
496
497 q->size = 0;
498}
499
500static int ecb_noinline
501reqq_push (etp_reqq *q, ETP_REQ *req)
502{
503 int pri = req->pri;
504 req->next = 0;
505
506 if (q->qe[pri])
507 {
508 q->qe[pri]->next = req;
509 q->qe[pri] = req;
510 }
511 else
512 q->qe[pri] = q->qs[pri] = req;
513
514 return q->size++;
515}
516
517static ETP_REQ * ecb_noinline
518reqq_shift (etp_reqq *q)
519{
520 int pri;
521
522 if (!q->size)
523 return 0;
524
525 --q->size;
526
527 for (pri = ETP_NUM_PRI; pri--; )
528 {
529 eio_req *req = q->qs[pri];
530
531 if (req)
532 {
533 if (!(q->qs[pri] = (eio_req *)req->next))
534 q->qe[pri] = 0;
535
536 return req;
537 }
538 }
539
540 abort ();
541}
542
543static int ecb_cold
544etp_init (void (*want_poll)(void), void (*done_poll)(void))
545{
546 X_MUTEX_CREATE (wrklock);
547 X_MUTEX_CREATE (reslock);
548 X_MUTEX_CREATE (reqlock);
549 X_COND_CREATE (reqwait);
550
551 reqq_init (&req_queue);
552 reqq_init (&res_queue);
553
554 wrk_first.next =
555 wrk_first.prev = &wrk_first;
556
557 started = 0;
558 idle = 0;
559 nreqs = 0;
560 nready = 0;
561 npending = 0;
562
563 want_poll_cb = want_poll;
564 done_poll_cb = done_poll;
565
566 return 0;
567}
568
569X_THREAD_PROC (etp_proc);
570
571static void ecb_cold
572etp_start_thread (void)
573{
574 etp_worker *wrk = calloc (1, sizeof (etp_worker));
575
576 /*TODO*/
577 assert (("unable to allocate worker thread data", wrk));
578
579 X_LOCK (wrklock);
580
581 if (thread_create (&wrk->tid, etp_proc, (void *)wrk))
582 {
583 wrk->prev = &wrk_first;
584 wrk->next = wrk_first.next;
585 wrk_first.next->prev = wrk;
586 wrk_first.next = wrk;
587 ++started;
588 }
589 else
590 free (wrk);
591
592 X_UNLOCK (wrklock);
593}
594
595static void
596etp_maybe_start_thread (void)
597{
598 if (ecb_expect_true (etp_nthreads () >= wanted))
599 return;
600
601 /* todo: maybe use idle here, but might be less exact */
602 if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()))
603 return;
604
605 etp_start_thread ();
606}
607
608static void ecb_cold
609etp_end_thread (void)
610{
611 eio_req *req = calloc (1, sizeof (eio_req)); /* will be freed by worker */
612
613 req->type = -1;
614 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
615
616 X_LOCK (reqlock);
617 reqq_push (&req_queue, req);
618 X_COND_SIGNAL (reqwait);
619 X_UNLOCK (reqlock);
620
621 X_LOCK (wrklock);
622 --started;
623 X_UNLOCK (wrklock);
624}
625
626static int
627etp_poll (void)
628{
629 unsigned int maxreqs;
630 unsigned int maxtime;
631 struct timeval tv_start, tv_now;
632
633 X_LOCK (reslock);
634 maxreqs = max_poll_reqs;
635 maxtime = max_poll_time;
636 X_UNLOCK (reslock);
637
638 if (maxtime)
639 gettimeofday (&tv_start, 0);
640
641 for (;;)
642 {
643 ETP_REQ *req;
644
645 etp_maybe_start_thread ();
646
647 X_LOCK (reslock);
648 req = reqq_shift (&res_queue);
649
650 if (req)
651 {
652 --npending;
653
654 if (!res_queue.size && done_poll_cb)
655 done_poll_cb ();
656 }
657
658 X_UNLOCK (reslock);
659
660 if (!req)
661 return 0;
662
663 X_LOCK (reqlock);
664 --nreqs;
665 X_UNLOCK (reqlock);
666
667 if (ecb_expect_false (req->type == EIO_GROUP && req->size))
668 {
669 req->int1 = 1; /* mark request as delayed */
670 continue;
671 }
672 else
673 {
674 int res = ETP_FINISH (req);
675 if (ecb_expect_false (res))
676 return res;
677 }
678
679 if (ecb_expect_false (maxreqs && !--maxreqs))
680 break;
681
682 if (maxtime)
683 {
684 gettimeofday (&tv_now, 0);
685
686 if (tvdiff (&tv_start, &tv_now) >= maxtime)
687 break;
688 }
689 }
690
691 errno = EAGAIN;
692 return -1;
693}
694
695static void
696etp_cancel (ETP_REQ *req)
697{
698 req->cancelled = 1;
699
700 eio_grp_cancel (req);
701}
702
703static void
704etp_submit (ETP_REQ *req)
705{
706 req->pri -= ETP_PRI_MIN;
707
708 if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN;
709 if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
710
711 if (ecb_expect_false (req->type == EIO_GROUP))
712 {
713 /* I hope this is worth it :/ */
714 X_LOCK (reqlock);
715 ++nreqs;
716 X_UNLOCK (reqlock);
717
718 X_LOCK (reslock);
719
720 ++npending;
721
722 if (!reqq_push (&res_queue, req) && want_poll_cb)
723 want_poll_cb ();
724
725 X_UNLOCK (reslock);
726 }
727 else
728 {
729 X_LOCK (reqlock);
730 ++nreqs;
731 ++nready;
732 reqq_push (&req_queue, req);
733 X_COND_SIGNAL (reqwait);
734 X_UNLOCK (reqlock);
735
736 etp_maybe_start_thread ();
737 }
738}
739
740static void ecb_cold
741etp_set_max_poll_time (double nseconds)
742{
743 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
744 max_poll_time = nseconds * EIO_TICKS;
745 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
746}
747
748static void ecb_cold
749etp_set_max_poll_reqs (unsigned int maxreqs)
750{
751 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
752 max_poll_reqs = maxreqs;
753 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
754}
755
756static void ecb_cold
757etp_set_max_idle (unsigned int nthreads)
758{
759 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
760 max_idle = nthreads;
761 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
762}
763
764static void ecb_cold
765etp_set_idle_timeout (unsigned int seconds)
766{
767 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
768 idle_timeout = seconds;
769 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
770}
771
772static void ecb_cold
773etp_set_min_parallel (unsigned int nthreads)
774{
775 if (wanted < nthreads)
776 wanted = nthreads;
777}
778
779static void ecb_cold
780etp_set_max_parallel (unsigned int nthreads)
781{
782 if (wanted > nthreads)
783 wanted = nthreads;
784
785 while (started > wanted)
786 etp_end_thread ();
787}
788 393
789/*****************************************************************************/ 394/*****************************************************************************/
790 395
791static void 396static void
792grp_try_feed (eio_req *grp) 397grp_try_feed (eio_req *grp)
793{ 398{
794 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 399 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
795 { 400 {
796 grp->flags &= ~EIO_FLAG_GROUPADD; 401 grp->flags &= ~ETP_FLAG_GROUPADD;
797 402
798 EIO_FEED (grp); 403 EIO_FEED (grp);
799 404
800 /* stop if no progress has been made */ 405 /* stop if no progress has been made */
801 if (!(grp->flags & EIO_FLAG_GROUPADD)) 406 if (!(grp->flags & ETP_FLAG_GROUPADD))
802 { 407 {
803 grp->feed = 0; 408 grp->feed = 0;
804 break; 409 break;
805 } 410 }
806 } 411 }
813 418
814 /* call feeder, if applicable */ 419 /* call feeder, if applicable */
815 grp_try_feed (grp); 420 grp_try_feed (grp);
816 421
817 /* finish, if done */ 422 /* finish, if done */
818 if (!grp->size && grp->int1) 423 if (!grp->size && grp->flags & ETP_FLAG_DELAYED)
819 return eio_finish (grp); 424 return eio_finish (grp);
820 else 425 else
821 return 0; 426 return 0;
822} 427}
823 428
859} 464}
860 465
861void 466void
862eio_grp_cancel (eio_req *grp) 467eio_grp_cancel (eio_req *grp)
863{ 468{
864 for (grp = grp->grp_first; grp; grp = grp->grp_next) 469 etp_grp_cancel (EIO_POOL, grp);
865 eio_cancel (grp);
866} 470}
867 471
868void 472void
869eio_cancel (eio_req *req) 473eio_cancel (eio_req *req)
870{ 474{
871 etp_cancel (req); 475 etp_cancel (EIO_POOL, req);
872} 476}
873 477
874void 478void
875eio_submit (eio_req *req) 479eio_submit (eio_req *req)
876{ 480{
877 etp_submit (req); 481 etp_submit (EIO_POOL, req);
878} 482}
879 483
880unsigned int 484unsigned int
881eio_nreqs (void) 485eio_nreqs (void)
882{ 486{
883 return etp_nreqs (); 487 return etp_nreqs (EIO_POOL);
884} 488}
885 489
886unsigned int 490unsigned int
887eio_nready (void) 491eio_nready (void)
888{ 492{
889 return etp_nready (); 493 return etp_nready (EIO_POOL);
890} 494}
891 495
892unsigned int 496unsigned int
893eio_npending (void) 497eio_npending (void)
894{ 498{
895 return etp_npending (); 499 return etp_npending (EIO_POOL);
896} 500}
897 501
898unsigned int ecb_cold 502unsigned int ecb_cold
899eio_nthreads (void) 503eio_nthreads (void)
900{ 504{
901 return etp_nthreads (); 505 return etp_nthreads (EIO_POOL);
902} 506}
903 507
904void ecb_cold 508void ecb_cold
905eio_set_max_poll_time (double nseconds) 509eio_set_max_poll_time (double nseconds)
906{ 510{
907 etp_set_max_poll_time (nseconds); 511 etp_set_max_poll_time (EIO_POOL, nseconds);
908} 512}
909 513
910void ecb_cold 514void ecb_cold
911eio_set_max_poll_reqs (unsigned int maxreqs) 515eio_set_max_poll_reqs (unsigned int maxreqs)
912{ 516{
913 etp_set_max_poll_reqs (maxreqs); 517 etp_set_max_poll_reqs (EIO_POOL, maxreqs);
914} 518}
915 519
916void ecb_cold 520void ecb_cold
917eio_set_max_idle (unsigned int nthreads) 521eio_set_max_idle (unsigned int nthreads)
918{ 522{
919 etp_set_max_idle (nthreads); 523 etp_set_max_idle (EIO_POOL, nthreads);
920} 524}
921 525
922void ecb_cold 526void ecb_cold
923eio_set_idle_timeout (unsigned int seconds) 527eio_set_idle_timeout (unsigned int seconds)
924{ 528{
925 etp_set_idle_timeout (seconds); 529 etp_set_idle_timeout (EIO_POOL, seconds);
926} 530}
927 531
928void ecb_cold 532void ecb_cold
929eio_set_min_parallel (unsigned int nthreads) 533eio_set_min_parallel (unsigned int nthreads)
930{ 534{
931 etp_set_min_parallel (nthreads); 535 etp_set_min_parallel (EIO_POOL, nthreads);
932} 536}
933 537
934void ecb_cold 538void ecb_cold
935eio_set_max_parallel (unsigned int nthreads) 539eio_set_max_parallel (unsigned int nthreads)
936{ 540{
937 etp_set_max_parallel (nthreads); 541 etp_set_max_parallel (EIO_POOL, nthreads);
938} 542}
939 543
940int eio_poll (void) 544int eio_poll (void)
941{ 545{
942 return etp_poll (); 546 return etp_poll (EIO_POOL);
943} 547}
944 548
945/*****************************************************************************/ 549/*****************************************************************************/
946/* work around various missing functions */ 550/* work around various missing functions */
947
948#if !HAVE_PREADWRITE
949# undef pread
950# undef pwrite
951# define pread eio__pread
952# define pwrite eio__pwrite
953
954static eio_ssize_t
955eio__pread (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 = read (fd, buf, count);
964 lseek (fd, ooffset, SEEK_SET);
965 X_UNLOCK (preadwritelock);
966
967 return res;
968}
969
970static eio_ssize_t
971eio__pwrite (int fd, void *buf, size_t count, off_t offset)
972{
973 eio_ssize_t res;
974 off_t ooffset;
975
976 X_LOCK (preadwritelock);
977 ooffset = lseek (fd, 0, SEEK_CUR);
978 lseek (fd, offset, SEEK_SET);
979 res = write (fd, buf, count);
980 lseek (fd, ooffset, SEEK_SET);
981 X_UNLOCK (preadwritelock);
982
983 return res;
984}
985#endif
986 551
987#ifndef HAVE_UTIMES 552#ifndef HAVE_UTIMES
988 553
989# undef utimes 554# undef utimes
990# define utimes(path,times) eio__utimes (path, times) 555# define utimes(path,times) eio__utimes (path, times)
1032 int res; 597 int res;
1033 598
1034#if HAVE_SYS_SYNCFS 599#if HAVE_SYS_SYNCFS
1035 res = (int)syscall (__NR_syncfs, (int)(fd)); 600 res = (int)syscall (__NR_syncfs, (int)(fd));
1036#else 601#else
1037 res = -1; 602 res = EIO_ENOSYS ();
1038 errno = ENOSYS;
1039#endif 603#endif
1040 604
1041 if (res < 0 && errno == ENOSYS && fd >= 0) 605 if (res < 0 && errno == ENOSYS && fd >= 0)
1042 sync (); 606 sync ();
1043 607
1073} 637}
1074 638
1075static int 639static int
1076eio__fallocate (int fd, int mode, off_t offset, size_t len) 640eio__fallocate (int fd, int mode, off_t offset, size_t len)
1077{ 641{
1078#if HAVE_FALLOCATE 642#if HAVE_LINUX_FALLOCATE
1079 return fallocate (fd, mode, offset, len); 643 return fallocate (fd, mode, offset, len);
1080#else 644#else
1081 errno = ENOSYS; 645 return EIO_ENOSYS ();
1082 return -1;
1083#endif 646#endif
1084} 647}
1085 648
1086#if !HAVE_READAHEAD 649#if !HAVE_READAHEAD
1087# undef readahead 650# undef readahead
1149 712
1150 /* according to source inspection, this is correct, and useful behaviour */ 713 /* according to source inspection, this is correct, and useful behaviour */
1151 if (sbytes) 714 if (sbytes)
1152 res = sbytes; 715 res = sbytes;
1153 716
1154# elif defined (__APPLE__) 717# elif defined __APPLE__
1155 off_t sbytes = count; 718 off_t sbytes = count;
1156 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); 719 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0);
1157 720
1158 /* according to the manpage, sbytes is always valid */ 721 /* according to the manpage, sbytes is always valid */
1159 if (sbytes) 722 if (sbytes)
1186 HANDLE h = TO_SOCKET (ifd); 749 HANDLE h = TO_SOCKET (ifd);
1187 SetFilePointer (h, offset, 0, FILE_BEGIN); 750 SetFilePointer (h, offset, 0, FILE_BEGIN);
1188 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); 751 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0);
1189 752
1190#else 753#else
1191 res = -1; 754 res = EIO_ENOSYS ();
1192 errno = ENOSYS;
1193#endif 755#endif
1194 756
1195 /* we assume sendfile can copy at least 128mb in one go */ 757 /* we assume sendfile can copy at least 128mb in one go */
1196 if (res <= 128 * 1024 * 1024) 758 if (res <= 128 * 1024 * 1024)
1197 { 759 {
1383} 945}
1384 946
1385/*****************************************************************************/ 947/*****************************************************************************/
1386/* requests implemented outside eio_execute, because they are so large */ 948/* requests implemented outside eio_execute, because they are so large */
1387 949
950static void
951eio__lseek (eio_req *req)
952{
953 /* this usually gets optimised away completely, or your compiler sucks, */
954 /* or the whence constants really are not 0, 1, 2 */
955 int whence = req->int2 == EIO_SEEK_SET ? SEEK_SET
956 : req->int2 == EIO_SEEK_CUR ? SEEK_CUR
957 : req->int2 == EIO_SEEK_END ? SEEK_END
958 : req->int2;
959
960 req->offs = lseek (req->int1, req->offs, whence);
961 req->result = req->offs == (off_t)-1 ? -1 : 0;
962}
963
1388/* result will always end up in tmpbuf, there is always space for adding a 0-byte */ 964/* result will always end up in tmpbuf, there is always space for adding a 0-byte */
1389static int 965static int
1390eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 966eio__realpath (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
1391{ 967{
968 char *res;
1392 const char *rel = path; 969 const char *rel = path;
1393 char *res;
1394 char *tmp1, *tmp2; 970 char *tmp1, *tmp2;
1395#if SYMLOOP_MAX > 32 971#if SYMLOOP_MAX > 32
1396 int symlinks = SYMLOOP_MAX; 972 int symlinks = SYMLOOP_MAX;
1397#else 973#else
1398 int symlinks = 32; 974 int symlinks = 32;
1404 980
1405 errno = ENOENT; 981 errno = ENOENT;
1406 if (!*rel) 982 if (!*rel)
1407 return -1; 983 return -1;
1408 984
1409 res = tmpbuf_get (tmpbuf, PATH_MAX * 3); 985 res = etp_tmpbuf_get (tmpbuf, EIO_PATH_MAX * 3);
986#ifdef _WIN32
987 if (_access (rel, 4) != 0)
988 return -1;
989
990 symlinks = GetFullPathName (rel, EIO_PATH_MAX * 3, res, 0);
991
992 errno = ENAMETOOLONG;
993 if (symlinks >= EIO_PATH_MAX * 3)
994 return -1;
995
996 errno = EIO;
997 if (symlinks <= 0)
998 return -1;
999
1000 return symlinks;
1001
1002#else
1410 tmp1 = res + PATH_MAX; 1003 tmp1 = res + EIO_PATH_MAX;
1411 tmp2 = tmp1 + PATH_MAX; 1004 tmp2 = tmp1 + EIO_PATH_MAX;
1412 1005
1413#if 0 /* disabled, the musl way to do things is just too racy */ 1006#if 0 /* disabled, the musl way to do things is just too racy */
1414#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) 1007#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1415 /* on linux we may be able to ask the kernel */ 1008 /* on linux we may be able to ask the kernel */
1416 { 1009 {
1417 int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); 1010 int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME);
1418 1011
1419 if (fd >= 0) 1012 if (fd >= 0)
1420 { 1013 {
1421 sprintf (tmp1, "/proc/self/fd/%d", fd); 1014 sprintf (tmp1, "/proc/self/fd/%d", fd);
1422 req->result = readlink (tmp1, res, PATH_MAX); 1015 req->result = readlink (tmp1, res, EIO_PATH_MAX);
1016 /* here we should probably stat the open file and the disk file, to make sure they still match */
1423 close (fd); 1017 close (fd);
1424
1425 /* here we should probably stat the open file and the disk file, to make sure they still match */
1426 1018
1427 if (req->result > 0) 1019 if (req->result > 0)
1428 goto done; 1020 goto done;
1429 } 1021 }
1430 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) 1022 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO)
1431 return; 1023 return -1;
1432 } 1024 }
1433#endif 1025#endif
1434#endif 1026#endif
1435 1027
1436 if (*rel != '/') 1028 if (*rel != '/')
1441 if (wd == EIO_INVALID_WD) 1033 if (wd == EIO_INVALID_WD)
1442 return -1; 1034 return -1;
1443 1035
1444 if (wd == EIO_CWD) 1036 if (wd == EIO_CWD)
1445 { 1037 {
1446 if (!getcwd (res, PATH_MAX)) 1038 if (!getcwd (res, EIO_PATH_MAX))
1447 return -1; 1039 return -1;
1448 1040
1449 len = strlen (res); 1041 len = strlen (res);
1450 } 1042 }
1451 else 1043 else
1498 1090
1499 /* zero-terminate, for readlink */ 1091 /* zero-terminate, for readlink */
1500 res [len + 1] = 0; 1092 res [len + 1] = 0;
1501 1093
1502 /* now check if it's a symlink */ 1094 /* now check if it's a symlink */
1503 linklen = readlink (tmpbuf->ptr, tmp1, PATH_MAX); 1095 linklen = readlink (tmpbuf->ptr, tmp1, EIO_PATH_MAX);
1504 1096
1505 if (linklen < 0) 1097 if (linklen < 0)
1506 { 1098 {
1507 if (errno != EINVAL) 1099 if (errno != EINVAL)
1508 return -1; 1100 return -1;
1514 { 1106 {
1515 /* yay, it was a symlink - build new path in tmp2 */ 1107 /* yay, it was a symlink - build new path in tmp2 */
1516 int rellen = strlen (rel); 1108 int rellen = strlen (rel);
1517 1109
1518 errno = ENAMETOOLONG; 1110 errno = ENAMETOOLONG;
1519 if (linklen + 1 + rellen >= PATH_MAX) 1111 if (linklen + 1 + rellen >= EIO_PATH_MAX) /* also catch linklen >= EIO_PATH_MAX */
1520 return -1; 1112 return -1;
1521 1113
1522 errno = ELOOP; 1114 errno = ELOOP;
1523 if (!--symlinks) 1115 if (!--symlinks)
1524 return -1; 1116 return -1;
1538 /* special case for the lone root path */ 1130 /* special case for the lone root path */
1539 if (res == tmpbuf->ptr) 1131 if (res == tmpbuf->ptr)
1540 *res++ = '/'; 1132 *res++ = '/';
1541 1133
1542 return res - (char *)tmpbuf->ptr; 1134 return res - (char *)tmpbuf->ptr;
1135#endif
1543} 1136}
1544 1137
1545static signed char 1138static signed char
1546eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1139eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1547{ 1140{
1920 #ifdef DT_FIFO 1513 #ifdef DT_FIFO
1921 case DT_FIFO: ent->type = EIO_DT_FIFO; break; 1514 case DT_FIFO: ent->type = EIO_DT_FIFO; break;
1922 #endif 1515 #endif
1923 #ifdef DT_CHR 1516 #ifdef DT_CHR
1924 case DT_CHR: ent->type = EIO_DT_CHR; break; 1517 case DT_CHR: ent->type = EIO_DT_CHR; break;
1925 #endif 1518 #endif
1926 #ifdef DT_MPC 1519 #ifdef DT_MPC
1927 case DT_MPC: ent->type = EIO_DT_MPC; break; 1520 case DT_MPC: ent->type = EIO_DT_MPC; break;
1928 #endif 1521 #endif
1929 #ifdef DT_DIR 1522 #ifdef DT_DIR
1930 case DT_DIR: ent->type = EIO_DT_DIR; break; 1523 case DT_DIR: ent->type = EIO_DT_DIR; break;
1931 #endif 1524 #endif
1932 #ifdef DT_NAM 1525 #ifdef DT_NAM
1933 case DT_NAM: ent->type = EIO_DT_NAM; break; 1526 case DT_NAM: ent->type = EIO_DT_NAM; break;
1934 #endif 1527 #endif
1935 #ifdef DT_BLK 1528 #ifdef DT_BLK
1936 case DT_BLK: ent->type = EIO_DT_BLK; break; 1529 case DT_BLK: ent->type = EIO_DT_BLK; break;
1937 #endif 1530 #endif
1938 #ifdef DT_MPB 1531 #ifdef DT_MPB
1939 case DT_MPB: ent->type = EIO_DT_MPB; break; 1532 case DT_MPB: ent->type = EIO_DT_MPB; break;
1940 #endif 1533 #endif
1941 #ifdef DT_REG 1534 #ifdef DT_REG
1942 case DT_REG: ent->type = EIO_DT_REG; break; 1535 case DT_REG: ent->type = EIO_DT_REG; break;
1943 #endif 1536 #endif
1944 #ifdef DT_NWK 1537 #ifdef DT_NWK
1945 case DT_NWK: ent->type = EIO_DT_NWK; break; 1538 case DT_NWK: ent->type = EIO_DT_NWK; break;
1946 #endif 1539 #endif
1947 #ifdef DT_CMP 1540 #ifdef DT_CMP
1948 case DT_CMP: ent->type = EIO_DT_CMP; break; 1541 case DT_CMP: ent->type = EIO_DT_CMP; break;
1949 #endif 1542 #endif
1950 #ifdef DT_LNK 1543 #ifdef DT_LNK
1951 case DT_LNK: ent->type = EIO_DT_LNK; break; 1544 case DT_LNK: ent->type = EIO_DT_LNK; break;
1952 #endif 1545 #endif
1953 #ifdef DT_SOCK 1546 #ifdef DT_SOCK
1954 case DT_SOCK: ent->type = EIO_DT_SOCK; break; 1547 case DT_SOCK: ent->type = EIO_DT_SOCK; break;
2006#if !HAVE_AT 1599#if !HAVE_AT
2007 1600
2008/* a bit like realpath, but usually faster because it doesn'T have to return */ 1601/* a bit like realpath, but usually faster because it doesn'T have to return */
2009/* an absolute or canonical path */ 1602/* an absolute or canonical path */
2010static const char * 1603static const char *
2011wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1604wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
2012{ 1605{
2013 if (!wd || *path == '/') 1606 if (!wd || *path == '/')
2014 return path; 1607 return path;
2015 1608
2016 if (path [0] == '.' && !path [1]) 1609 if (path [0] == '.' && !path [1])
2018 1611
2019 { 1612 {
2020 int l1 = wd->len; 1613 int l1 = wd->len;
2021 int l2 = strlen (path); 1614 int l2 = strlen (path);
2022 1615
2023 char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2); 1616 char *res = etp_tmpbuf_get (tmpbuf, l1 + l2 + 2);
2024 1617
2025 memcpy (res, wd->str, l1); 1618 memcpy (res, wd->str, l1);
2026 res [l1] = '/'; 1619 res [l1] = '/';
2027 memcpy (res + l1 + 1, path, l2 + 1); 1620 memcpy (res + l1 + 1, path, l2 + 1);
2028 1621
2031} 1624}
2032 1625
2033#endif 1626#endif
2034 1627
2035static eio_wd 1628static eio_wd
2036eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1629eio__wd_open_sync (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
2037{ 1630{
2038 int fd; 1631 int fd;
2039 eio_wd res; 1632 eio_wd res;
2040 int len = eio__realpath (tmpbuf, wd, path); 1633 int len = eio__realpath (tmpbuf, wd, path);
2041 1634
2063} 1656}
2064 1657
2065eio_wd 1658eio_wd
2066eio_wd_open_sync (eio_wd wd, const char *path) 1659eio_wd_open_sync (eio_wd wd, const char *path)
2067{ 1660{
2068 struct tmpbuf tmpbuf = { 0 }; 1661 struct etp_tmpbuf tmpbuf = { };
2069 wd = eio__wd_open_sync (&tmpbuf, wd, path); 1662 wd = eio__wd_open_sync (&tmpbuf, wd, path);
2070 free (tmpbuf.ptr); 1663 free (tmpbuf.ptr);
2071 1664
2072 return wd; 1665 return wd;
2073} 1666}
2122/*****************************************************************************/ 1715/*****************************************************************************/
2123 1716
2124#define ALLOC(len) \ 1717#define ALLOC(len) \
2125 if (!req->ptr2) \ 1718 if (!req->ptr2) \
2126 { \ 1719 { \
2127 X_LOCK (wrklock); \ 1720 X_LOCK (EIO_POOL->wrklock); \
2128 req->flags |= EIO_FLAG_PTR2_FREE; \ 1721 req->flags |= EIO_FLAG_PTR2_FREE; \
2129 X_UNLOCK (wrklock); \ 1722 X_UNLOCK (EIO_POOL->wrklock); \
2130 req->ptr2 = malloc (len); \ 1723 req->ptr2 = malloc (len); \
2131 if (!req->ptr2) \ 1724 if (!req->ptr2) \
2132 { \ 1725 { \
2133 errno = ENOMEM; \ 1726 errno = ENOMEM; \
2134 req->result = -1; \ 1727 req->result = -1; \
2135 break; \ 1728 break; \
2136 } \ 1729 } \
2137 } 1730 }
2138 1731
2139static void ecb_noinline ecb_cold
2140etp_proc_init (void)
2141{
2142#if HAVE_PRCTL_SET_NAME
2143 /* provide a more sensible "thread name" */
2144 char name[16 + 1];
2145 const int namelen = sizeof (name) - 1;
2146 int len;
2147
2148 prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0);
2149 name [namelen] = 0;
2150 len = strlen (name);
2151 strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio");
2152 prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0);
2153#endif
2154}
2155
2156X_THREAD_PROC (etp_proc)
2157{
2158 ETP_REQ *req;
2159 struct timespec ts;
2160 etp_worker *self = (etp_worker *)thr_arg;
2161
2162 etp_proc_init ();
2163
2164 /* try to distribute timeouts somewhat evenly */
2165 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
2166
2167 for (;;)
2168 {
2169 ts.tv_sec = 0;
2170
2171 X_LOCK (reqlock);
2172
2173 for (;;)
2174 {
2175 req = reqq_shift (&req_queue);
2176
2177 if (req)
2178 break;
2179
2180 if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */
2181 {
2182 X_UNLOCK (reqlock);
2183 X_LOCK (wrklock);
2184 --started;
2185 X_UNLOCK (wrklock);
2186 goto quit;
2187 }
2188
2189 ++idle;
2190
2191 if (idle <= max_idle)
2192 /* we are allowed to idle, so do so without any timeout */
2193 X_COND_WAIT (reqwait, reqlock);
2194 else
2195 {
2196 /* initialise timeout once */
2197 if (!ts.tv_sec)
2198 ts.tv_sec = time (0) + idle_timeout;
2199
2200 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
2201 ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */
2202 }
2203
2204 --idle;
2205 }
2206
2207 --nready;
2208
2209 X_UNLOCK (reqlock);
2210
2211 if (req->type < 0)
2212 goto quit;
2213
2214 ETP_EXECUTE (self, req);
2215
2216 X_LOCK (reslock);
2217
2218 ++npending;
2219
2220 if (!reqq_push (&res_queue, req) && want_poll_cb)
2221 want_poll_cb ();
2222
2223 etp_worker_clear (self);
2224
2225 X_UNLOCK (reslock);
2226 }
2227
2228quit:
2229 free (req);
2230
2231 X_LOCK (wrklock);
2232 etp_worker_free (self);
2233 X_UNLOCK (wrklock);
2234
2235 return 0;
2236}
2237
2238/*****************************************************************************/ 1732/*****************************************************************************/
2239 1733
2240int ecb_cold 1734int ecb_cold
2241eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1735eio_init (void (*want_poll)(void), void (*done_poll)(void))
2242{ 1736{
2243#if !HAVE_PREADWRITE 1737 eio_want_poll_cb = want_poll;
2244 X_MUTEX_CREATE (preadwritelock); 1738 eio_done_poll_cb = done_poll;
2245#endif
2246 1739
2247 return etp_init (want_poll, done_poll); 1740 return etp_init (EIO_POOL, 0, 0, 0);
2248} 1741}
2249 1742
2250ecb_inline void 1743ecb_inline void
2251eio_api_destroy (eio_req *req) 1744eio_api_destroy (eio_req *req)
2252{ 1745{
2253 free (req); 1746 free (req);
2254} 1747}
2255 1748
2256#define REQ(rtype) \ 1749#define REQ(rtype) \
2257 eio_req *req; \ 1750 eio_req *req; \
2258 \ 1751 \
2259 req = (eio_req *)calloc (1, sizeof *req); \ 1752 req = (eio_req *)calloc (1, sizeof *req); \
2260 if (!req) \ 1753 if (!req) \
2261 return 0; \ 1754 return 0; \
2275 { \ 1768 { \
2276 eio_api_destroy (req); \ 1769 eio_api_destroy (req); \
2277 return 0; \ 1770 return 0; \
2278 } 1771 }
2279 1772
1773#define SINGLEDOT(ptr) (0[(char *)(ptr)] == '.' && !1[(char *)(ptr)])
1774
2280static void 1775static void
2281eio_execute (etp_worker *self, eio_req *req) 1776eio_execute (etp_worker *self, eio_req *req)
2282{ 1777{
2283#if HAVE_AT 1778#if HAVE_AT
2284 int dirfd; 1779 int dirfd;
2315 req->result = req->wd == EIO_INVALID_WD ? -1 : 0; 1810 req->result = req->wd == EIO_INVALID_WD ? -1 : 0;
2316 break; 1811 break;
2317 case EIO_WD_CLOSE: req->result = 0; 1812 case EIO_WD_CLOSE: req->result = 0;
2318 eio_wd_close_sync (req->wd); break; 1813 eio_wd_close_sync (req->wd); break;
2319 1814
1815 case EIO_SEEK: eio__lseek (req); break;
2320 case EIO_READ: ALLOC (req->size); 1816 case EIO_READ: ALLOC (req->size);
2321 req->result = req->offs >= 0 1817 req->result = req->offs >= 0
2322 ? pread (req->int1, req->ptr2, req->size, req->offs) 1818 ? pread (req->int1, req->ptr2, req->size, req->offs)
2323 : read (req->int1, req->ptr2, req->size); break; 1819 : read (req->int1, req->ptr2, req->size); break;
2324 case EIO_WRITE: req->result = req->offs >= 0 1820 case EIO_WRITE: req->result = req->offs >= 0
2325 ? pwrite (req->int1, req->ptr2, req->size, req->offs) 1821 ? pwrite (req->int1, req->ptr2, req->size, req->offs)
2326 : write (req->int1, req->ptr2, req->size); break; 1822 : write (req->int1, req->ptr2, req->size); break;
1823
1824 case EIO_FCNTL: req->result = fcntl (req->int1, (int) req->int2, req->ptr2); break;
1825 case EIO_IOCTL: req->result = ioctl (req->int1, (unsigned long)req->int2, req->ptr2); break;
2327 1826
2328 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; 1827 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
2329 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; 1828 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break;
2330 1829
2331#if HAVE_AT 1830#if HAVE_AT
2338 case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break; 1837 case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break;
2339 case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break; 1838 case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break;
2340 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break; 1839 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break;
2341 1840
2342 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; 1841 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break;
2343 case EIO_RMDIR: req->result = unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; 1842 case EIO_RMDIR: /* complications arise because "." cannot be removed, so we might have to expand */
1843 req->result = req->wd && SINGLEDOT (req->ptr1)
1844 ? rmdir (req->wd->str)
1845 : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break;
2344 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; 1846 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break;
2345 case EIO_RENAME: req->result = renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; 1847 case EIO_RENAME: /* complications arise because "." cannot be renamed, so we might have to expand */
1848 req->result = req->wd && SINGLEDOT (req->ptr1)
1849 ? rename (req->wd->str, req->ptr2)
1850 : renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break;
2346 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; 1851 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break;
2347 case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; 1852 case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break;
2348 case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; 1853 case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
2349 case EIO_READLINK: ALLOC (PATH_MAX);
2350 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break;
2351 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); 1854 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2352 req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; 1855 req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break;
1856 case EIO_READLINK: ALLOC (EIO_PATH_MAX);
1857 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, EIO_PATH_MAX);
1858 if (req->result == EIO_PATH_MAX)
1859 {
1860 req->result = -1;
1861 errno = ENAMETOOLONG;
1862 }
1863 break;
2353 case EIO_UTIME: 1864 case EIO_UTIME:
2354 case EIO_FUTIME: 1865 case EIO_FUTIME:
2355 { 1866 {
2356 struct timespec ts[2]; 1867 struct timespec ts[2];
2357 struct timespec *times; 1868 struct timespec *times;
2390 case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; 1901 case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break;
2391 case EIO_RENAME: req->result = rename (path , req->ptr2); break; 1902 case EIO_RENAME: req->result = rename (path , req->ptr2); break;
2392 case EIO_LINK: req->result = link (path , req->ptr2); break; 1903 case EIO_LINK: req->result = link (path , req->ptr2); break;
2393 case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; 1904 case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break;
2394 case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break; 1905 case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break;
2395 case EIO_READLINK: ALLOC (PATH_MAX);
2396 req->result = readlink (path, req->ptr2, PATH_MAX); break;
2397 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); 1906 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2398 req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; 1907 req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break;
1908 case EIO_READLINK: ALLOC (EIO_PATH_MAX);
1909 req->result = readlink (path, req->ptr2, EIO_PATH_MAX);
1910 if (req->result == EIO_PATH_MAX)
1911 {
1912 req->result = -1;
1913 errno = ENAMETOOLONG;
1914 }
1915 break;
2399 1916
2400 case EIO_UTIME: 1917 case EIO_UTIME:
2401 case EIO_FUTIME: 1918 case EIO_FUTIME:
2402 { 1919 {
2403 struct timeval tv[2]; 1920 struct timeval tv[2];
2468 req->result = select (0, 0, 0, 0, &tv); 1985 req->result = select (0, 0, 0, 0, &tv);
2469 } 1986 }
2470#endif 1987#endif
2471 break; 1988 break;
2472 1989
1990#if 0
2473 case EIO_GROUP: 1991 case EIO_GROUP:
2474 abort (); /* handled in eio_request */ 1992 abort (); /* handled in eio_request */
1993#endif
2475 1994
2476 case EIO_NOP: 1995 case EIO_NOP:
2477 req->result = 0; 1996 req->result = 0;
2478 break; 1997 break;
2479 1998
2480 case EIO_CUSTOM: 1999 case EIO_CUSTOM:
2481 req->feed (req); 2000 req->feed (req);
2482 break; 2001 break;
2483 2002
2484 default: 2003 default:
2485 errno = ENOSYS;
2486 req->result = -1; 2004 req->result = EIO_ENOSYS ();
2487 break; 2005 break;
2488 } 2006 }
2489 2007
2490 req->errorno = errno; 2008 req->errorno = errno;
2491} 2009}
2570eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) 2088eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data)
2571{ 2089{
2572 REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; 2090 REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND;
2573} 2091}
2574 2092
2093eio_req *eio_seek (int fd, off_t offset, int whence, int pri, eio_cb cb, void *data)
2094{
2095 REQ (EIO_SEEK); req->int1 = fd; req->offs = offset; req->int2 = whence; SEND;
2096}
2097
2575eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) 2098eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2576{ 2099{
2577 REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; 2100 REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
2578} 2101}
2579 2102
2580eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) 2103eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2581{ 2104{
2582 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; 2105 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
2106}
2107
2108eio_req *eio_fcntl (int fd, int cmd, void *arg, int pri, eio_cb cb, void *data)
2109{
2110 REQ (EIO_IOCTL); req->int1 = fd; req->int2 = cmd; req->ptr2 = arg; SEND;
2111}
2112
2113eio_req *eio_ioctl (int fd, unsigned long request, void *buf, int pri, eio_cb cb, void *data)
2114{
2115 REQ (EIO_IOCTL); req->int1 = fd; req->int2 = request; req->ptr2 = buf; SEND;
2583} 2116}
2584 2117
2585eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) 2118eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data)
2586{ 2119{
2587 REQ (EIO_FSTAT); req->int1 = fd; SEND; 2120 REQ (EIO_FSTAT); req->int1 = fd; SEND;
2775void 2308void
2776eio_grp_add (eio_req *grp, eio_req *req) 2309eio_grp_add (eio_req *grp, eio_req *req)
2777{ 2310{
2778 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); 2311 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
2779 2312
2780 grp->flags |= EIO_FLAG_GROUPADD; 2313 grp->flags |= ETP_FLAG_GROUPADD;
2781 2314
2782 ++grp->size; 2315 ++grp->size;
2783 req->grp = grp; 2316 req->grp = grp;
2784 2317
2785 req->grp_prev = 0; 2318 req->grp_prev = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines