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

Comparing libeio/eio.c (file contents):
Revision 1.54 by root, Tue Feb 23 14:32:45 2010 UTC vs.
Revision 1.74 by root, Mon Jun 20 07:28: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
140#endif 150#endif
141 151
155 errno = ENOMEM; \ 165 errno = ENOMEM; \
156 if (!eio_buf) \ 166 if (!eio_buf) \
157 return -1; 167 return -1;
158 168
159#define EIO_TICKS ((1000000 + 1023) >> 10) 169#define EIO_TICKS ((1000000 + 1023) >> 10)
160
161/*****************************************************************************/
162
163#if __GNUC__ >= 3
164# define expect(expr,value) __builtin_expect ((expr),(value))
165#else
166# define expect(expr,value) (expr)
167#endif
168
169#define expect_false(expr) expect ((expr) != 0, 0)
170#define expect_true(expr) expect ((expr) != 0, 1)
171
172/*****************************************************************************/
173 170
174#define ETP_PRI_MIN EIO_PRI_MIN 171#define ETP_PRI_MIN EIO_PRI_MIN
175#define ETP_PRI_MAX EIO_PRI_MAX 172#define ETP_PRI_MAX EIO_PRI_MAX
176 173
177struct etp_worker; 174struct etp_worker;
203/*****************************************************************************/ 200/*****************************************************************************/
204 201
205#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 202#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
206 203
207/* calculate time difference in ~1/EIO_TICKS of a second */ 204/* calculate time difference in ~1/EIO_TICKS of a second */
205ecb_inline int
208static int tvdiff (struct timeval *tv1, struct timeval *tv2) 206tvdiff (struct timeval *tv1, struct timeval *tv2)
209{ 207{
210 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS 208 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
211 + ((tv2->tv_usec - tv1->tv_usec) >> 10); 209 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
212} 210}
213 211
220static unsigned int max_poll_reqs; /* reslock */ 218static unsigned int max_poll_reqs; /* reslock */
221 219
222static volatile unsigned int nreqs; /* reqlock */ 220static volatile unsigned int nreqs; /* reqlock */
223static volatile unsigned int nready; /* reqlock */ 221static volatile unsigned int nready; /* reqlock */
224static volatile unsigned int npending; /* reqlock */ 222static volatile unsigned int npending; /* reqlock */
225static 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 */
226 225
227static mutex_t wrklock = X_MUTEX_INIT; 226static xmutex_t wrklock;
228static mutex_t reslock = X_MUTEX_INIT; 227static xmutex_t reslock;
229static mutex_t reqlock = X_MUTEX_INIT; 228static xmutex_t reqlock;
230static cond_t reqwait = X_COND_INIT; 229static xcond_t reqwait;
231 230
232#if !HAVE_PREADWRITE 231#if !HAVE_PREADWRITE
233/* 232/*
234 * make our pread/pwrite emulation safe against themselves, but not against 233 * make our pread/pwrite emulation safe against themselves, but not against
235 * 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,
236 * but that's your problem, not mine. 235 * but that's your problem, not mine.
237 */ 236 */
238static mutex_t preadwritelock = X_MUTEX_INIT; 237static xmutex_t preadwritelock = X_MUTEX_INIT;
239#endif 238#endif
240 239
241typedef struct etp_worker 240typedef struct etp_worker
242{ 241{
243 /* locked by wrklock */ 242 /* locked by wrklock */
244 struct etp_worker *prev, *next; 243 struct etp_worker *prev, *next;
245 244
246 thread_t tid; 245 xthread_t tid;
247 246
248 /* locked by reslock, reqlock or wrklock */ 247 /* locked by reslock, reqlock or wrklock */
249 ETP_REQ *req; /* currently processed request */ 248 ETP_REQ *req; /* currently processed request */
250 249
251 ETP_WORKER_COMMON 250 ETP_WORKER_COMMON
256#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) 255#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
257#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) 256#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
258 257
259/* worker threads management */ 258/* worker threads management */
260 259
260static void ecb_cold
261static void etp_worker_clear (etp_worker *wrk) 261etp_worker_clear (etp_worker *wrk)
262{ 262{
263 ETP_WORKER_CLEAR (wrk); 263 ETP_WORKER_CLEAR (wrk);
264} 264}
265 265
266static void ecb_cold
266static void etp_worker_free (etp_worker *wrk) 267etp_worker_free (etp_worker *wrk)
267{ 268{
268 wrk->next->prev = wrk->prev; 269 wrk->next->prev = wrk->prev;
269 wrk->prev->next = wrk->next; 270 wrk->prev->next = wrk->next;
270 271
271 free (wrk); 272 free (wrk);
272} 273}
273 274
274static unsigned int etp_nreqs (void) 275static unsigned int
276etp_nreqs (void)
275{ 277{
276 int retval; 278 int retval;
277 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 279 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
278 retval = nreqs; 280 retval = nreqs;
279 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 281 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
280 return retval; 282 return retval;
281} 283}
282 284
283static unsigned int etp_nready (void) 285static unsigned int
286etp_nready (void)
284{ 287{
285 unsigned int retval; 288 unsigned int retval;
286 289
287 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 290 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
288 retval = nready; 291 retval = nready;
289 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 292 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
290 293
291 return retval; 294 return retval;
292} 295}
293 296
294static unsigned int etp_npending (void) 297static unsigned int
298etp_npending (void)
295{ 299{
296 unsigned int retval; 300 unsigned int retval;
297 301
298 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 302 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
299 retval = npending; 303 retval = npending;
300 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 304 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
301 305
302 return retval; 306 return retval;
303} 307}
304 308
305static unsigned int etp_nthreads (void) 309static unsigned int
310etp_nthreads (void)
306{ 311{
307 unsigned int retval; 312 unsigned int retval;
308 313
309 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 314 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
310 retval = started; 315 retval = started;
324} etp_reqq; 329} etp_reqq;
325 330
326static etp_reqq req_queue; 331static etp_reqq req_queue;
327static etp_reqq res_queue; 332static etp_reqq res_queue;
328 333
334static int ecb_noinline
329static int reqq_push (etp_reqq *q, ETP_REQ *req) 335reqq_push (etp_reqq *q, ETP_REQ *req)
330{ 336{
331 int pri = req->pri; 337 int pri = req->pri;
332 req->next = 0; 338 req->next = 0;
333 339
334 if (q->qe[pri]) 340 if (q->qe[pri])
340 q->qe[pri] = q->qs[pri] = req; 346 q->qe[pri] = q->qs[pri] = req;
341 347
342 return q->size++; 348 return q->size++;
343} 349}
344 350
351static ETP_REQ * ecb_noinline
345static ETP_REQ *reqq_shift (etp_reqq *q) 352reqq_shift (etp_reqq *q)
346{ 353{
347 int pri; 354 int pri;
348 355
349 if (!q->size) 356 if (!q->size)
350 return 0; 357 return 0;
365 } 372 }
366 373
367 abort (); 374 abort ();
368} 375}
369 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
370static void etp_atfork_prepare (void) 387etp_atfork_prepare (void)
371{ 388{
372 X_LOCK (wrklock); 389 X_LOCK (wrklock);
373 X_LOCK (reqlock); 390 X_LOCK (reqlock);
374 X_LOCK (reslock); 391 X_LOCK (reslock);
375#if !HAVE_PREADWRITE 392#if !HAVE_PREADWRITE
376 X_LOCK (preadwritelock); 393 X_LOCK (preadwritelock);
377#endif 394#endif
378} 395}
379 396
397static void ecb_cold
380static void etp_atfork_parent (void) 398etp_atfork_parent (void)
381{ 399{
382#if !HAVE_PREADWRITE 400#if !HAVE_PREADWRITE
383 X_UNLOCK (preadwritelock); 401 X_UNLOCK (preadwritelock);
384#endif 402#endif
385 X_UNLOCK (reslock); 403 X_UNLOCK (reslock);
386 X_UNLOCK (reqlock); 404 X_UNLOCK (reqlock);
387 X_UNLOCK (wrklock); 405 X_UNLOCK (wrklock);
388} 406}
389 407
408static void ecb_cold
390static void etp_atfork_child (void) 409etp_atfork_child (void)
391{ 410{
392 ETP_REQ *prv; 411 ETP_REQ *prv;
393 412
394 while ((prv = reqq_shift (&req_queue))) 413 while ((prv = reqq_shift (&req_queue)))
395 ETP_DESTROY (prv); 414 ETP_DESTROY (prv);
412 idle = 0; 431 idle = 0;
413 nreqs = 0; 432 nreqs = 0;
414 nready = 0; 433 nready = 0;
415 npending = 0; 434 npending = 0;
416 435
417 etp_atfork_parent (); 436 etp_thread_init ();
418} 437}
419 438
420static void 439static void ecb_cold
421etp_once_init (void) 440etp_once_init (void)
422{ 441{
442 etp_thread_init ();
423 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);
424} 444}
425 445
426static int 446static int ecb_cold
427etp_init (void (*want_poll)(void), void (*done_poll)(void)) 447etp_init (void (*want_poll)(void), void (*done_poll)(void))
428{ 448{
429 static pthread_once_t doinit = PTHREAD_ONCE_INIT; 449 static pthread_once_t doinit = PTHREAD_ONCE_INIT;
430 450
431 pthread_once (&doinit, etp_once_init); 451 pthread_once (&doinit, etp_once_init);
436 return 0; 456 return 0;
437} 457}
438 458
439X_THREAD_PROC (etp_proc); 459X_THREAD_PROC (etp_proc);
440 460
461static void ecb_cold
441static void etp_start_thread (void) 462etp_start_thread (void)
442{ 463{
443 etp_worker *wrk = calloc (1, sizeof (etp_worker)); 464 etp_worker *wrk = calloc (1, sizeof (etp_worker));
444 465
445 /*TODO*/ 466 /*TODO*/
446 assert (("unable to allocate worker thread data", wrk)); 467 assert (("unable to allocate worker thread data", wrk));
459 free (wrk); 480 free (wrk);
460 481
461 X_UNLOCK (wrklock); 482 X_UNLOCK (wrklock);
462} 483}
463 484
485static void
464static void etp_maybe_start_thread (void) 486etp_maybe_start_thread (void)
465{ 487{
466 if (expect_true (etp_nthreads () >= wanted)) 488 if (ecb_expect_true (etp_nthreads () >= wanted))
467 return; 489 return;
468 490
469 /* todo: maybe use idle here, but might be less exact */ 491 /* todo: maybe use idle here, but might be less exact */
470 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 ()))
471 return; 493 return;
472 494
473 etp_start_thread (); 495 etp_start_thread ();
474} 496}
475 497
498static void ecb_cold
476static void etp_end_thread (void) 499etp_end_thread (void)
477{ 500{
478 eio_req *req = calloc (1, sizeof (eio_req)); 501 eio_req *req = calloc (1, sizeof (eio_req));
479 502
480 req->type = -1; 503 req->type = -1;
481 req->pri = ETP_PRI_MAX - ETP_PRI_MIN; 504 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
488 X_LOCK (wrklock); 511 X_LOCK (wrklock);
489 --started; 512 --started;
490 X_UNLOCK (wrklock); 513 X_UNLOCK (wrklock);
491} 514}
492 515
493static int etp_poll (void) 516static int
517etp_poll (void)
494{ 518{
495 unsigned int maxreqs; 519 unsigned int maxreqs;
496 unsigned int maxtime; 520 unsigned int maxtime;
497 struct timeval tv_start, tv_now; 521 struct timeval tv_start, tv_now;
498 522
528 552
529 X_LOCK (reqlock); 553 X_LOCK (reqlock);
530 --nreqs; 554 --nreqs;
531 X_UNLOCK (reqlock); 555 X_UNLOCK (reqlock);
532 556
533 if (expect_false (req->type == EIO_GROUP && req->size)) 557 if (ecb_expect_false (req->type == EIO_GROUP && req->size))
534 { 558 {
535 req->int1 = 1; /* mark request as delayed */ 559 req->int1 = 1; /* mark request as delayed */
536 continue; 560 continue;
537 } 561 }
538 else 562 else
539 { 563 {
540 int res = ETP_FINISH (req); 564 int res = ETP_FINISH (req);
541 if (expect_false (res)) 565 if (ecb_expect_false (res))
542 return res; 566 return res;
543 } 567 }
544 568
545 if (expect_false (maxreqs && !--maxreqs)) 569 if (ecb_expect_false (maxreqs && !--maxreqs))
546 break; 570 break;
547 571
548 if (maxtime) 572 if (maxtime)
549 { 573 {
550 gettimeofday (&tv_now, 0); 574 gettimeofday (&tv_now, 0);
556 580
557 errno = EAGAIN; 581 errno = EAGAIN;
558 return -1; 582 return -1;
559} 583}
560 584
585static void
561static void etp_cancel (ETP_REQ *req) 586etp_cancel (ETP_REQ *req)
562{ 587{
563 X_LOCK (wrklock); 588 X_LOCK (wrklock);
564 req->flags |= EIO_FLAG_CANCELLED; 589 req->flags |= EIO_FLAG_CANCELLED;
565 X_UNLOCK (wrklock); 590 X_UNLOCK (wrklock);
566 591
567 eio_grp_cancel (req); 592 eio_grp_cancel (req);
568} 593}
569 594
595static void
570static void etp_submit (ETP_REQ *req) 596etp_submit (ETP_REQ *req)
571{ 597{
572 req->pri -= ETP_PRI_MIN; 598 req->pri -= ETP_PRI_MIN;
573 599
574 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;
575 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;
576 602
577 if (expect_false (req->type == EIO_GROUP)) 603 if (ecb_expect_false (req->type == EIO_GROUP))
578 { 604 {
579 /* I hope this is worth it :/ */ 605 /* I hope this is worth it :/ */
580 X_LOCK (reqlock); 606 X_LOCK (reqlock);
581 ++nreqs; 607 ++nreqs;
582 X_UNLOCK (reqlock); 608 X_UNLOCK (reqlock);
601 627
602 etp_maybe_start_thread (); 628 etp_maybe_start_thread ();
603 } 629 }
604} 630}
605 631
632static void ecb_cold
606static void etp_set_max_poll_time (double nseconds) 633etp_set_max_poll_time (double nseconds)
607{ 634{
608 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 635 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
609 max_poll_time = nseconds * EIO_TICKS; 636 max_poll_time = nseconds * EIO_TICKS;
610 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); 637 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
611} 638}
612 639
640static void ecb_cold
613static void etp_set_max_poll_reqs (unsigned int maxreqs) 641etp_set_max_poll_reqs (unsigned int maxreqs)
614{ 642{
615 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 643 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
616 max_poll_reqs = maxreqs; 644 max_poll_reqs = maxreqs;
617 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); 645 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
618} 646}
619 647
648static void ecb_cold
620static void etp_set_max_idle (unsigned int nthreads) 649etp_set_max_idle (unsigned int nthreads)
621{ 650{
622 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 651 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
623 max_idle = nthreads <= 0 ? 1 : nthreads; 652 max_idle = nthreads;
624 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 653 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
625} 654}
626 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
627static void etp_set_min_parallel (unsigned int nthreads) 665etp_set_min_parallel (unsigned int nthreads)
628{ 666{
629 if (wanted < nthreads) 667 if (wanted < nthreads)
630 wanted = nthreads; 668 wanted = nthreads;
631} 669}
632 670
671static void ecb_cold
633static void etp_set_max_parallel (unsigned int nthreads) 672etp_set_max_parallel (unsigned int nthreads)
634{ 673{
635 if (wanted > nthreads) 674 if (wanted > nthreads)
636 wanted = nthreads; 675 wanted = nthreads;
637 676
638 while (started > wanted) 677 while (started > wanted)
639 etp_end_thread (); 678 etp_end_thread ();
640} 679}
641 680
642/*****************************************************************************/ 681/*****************************************************************************/
643 682
683static void
644static void grp_try_feed (eio_req *grp) 684grp_try_feed (eio_req *grp)
645{ 685{
646 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 686 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
647 { 687 {
648 grp->flags &= ~EIO_FLAG_GROUPADD; 688 grp->flags &= ~EIO_FLAG_GROUPADD;
649 689
656 break; 696 break;
657 } 697 }
658 } 698 }
659} 699}
660 700
701static int
661static int grp_dec (eio_req *grp) 702grp_dec (eio_req *grp)
662{ 703{
663 --grp->size; 704 --grp->size;
664 705
665 /* call feeder, if applicable */ 706 /* call feeder, if applicable */
666 grp_try_feed (grp); 707 grp_try_feed (grp);
670 return eio_finish (grp); 711 return eio_finish (grp);
671 else 712 else
672 return 0; 713 return 0;
673} 714}
674 715
716void
675void eio_destroy (eio_req *req) 717eio_destroy (eio_req *req)
676{ 718{
677 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); 719 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1);
678 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); 720 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2);
679 721
680 EIO_DESTROY (req); 722 EIO_DESTROY (req);
681} 723}
682 724
725static int
683static int eio_finish (eio_req *req) 726eio_finish (eio_req *req)
684{ 727{
685 int res = EIO_FINISH (req); 728 int res = EIO_FINISH (req);
686 729
687 if (req->grp) 730 if (req->grp)
688 { 731 {
705 eio_destroy (req); 748 eio_destroy (req);
706 749
707 return res; 750 return res;
708} 751}
709 752
753void
710void eio_grp_cancel (eio_req *grp) 754eio_grp_cancel (eio_req *grp)
711{ 755{
712 for (grp = grp->grp_first; grp; grp = grp->grp_next) 756 for (grp = grp->grp_first; grp; grp = grp->grp_next)
713 eio_cancel (grp); 757 eio_cancel (grp);
714} 758}
715 759
760void
716void eio_cancel (eio_req *req) 761eio_cancel (eio_req *req)
717{ 762{
718 etp_cancel (req); 763 etp_cancel (req);
719} 764}
720 765
766void
721void eio_submit (eio_req *req) 767eio_submit (eio_req *req)
722{ 768{
723 etp_submit (req); 769 etp_submit (req);
724} 770}
725 771
726unsigned int eio_nreqs (void) 772unsigned int
773eio_nreqs (void)
727{ 774{
728 return etp_nreqs (); 775 return etp_nreqs ();
729} 776}
730 777
731unsigned int eio_nready (void) 778unsigned int
779eio_nready (void)
732{ 780{
733 return etp_nready (); 781 return etp_nready ();
734} 782}
735 783
736unsigned int eio_npending (void) 784unsigned int
785eio_npending (void)
737{ 786{
738 return etp_npending (); 787 return etp_npending ();
739} 788}
740 789
741unsigned int eio_nthreads (void) 790unsigned int ecb_cold
791eio_nthreads (void)
742{ 792{
743 return etp_nthreads (); 793 return etp_nthreads ();
744} 794}
745 795
796void ecb_cold
746void eio_set_max_poll_time (double nseconds) 797eio_set_max_poll_time (double nseconds)
747{ 798{
748 etp_set_max_poll_time (nseconds); 799 etp_set_max_poll_time (nseconds);
749} 800}
750 801
802void ecb_cold
751void eio_set_max_poll_reqs (unsigned int maxreqs) 803eio_set_max_poll_reqs (unsigned int maxreqs)
752{ 804{
753 etp_set_max_poll_reqs (maxreqs); 805 etp_set_max_poll_reqs (maxreqs);
754} 806}
755 807
808void ecb_cold
756void eio_set_max_idle (unsigned int nthreads) 809eio_set_max_idle (unsigned int nthreads)
757{ 810{
758 etp_set_max_idle (nthreads); 811 etp_set_max_idle (nthreads);
759} 812}
760 813
814void ecb_cold
815eio_set_idle_timeout (unsigned int seconds)
816{
817 etp_set_idle_timeout (seconds);
818}
819
820void ecb_cold
761void eio_set_min_parallel (unsigned int nthreads) 821eio_set_min_parallel (unsigned int nthreads)
762{ 822{
763 etp_set_min_parallel (nthreads); 823 etp_set_min_parallel (nthreads);
764} 824}
765 825
826void ecb_cold
766void eio_set_max_parallel (unsigned int nthreads) 827eio_set_max_parallel (unsigned int nthreads)
767{ 828{
768 etp_set_max_parallel (nthreads); 829 etp_set_max_parallel (nthreads);
769} 830}
770 831
771int eio_poll (void) 832int eio_poll (void)
813 874
814 return res; 875 return res;
815} 876}
816#endif 877#endif
817 878
818#ifndef HAVE_FUTIMES 879#ifndef HAVE_UTIMES
819 880
820# undef utimes 881# undef utimes
821# undef futimes
822# define utimes(path,times) eio__utimes (path, times) 882# define utimes(path,times) eio__utimes (path, times)
823# define futimes(fd,times) eio__futimes (fd, times)
824 883
825static int 884static int
826eio__utimes (const char *filename, const struct timeval times[2]) 885eio__utimes (const char *filename, const struct timeval times[2])
827{ 886{
828 if (times) 887 if (times)
836 } 895 }
837 else 896 else
838 return utime (filename, 0); 897 return utime (filename, 0);
839} 898}
840 899
900#endif
901
902#ifndef HAVE_FUTIMES
903
904# undef futimes
905# define futimes(fd,times) eio__futimes (fd, times)
906
907static int
841static int eio__futimes (int fd, const struct timeval tv[2]) 908eio__futimes (int fd, const struct timeval tv[2])
842{ 909{
843 errno = ENOSYS; 910 errno = ENOSYS;
844 return -1; 911 return -1;
845} 912}
846 913
850# undef fdatasync 917# undef fdatasync
851# define fdatasync(fd) fsync (fd) 918# define fdatasync(fd) fsync (fd)
852#endif 919#endif
853 920
854/* sync_file_range always needs emulation */ 921/* sync_file_range always needs emulation */
855int 922static int
856eio__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)
857{ 924{
858#if HAVE_SYNC_FILE_RANGE 925#if HAVE_SYNC_FILE_RANGE
859 int res; 926 int res;
860 927
906 973
907/* sendfile always needs emulation */ 974/* sendfile always needs emulation */
908static ssize_t 975static ssize_t
909eio__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)
910{ 977{
978 ssize_t written = 0;
911 ssize_t res; 979 ssize_t res;
912 980
913 if (!count) 981 if (!count)
914 return 0; 982 return 0;
915 983
984 for (;;)
985 {
916#if HAVE_SENDFILE 986#if HAVE_SENDFILE
917# if __linux 987# if __linux
988 off_t soffset = offset;
918 res = sendfile (ofd, ifd, &offset, count); 989 res = sendfile (ofd, ifd, &soffset, count);
919 990
920# elif __FreeBSD__ 991# elif __FreeBSD__
921 /* 992 /*
922 * 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
923 * wasted on making it similar to other I/O functions. 994 * wasted on making it similar to other I/O functions.
924 */ 995 */
925 {
926 off_t sbytes; 996 off_t sbytes;
927 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 997 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
928 998
929 #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 */
930 /* freebsd' sendfile will return 0 on success */ 1000 /* freebsd' sendfile will return 0 on success */
931 /* 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 */
932 /* not on e.g. EIO or EPIPE - sounds broken */ 1002 /* not on e.g. EIO or EPIPE - sounds broken */
933 if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0) 1003 if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0)
934 res = sbytes; 1004 res = sbytes;
935 #endif 1005 #endif
936 1006
937 /* according to source inspection, this is correct, and useful behaviour */ 1007 /* according to source inspection, this is correct, and useful behaviour */
938 if (sbytes) 1008 if (sbytes)
939 res = sbytes; 1009 res = sbytes;
940 }
941 1010
942# elif defined (__APPLE__) 1011# elif defined (__APPLE__)
943
944 {
945 off_t sbytes = count; 1012 off_t sbytes = count;
946 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); 1013 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0);
947 1014
948 /* according to the manpage, sbytes is always valid */ 1015 /* according to the manpage, sbytes is always valid */
949 if (sbytes) 1016 if (sbytes)
950 res = sbytes; 1017 res = sbytes;
951 }
952 1018
953# elif __hpux 1019# elif __hpux
954 res = sendfile (ofd, ifd, offset, count, 0, 0); 1020 res = sendfile (ofd, ifd, offset, count, 0, 0);
955 1021
956# elif __solaris 1022# elif __solaris
957 {
958 struct sendfilevec vec; 1023 struct sendfilevec vec;
959 size_t sbytes; 1024 size_t sbytes;
960 1025
961 vec.sfv_fd = ifd; 1026 vec.sfv_fd = ifd;
962 vec.sfv_flag = 0; 1027 vec.sfv_flag = 0;
963 vec.sfv_off = offset; 1028 vec.sfv_off = offset;
964 vec.sfv_len = count; 1029 vec.sfv_len = count;
965 1030
966 res = sendfilev (ofd, &vec, 1, &sbytes); 1031 res = sendfilev (ofd, &vec, 1, &sbytes);
967 1032
968 if (res < 0 && sbytes) 1033 if (res < 0 && sbytes)
969 res = sbytes; 1034 res = sbytes;
970 }
971 1035
972# endif 1036# endif
973 1037
974#elif defined (_WIN32) 1038#elif defined (_WIN32)
975
976 /* 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 */
977 { 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 */
978 HANDLE h = TO_SOCKET (ifd); 1043 HANDLE h = TO_SOCKET (ifd);
979 SetFilePointer (h, offset, 0, FILE_BEGIN); 1044 SetFilePointer (h, offset, 0, FILE_BEGIN);
980 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); 1045 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0);
981 }
982 1046
983#else 1047#else
984 res = -1; 1048 res = -1;
985 errno = ENOSYS; 1049 errno = ENOSYS;
986#endif 1050#endif
987 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
988 if (res < 0 1075 if (res < 0
989 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK 1076 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
990 /* BSDs */ 1077 /* BSDs */
991#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... */
992 || errno == ENOTSUP 1079 || errno == ENOTSUP
993#endif 1080#endif
1033} 1120}
1034 1121
1035static signed char 1122static signed char
1036eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1123eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1037{ 1124{
1038 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 */
1039 : 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;
1040} 1129}
1041 1130
1042#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
1043 1132
1044#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 */
1050 unsigned char bits [9 + sizeof (ino_t) * 8]; 1139 unsigned char bits [9 + sizeof (ino_t) * 8];
1051 unsigned char *bit = bits; 1140 unsigned char *bit = bits;
1052 1141
1053 assert (CHAR_BIT == 8); 1142 assert (CHAR_BIT == 8);
1054 assert (sizeof (eio_dirent) * 8 < 256); 1143 assert (sizeof (eio_dirent) * 8 < 256);
1055 assert (offsetof (eio_dirent, inode)); /* we use 0 as sentinel */ 1144 assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */
1056 assert (offsetof (eio_dirent, score)); /* we use 0 as sentinel */ 1145 assert (offsetof (eio_dirent, score)); /* we use bit #0 as sentinel */
1057 1146
1058 if (size <= EIO_SORT_FAST) 1147 if (size <= EIO_SORT_FAST)
1059 return; 1148 return;
1060 1149
1061 /* 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 */
1216 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); 1305 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER);
1217 1306
1218 X_LOCK (wrklock); 1307 X_LOCK (wrklock);
1219 /* the corresponding closedir is in ETP_WORKER_CLEAR */ 1308 /* the corresponding closedir is in ETP_WORKER_CLEAR */
1220 self->dirp = dirp = opendir (req->ptr1); 1309 self->dirp = dirp = opendir (req->ptr1);
1310
1311 if (req->flags & EIO_FLAG_PTR1_FREE)
1312 free (req->ptr1);
1313
1221 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; 1314 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE;
1222 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; 1315 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0;
1223 req->ptr2 = names = malloc (namesalloc); 1316 req->ptr2 = names = malloc (namesalloc);
1224 X_UNLOCK (wrklock); 1317 X_UNLOCK (wrklock);
1225 1318
1237 /* sort etc. */ 1330 /* sort etc. */
1238 req->int1 = flags; 1331 req->int1 = flags;
1239 req->result = dentoffs; 1332 req->result = dentoffs;
1240 1333
1241 if (flags & EIO_READDIR_STAT_ORDER) 1334 if (flags & EIO_READDIR_STAT_ORDER)
1242 eio_dent_sort (dents, dentoffs, 0, inode_bits); /* sort by inode exclusively */ 1335 eio_dent_sort (dents, dentoffs, flags & EIO_READDIR_DIRS_FIRST ? 7 : 0, inode_bits);
1243 else if (flags & EIO_READDIR_DIRS_FIRST) 1336 else if (flags & EIO_READDIR_DIRS_FIRST)
1244 if (flags & EIO_READDIR_FOUND_UNKNOWN) 1337 if (flags & EIO_READDIR_FOUND_UNKNOWN)
1245 eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */ 1338 eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */
1246 else 1339 else
1247 { 1340 {
1249 eio_dirent *oth = dents + dentoffs; 1342 eio_dirent *oth = dents + dentoffs;
1250 eio_dirent *dir = dents; 1343 eio_dirent *dir = dents;
1251 1344
1252 /* now partition dirs to the front, and non-dirs to the back */ 1345 /* now partition dirs to the front, and non-dirs to the back */
1253 /* by walking from both sides and swapping if necessary */ 1346 /* by walking from both sides and swapping if necessary */
1254 /* also clear score, so it doesn't influence sorting */
1255 while (oth > dir) 1347 while (oth > dir)
1256 { 1348 {
1257 if (dir->type == EIO_DT_DIR) 1349 if (dir->type == EIO_DT_DIR)
1258 ++dir; 1350 ++dir;
1259 else if ((--oth)->type == EIO_DT_DIR) 1351 else if ((--oth)->type == EIO_DT_DIR)
1262 1354
1263 ++dir; 1355 ++dir;
1264 } 1356 }
1265 } 1357 }
1266 1358
1267 /* now sort the dirs only */ 1359 /* now sort the dirs only (dirs all have the same score) */
1268 eio_dent_sort (dents, dir - dents, 0, inode_bits); 1360 eio_dent_sort (dents, dir - dents, 0, inode_bits);
1269 } 1361 }
1270 1362
1271 break; 1363 break;
1272 } 1364 }
1277 /* skip . and .. entries */ 1369 /* skip . and .. entries */
1278 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 1370 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
1279 { 1371 {
1280 int len = D_NAMLEN (entp) + 1; 1372 int len = D_NAMLEN (entp) + 1;
1281 1373
1282 while (expect_false (namesoffs + len > namesalloc)) 1374 while (ecb_expect_false (namesoffs + len > namesalloc))
1283 { 1375 {
1284 namesalloc *= 2; 1376 namesalloc *= 2;
1285 X_LOCK (wrklock); 1377 X_LOCK (wrklock);
1286 req->ptr2 = names = realloc (names, namesalloc); 1378 req->ptr2 = names = realloc (names, namesalloc);
1287 X_UNLOCK (wrklock); 1379 X_UNLOCK (wrklock);
1294 1386
1295 if (dents) 1387 if (dents)
1296 { 1388 {
1297 struct eio_dirent *ent; 1389 struct eio_dirent *ent;
1298 1390
1299 if (expect_false (dentoffs == dentalloc)) 1391 if (ecb_expect_false (dentoffs == dentalloc))
1300 { 1392 {
1301 dentalloc *= 2; 1393 dentalloc *= 2;
1302 X_LOCK (wrklock); 1394 X_LOCK (wrklock);
1303 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); 1395 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent));
1304 X_UNLOCK (wrklock); 1396 X_UNLOCK (wrklock);
1392 break; 1484 break;
1393 } 1485 }
1394 } 1486 }
1395} 1487}
1396 1488
1489#ifdef PAGESIZE
1490# define eio_pagesize() PAGESIZE
1491#else
1492static intptr_t
1493eio_pagesize (void)
1494{
1495 static intptr_t page;
1496
1497 if (!page)
1498 page = sysconf (_SC_PAGESIZE);
1499
1500 return page;
1501}
1502#endif
1503
1504static void
1505eio_page_align (void **addr, size_t *length)
1506{
1507 intptr_t mask = eio_pagesize () - 1;
1508
1509 /* round down addr */
1510 intptr_t adj = mask & (intptr_t)*addr;
1511
1512 *addr = (void *)((intptr_t)*addr - adj);
1513 *length += adj;
1514
1515 /* round up length */
1516 *length = (*length + mask) & ~mask;
1517}
1518
1519#if !_POSIX_MEMLOCK
1520# define eio__mlockall(a) ((errno = ENOSYS), -1)
1521#else
1522
1523static int
1524eio__mlockall (int flags)
1525{
1526 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1527 extern int mallopt (int, int);
1528 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1529 #endif
1530
1531 if (EIO_MCL_CURRENT != MCL_CURRENT
1532 || EIO_MCL_FUTURE != MCL_FUTURE)
1533 {
1534 flags = 0
1535 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0)
1536 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0);
1537 }
1538
1539 return mlockall (flags);
1540}
1541#endif
1542
1543#if !_POSIX_MEMLOCK_RANGE
1544# define eio__mlock(a,b) ((errno = ENOSYS), -1)
1545#else
1546
1547static int
1548eio__mlock (void *addr, size_t length)
1549{
1550 eio_page_align (&addr, &length);
1551
1552 return mlock (addr, length);
1553}
1554
1555#endif
1556
1397#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) 1557#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1398# define eio__msync(a,b,c) ((errno = ENOSYS), -1) 1558# define eio__msync(a,b,c) ((errno = ENOSYS), -1)
1399#else 1559#else
1400 1560
1401int 1561static int
1402eio__msync (void *mem, size_t len, int flags) 1562eio__msync (void *mem, size_t len, int flags)
1403{ 1563{
1564 eio_page_align (&mem, &len);
1565
1404 if (EIO_MS_ASYNC != MS_SYNC 1566 if (EIO_MS_ASYNC != MS_SYNC
1405 || EIO_MS_INVALIDATE != MS_INVALIDATE 1567 || EIO_MS_INVALIDATE != MS_INVALIDATE
1406 || EIO_MS_SYNC != MS_SYNC) 1568 || EIO_MS_SYNC != MS_SYNC)
1407 { 1569 {
1408 flags = 0 1570 flags = 0
1414 return msync (mem, len, flags); 1576 return msync (mem, len, flags);
1415} 1577}
1416 1578
1417#endif 1579#endif
1418 1580
1419int 1581static int
1420eio__mtouch (void *mem, size_t len, int flags) 1582eio__mtouch (eio_req *req)
1421{ 1583{
1584 void *mem = req->ptr2;
1585 size_t len = req->size;
1586 int flags = req->int1;
1587
1588 eio_page_align (&mem, &len);
1589
1590 {
1422 intptr_t addr = (intptr_t)mem; 1591 intptr_t addr = (intptr_t)mem;
1423 intptr_t end = addr + len; 1592 intptr_t end = addr + len;
1424#ifdef PAGESIZE 1593 intptr_t page = eio_pagesize ();
1425 const intptr_t page = PAGESIZE;
1426#else
1427 static intptr_t page;
1428 1594
1429 if (!page)
1430 page = sysconf (_SC_PAGESIZE);
1431#endif
1432
1433 /* round down to start of page, although this is probably useless */
1434 addr &= ~(page - 1); /* assume page size is always a power of two */
1435
1436 if (addr < end) 1595 if (addr < end)
1437 if (flags & EIO_MT_MODIFY) /* modify */ 1596 if (flags & EIO_MT_MODIFY) /* modify */
1438 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len); 1597 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req));
1439 else 1598 else
1440 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len); 1599 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req));
1600 }
1441 1601
1442 return 0; 1602 return 0;
1443} 1603}
1444 1604
1445/*****************************************************************************/ 1605/*****************************************************************************/
1479 if (req) 1639 if (req)
1480 break; 1640 break;
1481 1641
1482 ++idle; 1642 ++idle;
1483 1643
1484 ts.tv_sec = time (0) + IDLE_TIMEOUT; 1644 ts.tv_sec = time (0) + idle_timeout;
1485 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) 1645 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
1486 { 1646 {
1487 if (idle > max_idle) 1647 if (idle > max_idle)
1488 { 1648 {
1489 --idle; 1649 --idle;
1532 return 0; 1692 return 0;
1533} 1693}
1534 1694
1535/*****************************************************************************/ 1695/*****************************************************************************/
1536 1696
1697int ecb_cold
1537int eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1698eio_init (void (*want_poll)(void), void (*done_poll)(void))
1538{ 1699{
1539 return etp_init (want_poll, done_poll); 1700 return etp_init (want_poll, done_poll);
1540} 1701}
1541 1702
1703ecb_inline void
1542static void eio_api_destroy (eio_req *req) 1704eio_api_destroy (eio_req *req)
1543{ 1705{
1544 free (req); 1706 free (req);
1545} 1707}
1546 1708
1547#define REQ(rtype) \ 1709#define REQ(rtype) \
1566 { \ 1728 { \
1567 eio_api_destroy (req); \ 1729 eio_api_destroy (req); \
1568 return 0; \ 1730 return 0; \
1569 } 1731 }
1570 1732
1733static void
1571static void eio_execute (etp_worker *self, eio_req *req) 1734eio_execute (etp_worker *self, eio_req *req)
1572{ 1735{
1573 errno = 0;
1574
1575 switch (req->type) 1736 switch (req->type)
1576 { 1737 {
1577 case EIO_READ: ALLOC (req->size); 1738 case EIO_READ: ALLOC (req->size);
1578 req->result = req->offs >= 0 1739 req->result = req->offs >= 0
1579 ? pread (req->int1, req->ptr2, req->size, req->offs) 1740 ? pread (req->int1, req->ptr2, req->size, req->offs)
1611 case EIO_RMDIR: req->result = rmdir (req->ptr1); break; 1772 case EIO_RMDIR: req->result = rmdir (req->ptr1); break;
1612 case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break; 1773 case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break;
1613 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; 1774 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break;
1614 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; 1775 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break;
1615 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; 1776 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break;
1616 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->int3); break; 1777 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
1617 1778
1618 case EIO_READLINK: ALLOC (PATH_MAX); 1779 case EIO_READLINK: ALLOC (PATH_MAX);
1619 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break; 1780 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break;
1620 1781
1621 case EIO_SYNC: req->result = 0; sync (); break; 1782 case EIO_SYNC: req->result = 0; sync (); break;
1622 case EIO_FSYNC: req->result = fsync (req->int1); break; 1783 case EIO_FSYNC: req->result = fsync (req->int1); break;
1623 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; 1784 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
1624 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; 1785 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break;
1786 case EIO_MTOUCH: req->result = eio__mtouch (req); break;
1625 case EIO_MTOUCH: req->result = eio__mtouch (req->ptr2, req->size, req->int1); break; 1787 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break;
1788 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break;
1626 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; 1789 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break;
1627 1790
1628 case EIO_READDIR: eio__scandir (req, self); break; 1791 case EIO_READDIR: eio__scandir (req, self); break;
1629 1792
1630 case EIO_BUSY: 1793 case EIO_BUSY:
1631#ifdef _WIN32 1794#ifdef _WIN32
1632 Sleep (req->nv1 * 1000.); 1795 Sleep (req->nv1 * 1e3);
1633#else 1796#else
1634 { 1797 {
1635 struct timeval tv; 1798 struct timeval tv;
1636 1799
1637 tv.tv_sec = req->nv1; 1800 tv.tv_sec = req->nv1;
1638 tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; 1801 tv.tv_usec = (req->nv1 - tv.tv_sec) * 1e6;
1639 1802
1640 req->result = select (0, 0, 0, 0, &tv); 1803 req->result = select (0, 0, 0, 0, &tv);
1641 } 1804 }
1642#endif 1805#endif
1643 break; 1806 break;
1658 times = tv; 1821 times = tv;
1659 } 1822 }
1660 else 1823 else
1661 times = 0; 1824 times = 0;
1662 1825
1663
1664 req->result = req->type == EIO_FUTIME 1826 req->result = req->type == EIO_FUTIME
1665 ? futimes (req->int1, times) 1827 ? futimes (req->int1, times)
1666 : utimes (req->ptr1, times); 1828 : utimes (req->ptr1, times);
1667 } 1829 }
1668 break; 1830 break;
1673 case EIO_NOP: 1835 case EIO_NOP:
1674 req->result = 0; 1836 req->result = 0;
1675 break; 1837 break;
1676 1838
1677 case EIO_CUSTOM: 1839 case EIO_CUSTOM:
1678 ((void (*)(eio_req *))req->feed) (req); 1840 req->feed (req);
1679 break; 1841 break;
1680 1842
1681 default: 1843 default:
1844 errno = ENOSYS;
1682 req->result = -1; 1845 req->result = -1;
1683 break; 1846 break;
1684 } 1847 }
1685 1848
1686 req->errorno = errno; 1849 req->errorno = errno;
1716eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) 1879eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data)
1717{ 1880{
1718 REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; 1881 REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND;
1719} 1882}
1720 1883
1884eio_req *eio_mlock (void *addr, size_t length, int pri, eio_cb cb, void *data)
1885{
1886 REQ (EIO_MLOCK); req->ptr2 = addr; req->size = length; SEND;
1887}
1888
1889eio_req *eio_mlockall (int flags, int pri, eio_cb cb, void *data)
1890{
1891 REQ (EIO_MLOCKALL); req->int1 = flags; SEND;
1892}
1893
1721eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) 1894eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data)
1722{ 1895{
1723 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; 1896 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND;
1724} 1897}
1725 1898
1859 REQ (EIO_READDIR); PATH; req->int1 = flags; SEND; 2032 REQ (EIO_READDIR); PATH; req->int1 = flags; SEND;
1860} 2033}
1861 2034
1862eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) 2035eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data)
1863{ 2036{
1864 REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND; 2037 REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->offs = (off_t)dev; SEND;
1865} 2038}
1866 2039
1867static eio_req * 2040static eio_req *
1868eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data) 2041eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1869{ 2042{
1893eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) 2066eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1894{ 2067{
1895 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); 2068 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
1896} 2069}
1897 2070
1898eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) 2071eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data)
1899{ 2072{
1900 REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND; 2073 REQ (EIO_CUSTOM); req->feed = execute; SEND;
1901} 2074}
1902 2075
1903#endif 2076#endif
1904 2077
1905eio_req *eio_grp (eio_cb cb, void *data) 2078eio_req *eio_grp (eio_cb cb, void *data)
1914#undef SEND 2087#undef SEND
1915 2088
1916/*****************************************************************************/ 2089/*****************************************************************************/
1917/* grp functions */ 2090/* grp functions */
1918 2091
2092void
1919void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) 2093eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit)
1920{ 2094{
1921 grp->int2 = limit; 2095 grp->int2 = limit;
1922 grp->feed = feed; 2096 grp->feed = feed;
1923 2097
1924 grp_try_feed (grp); 2098 grp_try_feed (grp);
1925} 2099}
1926 2100
2101void
1927void eio_grp_limit (eio_req *grp, int limit) 2102eio_grp_limit (eio_req *grp, int limit)
1928{ 2103{
1929 grp->int2 = limit; 2104 grp->int2 = limit;
1930 2105
1931 grp_try_feed (grp); 2106 grp_try_feed (grp);
1932} 2107}
1933 2108
2109void
1934void eio_grp_add (eio_req *grp, eio_req *req) 2110eio_grp_add (eio_req *grp, eio_req *req)
1935{ 2111{
1936 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); 2112 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
1937 2113
1938 grp->flags |= EIO_FLAG_GROUPADD; 2114 grp->flags |= EIO_FLAG_GROUPADD;
1939 2115
1950} 2126}
1951 2127
1952/*****************************************************************************/ 2128/*****************************************************************************/
1953/* misc garbage */ 2129/* misc garbage */
1954 2130
2131ssize_t
1955ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) 2132eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
1956{ 2133{
1957 etp_worker wrk; 2134 etp_worker wrk;
1958 ssize_t ret; 2135 ssize_t ret;
1959 2136
1960 wrk.dbuf = 0; 2137 wrk.dbuf = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines