… | |
… | |
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 from stdint.h, says idiot openbsd coder */ |
|
|
66 | #if HAVE_STDINT_H |
|
|
67 | # include <stdint.h> |
|
|
68 | #endif |
|
|
69 | |
|
|
70 | #ifndef ECANCELED |
|
|
71 | # define ECANCELED EDOM |
|
|
72 | #endif |
|
|
73 | |
|
|
74 | static void eio_destroy (eio_req *req); |
|
|
75 | |
59 | #ifndef EIO_FINISH |
76 | #ifndef EIO_FINISH |
60 | # define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 |
77 | # define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 |
61 | #endif |
78 | #endif |
62 | |
79 | |
63 | #ifndef EIO_DESTROY |
80 | #ifndef EIO_DESTROY |
… | |
… | |
71 | #ifdef _WIN32 |
88 | #ifdef _WIN32 |
72 | |
89 | |
73 | /*doh*/ |
90 | /*doh*/ |
74 | #else |
91 | #else |
75 | |
92 | |
76 | # include "config.h" |
|
|
77 | # include <sys/time.h> |
93 | # include <sys/time.h> |
78 | # include <sys/select.h> |
94 | # include <sys/select.h> |
79 | # include <unistd.h> |
95 | # include <unistd.h> |
80 | # include <utime.h> |
96 | # include <utime.h> |
81 | # include <signal.h> |
97 | # include <signal.h> |
… | |
… | |
132 | #endif |
148 | #endif |
133 | #ifndef D_NAMLEN |
149 | #ifndef D_NAMLEN |
134 | # define D_NAMLEN(de) strlen ((de)->d_name) |
150 | # define D_NAMLEN(de) strlen ((de)->d_name) |
135 | #endif |
151 | #endif |
136 | |
152 | |
137 | /* number of seconds after which an idle threads exit */ |
|
|
138 | #define IDLE_TIMEOUT 10 |
|
|
139 | |
|
|
140 | /* used for struct dirent, AIX doesn't provide it */ |
153 | /* used for struct dirent, AIX doesn't provide it */ |
141 | #ifndef NAME_MAX |
154 | #ifndef NAME_MAX |
142 | # define NAME_MAX 4096 |
155 | # define NAME_MAX 4096 |
143 | #endif |
156 | #endif |
144 | |
157 | |
… | |
… | |
158 | errno = ENOMEM; \ |
171 | errno = ENOMEM; \ |
159 | if (!eio_buf) \ |
172 | if (!eio_buf) \ |
160 | return -1; |
173 | return -1; |
161 | |
174 | |
162 | #define EIO_TICKS ((1000000 + 1023) >> 10) |
175 | #define EIO_TICKS ((1000000 + 1023) >> 10) |
163 | |
|
|
164 | /*****************************************************************************/ |
|
|
165 | |
|
|
166 | #if __GNUC__ >= 3 |
|
|
167 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
168 | #else |
|
|
169 | # define expect(expr,value) (expr) |
|
|
170 | #endif |
|
|
171 | |
|
|
172 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
173 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
174 | |
|
|
175 | /*****************************************************************************/ |
|
|
176 | |
176 | |
177 | #define ETP_PRI_MIN EIO_PRI_MIN |
177 | #define ETP_PRI_MIN EIO_PRI_MIN |
178 | #define ETP_PRI_MAX EIO_PRI_MAX |
178 | #define ETP_PRI_MAX EIO_PRI_MAX |
179 | |
179 | |
180 | struct etp_worker; |
180 | struct etp_worker; |
… | |
… | |
206 | /*****************************************************************************/ |
206 | /*****************************************************************************/ |
207 | |
207 | |
208 | #define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) |
208 | #define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) |
209 | |
209 | |
210 | /* calculate time difference in ~1/EIO_TICKS of a second */ |
210 | /* calculate time difference in ~1/EIO_TICKS of a second */ |
|
|
211 | ecb_inline int |
211 | static int tvdiff (struct timeval *tv1, struct timeval *tv2) |
212 | tvdiff (struct timeval *tv1, struct timeval *tv2) |
212 | { |
213 | { |
213 | return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS |
214 | return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS |
214 | + ((tv2->tv_usec - tv1->tv_usec) >> 10); |
215 | + ((tv2->tv_usec - tv1->tv_usec) >> 10); |
215 | } |
216 | } |
216 | |
217 | |
… | |
… | |
223 | static unsigned int max_poll_reqs; /* reslock */ |
224 | static unsigned int max_poll_reqs; /* reslock */ |
224 | |
225 | |
225 | static volatile unsigned int nreqs; /* reqlock */ |
226 | static volatile unsigned int nreqs; /* reqlock */ |
226 | static volatile unsigned int nready; /* reqlock */ |
227 | static volatile unsigned int nready; /* reqlock */ |
227 | static volatile unsigned int npending; /* reqlock */ |
228 | static volatile unsigned int npending; /* reqlock */ |
228 | static volatile unsigned int max_idle = 4; |
229 | static volatile unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */ |
|
|
230 | static volatile unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */ |
229 | |
231 | |
230 | static xmutex_t wrklock = X_MUTEX_INIT; |
232 | static xmutex_t wrklock; |
231 | static xmutex_t reslock = X_MUTEX_INIT; |
233 | static xmutex_t reslock; |
232 | static xmutex_t reqlock = X_MUTEX_INIT; |
234 | static xmutex_t reqlock; |
233 | static xcond_t reqwait = X_COND_INIT; |
235 | static xcond_t reqwait; |
234 | |
236 | |
235 | #if !HAVE_PREADWRITE |
237 | #if !HAVE_PREADWRITE |
236 | /* |
238 | /* |
237 | * make our pread/pwrite emulation safe against themselves, but not against |
239 | * make our pread/pwrite emulation safe against themselves, but not against |
238 | * normal read/write by using a mutex. slows down execution a lot, |
240 | * normal read/write by using a mutex. slows down execution a lot, |
… | |
… | |
259 | #define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) |
261 | #define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) |
260 | #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) |
262 | #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) |
261 | |
263 | |
262 | /* worker threads management */ |
264 | /* worker threads management */ |
263 | |
265 | |
|
|
266 | static void ecb_cold |
264 | static void etp_worker_clear (etp_worker *wrk) |
267 | etp_worker_clear (etp_worker *wrk) |
265 | { |
268 | { |
266 | ETP_WORKER_CLEAR (wrk); |
269 | ETP_WORKER_CLEAR (wrk); |
267 | } |
270 | } |
268 | |
271 | |
|
|
272 | static void ecb_cold |
269 | static void etp_worker_free (etp_worker *wrk) |
273 | etp_worker_free (etp_worker *wrk) |
270 | { |
274 | { |
271 | wrk->next->prev = wrk->prev; |
275 | wrk->next->prev = wrk->prev; |
272 | wrk->prev->next = wrk->next; |
276 | wrk->prev->next = wrk->next; |
273 | |
277 | |
274 | free (wrk); |
278 | free (wrk); |
275 | } |
279 | } |
276 | |
280 | |
277 | static unsigned int etp_nreqs (void) |
281 | static unsigned int |
|
|
282 | etp_nreqs (void) |
278 | { |
283 | { |
279 | int retval; |
284 | int retval; |
280 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
285 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
281 | retval = nreqs; |
286 | retval = nreqs; |
282 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
287 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
283 | return retval; |
288 | return retval; |
284 | } |
289 | } |
285 | |
290 | |
286 | static unsigned int etp_nready (void) |
291 | static unsigned int |
|
|
292 | etp_nready (void) |
287 | { |
293 | { |
288 | unsigned int retval; |
294 | unsigned int retval; |
289 | |
295 | |
290 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
296 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
291 | retval = nready; |
297 | retval = nready; |
292 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
298 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
293 | |
299 | |
294 | return retval; |
300 | return retval; |
295 | } |
301 | } |
296 | |
302 | |
297 | static unsigned int etp_npending (void) |
303 | static unsigned int |
|
|
304 | etp_npending (void) |
298 | { |
305 | { |
299 | unsigned int retval; |
306 | unsigned int retval; |
300 | |
307 | |
301 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
308 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
302 | retval = npending; |
309 | retval = npending; |
303 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
310 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
304 | |
311 | |
305 | return retval; |
312 | return retval; |
306 | } |
313 | } |
307 | |
314 | |
308 | static unsigned int etp_nthreads (void) |
315 | static unsigned int |
|
|
316 | etp_nthreads (void) |
309 | { |
317 | { |
310 | unsigned int retval; |
318 | unsigned int retval; |
311 | |
319 | |
312 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
320 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
313 | retval = started; |
321 | retval = started; |
… | |
… | |
327 | } etp_reqq; |
335 | } etp_reqq; |
328 | |
336 | |
329 | static etp_reqq req_queue; |
337 | static etp_reqq req_queue; |
330 | static etp_reqq res_queue; |
338 | static etp_reqq res_queue; |
331 | |
339 | |
|
|
340 | static int ecb_noinline |
332 | static int reqq_push (etp_reqq *q, ETP_REQ *req) |
341 | reqq_push (etp_reqq *q, ETP_REQ *req) |
333 | { |
342 | { |
334 | int pri = req->pri; |
343 | int pri = req->pri; |
335 | req->next = 0; |
344 | req->next = 0; |
336 | |
345 | |
337 | if (q->qe[pri]) |
346 | if (q->qe[pri]) |
… | |
… | |
343 | q->qe[pri] = q->qs[pri] = req; |
352 | q->qe[pri] = q->qs[pri] = req; |
344 | |
353 | |
345 | return q->size++; |
354 | return q->size++; |
346 | } |
355 | } |
347 | |
356 | |
|
|
357 | static ETP_REQ * ecb_noinline |
348 | static ETP_REQ *reqq_shift (etp_reqq *q) |
358 | reqq_shift (etp_reqq *q) |
349 | { |
359 | { |
350 | int pri; |
360 | int pri; |
351 | |
361 | |
352 | if (!q->size) |
362 | if (!q->size) |
353 | return 0; |
363 | return 0; |
… | |
… | |
368 | } |
378 | } |
369 | |
379 | |
370 | abort (); |
380 | abort (); |
371 | } |
381 | } |
372 | |
382 | |
373 | static void etp_atfork_prepare (void) |
383 | static void ecb_cold |
|
|
384 | etp_thread_init (void) |
374 | { |
385 | { |
375 | X_LOCK (wrklock); |
|
|
376 | X_LOCK (reqlock); |
|
|
377 | X_LOCK (reslock); |
|
|
378 | #if !HAVE_PREADWRITE |
386 | #if !HAVE_PREADWRITE |
379 | X_LOCK (preadwritelock); |
387 | X_MUTEX_CREATE (preadwritelock); |
380 | #endif |
388 | #endif |
|
|
389 | X_MUTEX_CREATE (wrklock); |
|
|
390 | X_MUTEX_CREATE (reslock); |
|
|
391 | X_MUTEX_CREATE (reqlock); |
|
|
392 | X_COND_CREATE (reqwait); |
381 | } |
393 | } |
382 | |
394 | |
|
|
395 | static void ecb_cold |
|
|
396 | etp_atfork_prepare (void) |
|
|
397 | { |
|
|
398 | } |
|
|
399 | |
|
|
400 | static void ecb_cold |
383 | static void etp_atfork_parent (void) |
401 | etp_atfork_parent (void) |
384 | { |
402 | { |
385 | #if !HAVE_PREADWRITE |
|
|
386 | X_UNLOCK (preadwritelock); |
|
|
387 | #endif |
|
|
388 | X_UNLOCK (reslock); |
|
|
389 | X_UNLOCK (reqlock); |
|
|
390 | X_UNLOCK (wrklock); |
|
|
391 | } |
403 | } |
392 | |
404 | |
|
|
405 | static void ecb_cold |
393 | static void etp_atfork_child (void) |
406 | etp_atfork_child (void) |
394 | { |
407 | { |
395 | ETP_REQ *prv; |
408 | ETP_REQ *prv; |
396 | |
409 | |
397 | while ((prv = reqq_shift (&req_queue))) |
410 | while ((prv = reqq_shift (&req_queue))) |
398 | ETP_DESTROY (prv); |
411 | ETP_DESTROY (prv); |
… | |
… | |
415 | idle = 0; |
428 | idle = 0; |
416 | nreqs = 0; |
429 | nreqs = 0; |
417 | nready = 0; |
430 | nready = 0; |
418 | npending = 0; |
431 | npending = 0; |
419 | |
432 | |
420 | etp_atfork_parent (); |
433 | etp_thread_init (); |
421 | } |
434 | } |
422 | |
435 | |
423 | static void |
436 | static void ecb_cold |
424 | etp_once_init (void) |
437 | etp_once_init (void) |
425 | { |
438 | { |
|
|
439 | etp_thread_init (); |
426 | X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child); |
440 | X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child); |
427 | } |
441 | } |
428 | |
442 | |
429 | static int |
443 | static int ecb_cold |
430 | etp_init (void (*want_poll)(void), void (*done_poll)(void)) |
444 | etp_init (void (*want_poll)(void), void (*done_poll)(void)) |
431 | { |
445 | { |
432 | static pthread_once_t doinit = PTHREAD_ONCE_INIT; |
446 | static pthread_once_t doinit = PTHREAD_ONCE_INIT; |
433 | |
447 | |
434 | pthread_once (&doinit, etp_once_init); |
448 | pthread_once (&doinit, etp_once_init); |
… | |
… | |
439 | return 0; |
453 | return 0; |
440 | } |
454 | } |
441 | |
455 | |
442 | X_THREAD_PROC (etp_proc); |
456 | X_THREAD_PROC (etp_proc); |
443 | |
457 | |
|
|
458 | static void ecb_cold |
444 | static void etp_start_thread (void) |
459 | etp_start_thread (void) |
445 | { |
460 | { |
446 | etp_worker *wrk = calloc (1, sizeof (etp_worker)); |
461 | etp_worker *wrk = calloc (1, sizeof (etp_worker)); |
447 | |
462 | |
448 | /*TODO*/ |
463 | /*TODO*/ |
449 | assert (("unable to allocate worker thread data", wrk)); |
464 | assert (("unable to allocate worker thread data", wrk)); |
… | |
… | |
462 | free (wrk); |
477 | free (wrk); |
463 | |
478 | |
464 | X_UNLOCK (wrklock); |
479 | X_UNLOCK (wrklock); |
465 | } |
480 | } |
466 | |
481 | |
|
|
482 | static void |
467 | static void etp_maybe_start_thread (void) |
483 | etp_maybe_start_thread (void) |
468 | { |
484 | { |
469 | if (expect_true (etp_nthreads () >= wanted)) |
485 | if (ecb_expect_true (etp_nthreads () >= wanted)) |
470 | return; |
486 | return; |
471 | |
487 | |
472 | /* todo: maybe use idle here, but might be less exact */ |
488 | /* todo: maybe use idle here, but might be less exact */ |
473 | if (expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) |
489 | if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) |
474 | return; |
490 | return; |
475 | |
491 | |
476 | etp_start_thread (); |
492 | etp_start_thread (); |
477 | } |
493 | } |
478 | |
494 | |
|
|
495 | static void ecb_cold |
479 | static void etp_end_thread (void) |
496 | etp_end_thread (void) |
480 | { |
497 | { |
481 | eio_req *req = calloc (1, sizeof (eio_req)); |
498 | eio_req *req = calloc (1, sizeof (eio_req)); |
482 | |
499 | |
483 | req->type = -1; |
500 | req->type = -1; |
484 | req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
501 | req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
… | |
… | |
491 | X_LOCK (wrklock); |
508 | X_LOCK (wrklock); |
492 | --started; |
509 | --started; |
493 | X_UNLOCK (wrklock); |
510 | X_UNLOCK (wrklock); |
494 | } |
511 | } |
495 | |
512 | |
496 | static int etp_poll (void) |
513 | static int |
|
|
514 | etp_poll (void) |
497 | { |
515 | { |
498 | unsigned int maxreqs; |
516 | unsigned int maxreqs; |
499 | unsigned int maxtime; |
517 | unsigned int maxtime; |
500 | struct timeval tv_start, tv_now; |
518 | struct timeval tv_start, tv_now; |
501 | |
519 | |
… | |
… | |
531 | |
549 | |
532 | X_LOCK (reqlock); |
550 | X_LOCK (reqlock); |
533 | --nreqs; |
551 | --nreqs; |
534 | X_UNLOCK (reqlock); |
552 | X_UNLOCK (reqlock); |
535 | |
553 | |
536 | if (expect_false (req->type == EIO_GROUP && req->size)) |
554 | if (ecb_expect_false (req->type == EIO_GROUP && req->size)) |
537 | { |
555 | { |
538 | req->int1 = 1; /* mark request as delayed */ |
556 | req->int1 = 1; /* mark request as delayed */ |
539 | continue; |
557 | continue; |
540 | } |
558 | } |
541 | else |
559 | else |
542 | { |
560 | { |
543 | int res = ETP_FINISH (req); |
561 | int res = ETP_FINISH (req); |
544 | if (expect_false (res)) |
562 | if (ecb_expect_false (res)) |
545 | return res; |
563 | return res; |
546 | } |
564 | } |
547 | |
565 | |
548 | if (expect_false (maxreqs && !--maxreqs)) |
566 | if (ecb_expect_false (maxreqs && !--maxreqs)) |
549 | break; |
567 | break; |
550 | |
568 | |
551 | if (maxtime) |
569 | if (maxtime) |
552 | { |
570 | { |
553 | gettimeofday (&tv_now, 0); |
571 | gettimeofday (&tv_now, 0); |
… | |
… | |
559 | |
577 | |
560 | errno = EAGAIN; |
578 | errno = EAGAIN; |
561 | return -1; |
579 | return -1; |
562 | } |
580 | } |
563 | |
581 | |
|
|
582 | static void |
564 | static void etp_cancel (ETP_REQ *req) |
583 | etp_cancel (ETP_REQ *req) |
565 | { |
584 | { |
566 | X_LOCK (wrklock); |
585 | req->cancelled = 1; |
567 | req->flags |= EIO_FLAG_CANCELLED; |
|
|
568 | X_UNLOCK (wrklock); |
|
|
569 | |
586 | |
570 | eio_grp_cancel (req); |
587 | eio_grp_cancel (req); |
571 | } |
588 | } |
572 | |
589 | |
|
|
590 | static void |
573 | static void etp_submit (ETP_REQ *req) |
591 | etp_submit (ETP_REQ *req) |
574 | { |
592 | { |
575 | req->pri -= ETP_PRI_MIN; |
593 | req->pri -= ETP_PRI_MIN; |
576 | |
594 | |
577 | if (expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; |
595 | if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; |
578 | if (expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
596 | if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
579 | |
597 | |
580 | if (expect_false (req->type == EIO_GROUP)) |
598 | if (ecb_expect_false (req->type == EIO_GROUP)) |
581 | { |
599 | { |
582 | /* I hope this is worth it :/ */ |
600 | /* I hope this is worth it :/ */ |
583 | X_LOCK (reqlock); |
601 | X_LOCK (reqlock); |
584 | ++nreqs; |
602 | ++nreqs; |
585 | X_UNLOCK (reqlock); |
603 | X_UNLOCK (reqlock); |
… | |
… | |
604 | |
622 | |
605 | etp_maybe_start_thread (); |
623 | etp_maybe_start_thread (); |
606 | } |
624 | } |
607 | } |
625 | } |
608 | |
626 | |
|
|
627 | static void ecb_cold |
609 | static void etp_set_max_poll_time (double nseconds) |
628 | etp_set_max_poll_time (double nseconds) |
610 | { |
629 | { |
611 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
630 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
612 | max_poll_time = nseconds * EIO_TICKS; |
631 | max_poll_time = nseconds * EIO_TICKS; |
613 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
632 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
614 | } |
633 | } |
615 | |
634 | |
|
|
635 | static void ecb_cold |
616 | static void etp_set_max_poll_reqs (unsigned int maxreqs) |
636 | etp_set_max_poll_reqs (unsigned int maxreqs) |
617 | { |
637 | { |
618 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
638 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
619 | max_poll_reqs = maxreqs; |
639 | max_poll_reqs = maxreqs; |
620 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
640 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
621 | } |
641 | } |
622 | |
642 | |
|
|
643 | static void ecb_cold |
623 | static void etp_set_max_idle (unsigned int nthreads) |
644 | etp_set_max_idle (unsigned int nthreads) |
624 | { |
645 | { |
625 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
646 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
626 | max_idle = nthreads <= 0 ? 1 : nthreads; |
647 | max_idle = nthreads; |
627 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
648 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
628 | } |
649 | } |
629 | |
650 | |
|
|
651 | static void ecb_cold |
|
|
652 | etp_set_idle_timeout (unsigned int seconds) |
|
|
653 | { |
|
|
654 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
|
|
655 | idle_timeout = seconds; |
|
|
656 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
|
|
657 | } |
|
|
658 | |
|
|
659 | static void ecb_cold |
630 | static void etp_set_min_parallel (unsigned int nthreads) |
660 | etp_set_min_parallel (unsigned int nthreads) |
631 | { |
661 | { |
632 | if (wanted < nthreads) |
662 | if (wanted < nthreads) |
633 | wanted = nthreads; |
663 | wanted = nthreads; |
634 | } |
664 | } |
635 | |
665 | |
|
|
666 | static void ecb_cold |
636 | static void etp_set_max_parallel (unsigned int nthreads) |
667 | etp_set_max_parallel (unsigned int nthreads) |
637 | { |
668 | { |
638 | if (wanted > nthreads) |
669 | if (wanted > nthreads) |
639 | wanted = nthreads; |
670 | wanted = nthreads; |
640 | |
671 | |
641 | while (started > wanted) |
672 | while (started > wanted) |
642 | etp_end_thread (); |
673 | etp_end_thread (); |
643 | } |
674 | } |
644 | |
675 | |
645 | /*****************************************************************************/ |
676 | /*****************************************************************************/ |
646 | |
677 | |
|
|
678 | static void |
647 | static void grp_try_feed (eio_req *grp) |
679 | grp_try_feed (eio_req *grp) |
648 | { |
680 | { |
649 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
681 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
650 | { |
682 | { |
651 | grp->flags &= ~EIO_FLAG_GROUPADD; |
683 | grp->flags &= ~EIO_FLAG_GROUPADD; |
652 | |
684 | |
… | |
… | |
659 | break; |
691 | break; |
660 | } |
692 | } |
661 | } |
693 | } |
662 | } |
694 | } |
663 | |
695 | |
|
|
696 | static int |
664 | static int grp_dec (eio_req *grp) |
697 | grp_dec (eio_req *grp) |
665 | { |
698 | { |
666 | --grp->size; |
699 | --grp->size; |
667 | |
700 | |
668 | /* call feeder, if applicable */ |
701 | /* call feeder, if applicable */ |
669 | grp_try_feed (grp); |
702 | grp_try_feed (grp); |
… | |
… | |
673 | return eio_finish (grp); |
706 | return eio_finish (grp); |
674 | else |
707 | else |
675 | return 0; |
708 | return 0; |
676 | } |
709 | } |
677 | |
710 | |
|
|
711 | static void |
678 | void eio_destroy (eio_req *req) |
712 | eio_destroy (eio_req *req) |
679 | { |
713 | { |
680 | if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); |
714 | if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); |
681 | if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); |
715 | if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); |
682 | |
716 | |
683 | EIO_DESTROY (req); |
717 | EIO_DESTROY (req); |
684 | } |
718 | } |
685 | |
719 | |
|
|
720 | static int |
686 | static int eio_finish (eio_req *req) |
721 | eio_finish (eio_req *req) |
687 | { |
722 | { |
688 | int res = EIO_FINISH (req); |
723 | int res = EIO_FINISH (req); |
689 | |
724 | |
690 | if (req->grp) |
725 | if (req->grp) |
691 | { |
726 | { |
… | |
… | |
699 | if (grp->grp_first == req) |
734 | if (grp->grp_first == req) |
700 | grp->grp_first = req->grp_next; |
735 | grp->grp_first = req->grp_next; |
701 | |
736 | |
702 | res2 = grp_dec (grp); |
737 | res2 = grp_dec (grp); |
703 | |
738 | |
704 | if (!res && res2) |
739 | if (!res) |
705 | res = res2; |
740 | res = res2; |
706 | } |
741 | } |
707 | |
742 | |
708 | eio_destroy (req); |
743 | eio_destroy (req); |
709 | |
744 | |
710 | return res; |
745 | return res; |
711 | } |
746 | } |
712 | |
747 | |
|
|
748 | void |
713 | void eio_grp_cancel (eio_req *grp) |
749 | eio_grp_cancel (eio_req *grp) |
714 | { |
750 | { |
715 | for (grp = grp->grp_first; grp; grp = grp->grp_next) |
751 | for (grp = grp->grp_first; grp; grp = grp->grp_next) |
716 | eio_cancel (grp); |
752 | eio_cancel (grp); |
717 | } |
753 | } |
718 | |
754 | |
|
|
755 | void |
719 | void eio_cancel (eio_req *req) |
756 | eio_cancel (eio_req *req) |
720 | { |
757 | { |
721 | etp_cancel (req); |
758 | etp_cancel (req); |
722 | } |
759 | } |
723 | |
760 | |
|
|
761 | void |
724 | void eio_submit (eio_req *req) |
762 | eio_submit (eio_req *req) |
725 | { |
763 | { |
726 | etp_submit (req); |
764 | etp_submit (req); |
727 | } |
765 | } |
728 | |
766 | |
729 | unsigned int eio_nreqs (void) |
767 | unsigned int |
|
|
768 | eio_nreqs (void) |
730 | { |
769 | { |
731 | return etp_nreqs (); |
770 | return etp_nreqs (); |
732 | } |
771 | } |
733 | |
772 | |
734 | unsigned int eio_nready (void) |
773 | unsigned int |
|
|
774 | eio_nready (void) |
735 | { |
775 | { |
736 | return etp_nready (); |
776 | return etp_nready (); |
737 | } |
777 | } |
738 | |
778 | |
739 | unsigned int eio_npending (void) |
779 | unsigned int |
|
|
780 | eio_npending (void) |
740 | { |
781 | { |
741 | return etp_npending (); |
782 | return etp_npending (); |
742 | } |
783 | } |
743 | |
784 | |
744 | unsigned int eio_nthreads (void) |
785 | unsigned int ecb_cold |
|
|
786 | eio_nthreads (void) |
745 | { |
787 | { |
746 | return etp_nthreads (); |
788 | return etp_nthreads (); |
747 | } |
789 | } |
748 | |
790 | |
|
|
791 | void ecb_cold |
749 | void eio_set_max_poll_time (double nseconds) |
792 | eio_set_max_poll_time (double nseconds) |
750 | { |
793 | { |
751 | etp_set_max_poll_time (nseconds); |
794 | etp_set_max_poll_time (nseconds); |
752 | } |
795 | } |
753 | |
796 | |
|
|
797 | void ecb_cold |
754 | void eio_set_max_poll_reqs (unsigned int maxreqs) |
798 | eio_set_max_poll_reqs (unsigned int maxreqs) |
755 | { |
799 | { |
756 | etp_set_max_poll_reqs (maxreqs); |
800 | etp_set_max_poll_reqs (maxreqs); |
757 | } |
801 | } |
758 | |
802 | |
|
|
803 | void ecb_cold |
759 | void eio_set_max_idle (unsigned int nthreads) |
804 | eio_set_max_idle (unsigned int nthreads) |
760 | { |
805 | { |
761 | etp_set_max_idle (nthreads); |
806 | etp_set_max_idle (nthreads); |
762 | } |
807 | } |
763 | |
808 | |
|
|
809 | void ecb_cold |
|
|
810 | eio_set_idle_timeout (unsigned int seconds) |
|
|
811 | { |
|
|
812 | etp_set_idle_timeout (seconds); |
|
|
813 | } |
|
|
814 | |
|
|
815 | void ecb_cold |
764 | void eio_set_min_parallel (unsigned int nthreads) |
816 | eio_set_min_parallel (unsigned int nthreads) |
765 | { |
817 | { |
766 | etp_set_min_parallel (nthreads); |
818 | etp_set_min_parallel (nthreads); |
767 | } |
819 | } |
768 | |
820 | |
|
|
821 | void ecb_cold |
769 | void eio_set_max_parallel (unsigned int nthreads) |
822 | eio_set_max_parallel (unsigned int nthreads) |
770 | { |
823 | { |
771 | etp_set_max_parallel (nthreads); |
824 | etp_set_max_parallel (nthreads); |
772 | } |
825 | } |
773 | |
826 | |
774 | int eio_poll (void) |
827 | int eio_poll (void) |
… | |
… | |
844 | #ifndef HAVE_FUTIMES |
897 | #ifndef HAVE_FUTIMES |
845 | |
898 | |
846 | # undef futimes |
899 | # undef futimes |
847 | # define futimes(fd,times) eio__futimes (fd, times) |
900 | # define futimes(fd,times) eio__futimes (fd, times) |
848 | |
901 | |
|
|
902 | static int |
849 | static int eio__futimes (int fd, const struct timeval tv[2]) |
903 | eio__futimes (int fd, const struct timeval tv[2]) |
850 | { |
904 | { |
851 | errno = ENOSYS; |
905 | errno = ENOSYS; |
852 | return -1; |
906 | return -1; |
853 | } |
907 | } |
854 | |
908 | |
… | |
… | |
858 | # undef fdatasync |
912 | # undef fdatasync |
859 | # define fdatasync(fd) fsync (fd) |
913 | # define fdatasync(fd) fsync (fd) |
860 | #endif |
914 | #endif |
861 | |
915 | |
862 | /* sync_file_range always needs emulation */ |
916 | /* sync_file_range always needs emulation */ |
863 | int |
917 | static int |
864 | eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) |
918 | eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) |
865 | { |
919 | { |
866 | #if HAVE_SYNC_FILE_RANGE |
920 | #if HAVE_SYNC_FILE_RANGE |
867 | int res; |
921 | int res; |
868 | |
922 | |
… | |
… | |
885 | /* even though we could play tricks with the flags, it's better to always |
939 | /* even though we could play tricks with the flags, it's better to always |
886 | * call fdatasync, as that matches the expectation of its users best */ |
940 | * call fdatasync, as that matches the expectation of its users best */ |
887 | return fdatasync (fd); |
941 | return fdatasync (fd); |
888 | } |
942 | } |
889 | |
943 | |
|
|
944 | static int |
|
|
945 | eio__fallocate (int fd, int mode, off_t offset, size_t len) |
|
|
946 | { |
|
|
947 | #if HAVE_FALLOCATE |
|
|
948 | return fallocate (fd, mode, offset, len); |
|
|
949 | #else |
|
|
950 | errno = ENOSYS; |
|
|
951 | return -1; |
|
|
952 | #endif |
|
|
953 | } |
|
|
954 | |
890 | #if !HAVE_READAHEAD |
955 | #if !HAVE_READAHEAD |
891 | # undef readahead |
956 | # undef readahead |
892 | # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) |
957 | # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) |
893 | |
958 | |
894 | static ssize_t |
959 | static ssize_t |
… | |
… | |
914 | |
979 | |
915 | /* sendfile always needs emulation */ |
980 | /* sendfile always needs emulation */ |
916 | static ssize_t |
981 | static ssize_t |
917 | eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) |
982 | eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) |
918 | { |
983 | { |
|
|
984 | ssize_t written = 0; |
919 | ssize_t res; |
985 | ssize_t res; |
920 | |
986 | |
921 | if (!count) |
987 | if (!count) |
922 | return 0; |
988 | return 0; |
923 | |
989 | |
|
|
990 | for (;;) |
|
|
991 | { |
|
|
992 | #ifdef __APPLE__ |
|
|
993 | # undef HAVE_SENDFILE /* broken, as everything on os x */ |
|
|
994 | #endif |
924 | #if HAVE_SENDFILE |
995 | #if HAVE_SENDFILE |
925 | # if __linux |
996 | # if __linux |
|
|
997 | off_t soffset = offset; |
926 | res = sendfile (ofd, ifd, &offset, count); |
998 | res = sendfile (ofd, ifd, &soffset, count); |
927 | |
999 | |
928 | # elif __FreeBSD__ |
1000 | # elif __FreeBSD__ |
929 | /* |
1001 | /* |
930 | * Of course, the freebsd sendfile is a dire hack with no thoughts |
1002 | * Of course, the freebsd sendfile is a dire hack with no thoughts |
931 | * wasted on making it similar to other I/O functions. |
1003 | * wasted on making it similar to other I/O functions. |
932 | */ |
1004 | */ |
933 | { |
|
|
934 | off_t sbytes; |
1005 | off_t sbytes; |
935 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
1006 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
936 | |
1007 | |
937 | #if 0 /* according to the manpage, this is correct, but broken behaviour */ |
1008 | #if 0 /* according to the manpage, this is correct, but broken behaviour */ |
938 | /* freebsd' sendfile will return 0 on success */ |
1009 | /* freebsd' sendfile will return 0 on success */ |
939 | /* freebsd 8 documents it as only setting *sbytes on EINTR and EAGAIN, but */ |
1010 | /* freebsd 8 documents it as only setting *sbytes on EINTR and EAGAIN, but */ |
940 | /* not on e.g. EIO or EPIPE - sounds broken */ |
1011 | /* not on e.g. EIO or EPIPE - sounds broken */ |
941 | if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0) |
1012 | if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0) |
942 | res = sbytes; |
1013 | res = sbytes; |
943 | #endif |
1014 | #endif |
944 | |
1015 | |
945 | /* according to source inspection, this is correct, and useful behaviour */ |
1016 | /* according to source inspection, this is correct, and useful behaviour */ |
946 | if (sbytes) |
1017 | if (sbytes) |
947 | res = sbytes; |
1018 | res = sbytes; |
948 | } |
|
|
949 | |
1019 | |
950 | # elif defined (__APPLE__) |
1020 | # elif defined (__APPLE__) |
951 | |
|
|
952 | { |
|
|
953 | off_t sbytes = count; |
1021 | off_t sbytes = count; |
954 | res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); |
1022 | res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); |
955 | |
1023 | |
956 | /* according to the manpage, sbytes is always valid */ |
1024 | /* according to the manpage, sbytes is always valid */ |
957 | if (sbytes) |
1025 | if (sbytes) |
958 | res = sbytes; |
1026 | res = sbytes; |
959 | } |
|
|
960 | |
1027 | |
961 | # elif __hpux |
1028 | # elif __hpux |
962 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
1029 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
963 | |
1030 | |
964 | # elif __solaris |
1031 | # elif __solaris |
965 | { |
|
|
966 | struct sendfilevec vec; |
1032 | struct sendfilevec vec; |
967 | size_t sbytes; |
1033 | size_t sbytes; |
968 | |
1034 | |
969 | vec.sfv_fd = ifd; |
1035 | vec.sfv_fd = ifd; |
970 | vec.sfv_flag = 0; |
1036 | vec.sfv_flag = 0; |
971 | vec.sfv_off = offset; |
1037 | vec.sfv_off = offset; |
972 | vec.sfv_len = count; |
1038 | vec.sfv_len = count; |
973 | |
1039 | |
974 | res = sendfilev (ofd, &vec, 1, &sbytes); |
1040 | res = sendfilev (ofd, &vec, 1, &sbytes); |
975 | |
1041 | |
976 | if (res < 0 && sbytes) |
1042 | if (res < 0 && sbytes) |
977 | res = sbytes; |
1043 | res = sbytes; |
978 | } |
|
|
979 | |
1044 | |
980 | # endif |
1045 | # endif |
981 | |
1046 | |
982 | #elif defined (_WIN32) |
1047 | #elif defined (_WIN32) |
983 | |
|
|
984 | /* does not work, just for documentation of what would need to be done */ |
1048 | /* does not work, just for documentation of what would need to be done */ |
985 | { |
1049 | /* actually, cannot be done like this, as TransmitFile changes the file offset, */ |
|
|
1050 | /* libeio guarantees that the file offset does not change, and windows */ |
|
|
1051 | /* has no way to get an independent handle to the same file description */ |
986 | HANDLE h = TO_SOCKET (ifd); |
1052 | HANDLE h = TO_SOCKET (ifd); |
987 | SetFilePointer (h, offset, 0, FILE_BEGIN); |
1053 | SetFilePointer (h, offset, 0, FILE_BEGIN); |
988 | res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); |
1054 | res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); |
989 | } |
|
|
990 | |
1055 | |
991 | #else |
1056 | #else |
992 | res = -1; |
1057 | res = -1; |
993 | errno = ENOSYS; |
1058 | errno = ENOSYS; |
994 | #endif |
1059 | #endif |
995 | |
1060 | |
|
|
1061 | /* we assume sendfile can copy at least 128mb in one go */ |
|
|
1062 | if (res <= 128 * 1024 * 1024) |
|
|
1063 | { |
|
|
1064 | if (res > 0) |
|
|
1065 | written += res; |
|
|
1066 | |
|
|
1067 | if (written) |
|
|
1068 | return written; |
|
|
1069 | |
|
|
1070 | break; |
|
|
1071 | } |
|
|
1072 | else |
|
|
1073 | { |
|
|
1074 | /* if we requested more, then probably the kernel was lazy */ |
|
|
1075 | written += res; |
|
|
1076 | offset += res; |
|
|
1077 | count -= res; |
|
|
1078 | |
|
|
1079 | if (!count) |
|
|
1080 | return written; |
|
|
1081 | } |
|
|
1082 | } |
|
|
1083 | |
996 | if (res < 0 |
1084 | if (res < 0 |
997 | && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK |
1085 | && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK |
998 | /* BSDs */ |
1086 | /* BSDs */ |
999 | #ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */ |
1087 | #ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */ |
1000 | || errno == ENOTSUP |
1088 | || errno == ENOTSUP |
1001 | #endif |
1089 | #endif |
… | |
… | |
1038 | } |
1126 | } |
1039 | |
1127 | |
1040 | return res; |
1128 | return res; |
1041 | } |
1129 | } |
1042 | |
1130 | |
|
|
1131 | #ifdef PAGESIZE |
|
|
1132 | # define eio_pagesize() PAGESIZE |
|
|
1133 | #else |
|
|
1134 | static intptr_t |
|
|
1135 | eio_pagesize (void) |
|
|
1136 | { |
|
|
1137 | static intptr_t page; |
|
|
1138 | |
|
|
1139 | if (!page) |
|
|
1140 | page = sysconf (_SC_PAGESIZE); |
|
|
1141 | |
|
|
1142 | return page; |
|
|
1143 | } |
|
|
1144 | #endif |
|
|
1145 | |
|
|
1146 | static void |
|
|
1147 | eio_page_align (void **addr, size_t *length) |
|
|
1148 | { |
|
|
1149 | intptr_t mask = eio_pagesize () - 1; |
|
|
1150 | |
|
|
1151 | /* round down addr */ |
|
|
1152 | intptr_t adj = mask & (intptr_t)*addr; |
|
|
1153 | |
|
|
1154 | *addr = (void *)((intptr_t)*addr - adj); |
|
|
1155 | *length += adj; |
|
|
1156 | |
|
|
1157 | /* round up length */ |
|
|
1158 | *length = (*length + mask) & ~mask; |
|
|
1159 | } |
|
|
1160 | |
|
|
1161 | #if !_POSIX_MEMLOCK |
|
|
1162 | # define eio__mlockall(a) ((errno = ENOSYS), -1) |
|
|
1163 | #else |
|
|
1164 | |
|
|
1165 | static int |
|
|
1166 | eio__mlockall (int flags) |
|
|
1167 | { |
|
|
1168 | #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 |
|
|
1169 | extern int mallopt (int, int); |
|
|
1170 | mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ |
|
|
1171 | #endif |
|
|
1172 | |
|
|
1173 | if (EIO_MCL_CURRENT != MCL_CURRENT |
|
|
1174 | || EIO_MCL_FUTURE != MCL_FUTURE) |
|
|
1175 | { |
|
|
1176 | flags = 0 |
|
|
1177 | | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0) |
|
|
1178 | | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0); |
|
|
1179 | } |
|
|
1180 | |
|
|
1181 | return mlockall (flags); |
|
|
1182 | } |
|
|
1183 | #endif |
|
|
1184 | |
|
|
1185 | #if !_POSIX_MEMLOCK_RANGE |
|
|
1186 | # define eio__mlock(a,b) ((errno = ENOSYS), -1) |
|
|
1187 | #else |
|
|
1188 | |
|
|
1189 | static int |
|
|
1190 | eio__mlock (void *addr, size_t length) |
|
|
1191 | { |
|
|
1192 | eio_page_align (&addr, &length); |
|
|
1193 | |
|
|
1194 | return mlock (addr, length); |
|
|
1195 | } |
|
|
1196 | |
|
|
1197 | #endif |
|
|
1198 | |
|
|
1199 | #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) |
|
|
1200 | # define eio__msync(a,b,c) ((errno = ENOSYS), -1) |
|
|
1201 | #else |
|
|
1202 | |
|
|
1203 | static int |
|
|
1204 | eio__msync (void *mem, size_t len, int flags) |
|
|
1205 | { |
|
|
1206 | eio_page_align (&mem, &len); |
|
|
1207 | |
|
|
1208 | if (EIO_MS_ASYNC != MS_SYNC |
|
|
1209 | || EIO_MS_INVALIDATE != MS_INVALIDATE |
|
|
1210 | || EIO_MS_SYNC != MS_SYNC) |
|
|
1211 | { |
|
|
1212 | flags = 0 |
|
|
1213 | | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0) |
|
|
1214 | | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0) |
|
|
1215 | | (flags & EIO_MS_SYNC ? MS_SYNC : 0); |
|
|
1216 | } |
|
|
1217 | |
|
|
1218 | return msync (mem, len, flags); |
|
|
1219 | } |
|
|
1220 | |
|
|
1221 | #endif |
|
|
1222 | |
|
|
1223 | static int |
|
|
1224 | eio__mtouch (eio_req *req) |
|
|
1225 | { |
|
|
1226 | void *mem = req->ptr2; |
|
|
1227 | size_t len = req->size; |
|
|
1228 | int flags = req->int1; |
|
|
1229 | |
|
|
1230 | eio_page_align (&mem, &len); |
|
|
1231 | |
|
|
1232 | { |
|
|
1233 | intptr_t addr = (intptr_t)mem; |
|
|
1234 | intptr_t end = addr + len; |
|
|
1235 | intptr_t page = eio_pagesize (); |
|
|
1236 | |
|
|
1237 | if (addr < end) |
|
|
1238 | if (flags & EIO_MT_MODIFY) /* modify */ |
|
|
1239 | do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req)); |
|
|
1240 | else |
|
|
1241 | do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req)); |
|
|
1242 | } |
|
|
1243 | |
|
|
1244 | return 0; |
|
|
1245 | } |
|
|
1246 | |
|
|
1247 | /*****************************************************************************/ |
|
|
1248 | /* requests implemented outside eio_execute, because they are so large */ |
|
|
1249 | |
|
|
1250 | static void |
|
|
1251 | eio__realpath (eio_req *req, etp_worker *self) |
|
|
1252 | { |
|
|
1253 | char *rel = req->ptr1; |
|
|
1254 | char *res; |
|
|
1255 | char *tmp1, *tmp2; |
|
|
1256 | #if SYMLOOP_MAX > 32 |
|
|
1257 | int symlinks = SYMLOOP_MAX; |
|
|
1258 | #else |
|
|
1259 | int symlinks = 32; |
|
|
1260 | #endif |
|
|
1261 | |
|
|
1262 | req->result = -1; |
|
|
1263 | |
|
|
1264 | errno = EINVAL; |
|
|
1265 | if (!rel) |
|
|
1266 | return; |
|
|
1267 | |
|
|
1268 | errno = ENOENT; |
|
|
1269 | if (!*rel) |
|
|
1270 | return; |
|
|
1271 | |
|
|
1272 | if (!req->ptr2) |
|
|
1273 | { |
|
|
1274 | X_LOCK (wrklock); |
|
|
1275 | req->flags |= EIO_FLAG_PTR2_FREE; |
|
|
1276 | X_UNLOCK (wrklock); |
|
|
1277 | req->ptr2 = malloc (PATH_MAX * 3); |
|
|
1278 | |
|
|
1279 | errno = ENOMEM; |
|
|
1280 | if (!req->ptr2) |
|
|
1281 | return; |
|
|
1282 | } |
|
|
1283 | |
|
|
1284 | res = req->ptr2; |
|
|
1285 | tmp1 = res + PATH_MAX; |
|
|
1286 | tmp2 = tmp1 + PATH_MAX; |
|
|
1287 | |
|
|
1288 | #if 0 /* disabled, the musl way to do things is just too racy */ |
|
|
1289 | #if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) |
|
|
1290 | /* on linux we may be able to ask the kernel */ |
|
|
1291 | { |
|
|
1292 | int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); |
|
|
1293 | |
|
|
1294 | if (fd >= 0) |
|
|
1295 | { |
|
|
1296 | sprintf (tmp1, "/proc/self/fd/%d", fd); |
|
|
1297 | req->result = readlink (tmp1, res, PATH_MAX); |
|
|
1298 | close (fd); |
|
|
1299 | |
|
|
1300 | /* here we should probably stat the open file and the disk file, to make sure they still match */ |
|
|
1301 | |
|
|
1302 | if (req->result > 0) |
|
|
1303 | goto done; |
|
|
1304 | } |
|
|
1305 | else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) |
|
|
1306 | return; |
|
|
1307 | } |
|
|
1308 | #endif |
|
|
1309 | #endif |
|
|
1310 | |
|
|
1311 | if (*rel != '/') |
|
|
1312 | { |
|
|
1313 | if (!getcwd (res, PATH_MAX)) |
|
|
1314 | return; |
|
|
1315 | |
|
|
1316 | if (res [1]) /* only use if not / */ |
|
|
1317 | res += strlen (res); |
|
|
1318 | } |
|
|
1319 | |
|
|
1320 | while (*rel) |
|
|
1321 | { |
|
|
1322 | ssize_t len, linklen; |
|
|
1323 | char *beg = rel; |
|
|
1324 | |
|
|
1325 | while (*rel && *rel != '/') |
|
|
1326 | ++rel; |
|
|
1327 | |
|
|
1328 | len = rel - beg; |
|
|
1329 | |
|
|
1330 | if (!len) /* skip slashes */ |
|
|
1331 | { |
|
|
1332 | ++rel; |
|
|
1333 | continue; |
|
|
1334 | } |
|
|
1335 | |
|
|
1336 | if (beg [0] == '.') |
|
|
1337 | { |
|
|
1338 | if (len == 1) |
|
|
1339 | continue; /* . - nop */ |
|
|
1340 | |
|
|
1341 | if (beg [1] == '.' && len == 2) |
|
|
1342 | { |
|
|
1343 | /* .. - back up one component, if possible */ |
|
|
1344 | |
|
|
1345 | while (res != req->ptr2) |
|
|
1346 | if (*--res == '/') |
|
|
1347 | break; |
|
|
1348 | |
|
|
1349 | continue; |
|
|
1350 | } |
|
|
1351 | } |
|
|
1352 | |
|
|
1353 | errno = ENAMETOOLONG; |
|
|
1354 | if (res + 1 + len + 1 >= tmp1) |
|
|
1355 | return; |
|
|
1356 | |
|
|
1357 | /* copy one component */ |
|
|
1358 | *res = '/'; |
|
|
1359 | memcpy (res + 1, beg, len); |
|
|
1360 | |
|
|
1361 | /* zero-terminate, for readlink */ |
|
|
1362 | res [len + 1] = 0; |
|
|
1363 | |
|
|
1364 | /* now check if it's a symlink */ |
|
|
1365 | linklen = readlink (req->ptr2, tmp1, PATH_MAX); |
|
|
1366 | |
|
|
1367 | if (linklen < 0) |
|
|
1368 | { |
|
|
1369 | if (errno != EINVAL) |
|
|
1370 | return; |
|
|
1371 | |
|
|
1372 | /* it's a normal directory. hopefully */ |
|
|
1373 | res += len + 1; |
|
|
1374 | } |
|
|
1375 | else |
|
|
1376 | { |
|
|
1377 | /* yay, it was a symlink - build new path in tmp2 */ |
|
|
1378 | int rellen = strlen (rel); |
|
|
1379 | |
|
|
1380 | errno = ENAMETOOLONG; |
|
|
1381 | if (linklen + 1 + rellen >= PATH_MAX) |
|
|
1382 | return; |
|
|
1383 | |
|
|
1384 | errno = ELOOP; |
|
|
1385 | if (!--symlinks) |
|
|
1386 | return; |
|
|
1387 | |
|
|
1388 | if (*tmp1 == '/') |
|
|
1389 | res = req->ptr2; /* symlink resolves to an absolute path */ |
|
|
1390 | |
|
|
1391 | /* we need to be careful, as rel might point into tmp2 already */ |
|
|
1392 | memmove (tmp2 + linklen + 1, rel, rellen + 1); |
|
|
1393 | tmp2 [linklen] = '/'; |
|
|
1394 | memcpy (tmp2, tmp1, linklen); |
|
|
1395 | |
|
|
1396 | rel = tmp2; |
|
|
1397 | } |
|
|
1398 | } |
|
|
1399 | |
|
|
1400 | /* special case for the lone root path */ |
|
|
1401 | if (res == req->ptr2) |
|
|
1402 | *res++ = '/'; |
|
|
1403 | |
|
|
1404 | req->result = res - (char *)req->ptr2; |
|
|
1405 | |
|
|
1406 | done: |
|
|
1407 | req->ptr2 = realloc (req->ptr2, req->result); /* trade time for space savings */ |
|
|
1408 | } |
|
|
1409 | |
1043 | static signed char |
1410 | static signed char |
1044 | eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) |
1411 | eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) |
1045 | { |
1412 | { |
1046 | return a->score - b->score ? a->score - b->score /* works because our signed char is always 0..100 */ |
1413 | return a->score - b->score ? a->score - b->score /* works because our signed char is always 0..100 */ |
1047 | : a->inode < b->inode ? -1 : a->inode > b->inode ? 1 : 0; |
1414 | : a->inode < b->inode ? -1 |
|
|
1415 | : a->inode > b->inode ? 1 |
|
|
1416 | : 0; |
1048 | } |
1417 | } |
1049 | |
1418 | |
1050 | #define EIO_DENT_CMP(i,op,j) eio_dent_cmp (&i, &j) op 0 |
1419 | #define EIO_DENT_CMP(i,op,j) eio_dent_cmp (&i, &j) op 0 |
1051 | |
1420 | |
1052 | #define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */ |
1421 | #define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */ |
… | |
… | |
1058 | unsigned char bits [9 + sizeof (ino_t) * 8]; |
1427 | unsigned char bits [9 + sizeof (ino_t) * 8]; |
1059 | unsigned char *bit = bits; |
1428 | unsigned char *bit = bits; |
1060 | |
1429 | |
1061 | assert (CHAR_BIT == 8); |
1430 | assert (CHAR_BIT == 8); |
1062 | assert (sizeof (eio_dirent) * 8 < 256); |
1431 | assert (sizeof (eio_dirent) * 8 < 256); |
1063 | assert (offsetof (eio_dirent, inode)); /* we use 0 as sentinel */ |
1432 | assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */ |
1064 | assert (offsetof (eio_dirent, score)); /* we use 0 as sentinel */ |
1433 | assert (offsetof (eio_dirent, score)); /* we use bit #0 as sentinel */ |
1065 | |
1434 | |
1066 | if (size <= EIO_SORT_FAST) |
1435 | if (size <= EIO_SORT_FAST) |
1067 | return; |
1436 | return; |
1068 | |
1437 | |
1069 | /* first prepare an array of bits to test in our radix sort */ |
1438 | /* first prepare an array of bits to test in our radix sort */ |
… | |
… | |
1224 | flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); |
1593 | flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); |
1225 | |
1594 | |
1226 | X_LOCK (wrklock); |
1595 | X_LOCK (wrklock); |
1227 | /* the corresponding closedir is in ETP_WORKER_CLEAR */ |
1596 | /* the corresponding closedir is in ETP_WORKER_CLEAR */ |
1228 | self->dirp = dirp = opendir (req->ptr1); |
1597 | self->dirp = dirp = opendir (req->ptr1); |
|
|
1598 | |
|
|
1599 | if (req->flags & EIO_FLAG_PTR1_FREE) |
|
|
1600 | free (req->ptr1); |
|
|
1601 | |
1229 | req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; |
1602 | req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; |
1230 | req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; |
1603 | req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; |
1231 | req->ptr2 = names = malloc (namesalloc); |
1604 | req->ptr2 = names = malloc (namesalloc); |
1232 | X_UNLOCK (wrklock); |
1605 | X_UNLOCK (wrklock); |
1233 | |
1606 | |
… | |
… | |
1245 | /* sort etc. */ |
1618 | /* sort etc. */ |
1246 | req->int1 = flags; |
1619 | req->int1 = flags; |
1247 | req->result = dentoffs; |
1620 | req->result = dentoffs; |
1248 | |
1621 | |
1249 | if (flags & EIO_READDIR_STAT_ORDER) |
1622 | if (flags & EIO_READDIR_STAT_ORDER) |
1250 | eio_dent_sort (dents, dentoffs, 0, inode_bits); /* sort by inode exclusively */ |
1623 | eio_dent_sort (dents, dentoffs, flags & EIO_READDIR_DIRS_FIRST ? 7 : 0, inode_bits); |
1251 | else if (flags & EIO_READDIR_DIRS_FIRST) |
1624 | else if (flags & EIO_READDIR_DIRS_FIRST) |
1252 | if (flags & EIO_READDIR_FOUND_UNKNOWN) |
1625 | if (flags & EIO_READDIR_FOUND_UNKNOWN) |
1253 | eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */ |
1626 | eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */ |
1254 | else |
1627 | else |
1255 | { |
1628 | { |
… | |
… | |
1257 | eio_dirent *oth = dents + dentoffs; |
1630 | eio_dirent *oth = dents + dentoffs; |
1258 | eio_dirent *dir = dents; |
1631 | eio_dirent *dir = dents; |
1259 | |
1632 | |
1260 | /* now partition dirs to the front, and non-dirs to the back */ |
1633 | /* now partition dirs to the front, and non-dirs to the back */ |
1261 | /* by walking from both sides and swapping if necessary */ |
1634 | /* by walking from both sides and swapping if necessary */ |
1262 | /* also clear score, so it doesn't influence sorting */ |
|
|
1263 | while (oth > dir) |
1635 | while (oth > dir) |
1264 | { |
1636 | { |
1265 | if (dir->type == EIO_DT_DIR) |
1637 | if (dir->type == EIO_DT_DIR) |
1266 | ++dir; |
1638 | ++dir; |
1267 | else if ((--oth)->type == EIO_DT_DIR) |
1639 | else if ((--oth)->type == EIO_DT_DIR) |
… | |
… | |
1270 | |
1642 | |
1271 | ++dir; |
1643 | ++dir; |
1272 | } |
1644 | } |
1273 | } |
1645 | } |
1274 | |
1646 | |
1275 | /* now sort the dirs only */ |
1647 | /* now sort the dirs only (dirs all have the same score) */ |
1276 | eio_dent_sort (dents, dir - dents, 0, inode_bits); |
1648 | eio_dent_sort (dents, dir - dents, 0, inode_bits); |
1277 | } |
1649 | } |
1278 | |
1650 | |
1279 | break; |
1651 | break; |
1280 | } |
1652 | } |
… | |
… | |
1285 | /* skip . and .. entries */ |
1657 | /* skip . and .. entries */ |
1286 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
1658 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
1287 | { |
1659 | { |
1288 | int len = D_NAMLEN (entp) + 1; |
1660 | int len = D_NAMLEN (entp) + 1; |
1289 | |
1661 | |
1290 | while (expect_false (namesoffs + len > namesalloc)) |
1662 | while (ecb_expect_false (namesoffs + len > namesalloc)) |
1291 | { |
1663 | { |
1292 | namesalloc *= 2; |
1664 | namesalloc *= 2; |
1293 | X_LOCK (wrklock); |
1665 | X_LOCK (wrklock); |
1294 | req->ptr2 = names = realloc (names, namesalloc); |
1666 | req->ptr2 = names = realloc (names, namesalloc); |
1295 | X_UNLOCK (wrklock); |
1667 | X_UNLOCK (wrklock); |
… | |
… | |
1302 | |
1674 | |
1303 | if (dents) |
1675 | if (dents) |
1304 | { |
1676 | { |
1305 | struct eio_dirent *ent; |
1677 | struct eio_dirent *ent; |
1306 | |
1678 | |
1307 | if (expect_false (dentoffs == dentalloc)) |
1679 | if (ecb_expect_false (dentoffs == dentalloc)) |
1308 | { |
1680 | { |
1309 | dentalloc *= 2; |
1681 | dentalloc *= 2; |
1310 | X_LOCK (wrklock); |
1682 | X_LOCK (wrklock); |
1311 | req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); |
1683 | req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); |
1312 | X_UNLOCK (wrklock); |
1684 | X_UNLOCK (wrklock); |
… | |
… | |
1400 | break; |
1772 | break; |
1401 | } |
1773 | } |
1402 | } |
1774 | } |
1403 | } |
1775 | } |
1404 | |
1776 | |
1405 | #ifdef PAGESIZE |
|
|
1406 | # define eio_pagesize() PAGESIZE |
|
|
1407 | #else |
|
|
1408 | static intptr_t |
|
|
1409 | eio_pagesize (void) |
|
|
1410 | { |
|
|
1411 | static intptr_t page; |
|
|
1412 | |
|
|
1413 | if (!page) |
|
|
1414 | page = sysconf (_SC_PAGESIZE); |
|
|
1415 | |
|
|
1416 | return page; |
|
|
1417 | } |
|
|
1418 | #endif |
|
|
1419 | |
|
|
1420 | static void |
|
|
1421 | eio_page_align (void **addr, size_t *length) |
|
|
1422 | { |
|
|
1423 | intptr_t mask = eio_pagesize () - 1; |
|
|
1424 | |
|
|
1425 | /* round down addr */ |
|
|
1426 | intptr_t adj = mask & (intptr_t)*addr; |
|
|
1427 | |
|
|
1428 | *addr = (void *)((intptr_t)*addr - adj); |
|
|
1429 | *length += adj; |
|
|
1430 | |
|
|
1431 | /* round up length */ |
|
|
1432 | *length = (*length + mask) & ~mask; |
|
|
1433 | } |
|
|
1434 | |
|
|
1435 | #if !_POSIX_MEMLOCK |
|
|
1436 | # define eio__mlockall(a) ((errno = ENOSYS), -1) |
|
|
1437 | #else |
|
|
1438 | |
|
|
1439 | static int |
|
|
1440 | eio__mlockall (int flags) |
|
|
1441 | { |
|
|
1442 | #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 |
|
|
1443 | extern int mallopt (int, int); |
|
|
1444 | mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ |
|
|
1445 | #endif |
|
|
1446 | |
|
|
1447 | if (EIO_MCL_CURRENT != MCL_CURRENT |
|
|
1448 | || EIO_MCL_FUTURE != MCL_FUTURE) |
|
|
1449 | { |
|
|
1450 | flags = 0 |
|
|
1451 | | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0) |
|
|
1452 | | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0); |
|
|
1453 | } |
|
|
1454 | |
|
|
1455 | return mlockall (flags); |
|
|
1456 | } |
|
|
1457 | #endif |
|
|
1458 | |
|
|
1459 | #if !_POSIX_MEMLOCK_RANGE |
|
|
1460 | # define eio__mlock(a,b) ((errno = ENOSYS), -1) |
|
|
1461 | #else |
|
|
1462 | |
|
|
1463 | static int |
|
|
1464 | eio__mlock (void *addr, size_t length) |
|
|
1465 | { |
|
|
1466 | eio_page_align (&addr, &length); |
|
|
1467 | |
|
|
1468 | return mlock (addr, length); |
|
|
1469 | } |
|
|
1470 | |
|
|
1471 | #endif |
|
|
1472 | |
|
|
1473 | #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) |
|
|
1474 | # define eio__msync(a,b,c) ((errno = ENOSYS), -1) |
|
|
1475 | #else |
|
|
1476 | |
|
|
1477 | int |
|
|
1478 | eio__msync (void *mem, size_t len, int flags) |
|
|
1479 | { |
|
|
1480 | eio_page_align (&mem, &len); |
|
|
1481 | |
|
|
1482 | if (EIO_MS_ASYNC != MS_SYNC |
|
|
1483 | || EIO_MS_INVALIDATE != MS_INVALIDATE |
|
|
1484 | || EIO_MS_SYNC != MS_SYNC) |
|
|
1485 | { |
|
|
1486 | flags = 0 |
|
|
1487 | | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0) |
|
|
1488 | | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0) |
|
|
1489 | | (flags & EIO_MS_SYNC ? MS_SYNC : 0); |
|
|
1490 | } |
|
|
1491 | |
|
|
1492 | return msync (mem, len, flags); |
|
|
1493 | } |
|
|
1494 | |
|
|
1495 | #endif |
|
|
1496 | |
|
|
1497 | int |
|
|
1498 | eio__mtouch (void *mem, size_t len, int flags) |
|
|
1499 | { |
|
|
1500 | eio_page_align (&mem, &len); |
|
|
1501 | |
|
|
1502 | { |
|
|
1503 | intptr_t addr = (intptr_t)mem; |
|
|
1504 | intptr_t end = addr + len; |
|
|
1505 | intptr_t page = eio_pagesize (); |
|
|
1506 | |
|
|
1507 | if (addr < end) |
|
|
1508 | if (flags & EIO_MT_MODIFY) /* modify */ |
|
|
1509 | do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len); |
|
|
1510 | else |
|
|
1511 | do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len); |
|
|
1512 | } |
|
|
1513 | |
|
|
1514 | return 0; |
|
|
1515 | } |
|
|
1516 | |
|
|
1517 | /*****************************************************************************/ |
1777 | /*****************************************************************************/ |
1518 | |
1778 | |
1519 | #define ALLOC(len) \ |
1779 | #define ALLOC(len) \ |
1520 | if (!req->ptr2) \ |
1780 | if (!req->ptr2) \ |
1521 | { \ |
1781 | { \ |
… | |
… | |
1534 | X_THREAD_PROC (etp_proc) |
1794 | X_THREAD_PROC (etp_proc) |
1535 | { |
1795 | { |
1536 | ETP_REQ *req; |
1796 | ETP_REQ *req; |
1537 | struct timespec ts; |
1797 | struct timespec ts; |
1538 | etp_worker *self = (etp_worker *)thr_arg; |
1798 | etp_worker *self = (etp_worker *)thr_arg; |
|
|
1799 | int timeout; |
1539 | |
1800 | |
1540 | /* try to distribute timeouts somewhat randomly */ |
1801 | /* try to distribute timeouts somewhat evenly */ |
1541 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
1802 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
1542 | |
1803 | |
1543 | for (;;) |
1804 | for (;;) |
1544 | { |
1805 | { |
|
|
1806 | ts.tv_sec = 0; |
|
|
1807 | |
1545 | X_LOCK (reqlock); |
1808 | X_LOCK (reqlock); |
1546 | |
1809 | |
1547 | for (;;) |
1810 | for (;;) |
1548 | { |
1811 | { |
1549 | self->req = req = reqq_shift (&req_queue); |
1812 | self->req = req = reqq_shift (&req_queue); |
1550 | |
1813 | |
1551 | if (req) |
1814 | if (req) |
1552 | break; |
1815 | break; |
1553 | |
1816 | |
|
|
1817 | if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */ |
|
|
1818 | { |
|
|
1819 | X_UNLOCK (reqlock); |
|
|
1820 | X_LOCK (wrklock); |
|
|
1821 | --started; |
|
|
1822 | X_UNLOCK (wrklock); |
|
|
1823 | goto quit; |
|
|
1824 | } |
|
|
1825 | |
1554 | ++idle; |
1826 | ++idle; |
1555 | |
1827 | |
1556 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1828 | if (idle <= max_idle) |
1557 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) |
1829 | /* we are allowed to idle, so do so without any timeout */ |
|
|
1830 | X_COND_WAIT (reqwait, reqlock); |
|
|
1831 | else |
1558 | { |
1832 | { |
1559 | if (idle > max_idle) |
1833 | /* initialise timeout once */ |
1560 | { |
1834 | if (!ts.tv_sec) |
1561 | --idle; |
1835 | ts.tv_sec = time (0) + idle_timeout; |
1562 | X_UNLOCK (reqlock); |
|
|
1563 | X_LOCK (wrklock); |
|
|
1564 | --started; |
|
|
1565 | X_UNLOCK (wrklock); |
|
|
1566 | goto quit; |
|
|
1567 | } |
|
|
1568 | |
1836 | |
1569 | /* we are allowed to idle, so do so without any timeout */ |
|
|
1570 | X_COND_WAIT (reqwait, reqlock); |
1837 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) |
|
|
1838 | ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */ |
1571 | } |
1839 | } |
1572 | |
1840 | |
1573 | --idle; |
1841 | --idle; |
1574 | } |
1842 | } |
1575 | |
1843 | |
… | |
… | |
1578 | X_UNLOCK (reqlock); |
1846 | X_UNLOCK (reqlock); |
1579 | |
1847 | |
1580 | if (req->type < 0) |
1848 | if (req->type < 0) |
1581 | goto quit; |
1849 | goto quit; |
1582 | |
1850 | |
1583 | if (!EIO_CANCELLED (req)) |
|
|
1584 | ETP_EXECUTE (self, req); |
1851 | ETP_EXECUTE (self, req); |
1585 | |
1852 | |
1586 | X_LOCK (reslock); |
1853 | X_LOCK (reslock); |
1587 | |
1854 | |
1588 | ++npending; |
1855 | ++npending; |
1589 | |
1856 | |
… | |
… | |
1604 | return 0; |
1871 | return 0; |
1605 | } |
1872 | } |
1606 | |
1873 | |
1607 | /*****************************************************************************/ |
1874 | /*****************************************************************************/ |
1608 | |
1875 | |
|
|
1876 | int ecb_cold |
1609 | int eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1877 | eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1610 | { |
1878 | { |
1611 | return etp_init (want_poll, done_poll); |
1879 | return etp_init (want_poll, done_poll); |
1612 | } |
1880 | } |
1613 | |
1881 | |
|
|
1882 | ecb_inline void |
1614 | static void eio_api_destroy (eio_req *req) |
1883 | eio_api_destroy (eio_req *req) |
1615 | { |
1884 | { |
1616 | free (req); |
1885 | free (req); |
1617 | } |
1886 | } |
1618 | |
1887 | |
1619 | #define REQ(rtype) \ |
1888 | #define REQ(rtype) \ |
… | |
… | |
1638 | { \ |
1907 | { \ |
1639 | eio_api_destroy (req); \ |
1908 | eio_api_destroy (req); \ |
1640 | return 0; \ |
1909 | return 0; \ |
1641 | } |
1910 | } |
1642 | |
1911 | |
|
|
1912 | static void |
1643 | static void eio_execute (etp_worker *self, eio_req *req) |
1913 | eio_execute (etp_worker *self, eio_req *req) |
1644 | { |
1914 | { |
|
|
1915 | if (ecb_expect_false (EIO_CANCELLED (req))) |
|
|
1916 | { |
|
|
1917 | req->result = -1; |
|
|
1918 | req->errorno = ECANCELED; |
|
|
1919 | return; |
|
|
1920 | } |
|
|
1921 | |
1645 | switch (req->type) |
1922 | switch (req->type) |
1646 | { |
1923 | { |
1647 | case EIO_READ: ALLOC (req->size); |
1924 | case EIO_READ: ALLOC (req->size); |
1648 | req->result = req->offs >= 0 |
1925 | req->result = req->offs >= 0 |
1649 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
1926 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
… | |
… | |
1683 | case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; |
1960 | case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; |
1684 | case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; |
1961 | case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; |
1685 | case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; |
1962 | case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; |
1686 | case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
1963 | case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
1687 | |
1964 | |
|
|
1965 | case EIO_REALPATH: eio__realpath (req, self); break; |
|
|
1966 | |
1688 | case EIO_READLINK: ALLOC (PATH_MAX); |
1967 | case EIO_READLINK: ALLOC (PATH_MAX); |
1689 | req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break; |
1968 | req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break; |
1690 | |
1969 | |
1691 | case EIO_SYNC: req->result = 0; sync (); break; |
1970 | case EIO_SYNC: req->result = 0; sync (); break; |
1692 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
1971 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
1693 | case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; |
1972 | case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; |
1694 | case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; |
1973 | case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; |
1695 | case EIO_MTOUCH: req->result = eio__mtouch (req->ptr2, req->size, req->int1); break; |
1974 | case EIO_MTOUCH: req->result = eio__mtouch (req); break; |
1696 | case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; |
1975 | case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; |
1697 | case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; |
1976 | case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; |
1698 | case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; |
1977 | case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; |
|
|
1978 | case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break; |
1699 | |
1979 | |
1700 | case EIO_READDIR: eio__scandir (req, self); break; |
1980 | case EIO_READDIR: eio__scandir (req, self); break; |
1701 | |
1981 | |
1702 | case EIO_BUSY: |
1982 | case EIO_BUSY: |
1703 | #ifdef _WIN32 |
1983 | #ifdef _WIN32 |
… | |
… | |
1744 | case EIO_NOP: |
2024 | case EIO_NOP: |
1745 | req->result = 0; |
2025 | req->result = 0; |
1746 | break; |
2026 | break; |
1747 | |
2027 | |
1748 | case EIO_CUSTOM: |
2028 | case EIO_CUSTOM: |
1749 | ((void (*)(eio_req *))req->feed) (req); |
2029 | req->feed (req); |
1750 | break; |
2030 | break; |
1751 | |
2031 | |
1752 | default: |
2032 | default: |
1753 | errno = ENOSYS; |
2033 | errno = ENOSYS; |
1754 | req->result = -1; |
2034 | req->result = -1; |
… | |
… | |
1803 | eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) |
2083 | eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) |
1804 | { |
2084 | { |
1805 | REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; |
2085 | REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; |
1806 | } |
2086 | } |
1807 | |
2087 | |
|
|
2088 | eio_req *eio_fallocate (int fd, int mode, off_t offset, size_t len, int pri, eio_cb cb, void *data) |
|
|
2089 | { |
|
|
2090 | REQ (EIO_FALLOCATE); req->int1 = fd; req->int2 = mode; req->offs = offset; req->size = len; SEND; |
|
|
2091 | } |
|
|
2092 | |
1808 | eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) |
2093 | eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) |
1809 | { |
2094 | { |
1810 | REQ (EIO_FDATASYNC); req->int1 = fd; SEND; |
2095 | REQ (EIO_FDATASYNC); req->int1 = fd; SEND; |
1811 | } |
2096 | } |
1812 | |
2097 | |
… | |
… | |
1907 | } |
2192 | } |
1908 | |
2193 | |
1909 | eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data) |
2194 | eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data) |
1910 | { |
2195 | { |
1911 | return eio__1path (EIO_READLINK, path, pri, cb, data); |
2196 | return eio__1path (EIO_READLINK, path, pri, cb, data); |
|
|
2197 | } |
|
|
2198 | |
|
|
2199 | eio_req *eio_realpath (const char *path, int pri, eio_cb cb, void *data) |
|
|
2200 | { |
|
|
2201 | return eio__1path (EIO_REALPATH, path, pri, cb, data); |
1912 | } |
2202 | } |
1913 | |
2203 | |
1914 | eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data) |
2204 | eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data) |
1915 | { |
2205 | { |
1916 | return eio__1path (EIO_STAT, path, pri, cb, data); |
2206 | return eio__1path (EIO_STAT, path, pri, cb, data); |
… | |
… | |
1975 | eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
2265 | eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1976 | { |
2266 | { |
1977 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
2267 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
1978 | } |
2268 | } |
1979 | |
2269 | |
1980 | eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) |
2270 | eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data) |
1981 | { |
2271 | { |
1982 | REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND; |
2272 | REQ (EIO_CUSTOM); req->feed = execute; SEND; |
1983 | } |
2273 | } |
1984 | |
2274 | |
1985 | #endif |
2275 | #endif |
1986 | |
2276 | |
1987 | eio_req *eio_grp (eio_cb cb, void *data) |
2277 | eio_req *eio_grp (eio_cb cb, void *data) |
… | |
… | |
1996 | #undef SEND |
2286 | #undef SEND |
1997 | |
2287 | |
1998 | /*****************************************************************************/ |
2288 | /*****************************************************************************/ |
1999 | /* grp functions */ |
2289 | /* grp functions */ |
2000 | |
2290 | |
|
|
2291 | void |
2001 | void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) |
2292 | eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) |
2002 | { |
2293 | { |
2003 | grp->int2 = limit; |
2294 | grp->int2 = limit; |
2004 | grp->feed = feed; |
2295 | grp->feed = feed; |
2005 | |
2296 | |
2006 | grp_try_feed (grp); |
2297 | grp_try_feed (grp); |
2007 | } |
2298 | } |
2008 | |
2299 | |
|
|
2300 | void |
2009 | void eio_grp_limit (eio_req *grp, int limit) |
2301 | eio_grp_limit (eio_req *grp, int limit) |
2010 | { |
2302 | { |
2011 | grp->int2 = limit; |
2303 | grp->int2 = limit; |
2012 | |
2304 | |
2013 | grp_try_feed (grp); |
2305 | grp_try_feed (grp); |
2014 | } |
2306 | } |
2015 | |
2307 | |
|
|
2308 | void |
2016 | void eio_grp_add (eio_req *grp, eio_req *req) |
2309 | eio_grp_add (eio_req *grp, eio_req *req) |
2017 | { |
2310 | { |
2018 | assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); |
2311 | assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); |
2019 | |
2312 | |
2020 | grp->flags |= EIO_FLAG_GROUPADD; |
2313 | grp->flags |= EIO_FLAG_GROUPADD; |
2021 | |
2314 | |
… | |
… | |
2032 | } |
2325 | } |
2033 | |
2326 | |
2034 | /*****************************************************************************/ |
2327 | /*****************************************************************************/ |
2035 | /* misc garbage */ |
2328 | /* misc garbage */ |
2036 | |
2329 | |
|
|
2330 | ssize_t |
2037 | ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) |
2331 | eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) |
2038 | { |
2332 | { |
2039 | etp_worker wrk; |
2333 | etp_worker wrk; |
2040 | ssize_t ret; |
2334 | ssize_t ret; |
2041 | |
2335 | |
2042 | wrk.dbuf = 0; |
2336 | wrk.dbuf = 0; |