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

Comparing libeio/eio.c (file contents):
Revision 1.53 by root, Sat Jan 9 10:03:09 2010 UTC vs.
Revision 1.77 by root, Tue Jul 5 14:02:15 2011 UTC

1/* 1/*
2 * libeio implementation 2 * libeio implementation
3 * 3 *
4 * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libeio@schmorp.de> 4 * Copyright (c) 2007,2008,2009,2010,2011 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 *
35 * and other provisions required by the GPL. If you do not delete the 35 * and other provisions required by the GPL. If you do not delete the
36 * provisions above, a recipient may use your version of this file under 36 * provisions above, a recipient may use your version of this file under
37 * either the BSD or the GPL. 37 * either the BSD or the GPL.
38 */ 38 */
39 39
40#ifndef _WIN32
41# include "config.h"
42#endif
43
40#include "eio.h" 44#include "eio.h"
45#include "ecb.h"
41 46
42#ifdef EIO_STACKSIZE 47#ifdef EIO_STACKSIZE
43# define XTHREAD_STACKSIZE EIO_STACKSIZE 48# define XTHREAD_STACKSIZE EIO_STACKSIZE
44#endif 49#endif
45#include "xthread.h" 50#include "xthread.h"
54#include <sys/statvfs.h> 59#include <sys/statvfs.h>
55#include <limits.h> 60#include <limits.h>
56#include <fcntl.h> 61#include <fcntl.h>
57#include <assert.h> 62#include <assert.h>
58 63
64/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */
65/* intptr_t only comes form stdint.h, says idiot openbsd coder */
66#if HAVE_STDINT_H
67# include <stdint.h>
68#endif
69
59#ifndef EIO_FINISH 70#ifndef EIO_FINISH
60# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 71# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0
61#endif 72#endif
62 73
63#ifndef EIO_DESTROY 74#ifndef EIO_DESTROY
71#ifdef _WIN32 82#ifdef _WIN32
72 83
73 /*doh*/ 84 /*doh*/
74#else 85#else
75 86
76# include "config.h"
77# include <sys/time.h> 87# include <sys/time.h>
78# include <sys/select.h> 88# include <sys/select.h>
79# include <sys/mman.h>
80# include <unistd.h> 89# include <unistd.h>
81# include <utime.h> 90# include <utime.h>
82# include <signal.h> 91# include <signal.h>
83# include <dirent.h> 92# include <dirent.h>
93
94#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
95# include <sys/mman.h>
96#endif
84 97
85/* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ 98/* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */
86# if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ 99# if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
87# define _DIRENT_HAVE_D_TYPE /* sigh */ 100# define _DIRENT_HAVE_D_TYPE /* sigh */
88# define D_INO(de) (de)->d_fileno 101# define D_INO(de) (de)->d_fileno
129#endif 142#endif
130#ifndef D_NAMLEN 143#ifndef D_NAMLEN
131# define D_NAMLEN(de) strlen ((de)->d_name) 144# define D_NAMLEN(de) strlen ((de)->d_name)
132#endif 145#endif
133 146
134/* number of seconds after which an idle threads exit */
135#define IDLE_TIMEOUT 10
136
137/* used for struct dirent, AIX doesn't provide it */ 147/* used for struct dirent, AIX doesn't provide it */
138#ifndef NAME_MAX 148#ifndef NAME_MAX
139# define NAME_MAX 4096 149# define NAME_MAX 4096
150#endif
151
152/* used for readlink etc. */
153#ifndef PATH_MAX
154# define PATH_MAX 4096
140#endif 155#endif
141 156
142/* buffer size for various temporary buffers */ 157/* buffer size for various temporary buffers */
143#define EIO_BUFSIZE 65536 158#define EIO_BUFSIZE 65536
144 159
150 errno = ENOMEM; \ 165 errno = ENOMEM; \
151 if (!eio_buf) \ 166 if (!eio_buf) \
152 return -1; 167 return -1;
153 168
154#define EIO_TICKS ((1000000 + 1023) >> 10) 169#define EIO_TICKS ((1000000 + 1023) >> 10)
155
156/*****************************************************************************/
157
158#if __GNUC__ >= 3
159# define expect(expr,value) __builtin_expect ((expr),(value))
160#else
161# define expect(expr,value) (expr)
162#endif
163
164#define expect_false(expr) expect ((expr) != 0, 0)
165#define expect_true(expr) expect ((expr) != 0, 1)
166
167/*****************************************************************************/
168 170
169#define ETP_PRI_MIN EIO_PRI_MIN 171#define ETP_PRI_MIN EIO_PRI_MIN
170#define ETP_PRI_MAX EIO_PRI_MAX 172#define ETP_PRI_MAX EIO_PRI_MAX
171 173
172struct etp_worker; 174struct etp_worker;
198/*****************************************************************************/ 200/*****************************************************************************/
199 201
200#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 202#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
201 203
202/* calculate time difference in ~1/EIO_TICKS of a second */ 204/* calculate time difference in ~1/EIO_TICKS of a second */
205ecb_inline int
203static int tvdiff (struct timeval *tv1, struct timeval *tv2) 206tvdiff (struct timeval *tv1, struct timeval *tv2)
204{ 207{
205 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS 208 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
206 + ((tv2->tv_usec - tv1->tv_usec) >> 10); 209 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
207} 210}
208 211
215static unsigned int max_poll_reqs; /* reslock */ 218static unsigned int max_poll_reqs; /* reslock */
216 219
217static volatile unsigned int nreqs; /* reqlock */ 220static volatile unsigned int nreqs; /* reqlock */
218static volatile unsigned int nready; /* reqlock */ 221static volatile unsigned int nready; /* reqlock */
219static volatile unsigned int npending; /* reqlock */ 222static volatile unsigned int npending; /* reqlock */
220static volatile unsigned int max_idle = 4; 223static volatile unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */
224static volatile unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */
221 225
222static mutex_t wrklock = X_MUTEX_INIT; 226static xmutex_t wrklock;
223static mutex_t reslock = X_MUTEX_INIT; 227static xmutex_t reslock;
224static mutex_t reqlock = X_MUTEX_INIT; 228static xmutex_t reqlock;
225static cond_t reqwait = X_COND_INIT; 229static xcond_t reqwait;
226 230
227#if !HAVE_PREADWRITE 231#if !HAVE_PREADWRITE
228/* 232/*
229 * make our pread/pwrite emulation safe against themselves, but not against 233 * make our pread/pwrite emulation safe against themselves, but not against
230 * normal read/write by using a mutex. slows down execution a lot, 234 * normal read/write by using a mutex. slows down execution a lot,
231 * but that's your problem, not mine. 235 * but that's your problem, not mine.
232 */ 236 */
233static mutex_t preadwritelock = X_MUTEX_INIT; 237static xmutex_t preadwritelock = X_MUTEX_INIT;
234#endif 238#endif
235 239
236typedef struct etp_worker 240typedef struct etp_worker
237{ 241{
238 /* locked by wrklock */ 242 /* locked by wrklock */
239 struct etp_worker *prev, *next; 243 struct etp_worker *prev, *next;
240 244
241 thread_t tid; 245 xthread_t tid;
242 246
243 /* locked by reslock, reqlock or wrklock */ 247 /* locked by reslock, reqlock or wrklock */
244 ETP_REQ *req; /* currently processed request */ 248 ETP_REQ *req; /* currently processed request */
245 249
246 ETP_WORKER_COMMON 250 ETP_WORKER_COMMON
251#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) 255#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
252#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) 256#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
253 257
254/* worker threads management */ 258/* worker threads management */
255 259
260static void ecb_cold
256static void etp_worker_clear (etp_worker *wrk) 261etp_worker_clear (etp_worker *wrk)
257{ 262{
258 ETP_WORKER_CLEAR (wrk); 263 ETP_WORKER_CLEAR (wrk);
259} 264}
260 265
266static void ecb_cold
261static void etp_worker_free (etp_worker *wrk) 267etp_worker_free (etp_worker *wrk)
262{ 268{
263 wrk->next->prev = wrk->prev; 269 wrk->next->prev = wrk->prev;
264 wrk->prev->next = wrk->next; 270 wrk->prev->next = wrk->next;
265 271
266 free (wrk); 272 free (wrk);
267} 273}
268 274
269static unsigned int etp_nreqs (void) 275static unsigned int
276etp_nreqs (void)
270{ 277{
271 int retval; 278 int retval;
272 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 279 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
273 retval = nreqs; 280 retval = nreqs;
274 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 281 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
275 return retval; 282 return retval;
276} 283}
277 284
278static unsigned int etp_nready (void) 285static unsigned int
286etp_nready (void)
279{ 287{
280 unsigned int retval; 288 unsigned int retval;
281 289
282 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 290 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
283 retval = nready; 291 retval = nready;
284 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 292 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
285 293
286 return retval; 294 return retval;
287} 295}
288 296
289static unsigned int etp_npending (void) 297static unsigned int
298etp_npending (void)
290{ 299{
291 unsigned int retval; 300 unsigned int retval;
292 301
293 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 302 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
294 retval = npending; 303 retval = npending;
295 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 304 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
296 305
297 return retval; 306 return retval;
298} 307}
299 308
300static unsigned int etp_nthreads (void) 309static unsigned int
310etp_nthreads (void)
301{ 311{
302 unsigned int retval; 312 unsigned int retval;
303 313
304 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 314 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
305 retval = started; 315 retval = started;
319} etp_reqq; 329} etp_reqq;
320 330
321static etp_reqq req_queue; 331static etp_reqq req_queue;
322static etp_reqq res_queue; 332static etp_reqq res_queue;
323 333
334static int ecb_noinline
324static int reqq_push (etp_reqq *q, ETP_REQ *req) 335reqq_push (etp_reqq *q, ETP_REQ *req)
325{ 336{
326 int pri = req->pri; 337 int pri = req->pri;
327 req->next = 0; 338 req->next = 0;
328 339
329 if (q->qe[pri]) 340 if (q->qe[pri])
335 q->qe[pri] = q->qs[pri] = req; 346 q->qe[pri] = q->qs[pri] = req;
336 347
337 return q->size++; 348 return q->size++;
338} 349}
339 350
351static ETP_REQ * ecb_noinline
340static ETP_REQ *reqq_shift (etp_reqq *q) 352reqq_shift (etp_reqq *q)
341{ 353{
342 int pri; 354 int pri;
343 355
344 if (!q->size) 356 if (!q->size)
345 return 0; 357 return 0;
360 } 372 }
361 373
362 abort (); 374 abort ();
363} 375}
364 376
377static void ecb_cold
378etp_thread_init (void)
379{
380 X_MUTEX_CREATE (wrklock);
381 X_MUTEX_CREATE (reslock);
382 X_MUTEX_CREATE (reqlock);
383 X_COND_CREATE (reqwait);
384}
385
386static void ecb_cold
365static void etp_atfork_prepare (void) 387etp_atfork_prepare (void)
366{ 388{
367 X_LOCK (wrklock); 389 X_LOCK (wrklock);
368 X_LOCK (reqlock); 390 X_LOCK (reqlock);
369 X_LOCK (reslock); 391 X_LOCK (reslock);
370#if !HAVE_PREADWRITE 392#if !HAVE_PREADWRITE
371 X_LOCK (preadwritelock); 393 X_LOCK (preadwritelock);
372#endif 394#endif
373} 395}
374 396
397static void ecb_cold
375static void etp_atfork_parent (void) 398etp_atfork_parent (void)
376{ 399{
377#if !HAVE_PREADWRITE 400#if !HAVE_PREADWRITE
378 X_UNLOCK (preadwritelock); 401 X_UNLOCK (preadwritelock);
379#endif 402#endif
380 X_UNLOCK (reslock); 403 X_UNLOCK (reslock);
381 X_UNLOCK (reqlock); 404 X_UNLOCK (reqlock);
382 X_UNLOCK (wrklock); 405 X_UNLOCK (wrklock);
383} 406}
384 407
408static void ecb_cold
385static void etp_atfork_child (void) 409etp_atfork_child (void)
386{ 410{
387 ETP_REQ *prv; 411 ETP_REQ *prv;
388 412
389 while ((prv = reqq_shift (&req_queue))) 413 while ((prv = reqq_shift (&req_queue)))
390 ETP_DESTROY (prv); 414 ETP_DESTROY (prv);
407 idle = 0; 431 idle = 0;
408 nreqs = 0; 432 nreqs = 0;
409 nready = 0; 433 nready = 0;
410 npending = 0; 434 npending = 0;
411 435
412 etp_atfork_parent (); 436 etp_thread_init ();
413} 437}
414 438
415static void 439static void ecb_cold
416etp_once_init (void) 440etp_once_init (void)
417{ 441{
442 etp_thread_init ();
418 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child); 443 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child);
419} 444}
420 445
421static int 446static int ecb_cold
422etp_init (void (*want_poll)(void), void (*done_poll)(void)) 447etp_init (void (*want_poll)(void), void (*done_poll)(void))
423{ 448{
424 static pthread_once_t doinit = PTHREAD_ONCE_INIT; 449 static pthread_once_t doinit = PTHREAD_ONCE_INIT;
425 450
426 pthread_once (&doinit, etp_once_init); 451 pthread_once (&doinit, etp_once_init);
431 return 0; 456 return 0;
432} 457}
433 458
434X_THREAD_PROC (etp_proc); 459X_THREAD_PROC (etp_proc);
435 460
461static void ecb_cold
436static void etp_start_thread (void) 462etp_start_thread (void)
437{ 463{
438 etp_worker *wrk = calloc (1, sizeof (etp_worker)); 464 etp_worker *wrk = calloc (1, sizeof (etp_worker));
439 465
440 /*TODO*/ 466 /*TODO*/
441 assert (("unable to allocate worker thread data", wrk)); 467 assert (("unable to allocate worker thread data", wrk));
454 free (wrk); 480 free (wrk);
455 481
456 X_UNLOCK (wrklock); 482 X_UNLOCK (wrklock);
457} 483}
458 484
485static void
459static void etp_maybe_start_thread (void) 486etp_maybe_start_thread (void)
460{ 487{
461 if (expect_true (etp_nthreads () >= wanted)) 488 if (ecb_expect_true (etp_nthreads () >= wanted))
462 return; 489 return;
463 490
464 /* todo: maybe use idle here, but might be less exact */ 491 /* todo: maybe use idle here, but might be less exact */
465 if (expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) 492 if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()))
466 return; 493 return;
467 494
468 etp_start_thread (); 495 etp_start_thread ();
469} 496}
470 497
498static void ecb_cold
471static void etp_end_thread (void) 499etp_end_thread (void)
472{ 500{
473 eio_req *req = calloc (1, sizeof (eio_req)); 501 eio_req *req = calloc (1, sizeof (eio_req));
474 502
475 req->type = -1; 503 req->type = -1;
476 req->pri = ETP_PRI_MAX - ETP_PRI_MIN; 504 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
483 X_LOCK (wrklock); 511 X_LOCK (wrklock);
484 --started; 512 --started;
485 X_UNLOCK (wrklock); 513 X_UNLOCK (wrklock);
486} 514}
487 515
488static int etp_poll (void) 516static int
517etp_poll (void)
489{ 518{
490 unsigned int maxreqs; 519 unsigned int maxreqs;
491 unsigned int maxtime; 520 unsigned int maxtime;
492 struct timeval tv_start, tv_now; 521 struct timeval tv_start, tv_now;
493 522
523 552
524 X_LOCK (reqlock); 553 X_LOCK (reqlock);
525 --nreqs; 554 --nreqs;
526 X_UNLOCK (reqlock); 555 X_UNLOCK (reqlock);
527 556
528 if (expect_false (req->type == EIO_GROUP && req->size)) 557 if (ecb_expect_false (req->type == EIO_GROUP && req->size))
529 { 558 {
530 req->int1 = 1; /* mark request as delayed */ 559 req->int1 = 1; /* mark request as delayed */
531 continue; 560 continue;
532 } 561 }
533 else 562 else
534 { 563 {
535 int res = ETP_FINISH (req); 564 int res = ETP_FINISH (req);
536 if (expect_false (res)) 565 if (ecb_expect_false (res))
537 return res; 566 return res;
538 } 567 }
539 568
540 if (expect_false (maxreqs && !--maxreqs)) 569 if (ecb_expect_false (maxreqs && !--maxreqs))
541 break; 570 break;
542 571
543 if (maxtime) 572 if (maxtime)
544 { 573 {
545 gettimeofday (&tv_now, 0); 574 gettimeofday (&tv_now, 0);
551 580
552 errno = EAGAIN; 581 errno = EAGAIN;
553 return -1; 582 return -1;
554} 583}
555 584
585static void
556static void etp_cancel (ETP_REQ *req) 586etp_cancel (ETP_REQ *req)
557{ 587{
558 X_LOCK (wrklock); 588 X_LOCK (wrklock);
559 req->flags |= EIO_FLAG_CANCELLED; 589 req->flags |= EIO_FLAG_CANCELLED;
560 X_UNLOCK (wrklock); 590 X_UNLOCK (wrklock);
561 591
562 eio_grp_cancel (req); 592 eio_grp_cancel (req);
563} 593}
564 594
595static void
565static void etp_submit (ETP_REQ *req) 596etp_submit (ETP_REQ *req)
566{ 597{
567 req->pri -= ETP_PRI_MIN; 598 req->pri -= ETP_PRI_MIN;
568 599
569 if (expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; 600 if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN;
570 if (expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; 601 if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
571 602
572 if (expect_false (req->type == EIO_GROUP)) 603 if (ecb_expect_false (req->type == EIO_GROUP))
573 { 604 {
574 /* I hope this is worth it :/ */ 605 /* I hope this is worth it :/ */
575 X_LOCK (reqlock); 606 X_LOCK (reqlock);
576 ++nreqs; 607 ++nreqs;
577 X_UNLOCK (reqlock); 608 X_UNLOCK (reqlock);
596 627
597 etp_maybe_start_thread (); 628 etp_maybe_start_thread ();
598 } 629 }
599} 630}
600 631
632static void ecb_cold
601static void etp_set_max_poll_time (double nseconds) 633etp_set_max_poll_time (double nseconds)
602{ 634{
603 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 635 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
604 max_poll_time = nseconds * EIO_TICKS; 636 max_poll_time = nseconds * EIO_TICKS;
605 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); 637 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
606} 638}
607 639
640static void ecb_cold
608static void etp_set_max_poll_reqs (unsigned int maxreqs) 641etp_set_max_poll_reqs (unsigned int maxreqs)
609{ 642{
610 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 643 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
611 max_poll_reqs = maxreqs; 644 max_poll_reqs = maxreqs;
612 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); 645 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
613} 646}
614 647
648static void ecb_cold
615static void etp_set_max_idle (unsigned int nthreads) 649etp_set_max_idle (unsigned int nthreads)
616{ 650{
617 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 651 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
618 max_idle = nthreads <= 0 ? 1 : nthreads; 652 max_idle = nthreads;
619 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 653 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
620} 654}
621 655
656static void ecb_cold
657etp_set_idle_timeout (unsigned int seconds)
658{
659 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
660 idle_timeout = seconds;
661 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
662}
663
664static void ecb_cold
622static void etp_set_min_parallel (unsigned int nthreads) 665etp_set_min_parallel (unsigned int nthreads)
623{ 666{
624 if (wanted < nthreads) 667 if (wanted < nthreads)
625 wanted = nthreads; 668 wanted = nthreads;
626} 669}
627 670
671static void ecb_cold
628static void etp_set_max_parallel (unsigned int nthreads) 672etp_set_max_parallel (unsigned int nthreads)
629{ 673{
630 if (wanted > nthreads) 674 if (wanted > nthreads)
631 wanted = nthreads; 675 wanted = nthreads;
632 676
633 while (started > wanted) 677 while (started > wanted)
634 etp_end_thread (); 678 etp_end_thread ();
635} 679}
636 680
637/*****************************************************************************/ 681/*****************************************************************************/
638 682
683static void
639static void grp_try_feed (eio_req *grp) 684grp_try_feed (eio_req *grp)
640{ 685{
641 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 686 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
642 { 687 {
643 grp->flags &= ~EIO_FLAG_GROUPADD; 688 grp->flags &= ~EIO_FLAG_GROUPADD;
644 689
651 break; 696 break;
652 } 697 }
653 } 698 }
654} 699}
655 700
701static int
656static int grp_dec (eio_req *grp) 702grp_dec (eio_req *grp)
657{ 703{
658 --grp->size; 704 --grp->size;
659 705
660 /* call feeder, if applicable */ 706 /* call feeder, if applicable */
661 grp_try_feed (grp); 707 grp_try_feed (grp);
665 return eio_finish (grp); 711 return eio_finish (grp);
666 else 712 else
667 return 0; 713 return 0;
668} 714}
669 715
716void
670void eio_destroy (eio_req *req) 717eio_destroy (eio_req *req)
671{ 718{
672 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); 719 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1);
673 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); 720 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2);
674 721
675 EIO_DESTROY (req); 722 EIO_DESTROY (req);
676} 723}
677 724
725static int
678static int eio_finish (eio_req *req) 726eio_finish (eio_req *req)
679{ 727{
680 int res = EIO_FINISH (req); 728 int res = EIO_FINISH (req);
681 729
682 if (req->grp) 730 if (req->grp)
683 { 731 {
700 eio_destroy (req); 748 eio_destroy (req);
701 749
702 return res; 750 return res;
703} 751}
704 752
753void
705void eio_grp_cancel (eio_req *grp) 754eio_grp_cancel (eio_req *grp)
706{ 755{
707 for (grp = grp->grp_first; grp; grp = grp->grp_next) 756 for (grp = grp->grp_first; grp; grp = grp->grp_next)
708 eio_cancel (grp); 757 eio_cancel (grp);
709} 758}
710 759
760void
711void eio_cancel (eio_req *req) 761eio_cancel (eio_req *req)
712{ 762{
713 etp_cancel (req); 763 etp_cancel (req);
714} 764}
715 765
766void
716void eio_submit (eio_req *req) 767eio_submit (eio_req *req)
717{ 768{
718 etp_submit (req); 769 etp_submit (req);
719} 770}
720 771
721unsigned int eio_nreqs (void) 772unsigned int
773eio_nreqs (void)
722{ 774{
723 return etp_nreqs (); 775 return etp_nreqs ();
724} 776}
725 777
726unsigned int eio_nready (void) 778unsigned int
779eio_nready (void)
727{ 780{
728 return etp_nready (); 781 return etp_nready ();
729} 782}
730 783
731unsigned int eio_npending (void) 784unsigned int
785eio_npending (void)
732{ 786{
733 return etp_npending (); 787 return etp_npending ();
734} 788}
735 789
736unsigned int eio_nthreads (void) 790unsigned int ecb_cold
791eio_nthreads (void)
737{ 792{
738 return etp_nthreads (); 793 return etp_nthreads ();
739} 794}
740 795
796void ecb_cold
741void eio_set_max_poll_time (double nseconds) 797eio_set_max_poll_time (double nseconds)
742{ 798{
743 etp_set_max_poll_time (nseconds); 799 etp_set_max_poll_time (nseconds);
744} 800}
745 801
802void ecb_cold
746void eio_set_max_poll_reqs (unsigned int maxreqs) 803eio_set_max_poll_reqs (unsigned int maxreqs)
747{ 804{
748 etp_set_max_poll_reqs (maxreqs); 805 etp_set_max_poll_reqs (maxreqs);
749} 806}
750 807
808void ecb_cold
751void eio_set_max_idle (unsigned int nthreads) 809eio_set_max_idle (unsigned int nthreads)
752{ 810{
753 etp_set_max_idle (nthreads); 811 etp_set_max_idle (nthreads);
754} 812}
755 813
814void ecb_cold
815eio_set_idle_timeout (unsigned int seconds)
816{
817 etp_set_idle_timeout (seconds);
818}
819
820void ecb_cold
756void eio_set_min_parallel (unsigned int nthreads) 821eio_set_min_parallel (unsigned int nthreads)
757{ 822{
758 etp_set_min_parallel (nthreads); 823 etp_set_min_parallel (nthreads);
759} 824}
760 825
826void ecb_cold
761void eio_set_max_parallel (unsigned int nthreads) 827eio_set_max_parallel (unsigned int nthreads)
762{ 828{
763 etp_set_max_parallel (nthreads); 829 etp_set_max_parallel (nthreads);
764} 830}
765 831
766int eio_poll (void) 832int eio_poll (void)
808 874
809 return res; 875 return res;
810} 876}
811#endif 877#endif
812 878
813#ifndef HAVE_FUTIMES 879#ifndef HAVE_UTIMES
814 880
815# undef utimes 881# undef utimes
816# undef futimes
817# define utimes(path,times) eio__utimes (path, times) 882# define utimes(path,times) eio__utimes (path, times)
818# define futimes(fd,times) eio__futimes (fd, times)
819 883
820static int 884static int
821eio__utimes (const char *filename, const struct timeval times[2]) 885eio__utimes (const char *filename, const struct timeval times[2])
822{ 886{
823 if (times) 887 if (times)
831 } 895 }
832 else 896 else
833 return utime (filename, 0); 897 return utime (filename, 0);
834} 898}
835 899
900#endif
901
902#ifndef HAVE_FUTIMES
903
904# undef futimes
905# define futimes(fd,times) eio__futimes (fd, times)
906
907static int
836static int eio__futimes (int fd, const struct timeval tv[2]) 908eio__futimes (int fd, const struct timeval tv[2])
837{ 909{
838 errno = ENOSYS; 910 errno = ENOSYS;
839 return -1; 911 return -1;
840} 912}
841 913
845# undef fdatasync 917# undef fdatasync
846# define fdatasync(fd) fsync (fd) 918# define fdatasync(fd) fsync (fd)
847#endif 919#endif
848 920
849/* sync_file_range always needs emulation */ 921/* sync_file_range always needs emulation */
850int 922static int
851eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) 923eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags)
852{ 924{
853#if HAVE_SYNC_FILE_RANGE 925#if HAVE_SYNC_FILE_RANGE
854 int res; 926 int res;
855 927
901 973
902/* sendfile always needs emulation */ 974/* sendfile always needs emulation */
903static ssize_t 975static ssize_t
904eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) 976eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self)
905{ 977{
978 ssize_t written = 0;
906 ssize_t res; 979 ssize_t res;
907 980
908 if (!count) 981 if (!count)
909 return 0; 982 return 0;
910 983
984 for (;;)
985 {
911#if HAVE_SENDFILE 986#if HAVE_SENDFILE
912# if __linux 987# if __linux
988 off_t soffset = offset;
913 res = sendfile (ofd, ifd, &offset, count); 989 res = sendfile (ofd, ifd, &soffset, count);
914 990
915# elif __FreeBSD__ 991# elif __FreeBSD__
916 /* 992 /*
917 * Of course, the freebsd sendfile is a dire hack with no thoughts 993 * Of course, the freebsd sendfile is a dire hack with no thoughts
918 * wasted on making it similar to other I/O functions. 994 * wasted on making it similar to other I/O functions.
919 */ 995 */
920 {
921 off_t sbytes; 996 off_t sbytes;
922 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 997 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
923 998
924 #if 0 /* according to the manpage, this is correct, but broken behaviour */ 999 #if 0 /* according to the manpage, this is correct, but broken behaviour */
925 /* freebsd' sendfile will return 0 on success */ 1000 /* freebsd' sendfile will return 0 on success */
926 /* freebsd 8 documents it as only setting *sbytes on EINTR and EAGAIN, but */ 1001 /* freebsd 8 documents it as only setting *sbytes on EINTR and EAGAIN, but */
927 /* not on e.g. EIO or EPIPE - sounds broken */ 1002 /* not on e.g. EIO or EPIPE - sounds broken */
928 if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0) 1003 if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0)
929 res = sbytes; 1004 res = sbytes;
930 #endif 1005 #endif
931 1006
932 /* according to source inspection, this is correct, and useful behaviour */ 1007 /* according to source inspection, this is correct, and useful behaviour */
933 if (sbytes) 1008 if (sbytes)
934 res = sbytes; 1009 res = sbytes;
935 }
936 1010
937# elif defined (__APPLE__) 1011# elif defined (__APPLE__) && 0 /* broken, as everything on os x */
938
939 {
940 off_t sbytes = count; 1012 off_t sbytes = count;
941 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); 1013 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0);
942 1014
943 /* according to the manpage, sbytes is always valid */ 1015 /* according to the manpage, sbytes is always valid */
944 if (sbytes) 1016 if (sbytes)
945 res = sbytes; 1017 res = sbytes;
946 }
947 1018
948# elif __hpux 1019# elif __hpux
949 res = sendfile (ofd, ifd, offset, count, 0, 0); 1020 res = sendfile (ofd, ifd, offset, count, 0, 0);
950 1021
951# elif __solaris 1022# elif __solaris
952 {
953 struct sendfilevec vec; 1023 struct sendfilevec vec;
954 size_t sbytes; 1024 size_t sbytes;
955 1025
956 vec.sfv_fd = ifd; 1026 vec.sfv_fd = ifd;
957 vec.sfv_flag = 0; 1027 vec.sfv_flag = 0;
958 vec.sfv_off = offset; 1028 vec.sfv_off = offset;
959 vec.sfv_len = count; 1029 vec.sfv_len = count;
960 1030
961 res = sendfilev (ofd, &vec, 1, &sbytes); 1031 res = sendfilev (ofd, &vec, 1, &sbytes);
962 1032
963 if (res < 0 && sbytes) 1033 if (res < 0 && sbytes)
964 res = sbytes; 1034 res = sbytes;
965 }
966 1035
967# endif 1036# endif
968 1037
969#elif defined (_WIN32) 1038#elif defined (_WIN32)
970
971 /* does not work, just for documentation of what would need to be done */ 1039 /* does not work, just for documentation of what would need to be done */
972 { 1040 /* actually, cannot be done like this, as TransmitFile changes the file offset, */
1041 /* libeio guarantees that the file offset does not change, and windows */
1042 /* has no way to get an independent handle to the same file description */
973 HANDLE h = TO_SOCKET (ifd); 1043 HANDLE h = TO_SOCKET (ifd);
974 SetFilePointer (h, offset, 0, FILE_BEGIN); 1044 SetFilePointer (h, offset, 0, FILE_BEGIN);
975 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); 1045 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0);
976 }
977 1046
978#else 1047#else
979 res = -1; 1048 res = -1;
980 errno = ENOSYS; 1049 errno = ENOSYS;
981#endif 1050#endif
982 1051
1052 /* we assume sendfile can copy at least 128mb in one go */
1053 if (res <= 128 * 1024 * 1024)
1054 {
1055 if (res > 0)
1056 written += res;
1057
1058 if (written)
1059 return written;
1060
1061 break;
1062 }
1063 else
1064 {
1065 /* if we requested more, then probably the kernel was lazy */
1066 written += res;
1067 offset += res;
1068 count -= res;
1069
1070 if (!count)
1071 return written;
1072 }
1073 }
1074
983 if (res < 0 1075 if (res < 0
984 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK 1076 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
985 /* BSDs */ 1077 /* BSDs */
986#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */ 1078#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */
987 || errno == ENOTSUP 1079 || errno == ENOTSUP
988#endif 1080#endif
1025 } 1117 }
1026 1118
1027 return res; 1119 return res;
1028} 1120}
1029 1121
1122#ifdef PAGESIZE
1123# define eio_pagesize() PAGESIZE
1124#else
1125static intptr_t
1126eio_pagesize (void)
1127{
1128 static intptr_t page;
1129
1130 if (!page)
1131 page = sysconf (_SC_PAGESIZE);
1132
1133 return page;
1134}
1135#endif
1136
1137static void
1138eio_page_align (void **addr, size_t *length)
1139{
1140 intptr_t mask = eio_pagesize () - 1;
1141
1142 /* round down addr */
1143 intptr_t adj = mask & (intptr_t)*addr;
1144
1145 *addr = (void *)((intptr_t)*addr - adj);
1146 *length += adj;
1147
1148 /* round up length */
1149 *length = (*length + mask) & ~mask;
1150}
1151
1152#if !_POSIX_MEMLOCK
1153# define eio__mlockall(a) ((errno = ENOSYS), -1)
1154#else
1155
1156static int
1157eio__mlockall (int flags)
1158{
1159 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1160 extern int mallopt (int, int);
1161 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1162 #endif
1163
1164 if (EIO_MCL_CURRENT != MCL_CURRENT
1165 || EIO_MCL_FUTURE != MCL_FUTURE)
1166 {
1167 flags = 0
1168 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0)
1169 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0);
1170 }
1171
1172 return mlockall (flags);
1173}
1174#endif
1175
1176#if !_POSIX_MEMLOCK_RANGE
1177# define eio__mlock(a,b) ((errno = ENOSYS), -1)
1178#else
1179
1180static int
1181eio__mlock (void *addr, size_t length)
1182{
1183 eio_page_align (&addr, &length);
1184
1185 return mlock (addr, length);
1186}
1187
1188#endif
1189
1190#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1191# define eio__msync(a,b,c) ((errno = ENOSYS), -1)
1192#else
1193
1194static int
1195eio__msync (void *mem, size_t len, int flags)
1196{
1197 eio_page_align (&mem, &len);
1198
1199 if (EIO_MS_ASYNC != MS_SYNC
1200 || EIO_MS_INVALIDATE != MS_INVALIDATE
1201 || EIO_MS_SYNC != MS_SYNC)
1202 {
1203 flags = 0
1204 | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0)
1205 | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0)
1206 | (flags & EIO_MS_SYNC ? MS_SYNC : 0);
1207 }
1208
1209 return msync (mem, len, flags);
1210}
1211
1212#endif
1213
1214static int
1215eio__mtouch (eio_req *req)
1216{
1217 void *mem = req->ptr2;
1218 size_t len = req->size;
1219 int flags = req->int1;
1220
1221 eio_page_align (&mem, &len);
1222
1223 {
1224 intptr_t addr = (intptr_t)mem;
1225 intptr_t end = addr + len;
1226 intptr_t page = eio_pagesize ();
1227
1228 if (addr < end)
1229 if (flags & EIO_MT_MODIFY) /* modify */
1230 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req));
1231 else
1232 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req));
1233 }
1234
1235 return 0;
1236}
1237
1238/*****************************************************************************/
1239/* requests implemented outside eio_execute, because they are so large */
1240
1241static void
1242eio__realpath (eio_req *req, etp_worker *self)
1243{
1244 char *rel = req->ptr1;
1245 char *res;
1246 char *tmp1, *tmp2;
1247#if SYMLOOP_MAX > 32
1248 int links = SYMLOOP_MAX;
1249#else
1250 int links = 32;
1251#endif
1252
1253 req->result = -1;
1254
1255 errno = EINVAL;
1256 if (!rel)
1257 return;
1258
1259 errno = ENOENT;
1260 if (!*rel)
1261 return;
1262
1263 if (!req->ptr2)
1264 {
1265 X_LOCK (wrklock);
1266 req->flags |= EIO_FLAG_PTR2_FREE;
1267 X_UNLOCK (wrklock);
1268 req->ptr2 = malloc (PATH_MAX * 3);
1269
1270 errno = ENOMEM;
1271 if (!req->ptr2)
1272 return;
1273 }
1274
1275 res = req->ptr2;
1276 tmp1 = res + PATH_MAX;
1277 tmp2 = tmp1 + PATH_MAX;
1278
1279 if (*rel != '/')
1280 {
1281 if (!getcwd (res, PATH_MAX))
1282 return;
1283
1284 if (res [1]) /* only use if not / */
1285 res += strlen (res);
1286 }
1287
1288 while (*rel)
1289 {
1290 ssize_t len, linklen;
1291 char *beg = rel;
1292
1293 while (*rel && *rel != '/')
1294 ++rel;
1295
1296 len = rel - beg;
1297
1298 if (!len) /* skip slashes */
1299 {
1300 ++rel;
1301 continue;
1302 }
1303
1304 if (beg [0] == '.')
1305 {
1306 if (len == 1)
1307 continue; /* . - nop */
1308
1309 if (beg [1] == '.' && len == 2)
1310 {
1311 /* .. - back up one component, if possible */
1312
1313 while (res != req->ptr2)
1314 if (*--res == '/')
1315 break;
1316
1317 continue;
1318 }
1319 }
1320
1321 errno = ENAMETOOLONG;
1322 if (res + 1 + len + 1 >= tmp1)
1323 return;
1324
1325 /* copy one component */
1326 *res = '/';
1327 memcpy (res + 1, beg, len);
1328
1329 /* zero-terminate, for readlink */
1330 res [len + 1] = 0;
1331
1332 /* now check if it's a symlink */
1333 linklen = readlink (req->ptr2, tmp1, PATH_MAX);
1334
1335 if (linklen < 0)
1336 {
1337 if (errno != EINVAL)
1338 return;
1339
1340 /* it's a normal directory. hopefully */
1341 res += len + 1;
1342 }
1343 else
1344 {
1345 /* yay, it was a symlink - build new path in tmp2 */
1346 int rellen = strlen (rel);
1347
1348 errno = ENAMETOOLONG;
1349 if (linklen + 1 + rellen >= PATH_MAX)
1350 return;
1351
1352 if (*tmp1 == '/')
1353 res = req->ptr2; /* symlink resolves to an absolute path */
1354
1355 /* we need to be careful, as rel might point into tmp2 already */
1356 memmove (tmp2 + linklen + 1, rel, rellen + 1);
1357 tmp2 [linklen] = '/';
1358 memcpy (tmp2, tmp1, linklen);
1359
1360 rel = tmp2;
1361 }
1362 }
1363
1364 /* special case for the lone root path */
1365 if (res == req->ptr2)
1366 *res++ = '/';
1367
1368 req->result = res - (char *)req->ptr2;
1369 req->ptr2 = realloc (req->ptr2, req->result); /* trade time for space savings */
1370}
1371
1030static signed char 1372static signed char
1031eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1373eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1032{ 1374{
1033 return a->score - b->score ? a->score - b->score /* works because our signed char is always 0..100 */ 1375 return a->score - b->score ? a->score - b->score /* works because our signed char is always 0..100 */
1034 : a->inode < b->inode ? -1 : a->inode > b->inode ? 1 : 0; 1376 : a->inode < b->inode ? -1
1377 : a->inode > b->inode ? 1
1378 : 0;
1035} 1379}
1036 1380
1037#define EIO_DENT_CMP(i,op,j) eio_dent_cmp (&i, &j) op 0 1381#define EIO_DENT_CMP(i,op,j) eio_dent_cmp (&i, &j) op 0
1038 1382
1039#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */ 1383#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */
1045 unsigned char bits [9 + sizeof (ino_t) * 8]; 1389 unsigned char bits [9 + sizeof (ino_t) * 8];
1046 unsigned char *bit = bits; 1390 unsigned char *bit = bits;
1047 1391
1048 assert (CHAR_BIT == 8); 1392 assert (CHAR_BIT == 8);
1049 assert (sizeof (eio_dirent) * 8 < 256); 1393 assert (sizeof (eio_dirent) * 8 < 256);
1050 assert (offsetof (eio_dirent, inode)); /* we use 0 as sentinel */ 1394 assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */
1051 assert (offsetof (eio_dirent, score)); /* we use 0 as sentinel */ 1395 assert (offsetof (eio_dirent, score)); /* we use bit #0 as sentinel */
1052 1396
1053 if (size <= EIO_SORT_FAST) 1397 if (size <= EIO_SORT_FAST)
1054 return; 1398 return;
1055 1399
1056 /* first prepare an array of bits to test in our radix sort */ 1400 /* first prepare an array of bits to test in our radix sort */
1211 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); 1555 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER);
1212 1556
1213 X_LOCK (wrklock); 1557 X_LOCK (wrklock);
1214 /* the corresponding closedir is in ETP_WORKER_CLEAR */ 1558 /* the corresponding closedir is in ETP_WORKER_CLEAR */
1215 self->dirp = dirp = opendir (req->ptr1); 1559 self->dirp = dirp = opendir (req->ptr1);
1560
1561 if (req->flags & EIO_FLAG_PTR1_FREE)
1562 free (req->ptr1);
1563
1216 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; 1564 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE;
1217 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; 1565 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0;
1218 req->ptr2 = names = malloc (namesalloc); 1566 req->ptr2 = names = malloc (namesalloc);
1219 X_UNLOCK (wrklock); 1567 X_UNLOCK (wrklock);
1220 1568
1232 /* sort etc. */ 1580 /* sort etc. */
1233 req->int1 = flags; 1581 req->int1 = flags;
1234 req->result = dentoffs; 1582 req->result = dentoffs;
1235 1583
1236 if (flags & EIO_READDIR_STAT_ORDER) 1584 if (flags & EIO_READDIR_STAT_ORDER)
1237 eio_dent_sort (dents, dentoffs, 0, inode_bits); /* sort by inode exclusively */ 1585 eio_dent_sort (dents, dentoffs, flags & EIO_READDIR_DIRS_FIRST ? 7 : 0, inode_bits);
1238 else if (flags & EIO_READDIR_DIRS_FIRST) 1586 else if (flags & EIO_READDIR_DIRS_FIRST)
1239 if (flags & EIO_READDIR_FOUND_UNKNOWN) 1587 if (flags & EIO_READDIR_FOUND_UNKNOWN)
1240 eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */ 1588 eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */
1241 else 1589 else
1242 { 1590 {
1244 eio_dirent *oth = dents + dentoffs; 1592 eio_dirent *oth = dents + dentoffs;
1245 eio_dirent *dir = dents; 1593 eio_dirent *dir = dents;
1246 1594
1247 /* now partition dirs to the front, and non-dirs to the back */ 1595 /* now partition dirs to the front, and non-dirs to the back */
1248 /* by walking from both sides and swapping if necessary */ 1596 /* by walking from both sides and swapping if necessary */
1249 /* also clear score, so it doesn't influence sorting */
1250 while (oth > dir) 1597 while (oth > dir)
1251 { 1598 {
1252 if (dir->type == EIO_DT_DIR) 1599 if (dir->type == EIO_DT_DIR)
1253 ++dir; 1600 ++dir;
1254 else if ((--oth)->type == EIO_DT_DIR) 1601 else if ((--oth)->type == EIO_DT_DIR)
1257 1604
1258 ++dir; 1605 ++dir;
1259 } 1606 }
1260 } 1607 }
1261 1608
1262 /* now sort the dirs only */ 1609 /* now sort the dirs only (dirs all have the same score) */
1263 eio_dent_sort (dents, dir - dents, 0, inode_bits); 1610 eio_dent_sort (dents, dir - dents, 0, inode_bits);
1264 } 1611 }
1265 1612
1266 break; 1613 break;
1267 } 1614 }
1272 /* skip . and .. entries */ 1619 /* skip . and .. entries */
1273 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 1620 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
1274 { 1621 {
1275 int len = D_NAMLEN (entp) + 1; 1622 int len = D_NAMLEN (entp) + 1;
1276 1623
1277 while (expect_false (namesoffs + len > namesalloc)) 1624 while (ecb_expect_false (namesoffs + len > namesalloc))
1278 { 1625 {
1279 namesalloc *= 2; 1626 namesalloc *= 2;
1280 X_LOCK (wrklock); 1627 X_LOCK (wrklock);
1281 req->ptr2 = names = realloc (names, namesalloc); 1628 req->ptr2 = names = realloc (names, namesalloc);
1282 X_UNLOCK (wrklock); 1629 X_UNLOCK (wrklock);
1289 1636
1290 if (dents) 1637 if (dents)
1291 { 1638 {
1292 struct eio_dirent *ent; 1639 struct eio_dirent *ent;
1293 1640
1294 if (expect_false (dentoffs == dentalloc)) 1641 if (ecb_expect_false (dentoffs == dentalloc))
1295 { 1642 {
1296 dentalloc *= 2; 1643 dentalloc *= 2;
1297 X_LOCK (wrklock); 1644 X_LOCK (wrklock);
1298 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); 1645 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent));
1299 X_UNLOCK (wrklock); 1646 X_UNLOCK (wrklock);
1387 break; 1734 break;
1388 } 1735 }
1389 } 1736 }
1390} 1737}
1391 1738
1392#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1393# define eio__msync(a,b,c) ((errno = ENOSYS), -1)
1394#else
1395
1396int
1397eio__msync (void *mem, size_t len, int flags)
1398{
1399 if (EIO_MS_ASYNC != MS_SYNC
1400 || EIO_MS_INVALIDATE != MS_INVALIDATE
1401 || EIO_MS_SYNC != MS_SYNC)
1402 {
1403 flags = 0
1404 | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0)
1405 | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0)
1406 | (flags & EIO_MS_SYNC ? MS_SYNC : 0);
1407 }
1408
1409 return msync (mem, len, flags);
1410}
1411
1412#endif
1413
1414int
1415eio__mtouch (void *mem, size_t len, int flags)
1416{
1417 intptr_t addr = (intptr_t)mem;
1418 intptr_t end = addr + len;
1419#ifdef PAGESIZE
1420 const intptr_t page = PAGESIZE;
1421#else
1422 static intptr_t page;
1423
1424 if (!page)
1425 page = sysconf (_SC_PAGESIZE);
1426#endif
1427
1428 /* round down to start of page, although this is probably useless */
1429 addr &= ~(page - 1); /* assume page size is always a power of two */
1430
1431 if (addr < end)
1432 if (flags & EIO_MT_MODIFY) /* modify */
1433 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len);
1434 else
1435 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len);
1436
1437 return 0;
1438}
1439
1440/*****************************************************************************/ 1739/*****************************************************************************/
1441 1740
1442#define ALLOC(len) \ 1741#define ALLOC(len) \
1443 if (!req->ptr2) \ 1742 if (!req->ptr2) \
1444 { \ 1743 { \
1474 if (req) 1773 if (req)
1475 break; 1774 break;
1476 1775
1477 ++idle; 1776 ++idle;
1478 1777
1479 ts.tv_sec = time (0) + IDLE_TIMEOUT; 1778 ts.tv_sec = time (0) + idle_timeout;
1480 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) 1779 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
1481 { 1780 {
1482 if (idle > max_idle) 1781 if (idle > max_idle)
1483 { 1782 {
1484 --idle; 1783 --idle;
1527 return 0; 1826 return 0;
1528} 1827}
1529 1828
1530/*****************************************************************************/ 1829/*****************************************************************************/
1531 1830
1831int ecb_cold
1532int eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1832eio_init (void (*want_poll)(void), void (*done_poll)(void))
1533{ 1833{
1534 return etp_init (want_poll, done_poll); 1834 return etp_init (want_poll, done_poll);
1535} 1835}
1536 1836
1837ecb_inline void
1537static void eio_api_destroy (eio_req *req) 1838eio_api_destroy (eio_req *req)
1538{ 1839{
1539 free (req); 1840 free (req);
1540} 1841}
1541 1842
1542#define REQ(rtype) \ 1843#define REQ(rtype) \
1561 { \ 1862 { \
1562 eio_api_destroy (req); \ 1863 eio_api_destroy (req); \
1563 return 0; \ 1864 return 0; \
1564 } 1865 }
1565 1866
1867static void
1566static void eio_execute (etp_worker *self, eio_req *req) 1868eio_execute (etp_worker *self, eio_req *req)
1567{ 1869{
1568 errno = 0;
1569
1570 switch (req->type) 1870 switch (req->type)
1571 { 1871 {
1572 case EIO_READ: ALLOC (req->size); 1872 case EIO_READ: ALLOC (req->size);
1573 req->result = req->offs >= 0 1873 req->result = req->offs >= 0
1574 ? pread (req->int1, req->ptr2, req->size, req->offs) 1874 ? pread (req->int1, req->ptr2, req->size, req->offs)
1606 case EIO_RMDIR: req->result = rmdir (req->ptr1); break; 1906 case EIO_RMDIR: req->result = rmdir (req->ptr1); break;
1607 case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break; 1907 case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break;
1608 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; 1908 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break;
1609 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; 1909 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break;
1610 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; 1910 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break;
1611 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->int3); break; 1911 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
1612 1912
1913 case EIO_REALPATH: eio__realpath (req, self); break;
1914
1613 case EIO_READLINK: ALLOC (NAME_MAX); 1915 case EIO_READLINK: ALLOC (PATH_MAX);
1614 req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break; 1916 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break;
1615 1917
1616 case EIO_SYNC: req->result = 0; sync (); break; 1918 case EIO_SYNC: req->result = 0; sync (); break;
1617 case EIO_FSYNC: req->result = fsync (req->int1); break; 1919 case EIO_FSYNC: req->result = fsync (req->int1); break;
1618 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; 1920 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
1619 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; 1921 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break;
1922 case EIO_MTOUCH: req->result = eio__mtouch (req); break;
1620 case EIO_MTOUCH: req->result = eio__mtouch (req->ptr2, req->size, req->int1); break; 1923 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break;
1924 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break;
1621 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; 1925 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break;
1622 1926
1623 case EIO_READDIR: eio__scandir (req, self); break; 1927 case EIO_READDIR: eio__scandir (req, self); break;
1624 1928
1625 case EIO_BUSY: 1929 case EIO_BUSY:
1626#ifdef _WIN32 1930#ifdef _WIN32
1627 Sleep (req->nv1 * 1000.); 1931 Sleep (req->nv1 * 1e3);
1628#else 1932#else
1629 { 1933 {
1630 struct timeval tv; 1934 struct timeval tv;
1631 1935
1632 tv.tv_sec = req->nv1; 1936 tv.tv_sec = req->nv1;
1633 tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; 1937 tv.tv_usec = (req->nv1 - tv.tv_sec) * 1e6;
1634 1938
1635 req->result = select (0, 0, 0, 0, &tv); 1939 req->result = select (0, 0, 0, 0, &tv);
1636 } 1940 }
1637#endif 1941#endif
1638 break; 1942 break;
1653 times = tv; 1957 times = tv;
1654 } 1958 }
1655 else 1959 else
1656 times = 0; 1960 times = 0;
1657 1961
1658
1659 req->result = req->type == EIO_FUTIME 1962 req->result = req->type == EIO_FUTIME
1660 ? futimes (req->int1, times) 1963 ? futimes (req->int1, times)
1661 : utimes (req->ptr1, times); 1964 : utimes (req->ptr1, times);
1662 } 1965 }
1663 break; 1966 break;
1668 case EIO_NOP: 1971 case EIO_NOP:
1669 req->result = 0; 1972 req->result = 0;
1670 break; 1973 break;
1671 1974
1672 case EIO_CUSTOM: 1975 case EIO_CUSTOM:
1673 ((void (*)(eio_req *))req->feed) (req); 1976 req->feed (req);
1674 break; 1977 break;
1675 1978
1676 default: 1979 default:
1980 errno = ENOSYS;
1677 req->result = -1; 1981 req->result = -1;
1678 break; 1982 break;
1679 } 1983 }
1680 1984
1681 req->errorno = errno; 1985 req->errorno = errno;
1711eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) 2015eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data)
1712{ 2016{
1713 REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; 2017 REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND;
1714} 2018}
1715 2019
2020eio_req *eio_mlock (void *addr, size_t length, int pri, eio_cb cb, void *data)
2021{
2022 REQ (EIO_MLOCK); req->ptr2 = addr; req->size = length; SEND;
2023}
2024
2025eio_req *eio_mlockall (int flags, int pri, eio_cb cb, void *data)
2026{
2027 REQ (EIO_MLOCKALL); req->int1 = flags; SEND;
2028}
2029
1716eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) 2030eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data)
1717{ 2031{
1718 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; 2032 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND;
1719} 2033}
1720 2034
1822eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data) 2136eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data)
1823{ 2137{
1824 return eio__1path (EIO_READLINK, path, pri, cb, data); 2138 return eio__1path (EIO_READLINK, path, pri, cb, data);
1825} 2139}
1826 2140
2141eio_req *eio_realpath (const char *path, int pri, eio_cb cb, void *data)
2142{
2143 return eio__1path (EIO_REALPATH, path, pri, cb, data);
2144}
2145
1827eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data) 2146eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data)
1828{ 2147{
1829 return eio__1path (EIO_STAT, path, pri, cb, data); 2148 return eio__1path (EIO_STAT, path, pri, cb, data);
1830} 2149}
1831 2150
1854 REQ (EIO_READDIR); PATH; req->int1 = flags; SEND; 2173 REQ (EIO_READDIR); PATH; req->int1 = flags; SEND;
1855} 2174}
1856 2175
1857eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) 2176eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data)
1858{ 2177{
1859 REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND; 2178 REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->offs = (off_t)dev; SEND;
1860} 2179}
1861 2180
1862static eio_req * 2181static eio_req *
1863eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data) 2182eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1864{ 2183{
1888eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) 2207eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1889{ 2208{
1890 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); 2209 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
1891} 2210}
1892 2211
1893eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) 2212eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data)
1894{ 2213{
1895 REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND; 2214 REQ (EIO_CUSTOM); req->feed = execute; SEND;
1896} 2215}
1897 2216
1898#endif 2217#endif
1899 2218
1900eio_req *eio_grp (eio_cb cb, void *data) 2219eio_req *eio_grp (eio_cb cb, void *data)
1909#undef SEND 2228#undef SEND
1910 2229
1911/*****************************************************************************/ 2230/*****************************************************************************/
1912/* grp functions */ 2231/* grp functions */
1913 2232
2233void
1914void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) 2234eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit)
1915{ 2235{
1916 grp->int2 = limit; 2236 grp->int2 = limit;
1917 grp->feed = feed; 2237 grp->feed = feed;
1918 2238
1919 grp_try_feed (grp); 2239 grp_try_feed (grp);
1920} 2240}
1921 2241
2242void
1922void eio_grp_limit (eio_req *grp, int limit) 2243eio_grp_limit (eio_req *grp, int limit)
1923{ 2244{
1924 grp->int2 = limit; 2245 grp->int2 = limit;
1925 2246
1926 grp_try_feed (grp); 2247 grp_try_feed (grp);
1927} 2248}
1928 2249
2250void
1929void eio_grp_add (eio_req *grp, eio_req *req) 2251eio_grp_add (eio_req *grp, eio_req *req)
1930{ 2252{
1931 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); 2253 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
1932 2254
1933 grp->flags |= EIO_FLAG_GROUPADD; 2255 grp->flags |= EIO_FLAG_GROUPADD;
1934 2256
1945} 2267}
1946 2268
1947/*****************************************************************************/ 2269/*****************************************************************************/
1948/* misc garbage */ 2270/* misc garbage */
1949 2271
2272ssize_t
1950ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) 2273eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
1951{ 2274{
1952 etp_worker wrk; 2275 etp_worker wrk;
1953 ssize_t ret; 2276 ssize_t ret;
1954 2277
1955 wrk.dbuf = 0; 2278 wrk.dbuf = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines