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

Comparing cvsroot/libeio/eio.c (file contents):
Revision 1.70 by root, Fri Jun 10 06:55:10 2011 UTC vs.
Revision 1.90 by root, Sat Jul 16 16:46:10 2011 UTC

40#ifndef _WIN32 40#ifndef _WIN32
41# include "config.h" 41# include "config.h"
42#endif 42#endif
43 43
44#include "eio.h" 44#include "eio.h"
45#include "ecb.h"
45 46
46#ifdef EIO_STACKSIZE 47#ifdef EIO_STACKSIZE
47# define XTHREAD_STACKSIZE EIO_STACKSIZE 48# define XTHREAD_STACKSIZE EIO_STACKSIZE
48#endif 49#endif
49#include "xthread.h" 50#include "xthread.h"
53#include <stdlib.h> 54#include <stdlib.h>
54#include <string.h> 55#include <string.h>
55#include <errno.h> 56#include <errno.h>
56#include <sys/types.h> 57#include <sys/types.h>
57#include <sys/stat.h> 58#include <sys/stat.h>
58#include <sys/statvfs.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/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ 63/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */
64/* intptr_t only comes form stdint.h, says idiot openbsd coder */ 64/* intptr_t only comes from stdint.h, says idiot openbsd coder */
65#if HAVE_STDINT_H 65#if HAVE_STDINT_H
66# include <stdint.h> 66# include <stdint.h>
67#endif 67#endif
68 68
69#ifndef ECANCELED
70# define ECANCELED EDOM
71#endif
72#ifndef ELOOP
73# define ELOOP EDOM
74#endif
75
76static void eio_destroy (eio_req *req);
77
69#ifndef EIO_FINISH 78#ifndef EIO_FINISH
70# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 79# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0
71#endif 80#endif
72 81
73#ifndef EIO_DESTROY 82#ifndef EIO_DESTROY
76 85
77#ifndef EIO_FEED 86#ifndef EIO_FEED
78# define EIO_FEED(req) do { if ((req)->feed ) (req)->feed (req); } while (0) 87# define EIO_FEED(req) do { if ((req)->feed ) (req)->feed (req); } while (0)
79#endif 88#endif
80 89
90#ifndef EIO_FD_TO_WIN32_HANDLE
91# define EIO_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd)
92#endif
93#ifndef EIO_WIN32_HANDLE_TO_FD
94# define EIO_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (handle, 0)
95#endif
96
97#define EIO_ERRNO(errval,retval) ((errno = errval), retval)
98
99#define EIO_ENOSYS() EIO_ERRNO (ENOSYS, -1)
100
81#ifdef _WIN32 101#ifdef _WIN32
82 102
83 /*doh*/ 103 #undef PAGESIZE
104 #define PAGESIZE 4096 /* GetSystemInfo? */
105
106 #ifdef EIO_STRUCT_STATI64
107 #define stat(path,buf) _stati64 (path,buf)
108 #define fstat(fd,buf) _fstati64 (path,buf)
109 #endif
110 #define lstat(path,buf) stat (path,buf)
111 #define fsync(fd) (FlushFileBuffers ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd)) ? 0 : EIO_ERRNO (EBADF, -1))
112 #define mkdir(path,mode) _mkdir (path)
113 #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1))
114
115 #define chmod(path,mode) _chmod (path, mode)
116 #define fchmod(fd,mode) EIO_ENOSYS ()
117 #define chown(path,uid,gid) EIO_ENOSYS ()
118 #define fchown(fd,uid,gid) EIO_ENOSYS ()
119 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */
120 #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */
121 #define mknod(path,mode,dev) EIO_ENOSYS ()
122 #define sync() EIO_ENOSYS ()
123 #define readlink(path,buf,s) EIO_ENOSYS ()
124 #define statvfs(path,buf) EIO_ENOSYS ()
125 #define fstatvfs(fd,buf) EIO_ENOSYS ()
126
127 /* we could even stat and see if it exists */
128 static int
129 symlink (const char *old, const char *neu)
130 {
131 #if WINVER >= 0x0600
132 if (CreateSymbolicLink (neu, old, 1))
133 return 0;
134
135 if (CreateSymbolicLink (neu, old, 0))
136 return 0;
137 #endif
138
139 return EIO_ERRNO (ENOENT, -1);
140 }
141
142 /* POSIX API only */
143 #define CreateHardLink(neu,old) 0
144 #define CreateSymbolicLink(neu,old,flags) 0
145
146 struct statvfs
147 {
148 int dummy;
149 };
150
84#else 151#else
85 152
86# include <sys/time.h> 153 #include <sys/time.h>
87# include <sys/select.h> 154 #include <sys/select.h>
155 #include <sys/statvfs.h>
88# include <unistd.h> 156 #include <unistd.h>
89# include <utime.h> 157 #include <utime.h>
90# include <signal.h> 158 #include <signal.h>
91# include <dirent.h> 159 #include <dirent.h>
92 160
93#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES 161 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
94# include <sys/mman.h> 162 #include <sys/mman.h>
95#endif 163 #endif
96 164
97/* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ 165 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */
98# if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ 166 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
99# define _DIRENT_HAVE_D_TYPE /* sigh */ 167 #define _DIRENT_HAVE_D_TYPE /* sigh */
100# define D_INO(de) (de)->d_fileno 168 #define D_INO(de) (de)->d_fileno
101# define D_NAMLEN(de) (de)->d_namlen 169 #define D_NAMLEN(de) (de)->d_namlen
102# elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 170 #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600
103# define D_INO(de) (de)->d_ino 171 #define D_INO(de) (de)->d_ino
104# endif 172 #endif
105 173
106#ifdef _D_EXACT_NAMLEN 174 #ifdef _D_EXACT_NAMLEN
107# undef D_NAMLEN 175 #undef D_NAMLEN
108# define D_NAMLEN(de) _D_EXACT_NAMLEN (de) 176 #define D_NAMLEN(de) _D_EXACT_NAMLEN (de)
109#endif 177 #endif
110 178
111# ifdef _DIRENT_HAVE_D_TYPE 179 #ifdef _DIRENT_HAVE_D_TYPE
112# define D_TYPE(de) (de)->d_type 180 #define D_TYPE(de) (de)->d_type
113# endif 181 #endif
114 182
115# ifndef EIO_STRUCT_DIRENT 183 #ifndef EIO_STRUCT_DIRENT
116# define EIO_STRUCT_DIRENT struct dirent 184 #define EIO_STRUCT_DIRENT struct dirent
117# endif 185 #endif
118 186
119#endif 187#endif
120 188
121#if HAVE_SENDFILE 189#if HAVE_SENDFILE
122# if __linux 190# if __linux
165 if (!eio_buf) \ 233 if (!eio_buf) \
166 return -1; 234 return -1;
167 235
168#define EIO_TICKS ((1000000 + 1023) >> 10) 236#define EIO_TICKS ((1000000 + 1023) >> 10)
169 237
170/*****************************************************************************/
171
172#if __GNUC__ >= 3
173# define expect(expr,value) __builtin_expect ((expr),(value))
174#else
175# define expect(expr,value) (expr)
176#endif
177
178#define expect_false(expr) expect ((expr) != 0, 0)
179#define expect_true(expr) expect ((expr) != 0, 1)
180
181/*****************************************************************************/
182
183#define ETP_PRI_MIN EIO_PRI_MIN 238#define ETP_PRI_MIN EIO_PRI_MIN
184#define ETP_PRI_MAX EIO_PRI_MAX 239#define ETP_PRI_MAX EIO_PRI_MAX
185 240
186struct etp_worker; 241struct etp_worker;
187 242
212/*****************************************************************************/ 267/*****************************************************************************/
213 268
214#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 269#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
215 270
216/* calculate time difference in ~1/EIO_TICKS of a second */ 271/* calculate time difference in ~1/EIO_TICKS of a second */
272ecb_inline int
217static int tvdiff (struct timeval *tv1, struct timeval *tv2) 273tvdiff (struct timeval *tv1, struct timeval *tv2)
218{ 274{
219 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS 275 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
220 + ((tv2->tv_usec - tv1->tv_usec) >> 10); 276 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
221} 277}
222 278
266#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) 322#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
267#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) 323#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
268 324
269/* worker threads management */ 325/* worker threads management */
270 326
327static void ecb_cold
271static void etp_worker_clear (etp_worker *wrk) 328etp_worker_clear (etp_worker *wrk)
272{ 329{
273 ETP_WORKER_CLEAR (wrk); 330 ETP_WORKER_CLEAR (wrk);
274} 331}
275 332
333static void ecb_cold
276static void etp_worker_free (etp_worker *wrk) 334etp_worker_free (etp_worker *wrk)
277{ 335{
278 wrk->next->prev = wrk->prev; 336 wrk->next->prev = wrk->prev;
279 wrk->prev->next = wrk->next; 337 wrk->prev->next = wrk->next;
280 338
281 free (wrk); 339 free (wrk);
282} 340}
283 341
284static unsigned int etp_nreqs (void) 342static unsigned int
343etp_nreqs (void)
285{ 344{
286 int retval; 345 int retval;
287 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 346 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
288 retval = nreqs; 347 retval = nreqs;
289 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 348 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
290 return retval; 349 return retval;
291} 350}
292 351
293static unsigned int etp_nready (void) 352static unsigned int
353etp_nready (void)
294{ 354{
295 unsigned int retval; 355 unsigned int retval;
296 356
297 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 357 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
298 retval = nready; 358 retval = nready;
299 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 359 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
300 360
301 return retval; 361 return retval;
302} 362}
303 363
304static unsigned int etp_npending (void) 364static unsigned int
365etp_npending (void)
305{ 366{
306 unsigned int retval; 367 unsigned int retval;
307 368
308 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 369 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
309 retval = npending; 370 retval = npending;
310 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 371 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
311 372
312 return retval; 373 return retval;
313} 374}
314 375
315static unsigned int etp_nthreads (void) 376static unsigned int
377etp_nthreads (void)
316{ 378{
317 unsigned int retval; 379 unsigned int retval;
318 380
319 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 381 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
320 retval = started; 382 retval = started;
334} etp_reqq; 396} etp_reqq;
335 397
336static etp_reqq req_queue; 398static etp_reqq req_queue;
337static etp_reqq res_queue; 399static etp_reqq res_queue;
338 400
401static int ecb_noinline
339static int reqq_push (etp_reqq *q, ETP_REQ *req) 402reqq_push (etp_reqq *q, ETP_REQ *req)
340{ 403{
341 int pri = req->pri; 404 int pri = req->pri;
342 req->next = 0; 405 req->next = 0;
343 406
344 if (q->qe[pri]) 407 if (q->qe[pri])
350 q->qe[pri] = q->qs[pri] = req; 413 q->qe[pri] = q->qs[pri] = req;
351 414
352 return q->size++; 415 return q->size++;
353} 416}
354 417
418static ETP_REQ * ecb_noinline
355static ETP_REQ *reqq_shift (etp_reqq *q) 419reqq_shift (etp_reqq *q)
356{ 420{
357 int pri; 421 int pri;
358 422
359 if (!q->size) 423 if (!q->size)
360 return 0; 424 return 0;
375 } 439 }
376 440
377 abort (); 441 abort ();
378} 442}
379 443
444static void ecb_cold
380static void etp_thread_init (void) 445etp_thread_init (void)
381{ 446{
447#if !HAVE_PREADWRITE
448 X_MUTEX_CREATE (preadwritelock);
449#endif
382 X_MUTEX_CREATE (wrklock); 450 X_MUTEX_CREATE (wrklock);
383 X_MUTEX_CREATE (reslock); 451 X_MUTEX_CREATE (reslock);
384 X_MUTEX_CREATE (reqlock); 452 X_MUTEX_CREATE (reqlock);
385 X_COND_CREATE (reqwait); 453 X_COND_CREATE (reqwait);
386} 454}
387 455
456static void ecb_cold
388static void etp_atfork_prepare (void) 457etp_atfork_prepare (void)
389{ 458{
390 X_LOCK (wrklock);
391 X_LOCK (reqlock);
392 X_LOCK (reslock);
393#if !HAVE_PREADWRITE
394 X_LOCK (preadwritelock);
395#endif
396} 459}
397 460
461static void ecb_cold
398static void etp_atfork_parent (void) 462etp_atfork_parent (void)
399{ 463{
400#if !HAVE_PREADWRITE
401 X_UNLOCK (preadwritelock);
402#endif
403 X_UNLOCK (reslock);
404 X_UNLOCK (reqlock);
405 X_UNLOCK (wrklock);
406} 464}
407 465
466static void ecb_cold
408static void etp_atfork_child (void) 467etp_atfork_child (void)
409{ 468{
410 ETP_REQ *prv; 469 ETP_REQ *prv;
411 470
412 while ((prv = reqq_shift (&req_queue))) 471 while ((prv = reqq_shift (&req_queue)))
413 ETP_DESTROY (prv); 472 ETP_DESTROY (prv);
433 npending = 0; 492 npending = 0;
434 493
435 etp_thread_init (); 494 etp_thread_init ();
436} 495}
437 496
438static void 497static void ecb_cold
439etp_once_init (void) 498etp_once_init (void)
440{ 499{
441 etp_thread_init (); 500 etp_thread_init ();
442 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child); 501 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child);
443} 502}
444 503
445static int 504static int ecb_cold
446etp_init (void (*want_poll)(void), void (*done_poll)(void)) 505etp_init (void (*want_poll)(void), void (*done_poll)(void))
447{ 506{
448 static pthread_once_t doinit = PTHREAD_ONCE_INIT; 507 static pthread_once_t doinit = PTHREAD_ONCE_INIT;
449 508
450 pthread_once (&doinit, etp_once_init); 509 pthread_once (&doinit, etp_once_init);
455 return 0; 514 return 0;
456} 515}
457 516
458X_THREAD_PROC (etp_proc); 517X_THREAD_PROC (etp_proc);
459 518
519static void ecb_cold
460static void etp_start_thread (void) 520etp_start_thread (void)
461{ 521{
462 etp_worker *wrk = calloc (1, sizeof (etp_worker)); 522 etp_worker *wrk = calloc (1, sizeof (etp_worker));
463 523
464 /*TODO*/ 524 /*TODO*/
465 assert (("unable to allocate worker thread data", wrk)); 525 assert (("unable to allocate worker thread data", wrk));
478 free (wrk); 538 free (wrk);
479 539
480 X_UNLOCK (wrklock); 540 X_UNLOCK (wrklock);
481} 541}
482 542
543static void
483static void etp_maybe_start_thread (void) 544etp_maybe_start_thread (void)
484{ 545{
485 if (expect_true (etp_nthreads () >= wanted)) 546 if (ecb_expect_true (etp_nthreads () >= wanted))
486 return; 547 return;
487 548
488 /* todo: maybe use idle here, but might be less exact */ 549 /* todo: maybe use idle here, but might be less exact */
489 if (expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) 550 if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()))
490 return; 551 return;
491 552
492 etp_start_thread (); 553 etp_start_thread ();
493} 554}
494 555
556static void ecb_cold
495static void etp_end_thread (void) 557etp_end_thread (void)
496{ 558{
497 eio_req *req = calloc (1, sizeof (eio_req)); 559 eio_req *req = calloc (1, sizeof (eio_req));
498 560
499 req->type = -1; 561 req->type = -1;
500 req->pri = ETP_PRI_MAX - ETP_PRI_MIN; 562 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
507 X_LOCK (wrklock); 569 X_LOCK (wrklock);
508 --started; 570 --started;
509 X_UNLOCK (wrklock); 571 X_UNLOCK (wrklock);
510} 572}
511 573
512static int etp_poll (void) 574static int
575etp_poll (void)
513{ 576{
514 unsigned int maxreqs; 577 unsigned int maxreqs;
515 unsigned int maxtime; 578 unsigned int maxtime;
516 struct timeval tv_start, tv_now; 579 struct timeval tv_start, tv_now;
517 580
547 610
548 X_LOCK (reqlock); 611 X_LOCK (reqlock);
549 --nreqs; 612 --nreqs;
550 X_UNLOCK (reqlock); 613 X_UNLOCK (reqlock);
551 614
552 if (expect_false (req->type == EIO_GROUP && req->size)) 615 if (ecb_expect_false (req->type == EIO_GROUP && req->size))
553 { 616 {
554 req->int1 = 1; /* mark request as delayed */ 617 req->int1 = 1; /* mark request as delayed */
555 continue; 618 continue;
556 } 619 }
557 else 620 else
558 { 621 {
559 int res = ETP_FINISH (req); 622 int res = ETP_FINISH (req);
560 if (expect_false (res)) 623 if (ecb_expect_false (res))
561 return res; 624 return res;
562 } 625 }
563 626
564 if (expect_false (maxreqs && !--maxreqs)) 627 if (ecb_expect_false (maxreqs && !--maxreqs))
565 break; 628 break;
566 629
567 if (maxtime) 630 if (maxtime)
568 { 631 {
569 gettimeofday (&tv_now, 0); 632 gettimeofday (&tv_now, 0);
575 638
576 errno = EAGAIN; 639 errno = EAGAIN;
577 return -1; 640 return -1;
578} 641}
579 642
643static void
580static void etp_cancel (ETP_REQ *req) 644etp_cancel (ETP_REQ *req)
581{ 645{
582 X_LOCK (wrklock); 646 req->cancelled = 1;
583 req->flags |= EIO_FLAG_CANCELLED;
584 X_UNLOCK (wrklock);
585 647
586 eio_grp_cancel (req); 648 eio_grp_cancel (req);
587} 649}
588 650
651static void
589static void etp_submit (ETP_REQ *req) 652etp_submit (ETP_REQ *req)
590{ 653{
591 req->pri -= ETP_PRI_MIN; 654 req->pri -= ETP_PRI_MIN;
592 655
593 if (expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; 656 if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN;
594 if (expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; 657 if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
595 658
596 if (expect_false (req->type == EIO_GROUP)) 659 if (ecb_expect_false (req->type == EIO_GROUP))
597 { 660 {
598 /* I hope this is worth it :/ */ 661 /* I hope this is worth it :/ */
599 X_LOCK (reqlock); 662 X_LOCK (reqlock);
600 ++nreqs; 663 ++nreqs;
601 X_UNLOCK (reqlock); 664 X_UNLOCK (reqlock);
620 683
621 etp_maybe_start_thread (); 684 etp_maybe_start_thread ();
622 } 685 }
623} 686}
624 687
688static void ecb_cold
625static void etp_set_max_poll_time (double nseconds) 689etp_set_max_poll_time (double nseconds)
626{ 690{
627 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 691 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
628 max_poll_time = nseconds * EIO_TICKS; 692 max_poll_time = nseconds * EIO_TICKS;
629 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); 693 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
630} 694}
631 695
696static void ecb_cold
632static void etp_set_max_poll_reqs (unsigned int maxreqs) 697etp_set_max_poll_reqs (unsigned int maxreqs)
633{ 698{
634 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 699 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
635 max_poll_reqs = maxreqs; 700 max_poll_reqs = maxreqs;
636 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); 701 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
637} 702}
638 703
704static void ecb_cold
639static void etp_set_max_idle (unsigned int nthreads) 705etp_set_max_idle (unsigned int nthreads)
640{ 706{
641 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 707 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
642 max_idle = nthreads; 708 max_idle = nthreads;
643 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 709 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
644} 710}
645 711
712static void ecb_cold
646static void etp_set_idle_timeout (unsigned int seconds) 713etp_set_idle_timeout (unsigned int seconds)
647{ 714{
648 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 715 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
649 idle_timeout = seconds; 716 idle_timeout = seconds;
650 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 717 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
651} 718}
652 719
720static void ecb_cold
653static void etp_set_min_parallel (unsigned int nthreads) 721etp_set_min_parallel (unsigned int nthreads)
654{ 722{
655 if (wanted < nthreads) 723 if (wanted < nthreads)
656 wanted = nthreads; 724 wanted = nthreads;
657} 725}
658 726
727static void ecb_cold
659static void etp_set_max_parallel (unsigned int nthreads) 728etp_set_max_parallel (unsigned int nthreads)
660{ 729{
661 if (wanted > nthreads) 730 if (wanted > nthreads)
662 wanted = nthreads; 731 wanted = nthreads;
663 732
664 while (started > wanted) 733 while (started > wanted)
665 etp_end_thread (); 734 etp_end_thread ();
666} 735}
667 736
668/*****************************************************************************/ 737/*****************************************************************************/
669 738
739static void
670static void grp_try_feed (eio_req *grp) 740grp_try_feed (eio_req *grp)
671{ 741{
672 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 742 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
673 { 743 {
674 grp->flags &= ~EIO_FLAG_GROUPADD; 744 grp->flags &= ~EIO_FLAG_GROUPADD;
675 745
682 break; 752 break;
683 } 753 }
684 } 754 }
685} 755}
686 756
757static int
687static int grp_dec (eio_req *grp) 758grp_dec (eio_req *grp)
688{ 759{
689 --grp->size; 760 --grp->size;
690 761
691 /* call feeder, if applicable */ 762 /* call feeder, if applicable */
692 grp_try_feed (grp); 763 grp_try_feed (grp);
696 return eio_finish (grp); 767 return eio_finish (grp);
697 else 768 else
698 return 0; 769 return 0;
699} 770}
700 771
772static void
701void eio_destroy (eio_req *req) 773eio_destroy (eio_req *req)
702{ 774{
703 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); 775 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1);
704 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); 776 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2);
705 777
706 EIO_DESTROY (req); 778 EIO_DESTROY (req);
707} 779}
708 780
781static int
709static int eio_finish (eio_req *req) 782eio_finish (eio_req *req)
710{ 783{
711 int res = EIO_FINISH (req); 784 int res = EIO_FINISH (req);
712 785
713 if (req->grp) 786 if (req->grp)
714 { 787 {
722 if (grp->grp_first == req) 795 if (grp->grp_first == req)
723 grp->grp_first = req->grp_next; 796 grp->grp_first = req->grp_next;
724 797
725 res2 = grp_dec (grp); 798 res2 = grp_dec (grp);
726 799
727 if (!res && res2) 800 if (!res)
728 res = res2; 801 res = res2;
729 } 802 }
730 803
731 eio_destroy (req); 804 eio_destroy (req);
732 805
733 return res; 806 return res;
734} 807}
735 808
809void
736void eio_grp_cancel (eio_req *grp) 810eio_grp_cancel (eio_req *grp)
737{ 811{
738 for (grp = grp->grp_first; grp; grp = grp->grp_next) 812 for (grp = grp->grp_first; grp; grp = grp->grp_next)
739 eio_cancel (grp); 813 eio_cancel (grp);
740} 814}
741 815
816void
742void eio_cancel (eio_req *req) 817eio_cancel (eio_req *req)
743{ 818{
744 etp_cancel (req); 819 etp_cancel (req);
745} 820}
746 821
822void
747void eio_submit (eio_req *req) 823eio_submit (eio_req *req)
748{ 824{
749 etp_submit (req); 825 etp_submit (req);
750} 826}
751 827
752unsigned int eio_nreqs (void) 828unsigned int
829eio_nreqs (void)
753{ 830{
754 return etp_nreqs (); 831 return etp_nreqs ();
755} 832}
756 833
757unsigned int eio_nready (void) 834unsigned int
835eio_nready (void)
758{ 836{
759 return etp_nready (); 837 return etp_nready ();
760} 838}
761 839
762unsigned int eio_npending (void) 840unsigned int
841eio_npending (void)
763{ 842{
764 return etp_npending (); 843 return etp_npending ();
765} 844}
766 845
767unsigned int eio_nthreads (void) 846unsigned int ecb_cold
847eio_nthreads (void)
768{ 848{
769 return etp_nthreads (); 849 return etp_nthreads ();
770} 850}
771 851
852void ecb_cold
772void eio_set_max_poll_time (double nseconds) 853eio_set_max_poll_time (double nseconds)
773{ 854{
774 etp_set_max_poll_time (nseconds); 855 etp_set_max_poll_time (nseconds);
775} 856}
776 857
858void ecb_cold
777void eio_set_max_poll_reqs (unsigned int maxreqs) 859eio_set_max_poll_reqs (unsigned int maxreqs)
778{ 860{
779 etp_set_max_poll_reqs (maxreqs); 861 etp_set_max_poll_reqs (maxreqs);
780} 862}
781 863
864void ecb_cold
782void eio_set_max_idle (unsigned int nthreads) 865eio_set_max_idle (unsigned int nthreads)
783{ 866{
784 etp_set_max_idle (nthreads); 867 etp_set_max_idle (nthreads);
785} 868}
786 869
870void ecb_cold
787void eio_set_idle_timeout (unsigned int seconds) 871eio_set_idle_timeout (unsigned int seconds)
788{ 872{
789 etp_set_idle_timeout (seconds); 873 etp_set_idle_timeout (seconds);
790} 874}
791 875
876void ecb_cold
792void eio_set_min_parallel (unsigned int nthreads) 877eio_set_min_parallel (unsigned int nthreads)
793{ 878{
794 etp_set_min_parallel (nthreads); 879 etp_set_min_parallel (nthreads);
795} 880}
796 881
882void ecb_cold
797void eio_set_max_parallel (unsigned int nthreads) 883eio_set_max_parallel (unsigned int nthreads)
798{ 884{
799 etp_set_max_parallel (nthreads); 885 etp_set_max_parallel (nthreads);
800} 886}
801 887
802int eio_poll (void) 888int eio_poll (void)
811# undef pread 897# undef pread
812# undef pwrite 898# undef pwrite
813# define pread eio__pread 899# define pread eio__pread
814# define pwrite eio__pwrite 900# define pwrite eio__pwrite
815 901
816static ssize_t 902static eio_ssize_t
817eio__pread (int fd, void *buf, size_t count, off_t offset) 903eio__pread (int fd, void *buf, size_t count, off_t offset)
818{ 904{
819 ssize_t res; 905 eio_ssize_t res;
820 off_t ooffset; 906 off_t ooffset;
821 907
822 X_LOCK (preadwritelock); 908 X_LOCK (preadwritelock);
823 ooffset = lseek (fd, 0, SEEK_CUR); 909 ooffset = lseek (fd, 0, SEEK_CUR);
824 lseek (fd, offset, SEEK_SET); 910 lseek (fd, offset, SEEK_SET);
827 X_UNLOCK (preadwritelock); 913 X_UNLOCK (preadwritelock);
828 914
829 return res; 915 return res;
830} 916}
831 917
832static ssize_t 918static eio_ssize_t
833eio__pwrite (int fd, void *buf, size_t count, off_t offset) 919eio__pwrite (int fd, void *buf, size_t count, off_t offset)
834{ 920{
835 ssize_t res; 921 eio_ssize_t res;
836 off_t ooffset; 922 off_t ooffset;
837 923
838 X_LOCK (preadwritelock); 924 X_LOCK (preadwritelock);
839 ooffset = lseek (fd, 0, SEEK_CUR); 925 ooffset = lseek (fd, 0, SEEK_CUR);
840 lseek (fd, offset, SEEK_SET); 926 lseek (fd, offset, SEEK_SET);
872#ifndef HAVE_FUTIMES 958#ifndef HAVE_FUTIMES
873 959
874# undef futimes 960# undef futimes
875# define futimes(fd,times) eio__futimes (fd, times) 961# define futimes(fd,times) eio__futimes (fd, times)
876 962
963static int
877static int eio__futimes (int fd, const struct timeval tv[2]) 964eio__futimes (int fd, const struct timeval tv[2])
878{ 965{
879 errno = ENOSYS; 966 errno = ENOSYS;
880 return -1; 967 return -1;
881} 968}
882 969
886# undef fdatasync 973# undef fdatasync
887# define fdatasync(fd) fsync (fd) 974# define fdatasync(fd) fsync (fd)
888#endif 975#endif
889 976
890/* sync_file_range always needs emulation */ 977/* sync_file_range always needs emulation */
891int 978static int
892eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) 979eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags)
893{ 980{
894#if HAVE_SYNC_FILE_RANGE 981#if HAVE_SYNC_FILE_RANGE
895 int res; 982 int res;
896 983
913 /* even though we could play tricks with the flags, it's better to always 1000 /* even though we could play tricks with the flags, it's better to always
914 * call fdatasync, as that matches the expectation of its users best */ 1001 * call fdatasync, as that matches the expectation of its users best */
915 return fdatasync (fd); 1002 return fdatasync (fd);
916} 1003}
917 1004
1005static int
1006eio__fallocate (int fd, int mode, off_t offset, size_t len)
1007{
1008#if HAVE_FALLOCATE
1009 return fallocate (fd, mode, offset, len);
1010#else
1011 errno = ENOSYS;
1012 return -1;
1013#endif
1014}
1015
918#if !HAVE_READAHEAD 1016#if !HAVE_READAHEAD
919# undef readahead 1017# undef readahead
920# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) 1018# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self)
921 1019
922static ssize_t 1020static eio_ssize_t
923eio__readahead (int fd, off_t offset, size_t count, etp_worker *self) 1021eio__readahead (int fd, off_t offset, size_t count, etp_worker *self)
924{ 1022{
925 size_t todo = count; 1023 size_t todo = count;
926 dBUF; 1024 dBUF;
927 1025
939} 1037}
940 1038
941#endif 1039#endif
942 1040
943/* sendfile always needs emulation */ 1041/* sendfile always needs emulation */
944static ssize_t 1042static eio_ssize_t
945eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) 1043eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self)
946{ 1044{
947 ssize_t written = 0; 1045 eio_ssize_t written = 0;
948 ssize_t res; 1046 eio_ssize_t res;
949 1047
950 if (!count) 1048 if (!count)
951 return 0; 1049 return 0;
952 1050
953 for (;;) 1051 for (;;)
954 { 1052 {
1053#ifdef __APPLE__
1054# undef HAVE_SENDFILE /* broken, as everything on os x */
1055#endif
955#if HAVE_SENDFILE 1056#if HAVE_SENDFILE
956# if __linux 1057# if __linux
957 off_t soffset = offset; 1058 off_t soffset = offset;
958 res = sendfile (ofd, ifd, &soffset, count); 1059 res = sendfile (ofd, ifd, &soffset, count);
959 1060
1002 if (res < 0 && sbytes) 1103 if (res < 0 && sbytes)
1003 res = sbytes; 1104 res = sbytes;
1004 1105
1005# endif 1106# endif
1006 1107
1007#elif defined (_WIN32) 1108#elif defined (_WIN32) && 0
1008 /* does not work, just for documentation of what would need to be done */ 1109 /* does not work, just for documentation of what would need to be done */
1009 /* actually, cannot be done like this, as TransmitFile changes the file offset, */ 1110 /* actually, cannot be done like this, as TransmitFile changes the file offset, */
1010 /* libeio guarantees that the file offset does not change, and windows */ 1111 /* libeio guarantees that the file offset does not change, and windows */
1011 /* has no way to get an independent handle to the same file description */ 1112 /* has no way to get an independent handle to the same file description */
1012 HANDLE h = TO_SOCKET (ifd); 1113 HANDLE h = TO_SOCKET (ifd);
1045 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK 1146 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
1046 /* BSDs */ 1147 /* BSDs */
1047#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */ 1148#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */
1048 || errno == ENOTSUP 1149 || errno == ENOTSUP
1049#endif 1150#endif
1151#ifdef EOPNOTSUPP /* windows */
1050 || errno == EOPNOTSUPP /* BSDs */ 1152 || errno == EOPNOTSUPP /* BSDs */
1153#endif
1051#if __solaris 1154#if __solaris
1052 || errno == EAFNOSUPPORT || errno == EPROTOTYPE 1155 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
1053#endif 1156#endif
1054 ) 1157 )
1055 ) 1158 )
1059 1162
1060 res = 0; 1163 res = 0;
1061 1164
1062 while (count) 1165 while (count)
1063 { 1166 {
1064 ssize_t cnt; 1167 eio_ssize_t cnt;
1065 1168
1066 cnt = pread (ifd, eio_buf, count > EIO_BUFSIZE ? EIO_BUFSIZE : count, offset); 1169 cnt = pread (ifd, eio_buf, count > EIO_BUFSIZE ? EIO_BUFSIZE : count, offset);
1067 1170
1068 if (cnt <= 0) 1171 if (cnt <= 0)
1069 { 1172 {
1084 count -= cnt; 1187 count -= cnt;
1085 } 1188 }
1086 } 1189 }
1087 1190
1088 return res; 1191 return res;
1192}
1193
1194#ifdef PAGESIZE
1195# define eio_pagesize() PAGESIZE
1196#else
1197static intptr_t
1198eio_pagesize (void)
1199{
1200 static intptr_t page;
1201
1202 if (!page)
1203 page = sysconf (_SC_PAGESIZE);
1204
1205 return page;
1206}
1207#endif
1208
1209static void
1210eio_page_align (void **addr, size_t *length)
1211{
1212 intptr_t mask = eio_pagesize () - 1;
1213
1214 /* round down addr */
1215 intptr_t adj = mask & (intptr_t)*addr;
1216
1217 *addr = (void *)((intptr_t)*addr - adj);
1218 *length += adj;
1219
1220 /* round up length */
1221 *length = (*length + mask) & ~mask;
1222}
1223
1224#if !_POSIX_MEMLOCK
1225# define eio__mlockall(a) EIO_ENOSYS ()
1226#else
1227
1228static int
1229eio__mlockall (int flags)
1230{
1231 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1232 extern int mallopt (int, int);
1233 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1234 #endif
1235
1236 if (EIO_MCL_CURRENT != MCL_CURRENT
1237 || EIO_MCL_FUTURE != MCL_FUTURE)
1238 {
1239 flags = 0
1240 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0)
1241 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0);
1242 }
1243
1244 return mlockall (flags);
1245}
1246#endif
1247
1248#if !_POSIX_MEMLOCK_RANGE
1249# define eio__mlock(a,b) EIO_ENOSYS ()
1250#else
1251
1252static int
1253eio__mlock (void *addr, size_t length)
1254{
1255 eio_page_align (&addr, &length);
1256
1257 return mlock (addr, length);
1258}
1259
1260#endif
1261
1262#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1263# define eio__msync(a,b,c) EIO_ENOSYS ()
1264#else
1265
1266static int
1267eio__msync (void *mem, size_t len, int flags)
1268{
1269 eio_page_align (&mem, &len);
1270
1271 if (EIO_MS_ASYNC != MS_SYNC
1272 || EIO_MS_INVALIDATE != MS_INVALIDATE
1273 || EIO_MS_SYNC != MS_SYNC)
1274 {
1275 flags = 0
1276 | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0)
1277 | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0)
1278 | (flags & EIO_MS_SYNC ? MS_SYNC : 0);
1279 }
1280
1281 return msync (mem, len, flags);
1282}
1283
1284#endif
1285
1286static int
1287eio__mtouch (eio_req *req)
1288{
1289 void *mem = req->ptr2;
1290 size_t len = req->size;
1291 int flags = req->int1;
1292
1293 eio_page_align (&mem, &len);
1294
1295 {
1296 intptr_t addr = (intptr_t)mem;
1297 intptr_t end = addr + len;
1298 intptr_t page = eio_pagesize ();
1299
1300 if (addr < end)
1301 if (flags & EIO_MT_MODIFY) /* modify */
1302 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req));
1303 else
1304 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req));
1305 }
1306
1307 return 0;
1308}
1309
1310/*****************************************************************************/
1311/* requests implemented outside eio_execute, because they are so large */
1312
1313static void
1314eio__realpath (eio_req *req, etp_worker *self)
1315{
1316 char *rel = req->ptr1;
1317 char *res;
1318 char *tmp1, *tmp2;
1319#if SYMLOOP_MAX > 32
1320 int symlinks = SYMLOOP_MAX;
1321#else
1322 int symlinks = 32;
1323#endif
1324
1325 req->result = -1;
1326
1327 errno = EINVAL;
1328 if (!rel)
1329 return;
1330
1331 errno = ENOENT;
1332 if (!*rel)
1333 return;
1334
1335 if (!req->ptr2)
1336 {
1337 X_LOCK (wrklock);
1338 req->flags |= EIO_FLAG_PTR2_FREE;
1339 X_UNLOCK (wrklock);
1340 req->ptr2 = malloc (PATH_MAX * 3);
1341
1342 errno = ENOMEM;
1343 if (!req->ptr2)
1344 return;
1345 }
1346
1347 res = req->ptr2;
1348 tmp1 = res + PATH_MAX;
1349 tmp2 = tmp1 + PATH_MAX;
1350
1351#if 0 /* disabled, the musl way to do things is just too racy */
1352#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1353 /* on linux we may be able to ask the kernel */
1354 {
1355 int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME);
1356
1357 if (fd >= 0)
1358 {
1359 sprintf (tmp1, "/proc/self/fd/%d", fd);
1360 req->result = readlink (tmp1, res, PATH_MAX);
1361 close (fd);
1362
1363 /* here we should probably stat the open file and the disk file, to make sure they still match */
1364
1365 if (req->result > 0)
1366 goto done;
1367 }
1368 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO)
1369 return;
1370 }
1371#endif
1372#endif
1373
1374 if (*rel != '/')
1375 {
1376 if (!getcwd (res, PATH_MAX))
1377 return;
1378
1379 if (res [1]) /* only use if not / */
1380 res += strlen (res);
1381 }
1382
1383 while (*rel)
1384 {
1385 eio_ssize_t len, linklen;
1386 char *beg = rel;
1387
1388 while (*rel && *rel != '/')
1389 ++rel;
1390
1391 len = rel - beg;
1392
1393 if (!len) /* skip slashes */
1394 {
1395 ++rel;
1396 continue;
1397 }
1398
1399 if (beg [0] == '.')
1400 {
1401 if (len == 1)
1402 continue; /* . - nop */
1403
1404 if (beg [1] == '.' && len == 2)
1405 {
1406 /* .. - back up one component, if possible */
1407
1408 while (res != req->ptr2)
1409 if (*--res == '/')
1410 break;
1411
1412 continue;
1413 }
1414 }
1415
1416 errno = ENAMETOOLONG;
1417 if (res + 1 + len + 1 >= tmp1)
1418 return;
1419
1420 /* copy one component */
1421 *res = '/';
1422 memcpy (res + 1, beg, len);
1423
1424 /* zero-terminate, for readlink */
1425 res [len + 1] = 0;
1426
1427 /* now check if it's a symlink */
1428 linklen = readlink (req->ptr2, tmp1, PATH_MAX);
1429
1430 if (linklen < 0)
1431 {
1432 if (errno != EINVAL)
1433 return;
1434
1435 /* it's a normal directory. hopefully */
1436 res += len + 1;
1437 }
1438 else
1439 {
1440 /* yay, it was a symlink - build new path in tmp2 */
1441 int rellen = strlen (rel);
1442
1443 errno = ENAMETOOLONG;
1444 if (linklen + 1 + rellen >= PATH_MAX)
1445 return;
1446
1447 errno = ELOOP;
1448 if (!--symlinks)
1449 return;
1450
1451 if (*tmp1 == '/')
1452 res = req->ptr2; /* symlink resolves to an absolute path */
1453
1454 /* we need to be careful, as rel might point into tmp2 already */
1455 memmove (tmp2 + linklen + 1, rel, rellen + 1);
1456 tmp2 [linklen] = '/';
1457 memcpy (tmp2, tmp1, linklen);
1458
1459 rel = tmp2;
1460 }
1461 }
1462
1463 /* special case for the lone root path */
1464 if (res == req->ptr2)
1465 *res++ = '/';
1466
1467 req->result = res - (char *)req->ptr2;
1468
1469done:
1470 req->ptr2 = realloc (req->ptr2, req->result); /* trade time for space savings */
1089} 1471}
1090 1472
1091static signed char 1473static signed char
1092eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1474eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1093{ 1475{
1275 1657
1276 X_LOCK (wrklock); 1658 X_LOCK (wrklock);
1277 /* the corresponding closedir is in ETP_WORKER_CLEAR */ 1659 /* the corresponding closedir is in ETP_WORKER_CLEAR */
1278 self->dirp = dirp = opendir (req->ptr1); 1660 self->dirp = dirp = opendir (req->ptr1);
1279 1661
1662 if (req->flags & EIO_FLAG_PTR1_FREE)
1663 free (req->ptr1);
1664
1280 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; 1665 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE;
1281 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; 1666 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0;
1282 req->ptr2 = names = malloc (namesalloc); 1667 req->ptr2 = names = malloc (namesalloc);
1283 X_UNLOCK (wrklock); 1668 X_UNLOCK (wrklock);
1284 1669
1335 /* skip . and .. entries */ 1720 /* skip . and .. entries */
1336 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 1721 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
1337 { 1722 {
1338 int len = D_NAMLEN (entp) + 1; 1723 int len = D_NAMLEN (entp) + 1;
1339 1724
1340 while (expect_false (namesoffs + len > namesalloc)) 1725 while (ecb_expect_false (namesoffs + len > namesalloc))
1341 { 1726 {
1342 namesalloc *= 2; 1727 namesalloc *= 2;
1343 X_LOCK (wrklock); 1728 X_LOCK (wrklock);
1344 req->ptr2 = names = realloc (names, namesalloc); 1729 req->ptr2 = names = realloc (names, namesalloc);
1345 X_UNLOCK (wrklock); 1730 X_UNLOCK (wrklock);
1352 1737
1353 if (dents) 1738 if (dents)
1354 { 1739 {
1355 struct eio_dirent *ent; 1740 struct eio_dirent *ent;
1356 1741
1357 if (expect_false (dentoffs == dentalloc)) 1742 if (ecb_expect_false (dentoffs == dentalloc))
1358 { 1743 {
1359 dentalloc *= 2; 1744 dentalloc *= 2;
1360 X_LOCK (wrklock); 1745 X_LOCK (wrklock);
1361 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); 1746 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent));
1362 X_UNLOCK (wrklock); 1747 X_UNLOCK (wrklock);
1450 break; 1835 break;
1451 } 1836 }
1452 } 1837 }
1453} 1838}
1454 1839
1455#ifdef PAGESIZE
1456# define eio_pagesize() PAGESIZE
1457#else
1458static intptr_t
1459eio_pagesize (void)
1460{
1461 static intptr_t page;
1462
1463 if (!page)
1464 page = sysconf (_SC_PAGESIZE);
1465
1466 return page;
1467}
1468#endif
1469
1470static void
1471eio_page_align (void **addr, size_t *length)
1472{
1473 intptr_t mask = eio_pagesize () - 1;
1474
1475 /* round down addr */
1476 intptr_t adj = mask & (intptr_t)*addr;
1477
1478 *addr = (void *)((intptr_t)*addr - adj);
1479 *length += adj;
1480
1481 /* round up length */
1482 *length = (*length + mask) & ~mask;
1483}
1484
1485#if !_POSIX_MEMLOCK
1486# define eio__mlockall(a) ((errno = ENOSYS), -1)
1487#else
1488
1489static int
1490eio__mlockall (int flags)
1491{
1492 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1493 extern int mallopt (int, int);
1494 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1495 #endif
1496
1497 if (EIO_MCL_CURRENT != MCL_CURRENT
1498 || EIO_MCL_FUTURE != MCL_FUTURE)
1499 {
1500 flags = 0
1501 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0)
1502 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0);
1503 }
1504
1505 return mlockall (flags);
1506}
1507#endif
1508
1509#if !_POSIX_MEMLOCK_RANGE
1510# define eio__mlock(a,b) ((errno = ENOSYS), -1)
1511#else
1512
1513static int
1514eio__mlock (void *addr, size_t length)
1515{
1516 eio_page_align (&addr, &length);
1517
1518 return mlock (addr, length);
1519}
1520
1521#endif
1522
1523#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1524# define eio__msync(a,b,c) ((errno = ENOSYS), -1)
1525#else
1526
1527int
1528eio__msync (void *mem, size_t len, int flags)
1529{
1530 eio_page_align (&mem, &len);
1531
1532 if (EIO_MS_ASYNC != MS_SYNC
1533 || EIO_MS_INVALIDATE != MS_INVALIDATE
1534 || EIO_MS_SYNC != MS_SYNC)
1535 {
1536 flags = 0
1537 | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0)
1538 | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0)
1539 | (flags & EIO_MS_SYNC ? MS_SYNC : 0);
1540 }
1541
1542 return msync (mem, len, flags);
1543}
1544
1545#endif
1546
1547int
1548eio__mtouch (eio_req *req)
1549{
1550 void *mem = req->ptr2;
1551 size_t len = req->size;
1552 int flags = req->int1;
1553
1554 eio_page_align (&mem, &len);
1555
1556 {
1557 intptr_t addr = (intptr_t)mem;
1558 intptr_t end = addr + len;
1559 intptr_t page = eio_pagesize ();
1560
1561 if (addr < end)
1562 if (flags & EIO_MT_MODIFY) /* modify */
1563 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req));
1564 else
1565 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req));
1566 }
1567
1568 return 0;
1569}
1570
1571/*****************************************************************************/ 1840/*****************************************************************************/
1572 1841
1573#define ALLOC(len) \ 1842#define ALLOC(len) \
1574 if (!req->ptr2) \ 1843 if (!req->ptr2) \
1575 { \ 1844 { \
1588X_THREAD_PROC (etp_proc) 1857X_THREAD_PROC (etp_proc)
1589{ 1858{
1590 ETP_REQ *req; 1859 ETP_REQ *req;
1591 struct timespec ts; 1860 struct timespec ts;
1592 etp_worker *self = (etp_worker *)thr_arg; 1861 etp_worker *self = (etp_worker *)thr_arg;
1862 int timeout;
1593 1863
1594 /* try to distribute timeouts somewhat randomly */ 1864 /* try to distribute timeouts somewhat evenly */
1595 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); 1865 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1596 1866
1597 for (;;) 1867 for (;;)
1598 { 1868 {
1869 ts.tv_sec = 0;
1870
1599 X_LOCK (reqlock); 1871 X_LOCK (reqlock);
1600 1872
1601 for (;;) 1873 for (;;)
1602 { 1874 {
1603 self->req = req = reqq_shift (&req_queue); 1875 self->req = req = reqq_shift (&req_queue);
1604 1876
1605 if (req) 1877 if (req)
1606 break; 1878 break;
1607 1879
1880 if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */
1881 {
1882 X_UNLOCK (reqlock);
1883 X_LOCK (wrklock);
1884 --started;
1885 X_UNLOCK (wrklock);
1886 goto quit;
1887 }
1888
1608 ++idle; 1889 ++idle;
1609 1890
1610 ts.tv_sec = time (0) + idle_timeout; 1891 if (idle <= max_idle)
1611 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) 1892 /* we are allowed to idle, so do so without any timeout */
1893 X_COND_WAIT (reqwait, reqlock);
1894 else
1612 { 1895 {
1613 if (idle > max_idle) 1896 /* initialise timeout once */
1614 { 1897 if (!ts.tv_sec)
1615 --idle; 1898 ts.tv_sec = time (0) + idle_timeout;
1616 X_UNLOCK (reqlock);
1617 X_LOCK (wrklock);
1618 --started;
1619 X_UNLOCK (wrklock);
1620 goto quit;
1621 }
1622 1899
1623 /* we are allowed to idle, so do so without any timeout */
1624 X_COND_WAIT (reqwait, reqlock); 1900 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
1901 ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */
1625 } 1902 }
1626 1903
1627 --idle; 1904 --idle;
1628 } 1905 }
1629 1906
1632 X_UNLOCK (reqlock); 1909 X_UNLOCK (reqlock);
1633 1910
1634 if (req->type < 0) 1911 if (req->type < 0)
1635 goto quit; 1912 goto quit;
1636 1913
1637 if (!EIO_CANCELLED (req))
1638 ETP_EXECUTE (self, req); 1914 ETP_EXECUTE (self, req);
1639 1915
1640 X_LOCK (reslock); 1916 X_LOCK (reslock);
1641 1917
1642 ++npending; 1918 ++npending;
1643 1919
1658 return 0; 1934 return 0;
1659} 1935}
1660 1936
1661/*****************************************************************************/ 1937/*****************************************************************************/
1662 1938
1939int ecb_cold
1663int eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1940eio_init (void (*want_poll)(void), void (*done_poll)(void))
1664{ 1941{
1665 return etp_init (want_poll, done_poll); 1942 return etp_init (want_poll, done_poll);
1666} 1943}
1667 1944
1945ecb_inline void
1668static void eio_api_destroy (eio_req *req) 1946eio_api_destroy (eio_req *req)
1669{ 1947{
1670 free (req); 1948 free (req);
1671} 1949}
1672 1950
1673#define REQ(rtype) \ 1951#define REQ(rtype) \
1692 { \ 1970 { \
1693 eio_api_destroy (req); \ 1971 eio_api_destroy (req); \
1694 return 0; \ 1972 return 0; \
1695 } 1973 }
1696 1974
1975static void
1697static void eio_execute (etp_worker *self, eio_req *req) 1976eio_execute (etp_worker *self, eio_req *req)
1698{ 1977{
1978 if (ecb_expect_false (EIO_CANCELLED (req)))
1979 {
1980 req->result = -1;
1981 req->errorno = ECANCELED;
1982 return;
1983 }
1984
1699 switch (req->type) 1985 switch (req->type)
1700 { 1986 {
1701 case EIO_READ: ALLOC (req->size); 1987 case EIO_READ: ALLOC (req->size);
1702 req->result = req->offs >= 0 1988 req->result = req->offs >= 0
1703 ? pread (req->int1, req->ptr2, req->size, req->offs) 1989 ? pread (req->int1, req->ptr2, req->size, req->offs)
1737 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; 2023 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break;
1738 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; 2024 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break;
1739 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; 2025 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break;
1740 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; 2026 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
1741 2027
2028 case EIO_REALPATH: eio__realpath (req, self); break;
2029
1742 case EIO_READLINK: ALLOC (PATH_MAX); 2030 case EIO_READLINK: ALLOC (PATH_MAX);
1743 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break; 2031 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break;
1744 2032
1745 case EIO_SYNC: req->result = 0; sync (); break; 2033 case EIO_SYNC: req->result = 0; sync (); break;
1746 case EIO_FSYNC: req->result = fsync (req->int1); break; 2034 case EIO_FSYNC: req->result = fsync (req->int1); break;
1748 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; 2036 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break;
1749 case EIO_MTOUCH: req->result = eio__mtouch (req); break; 2037 case EIO_MTOUCH: req->result = eio__mtouch (req); break;
1750 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; 2038 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break;
1751 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; 2039 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break;
1752 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; 2040 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break;
2041 case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break;
1753 2042
1754 case EIO_READDIR: eio__scandir (req, self); break; 2043 case EIO_READDIR: eio__scandir (req, self); break;
1755 2044
1756 case EIO_BUSY: 2045 case EIO_BUSY:
1757#ifdef _WIN32 2046#ifdef _WIN32
1857eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) 2146eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data)
1858{ 2147{
1859 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; 2148 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND;
1860} 2149}
1861 2150
2151eio_req *eio_fallocate (int fd, int mode, off_t offset, size_t len, int pri, eio_cb cb, void *data)
2152{
2153 REQ (EIO_FALLOCATE); req->int1 = fd; req->int2 = mode; req->offs = offset; req->size = len; SEND;
2154}
2155
1862eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) 2156eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data)
1863{ 2157{
1864 REQ (EIO_FDATASYNC); req->int1 = fd; SEND; 2158 REQ (EIO_FDATASYNC); req->int1 = fd; SEND;
1865} 2159}
1866 2160
1907eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data) 2201eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data)
1908{ 2202{
1909 REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND; 2203 REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND;
1910} 2204}
1911 2205
1912eio_req *eio_fchown (int fd, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data) 2206eio_req *eio_fchown (int fd, eio_uid_t uid, eio_gid_t gid, int pri, eio_cb cb, void *data)
1913{ 2207{
1914 REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND; 2208 REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
1915} 2209}
1916 2210
1917eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data) 2211eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data)
1937eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data) 2231eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data)
1938{ 2232{
1939 REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND; 2233 REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND;
1940} 2234}
1941 2235
1942eio_req *eio_chown (const char *path, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data) 2236eio_req *eio_chown (const char *path, eio_uid_t uid, eio_gid_t gid, int pri, eio_cb cb, void *data)
1943{ 2237{
1944 REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND; 2238 REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
1945} 2239}
1946 2240
1947eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data) 2241eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data)
1961} 2255}
1962 2256
1963eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data) 2257eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data)
1964{ 2258{
1965 return eio__1path (EIO_READLINK, path, pri, cb, data); 2259 return eio__1path (EIO_READLINK, path, pri, cb, data);
2260}
2261
2262eio_req *eio_realpath (const char *path, int pri, eio_cb cb, void *data)
2263{
2264 return eio__1path (EIO_REALPATH, path, pri, cb, data);
1966} 2265}
1967 2266
1968eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data) 2267eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data)
1969{ 2268{
1970 return eio__1path (EIO_STAT, path, pri, cb, data); 2269 return eio__1path (EIO_STAT, path, pri, cb, data);
2029eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) 2328eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
2030{ 2329{
2031 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); 2330 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
2032} 2331}
2033 2332
2034eio_req *eio_custom (void (*)(eio_req *) execute, int pri, eio_cb cb, void *data); 2333eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data)
2035{ 2334{
2036 REQ (EIO_CUSTOM); req->feed = execute; SEND; 2335 REQ (EIO_CUSTOM); req->feed = execute; SEND;
2037} 2336}
2038 2337
2039#endif 2338#endif
2050#undef SEND 2349#undef SEND
2051 2350
2052/*****************************************************************************/ 2351/*****************************************************************************/
2053/* grp functions */ 2352/* grp functions */
2054 2353
2354void
2055void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) 2355eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit)
2056{ 2356{
2057 grp->int2 = limit; 2357 grp->int2 = limit;
2058 grp->feed = feed; 2358 grp->feed = feed;
2059 2359
2060 grp_try_feed (grp); 2360 grp_try_feed (grp);
2061} 2361}
2062 2362
2363void
2063void eio_grp_limit (eio_req *grp, int limit) 2364eio_grp_limit (eio_req *grp, int limit)
2064{ 2365{
2065 grp->int2 = limit; 2366 grp->int2 = limit;
2066 2367
2067 grp_try_feed (grp); 2368 grp_try_feed (grp);
2068} 2369}
2069 2370
2371void
2070void eio_grp_add (eio_req *grp, eio_req *req) 2372eio_grp_add (eio_req *grp, eio_req *req)
2071{ 2373{
2072 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); 2374 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
2073 2375
2074 grp->flags |= EIO_FLAG_GROUPADD; 2376 grp->flags |= EIO_FLAG_GROUPADD;
2075 2377
2086} 2388}
2087 2389
2088/*****************************************************************************/ 2390/*****************************************************************************/
2089/* misc garbage */ 2391/* misc garbage */
2090 2392
2393eio_ssize_t
2091ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) 2394eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
2092{ 2395{
2093 etp_worker wrk; 2396 etp_worker wrk;
2094 ssize_t ret; 2397 eio_ssize_t ret;
2095 2398
2096 wrk.dbuf = 0; 2399 wrk.dbuf = 0;
2097 2400
2098 ret = eio__sendfile (ofd, ifd, offset, count, &wrk); 2401 ret = eio__sendfile (ofd, ifd, offset, count, &wrk);
2099 2402

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines