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.72 by root, Fri Jun 10 12:45:20 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__)
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
1028} 1120}
1029 1121
1030static signed char 1122static signed char
1031eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1123eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1032{ 1124{
1033 return a->score - b->score ? a->score - b->score /* works because our signed char is always 0..100 */ 1125 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; 1126 : a->inode < b->inode ? -1
1127 : a->inode > b->inode ? 1
1128 : 0;
1035} 1129}
1036 1130
1037#define EIO_DENT_CMP(i,op,j) eio_dent_cmp (&i, &j) op 0 1131#define EIO_DENT_CMP(i,op,j) eio_dent_cmp (&i, &j) op 0
1038 1132
1039#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */ 1133#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */
1045 unsigned char bits [9 + sizeof (ino_t) * 8]; 1139 unsigned char bits [9 + sizeof (ino_t) * 8];
1046 unsigned char *bit = bits; 1140 unsigned char *bit = bits;
1047 1141
1048 assert (CHAR_BIT == 8); 1142 assert (CHAR_BIT == 8);
1049 assert (sizeof (eio_dirent) * 8 < 256); 1143 assert (sizeof (eio_dirent) * 8 < 256);
1050 assert (offsetof (eio_dirent, inode)); /* we use 0 as sentinel */ 1144 assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */
1051 assert (offsetof (eio_dirent, score)); /* we use 0 as sentinel */ 1145 assert (offsetof (eio_dirent, score)); /* we use bit #0 as sentinel */
1052 1146
1053 if (size <= EIO_SORT_FAST) 1147 if (size <= EIO_SORT_FAST)
1054 return; 1148 return;
1055 1149
1056 /* first prepare an array of bits to test in our radix sort */ 1150 /* first prepare an array of bits to test in our radix sort */
1211 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); 1305 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER);
1212 1306
1213 X_LOCK (wrklock); 1307 X_LOCK (wrklock);
1214 /* the corresponding closedir is in ETP_WORKER_CLEAR */ 1308 /* the corresponding closedir is in ETP_WORKER_CLEAR */
1215 self->dirp = dirp = opendir (req->ptr1); 1309 self->dirp = dirp = opendir (req->ptr1);
1310
1216 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; 1311 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE;
1217 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; 1312 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0;
1218 req->ptr2 = names = malloc (namesalloc); 1313 req->ptr2 = names = malloc (namesalloc);
1219 X_UNLOCK (wrklock); 1314 X_UNLOCK (wrklock);
1220 1315
1232 /* sort etc. */ 1327 /* sort etc. */
1233 req->int1 = flags; 1328 req->int1 = flags;
1234 req->result = dentoffs; 1329 req->result = dentoffs;
1235 1330
1236 if (flags & EIO_READDIR_STAT_ORDER) 1331 if (flags & EIO_READDIR_STAT_ORDER)
1237 eio_dent_sort (dents, dentoffs, 0, inode_bits); /* sort by inode exclusively */ 1332 eio_dent_sort (dents, dentoffs, flags & EIO_READDIR_DIRS_FIRST ? 7 : 0, inode_bits);
1238 else if (flags & EIO_READDIR_DIRS_FIRST) 1333 else if (flags & EIO_READDIR_DIRS_FIRST)
1239 if (flags & EIO_READDIR_FOUND_UNKNOWN) 1334 if (flags & EIO_READDIR_FOUND_UNKNOWN)
1240 eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */ 1335 eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */
1241 else 1336 else
1242 { 1337 {
1244 eio_dirent *oth = dents + dentoffs; 1339 eio_dirent *oth = dents + dentoffs;
1245 eio_dirent *dir = dents; 1340 eio_dirent *dir = dents;
1246 1341
1247 /* now partition dirs to the front, and non-dirs to the back */ 1342 /* now partition dirs to the front, and non-dirs to the back */
1248 /* by walking from both sides and swapping if necessary */ 1343 /* by walking from both sides and swapping if necessary */
1249 /* also clear score, so it doesn't influence sorting */
1250 while (oth > dir) 1344 while (oth > dir)
1251 { 1345 {
1252 if (dir->type == EIO_DT_DIR) 1346 if (dir->type == EIO_DT_DIR)
1253 ++dir; 1347 ++dir;
1254 else if ((--oth)->type == EIO_DT_DIR) 1348 else if ((--oth)->type == EIO_DT_DIR)
1257 1351
1258 ++dir; 1352 ++dir;
1259 } 1353 }
1260 } 1354 }
1261 1355
1262 /* now sort the dirs only */ 1356 /* now sort the dirs only (dirs all have the same score) */
1263 eio_dent_sort (dents, dir - dents, 0, inode_bits); 1357 eio_dent_sort (dents, dir - dents, 0, inode_bits);
1264 } 1358 }
1265 1359
1266 break; 1360 break;
1267 } 1361 }
1272 /* skip . and .. entries */ 1366 /* skip . and .. entries */
1273 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 1367 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
1274 { 1368 {
1275 int len = D_NAMLEN (entp) + 1; 1369 int len = D_NAMLEN (entp) + 1;
1276 1370
1277 while (expect_false (namesoffs + len > namesalloc)) 1371 while (ecb_expect_false (namesoffs + len > namesalloc))
1278 { 1372 {
1279 namesalloc *= 2; 1373 namesalloc *= 2;
1280 X_LOCK (wrklock); 1374 X_LOCK (wrklock);
1281 req->ptr2 = names = realloc (names, namesalloc); 1375 req->ptr2 = names = realloc (names, namesalloc);
1282 X_UNLOCK (wrklock); 1376 X_UNLOCK (wrklock);
1289 1383
1290 if (dents) 1384 if (dents)
1291 { 1385 {
1292 struct eio_dirent *ent; 1386 struct eio_dirent *ent;
1293 1387
1294 if (expect_false (dentoffs == dentalloc)) 1388 if (ecb_expect_false (dentoffs == dentalloc))
1295 { 1389 {
1296 dentalloc *= 2; 1390 dentalloc *= 2;
1297 X_LOCK (wrklock); 1391 X_LOCK (wrklock);
1298 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); 1392 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent));
1299 X_UNLOCK (wrklock); 1393 X_UNLOCK (wrklock);
1387 break; 1481 break;
1388 } 1482 }
1389 } 1483 }
1390} 1484}
1391 1485
1486#ifdef PAGESIZE
1487# define eio_pagesize() PAGESIZE
1488#else
1489static intptr_t
1490eio_pagesize (void)
1491{
1492 static intptr_t page;
1493
1494 if (!page)
1495 page = sysconf (_SC_PAGESIZE);
1496
1497 return page;
1498}
1499#endif
1500
1501static void
1502eio_page_align (void **addr, size_t *length)
1503{
1504 intptr_t mask = eio_pagesize () - 1;
1505
1506 /* round down addr */
1507 intptr_t adj = mask & (intptr_t)*addr;
1508
1509 *addr = (void *)((intptr_t)*addr - adj);
1510 *length += adj;
1511
1512 /* round up length */
1513 *length = (*length + mask) & ~mask;
1514}
1515
1516#if !_POSIX_MEMLOCK
1517# define eio__mlockall(a) ((errno = ENOSYS), -1)
1518#else
1519
1520static int
1521eio__mlockall (int flags)
1522{
1523 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1524 extern int mallopt (int, int);
1525 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1526 #endif
1527
1528 if (EIO_MCL_CURRENT != MCL_CURRENT
1529 || EIO_MCL_FUTURE != MCL_FUTURE)
1530 {
1531 flags = 0
1532 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0)
1533 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0);
1534 }
1535
1536 return mlockall (flags);
1537}
1538#endif
1539
1540#if !_POSIX_MEMLOCK_RANGE
1541# define eio__mlock(a,b) ((errno = ENOSYS), -1)
1542#else
1543
1544static int
1545eio__mlock (void *addr, size_t length)
1546{
1547 eio_page_align (&addr, &length);
1548
1549 return mlock (addr, length);
1550}
1551
1552#endif
1553
1392#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) 1554#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1393# define eio__msync(a,b,c) ((errno = ENOSYS), -1) 1555# define eio__msync(a,b,c) ((errno = ENOSYS), -1)
1394#else 1556#else
1395 1557
1396int 1558static int
1397eio__msync (void *mem, size_t len, int flags) 1559eio__msync (void *mem, size_t len, int flags)
1398{ 1560{
1561 eio_page_align (&mem, &len);
1562
1399 if (EIO_MS_ASYNC != MS_SYNC 1563 if (EIO_MS_ASYNC != MS_SYNC
1400 || EIO_MS_INVALIDATE != MS_INVALIDATE 1564 || EIO_MS_INVALIDATE != MS_INVALIDATE
1401 || EIO_MS_SYNC != MS_SYNC) 1565 || EIO_MS_SYNC != MS_SYNC)
1402 { 1566 {
1403 flags = 0 1567 flags = 0
1409 return msync (mem, len, flags); 1573 return msync (mem, len, flags);
1410} 1574}
1411 1575
1412#endif 1576#endif
1413 1577
1414int 1578static int
1415eio__mtouch (void *mem, size_t len, int flags) 1579eio__mtouch (eio_req *req)
1416{ 1580{
1581 void *mem = req->ptr2;
1582 size_t len = req->size;
1583 int flags = req->int1;
1584
1585 eio_page_align (&mem, &len);
1586
1587 {
1417 intptr_t addr = (intptr_t)mem; 1588 intptr_t addr = (intptr_t)mem;
1418 intptr_t end = addr + len; 1589 intptr_t end = addr + len;
1419#ifdef PAGESIZE 1590 intptr_t page = eio_pagesize ();
1420 const intptr_t page = PAGESIZE;
1421#else
1422 static intptr_t page;
1423 1591
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) 1592 if (addr < end)
1432 if (flags & EIO_MT_MODIFY) /* modify */ 1593 if (flags & EIO_MT_MODIFY) /* modify */
1433 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len); 1594 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req));
1434 else 1595 else
1435 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len); 1596 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req));
1597 }
1436 1598
1437 return 0; 1599 return 0;
1438} 1600}
1439 1601
1440/*****************************************************************************/ 1602/*****************************************************************************/
1474 if (req) 1636 if (req)
1475 break; 1637 break;
1476 1638
1477 ++idle; 1639 ++idle;
1478 1640
1479 ts.tv_sec = time (0) + IDLE_TIMEOUT; 1641 ts.tv_sec = time (0) + idle_timeout;
1480 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) 1642 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
1481 { 1643 {
1482 if (idle > max_idle) 1644 if (idle > max_idle)
1483 { 1645 {
1484 --idle; 1646 --idle;
1527 return 0; 1689 return 0;
1528} 1690}
1529 1691
1530/*****************************************************************************/ 1692/*****************************************************************************/
1531 1693
1694int ecb_cold
1532int eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1695eio_init (void (*want_poll)(void), void (*done_poll)(void))
1533{ 1696{
1534 return etp_init (want_poll, done_poll); 1697 return etp_init (want_poll, done_poll);
1535} 1698}
1536 1699
1700ECB_INLINE void
1537static void eio_api_destroy (eio_req *req) 1701eio_api_destroy (eio_req *req)
1538{ 1702{
1539 free (req); 1703 free (req);
1540} 1704}
1541 1705
1542#define REQ(rtype) \ 1706#define REQ(rtype) \
1561 { \ 1725 { \
1562 eio_api_destroy (req); \ 1726 eio_api_destroy (req); \
1563 return 0; \ 1727 return 0; \
1564 } 1728 }
1565 1729
1730static void
1566static void eio_execute (etp_worker *self, eio_req *req) 1731eio_execute (etp_worker *self, eio_req *req)
1567{ 1732{
1568 errno = 0;
1569
1570 switch (req->type) 1733 switch (req->type)
1571 { 1734 {
1572 case EIO_READ: ALLOC (req->size); 1735 case EIO_READ: ALLOC (req->size);
1573 req->result = req->offs >= 0 1736 req->result = req->offs >= 0
1574 ? pread (req->int1, req->ptr2, req->size, req->offs) 1737 ? pread (req->int1, req->ptr2, req->size, req->offs)
1606 case EIO_RMDIR: req->result = rmdir (req->ptr1); break; 1769 case EIO_RMDIR: req->result = rmdir (req->ptr1); break;
1607 case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break; 1770 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; 1771 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break;
1609 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; 1772 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break;
1610 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; 1773 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; 1774 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
1612 1775
1613 case EIO_READLINK: ALLOC (NAME_MAX); 1776 case EIO_READLINK: ALLOC (PATH_MAX);
1614 req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break; 1777 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break;
1615 1778
1616 case EIO_SYNC: req->result = 0; sync (); break; 1779 case EIO_SYNC: req->result = 0; sync (); break;
1617 case EIO_FSYNC: req->result = fsync (req->int1); break; 1780 case EIO_FSYNC: req->result = fsync (req->int1); break;
1618 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; 1781 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
1619 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; 1782 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break;
1783 case EIO_MTOUCH: req->result = eio__mtouch (req); break;
1620 case EIO_MTOUCH: req->result = eio__mtouch (req->ptr2, req->size, req->int1); break; 1784 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break;
1785 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; 1786 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break;
1622 1787
1623 case EIO_READDIR: eio__scandir (req, self); break; 1788 case EIO_READDIR: eio__scandir (req, self); break;
1624 1789
1625 case EIO_BUSY: 1790 case EIO_BUSY:
1626#ifdef _WIN32 1791#ifdef _WIN32
1627 Sleep (req->nv1 * 1000.); 1792 Sleep (req->nv1 * 1e3);
1628#else 1793#else
1629 { 1794 {
1630 struct timeval tv; 1795 struct timeval tv;
1631 1796
1632 tv.tv_sec = req->nv1; 1797 tv.tv_sec = req->nv1;
1633 tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; 1798 tv.tv_usec = (req->nv1 - tv.tv_sec) * 1e6;
1634 1799
1635 req->result = select (0, 0, 0, 0, &tv); 1800 req->result = select (0, 0, 0, 0, &tv);
1636 } 1801 }
1637#endif 1802#endif
1638 break; 1803 break;
1653 times = tv; 1818 times = tv;
1654 } 1819 }
1655 else 1820 else
1656 times = 0; 1821 times = 0;
1657 1822
1658
1659 req->result = req->type == EIO_FUTIME 1823 req->result = req->type == EIO_FUTIME
1660 ? futimes (req->int1, times) 1824 ? futimes (req->int1, times)
1661 : utimes (req->ptr1, times); 1825 : utimes (req->ptr1, times);
1662 } 1826 }
1663 break; 1827 break;
1668 case EIO_NOP: 1832 case EIO_NOP:
1669 req->result = 0; 1833 req->result = 0;
1670 break; 1834 break;
1671 1835
1672 case EIO_CUSTOM: 1836 case EIO_CUSTOM:
1673 ((void (*)(eio_req *))req->feed) (req); 1837 req->feed (req);
1674 break; 1838 break;
1675 1839
1676 default: 1840 default:
1841 errno = ENOSYS;
1677 req->result = -1; 1842 req->result = -1;
1678 break; 1843 break;
1679 } 1844 }
1680 1845
1681 req->errorno = errno; 1846 req->errorno = errno;
1711eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) 1876eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data)
1712{ 1877{
1713 REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; 1878 REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND;
1714} 1879}
1715 1880
1881eio_req *eio_mlock (void *addr, size_t length, int pri, eio_cb cb, void *data)
1882{
1883 REQ (EIO_MLOCK); req->ptr2 = addr; req->size = length; SEND;
1884}
1885
1886eio_req *eio_mlockall (int flags, int pri, eio_cb cb, void *data)
1887{
1888 REQ (EIO_MLOCKALL); req->int1 = flags; SEND;
1889}
1890
1716eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) 1891eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data)
1717{ 1892{
1718 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; 1893 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND;
1719} 1894}
1720 1895
1854 REQ (EIO_READDIR); PATH; req->int1 = flags; SEND; 2029 REQ (EIO_READDIR); PATH; req->int1 = flags; SEND;
1855} 2030}
1856 2031
1857eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) 2032eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data)
1858{ 2033{
1859 REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND; 2034 REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->offs = (off_t)dev; SEND;
1860} 2035}
1861 2036
1862static eio_req * 2037static eio_req *
1863eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data) 2038eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1864{ 2039{
1888eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) 2063eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1889{ 2064{
1890 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); 2065 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
1891} 2066}
1892 2067
1893eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) 2068eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data)
1894{ 2069{
1895 REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND; 2070 REQ (EIO_CUSTOM); req->feed = execute; SEND;
1896} 2071}
1897 2072
1898#endif 2073#endif
1899 2074
1900eio_req *eio_grp (eio_cb cb, void *data) 2075eio_req *eio_grp (eio_cb cb, void *data)
1909#undef SEND 2084#undef SEND
1910 2085
1911/*****************************************************************************/ 2086/*****************************************************************************/
1912/* grp functions */ 2087/* grp functions */
1913 2088
2089void
1914void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) 2090eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit)
1915{ 2091{
1916 grp->int2 = limit; 2092 grp->int2 = limit;
1917 grp->feed = feed; 2093 grp->feed = feed;
1918 2094
1919 grp_try_feed (grp); 2095 grp_try_feed (grp);
1920} 2096}
1921 2097
2098void
1922void eio_grp_limit (eio_req *grp, int limit) 2099eio_grp_limit (eio_req *grp, int limit)
1923{ 2100{
1924 grp->int2 = limit; 2101 grp->int2 = limit;
1925 2102
1926 grp_try_feed (grp); 2103 grp_try_feed (grp);
1927} 2104}
1928 2105
2106void
1929void eio_grp_add (eio_req *grp, eio_req *req) 2107eio_grp_add (eio_req *grp, eio_req *req)
1930{ 2108{
1931 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); 2109 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
1932 2110
1933 grp->flags |= EIO_FLAG_GROUPADD; 2111 grp->flags |= EIO_FLAG_GROUPADD;
1934 2112
1945} 2123}
1946 2124
1947/*****************************************************************************/ 2125/*****************************************************************************/
1948/* misc garbage */ 2126/* misc garbage */
1949 2127
2128ssize_t
1950ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) 2129eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
1951{ 2130{
1952 etp_worker wrk; 2131 etp_worker wrk;
1953 ssize_t ret; 2132 ssize_t ret;
1954 2133
1955 wrk.dbuf = 0; 2134 wrk.dbuf = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines