1 | /* |
1 | /* |
2 | * libeio implementation |
2 | * libeio implementation |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libeio@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libeio@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
… | |
… | |
35 | * and other provisions required by the GPL. If you do not delete the |
35 | * and other provisions required by the GPL. If you do not delete the |
36 | * provisions above, a recipient may use your version of this file under |
36 | * provisions above, a recipient may use your version of this file under |
37 | * either the BSD or the GPL. |
37 | * either the BSD or the GPL. |
38 | */ |
38 | */ |
39 | |
39 | |
|
|
40 | #ifndef _WIN32 |
|
|
41 | # include "config.h" |
|
|
42 | #endif |
|
|
43 | |
40 | #include "eio.h" |
44 | #include "eio.h" |
|
|
45 | #include "ecb.h" |
41 | |
46 | |
42 | #ifdef EIO_STACKSIZE |
47 | #ifdef EIO_STACKSIZE |
43 | # define XTHREAD_STACKSIZE EIO_STACKSIZE |
48 | # define XTHREAD_STACKSIZE EIO_STACKSIZE |
44 | #endif |
49 | #endif |
45 | #include "xthread.h" |
50 | #include "xthread.h" |
… | |
… | |
54 | #include <sys/statvfs.h> |
59 | #include <sys/statvfs.h> |
55 | #include <limits.h> |
60 | #include <limits.h> |
56 | #include <fcntl.h> |
61 | #include <fcntl.h> |
57 | #include <assert.h> |
62 | #include <assert.h> |
58 | |
63 | |
|
|
64 | /* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ |
|
|
65 | /* intptr_t only comes form stdint.h, says idiot openbsd coder */ |
|
|
66 | #if HAVE_STDINT_H |
|
|
67 | # include <stdint.h> |
|
|
68 | #endif |
|
|
69 | |
59 | #ifndef EIO_FINISH |
70 | #ifndef EIO_FINISH |
60 | # define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 |
71 | # define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 |
61 | #endif |
72 | #endif |
62 | |
73 | |
63 | #ifndef EIO_DESTROY |
74 | #ifndef EIO_DESTROY |
… | |
… | |
71 | #ifdef _WIN32 |
82 | #ifdef _WIN32 |
72 | |
83 | |
73 | /*doh*/ |
84 | /*doh*/ |
74 | #else |
85 | #else |
75 | |
86 | |
76 | # include "config.h" |
|
|
77 | # include <sys/time.h> |
87 | # include <sys/time.h> |
78 | # include <sys/select.h> |
88 | # include <sys/select.h> |
79 | # include <sys/mman.h> |
|
|
80 | # include <unistd.h> |
89 | # include <unistd.h> |
81 | # include <utime.h> |
90 | # include <utime.h> |
82 | # include <signal.h> |
91 | # include <signal.h> |
83 | # include <dirent.h> |
92 | # include <dirent.h> |
|
|
93 | |
|
|
94 | #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES |
|
|
95 | # include <sys/mman.h> |
|
|
96 | #endif |
84 | |
97 | |
85 | /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ |
98 | /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ |
86 | # if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ |
99 | # if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ |
87 | # define _DIRENT_HAVE_D_TYPE /* sigh */ |
100 | # define _DIRENT_HAVE_D_TYPE /* sigh */ |
88 | # define D_INO(de) (de)->d_fileno |
101 | # define D_INO(de) (de)->d_fileno |
… | |
… | |
129 | #endif |
142 | #endif |
130 | #ifndef D_NAMLEN |
143 | #ifndef D_NAMLEN |
131 | # define D_NAMLEN(de) strlen ((de)->d_name) |
144 | # define D_NAMLEN(de) strlen ((de)->d_name) |
132 | #endif |
145 | #endif |
133 | |
146 | |
134 | /* number of seconds after which an idle threads exit */ |
|
|
135 | #define IDLE_TIMEOUT 10 |
|
|
136 | |
|
|
137 | /* used for struct dirent, AIX doesn't provide it */ |
147 | /* used for struct dirent, AIX doesn't provide it */ |
138 | #ifndef NAME_MAX |
148 | #ifndef NAME_MAX |
139 | # define NAME_MAX 4096 |
149 | # define NAME_MAX 4096 |
|
|
150 | #endif |
|
|
151 | |
|
|
152 | /* used for readlink etc. */ |
|
|
153 | #ifndef PATH_MAX |
|
|
154 | # define PATH_MAX 4096 |
140 | #endif |
155 | #endif |
141 | |
156 | |
142 | /* buffer size for various temporary buffers */ |
157 | /* buffer size for various temporary buffers */ |
143 | #define EIO_BUFSIZE 65536 |
158 | #define EIO_BUFSIZE 65536 |
144 | |
159 | |
… | |
… | |
150 | errno = ENOMEM; \ |
165 | errno = ENOMEM; \ |
151 | if (!eio_buf) \ |
166 | if (!eio_buf) \ |
152 | return -1; |
167 | return -1; |
153 | |
168 | |
154 | #define EIO_TICKS ((1000000 + 1023) >> 10) |
169 | #define EIO_TICKS ((1000000 + 1023) >> 10) |
155 | |
|
|
156 | /*****************************************************************************/ |
|
|
157 | |
|
|
158 | #if __GNUC__ >= 3 |
|
|
159 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
160 | #else |
|
|
161 | # define expect(expr,value) (expr) |
|
|
162 | #endif |
|
|
163 | |
|
|
164 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
165 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
166 | |
|
|
167 | /*****************************************************************************/ |
|
|
168 | |
170 | |
169 | #define ETP_PRI_MIN EIO_PRI_MIN |
171 | #define ETP_PRI_MIN EIO_PRI_MIN |
170 | #define ETP_PRI_MAX EIO_PRI_MAX |
172 | #define ETP_PRI_MAX EIO_PRI_MAX |
171 | |
173 | |
172 | struct etp_worker; |
174 | struct etp_worker; |
… | |
… | |
198 | /*****************************************************************************/ |
200 | /*****************************************************************************/ |
199 | |
201 | |
200 | #define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) |
202 | #define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) |
201 | |
203 | |
202 | /* calculate time difference in ~1/EIO_TICKS of a second */ |
204 | /* calculate time difference in ~1/EIO_TICKS of a second */ |
|
|
205 | ECB_INLINE int |
203 | static int tvdiff (struct timeval *tv1, struct timeval *tv2) |
206 | tvdiff (struct timeval *tv1, struct timeval *tv2) |
204 | { |
207 | { |
205 | return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS |
208 | return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS |
206 | + ((tv2->tv_usec - tv1->tv_usec) >> 10); |
209 | + ((tv2->tv_usec - tv1->tv_usec) >> 10); |
207 | } |
210 | } |
208 | |
211 | |
… | |
… | |
215 | static unsigned int max_poll_reqs; /* reslock */ |
218 | static unsigned int max_poll_reqs; /* reslock */ |
216 | |
219 | |
217 | static volatile unsigned int nreqs; /* reqlock */ |
220 | static volatile unsigned int nreqs; /* reqlock */ |
218 | static volatile unsigned int nready; /* reqlock */ |
221 | static volatile unsigned int nready; /* reqlock */ |
219 | static volatile unsigned int npending; /* reqlock */ |
222 | static volatile unsigned int npending; /* reqlock */ |
220 | static volatile unsigned int max_idle = 4; |
223 | static volatile unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */ |
|
|
224 | static volatile unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */ |
221 | |
225 | |
222 | static mutex_t wrklock = X_MUTEX_INIT; |
226 | static xmutex_t wrklock; |
223 | static mutex_t reslock = X_MUTEX_INIT; |
227 | static xmutex_t reslock; |
224 | static mutex_t reqlock = X_MUTEX_INIT; |
228 | static xmutex_t reqlock; |
225 | static cond_t reqwait = X_COND_INIT; |
229 | static xcond_t reqwait; |
226 | |
230 | |
227 | #if !HAVE_PREADWRITE |
231 | #if !HAVE_PREADWRITE |
228 | /* |
232 | /* |
229 | * make our pread/pwrite emulation safe against themselves, but not against |
233 | * make our pread/pwrite emulation safe against themselves, but not against |
230 | * normal read/write by using a mutex. slows down execution a lot, |
234 | * normal read/write by using a mutex. slows down execution a lot, |
231 | * but that's your problem, not mine. |
235 | * but that's your problem, not mine. |
232 | */ |
236 | */ |
233 | static mutex_t preadwritelock = X_MUTEX_INIT; |
237 | static xmutex_t preadwritelock = X_MUTEX_INIT; |
234 | #endif |
238 | #endif |
235 | |
239 | |
236 | typedef struct etp_worker |
240 | typedef struct etp_worker |
237 | { |
241 | { |
238 | /* locked by wrklock */ |
242 | /* locked by wrklock */ |
239 | struct etp_worker *prev, *next; |
243 | struct etp_worker *prev, *next; |
240 | |
244 | |
241 | thread_t tid; |
245 | xthread_t tid; |
242 | |
246 | |
243 | /* locked by reslock, reqlock or wrklock */ |
247 | /* locked by reslock, reqlock or wrklock */ |
244 | ETP_REQ *req; /* currently processed request */ |
248 | ETP_REQ *req; /* currently processed request */ |
245 | |
249 | |
246 | ETP_WORKER_COMMON |
250 | ETP_WORKER_COMMON |
… | |
… | |
251 | #define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) |
255 | #define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) |
252 | #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) |
256 | #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) |
253 | |
257 | |
254 | /* worker threads management */ |
258 | /* worker threads management */ |
255 | |
259 | |
|
|
260 | static void ecb_cold |
256 | static void etp_worker_clear (etp_worker *wrk) |
261 | etp_worker_clear (etp_worker *wrk) |
257 | { |
262 | { |
258 | ETP_WORKER_CLEAR (wrk); |
263 | ETP_WORKER_CLEAR (wrk); |
259 | } |
264 | } |
260 | |
265 | |
|
|
266 | static void ecb_cold |
261 | static void etp_worker_free (etp_worker *wrk) |
267 | etp_worker_free (etp_worker *wrk) |
262 | { |
268 | { |
263 | wrk->next->prev = wrk->prev; |
269 | wrk->next->prev = wrk->prev; |
264 | wrk->prev->next = wrk->next; |
270 | wrk->prev->next = wrk->next; |
265 | |
271 | |
266 | free (wrk); |
272 | free (wrk); |
267 | } |
273 | } |
268 | |
274 | |
269 | static unsigned int etp_nreqs (void) |
275 | static unsigned int |
|
|
276 | etp_nreqs (void) |
270 | { |
277 | { |
271 | int retval; |
278 | int retval; |
272 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
279 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
273 | retval = nreqs; |
280 | retval = nreqs; |
274 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
281 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
275 | return retval; |
282 | return retval; |
276 | } |
283 | } |
277 | |
284 | |
278 | static unsigned int etp_nready (void) |
285 | static unsigned int |
|
|
286 | etp_nready (void) |
279 | { |
287 | { |
280 | unsigned int retval; |
288 | unsigned int retval; |
281 | |
289 | |
282 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
290 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
283 | retval = nready; |
291 | retval = nready; |
284 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
292 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
285 | |
293 | |
286 | return retval; |
294 | return retval; |
287 | } |
295 | } |
288 | |
296 | |
289 | static unsigned int etp_npending (void) |
297 | static unsigned int |
|
|
298 | etp_npending (void) |
290 | { |
299 | { |
291 | unsigned int retval; |
300 | unsigned int retval; |
292 | |
301 | |
293 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
302 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
294 | retval = npending; |
303 | retval = npending; |
295 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
304 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
296 | |
305 | |
297 | return retval; |
306 | return retval; |
298 | } |
307 | } |
299 | |
308 | |
300 | static unsigned int etp_nthreads (void) |
309 | static unsigned int |
|
|
310 | etp_nthreads (void) |
301 | { |
311 | { |
302 | unsigned int retval; |
312 | unsigned int retval; |
303 | |
313 | |
304 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
314 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
305 | retval = started; |
315 | retval = started; |
… | |
… | |
319 | } etp_reqq; |
329 | } etp_reqq; |
320 | |
330 | |
321 | static etp_reqq req_queue; |
331 | static etp_reqq req_queue; |
322 | static etp_reqq res_queue; |
332 | static etp_reqq res_queue; |
323 | |
333 | |
|
|
334 | static int ecb_noinline |
324 | static int reqq_push (etp_reqq *q, ETP_REQ *req) |
335 | reqq_push (etp_reqq *q, ETP_REQ *req) |
325 | { |
336 | { |
326 | int pri = req->pri; |
337 | int pri = req->pri; |
327 | req->next = 0; |
338 | req->next = 0; |
328 | |
339 | |
329 | if (q->qe[pri]) |
340 | if (q->qe[pri]) |
… | |
… | |
335 | q->qe[pri] = q->qs[pri] = req; |
346 | q->qe[pri] = q->qs[pri] = req; |
336 | |
347 | |
337 | return q->size++; |
348 | return q->size++; |
338 | } |
349 | } |
339 | |
350 | |
|
|
351 | static ETP_REQ * ecb_noinline |
340 | static ETP_REQ *reqq_shift (etp_reqq *q) |
352 | reqq_shift (etp_reqq *q) |
341 | { |
353 | { |
342 | int pri; |
354 | int pri; |
343 | |
355 | |
344 | if (!q->size) |
356 | if (!q->size) |
345 | return 0; |
357 | return 0; |
… | |
… | |
360 | } |
372 | } |
361 | |
373 | |
362 | abort (); |
374 | abort (); |
363 | } |
375 | } |
364 | |
376 | |
|
|
377 | static void ecb_cold |
|
|
378 | etp_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 | |
|
|
386 | static void ecb_cold |
365 | static void etp_atfork_prepare (void) |
387 | etp_atfork_prepare (void) |
366 | { |
388 | { |
367 | X_LOCK (wrklock); |
389 | X_LOCK (wrklock); |
368 | X_LOCK (reqlock); |
390 | X_LOCK (reqlock); |
369 | X_LOCK (reslock); |
391 | X_LOCK (reslock); |
370 | #if !HAVE_PREADWRITE |
392 | #if !HAVE_PREADWRITE |
371 | X_LOCK (preadwritelock); |
393 | X_LOCK (preadwritelock); |
372 | #endif |
394 | #endif |
373 | } |
395 | } |
374 | |
396 | |
|
|
397 | static void ecb_cold |
375 | static void etp_atfork_parent (void) |
398 | etp_atfork_parent (void) |
376 | { |
399 | { |
377 | #if !HAVE_PREADWRITE |
400 | #if !HAVE_PREADWRITE |
378 | X_UNLOCK (preadwritelock); |
401 | X_UNLOCK (preadwritelock); |
379 | #endif |
402 | #endif |
380 | X_UNLOCK (reslock); |
403 | X_UNLOCK (reslock); |
381 | X_UNLOCK (reqlock); |
404 | X_UNLOCK (reqlock); |
382 | X_UNLOCK (wrklock); |
405 | X_UNLOCK (wrklock); |
383 | } |
406 | } |
384 | |
407 | |
|
|
408 | static void ecb_cold |
385 | static void etp_atfork_child (void) |
409 | etp_atfork_child (void) |
386 | { |
410 | { |
387 | ETP_REQ *prv; |
411 | ETP_REQ *prv; |
388 | |
412 | |
389 | while ((prv = reqq_shift (&req_queue))) |
413 | while ((prv = reqq_shift (&req_queue))) |
390 | ETP_DESTROY (prv); |
414 | ETP_DESTROY (prv); |
… | |
… | |
407 | idle = 0; |
431 | idle = 0; |
408 | nreqs = 0; |
432 | nreqs = 0; |
409 | nready = 0; |
433 | nready = 0; |
410 | npending = 0; |
434 | npending = 0; |
411 | |
435 | |
412 | etp_atfork_parent (); |
436 | etp_thread_init (); |
413 | } |
437 | } |
414 | |
438 | |
415 | static void |
439 | static void ecb_cold |
416 | etp_once_init (void) |
440 | etp_once_init (void) |
417 | { |
441 | { |
|
|
442 | etp_thread_init (); |
418 | X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child); |
443 | X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child); |
419 | } |
444 | } |
420 | |
445 | |
421 | static int |
446 | static int ecb_cold |
422 | etp_init (void (*want_poll)(void), void (*done_poll)(void)) |
447 | etp_init (void (*want_poll)(void), void (*done_poll)(void)) |
423 | { |
448 | { |
424 | static pthread_once_t doinit = PTHREAD_ONCE_INIT; |
449 | static pthread_once_t doinit = PTHREAD_ONCE_INIT; |
425 | |
450 | |
426 | pthread_once (&doinit, etp_once_init); |
451 | pthread_once (&doinit, etp_once_init); |
… | |
… | |
431 | return 0; |
456 | return 0; |
432 | } |
457 | } |
433 | |
458 | |
434 | X_THREAD_PROC (etp_proc); |
459 | X_THREAD_PROC (etp_proc); |
435 | |
460 | |
|
|
461 | static void ecb_cold |
436 | static void etp_start_thread (void) |
462 | etp_start_thread (void) |
437 | { |
463 | { |
438 | etp_worker *wrk = calloc (1, sizeof (etp_worker)); |
464 | etp_worker *wrk = calloc (1, sizeof (etp_worker)); |
439 | |
465 | |
440 | /*TODO*/ |
466 | /*TODO*/ |
441 | assert (("unable to allocate worker thread data", wrk)); |
467 | assert (("unable to allocate worker thread data", wrk)); |
… | |
… | |
454 | free (wrk); |
480 | free (wrk); |
455 | |
481 | |
456 | X_UNLOCK (wrklock); |
482 | X_UNLOCK (wrklock); |
457 | } |
483 | } |
458 | |
484 | |
|
|
485 | static void |
459 | static void etp_maybe_start_thread (void) |
486 | etp_maybe_start_thread (void) |
460 | { |
487 | { |
461 | if (expect_true (etp_nthreads () >= wanted)) |
488 | if (ecb_expect_true (etp_nthreads () >= wanted)) |
462 | return; |
489 | return; |
463 | |
490 | |
464 | /* todo: maybe use idle here, but might be less exact */ |
491 | /* todo: maybe use idle here, but might be less exact */ |
465 | if (expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) |
492 | if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) |
466 | return; |
493 | return; |
467 | |
494 | |
468 | etp_start_thread (); |
495 | etp_start_thread (); |
469 | } |
496 | } |
470 | |
497 | |
|
|
498 | static void ecb_cold |
471 | static void etp_end_thread (void) |
499 | etp_end_thread (void) |
472 | { |
500 | { |
473 | eio_req *req = calloc (1, sizeof (eio_req)); |
501 | eio_req *req = calloc (1, sizeof (eio_req)); |
474 | |
502 | |
475 | req->type = -1; |
503 | req->type = -1; |
476 | req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
504 | req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
… | |
… | |
483 | X_LOCK (wrklock); |
511 | X_LOCK (wrklock); |
484 | --started; |
512 | --started; |
485 | X_UNLOCK (wrklock); |
513 | X_UNLOCK (wrklock); |
486 | } |
514 | } |
487 | |
515 | |
488 | static int etp_poll (void) |
516 | static int |
|
|
517 | etp_poll (void) |
489 | { |
518 | { |
490 | unsigned int maxreqs; |
519 | unsigned int maxreqs; |
491 | unsigned int maxtime; |
520 | unsigned int maxtime; |
492 | struct timeval tv_start, tv_now; |
521 | struct timeval tv_start, tv_now; |
493 | |
522 | |
… | |
… | |
523 | |
552 | |
524 | X_LOCK (reqlock); |
553 | X_LOCK (reqlock); |
525 | --nreqs; |
554 | --nreqs; |
526 | X_UNLOCK (reqlock); |
555 | X_UNLOCK (reqlock); |
527 | |
556 | |
528 | if (expect_false (req->type == EIO_GROUP && req->size)) |
557 | if (ecb_expect_false (req->type == EIO_GROUP && req->size)) |
529 | { |
558 | { |
530 | req->int1 = 1; /* mark request as delayed */ |
559 | req->int1 = 1; /* mark request as delayed */ |
531 | continue; |
560 | continue; |
532 | } |
561 | } |
533 | else |
562 | else |
534 | { |
563 | { |
535 | int res = ETP_FINISH (req); |
564 | int res = ETP_FINISH (req); |
536 | if (expect_false (res)) |
565 | if (ecb_expect_false (res)) |
537 | return res; |
566 | return res; |
538 | } |
567 | } |
539 | |
568 | |
540 | if (expect_false (maxreqs && !--maxreqs)) |
569 | if (ecb_expect_false (maxreqs && !--maxreqs)) |
541 | break; |
570 | break; |
542 | |
571 | |
543 | if (maxtime) |
572 | if (maxtime) |
544 | { |
573 | { |
545 | gettimeofday (&tv_now, 0); |
574 | gettimeofday (&tv_now, 0); |
… | |
… | |
551 | |
580 | |
552 | errno = EAGAIN; |
581 | errno = EAGAIN; |
553 | return -1; |
582 | return -1; |
554 | } |
583 | } |
555 | |
584 | |
|
|
585 | static void |
556 | static void etp_cancel (ETP_REQ *req) |
586 | etp_cancel (ETP_REQ *req) |
557 | { |
587 | { |
558 | X_LOCK (wrklock); |
588 | X_LOCK (wrklock); |
559 | req->flags |= EIO_FLAG_CANCELLED; |
589 | req->flags |= EIO_FLAG_CANCELLED; |
560 | X_UNLOCK (wrklock); |
590 | X_UNLOCK (wrklock); |
561 | |
591 | |
562 | eio_grp_cancel (req); |
592 | eio_grp_cancel (req); |
563 | } |
593 | } |
564 | |
594 | |
|
|
595 | static void |
565 | static void etp_submit (ETP_REQ *req) |
596 | etp_submit (ETP_REQ *req) |
566 | { |
597 | { |
567 | req->pri -= ETP_PRI_MIN; |
598 | req->pri -= ETP_PRI_MIN; |
568 | |
599 | |
569 | if (expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; |
600 | if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; |
570 | if (expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
601 | if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
571 | |
602 | |
572 | if (expect_false (req->type == EIO_GROUP)) |
603 | if (ecb_expect_false (req->type == EIO_GROUP)) |
573 | { |
604 | { |
574 | /* I hope this is worth it :/ */ |
605 | /* I hope this is worth it :/ */ |
575 | X_LOCK (reqlock); |
606 | X_LOCK (reqlock); |
576 | ++nreqs; |
607 | ++nreqs; |
577 | X_UNLOCK (reqlock); |
608 | X_UNLOCK (reqlock); |
… | |
… | |
596 | |
627 | |
597 | etp_maybe_start_thread (); |
628 | etp_maybe_start_thread (); |
598 | } |
629 | } |
599 | } |
630 | } |
600 | |
631 | |
|
|
632 | static void ecb_cold |
601 | static void etp_set_max_poll_time (double nseconds) |
633 | etp_set_max_poll_time (double nseconds) |
602 | { |
634 | { |
603 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
635 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
604 | max_poll_time = nseconds * EIO_TICKS; |
636 | max_poll_time = nseconds * EIO_TICKS; |
605 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
637 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
606 | } |
638 | } |
607 | |
639 | |
|
|
640 | static void ecb_cold |
608 | static void etp_set_max_poll_reqs (unsigned int maxreqs) |
641 | etp_set_max_poll_reqs (unsigned int maxreqs) |
609 | { |
642 | { |
610 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
643 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
611 | max_poll_reqs = maxreqs; |
644 | max_poll_reqs = maxreqs; |
612 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
645 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
613 | } |
646 | } |
614 | |
647 | |
|
|
648 | static void ecb_cold |
615 | static void etp_set_max_idle (unsigned int nthreads) |
649 | etp_set_max_idle (unsigned int nthreads) |
616 | { |
650 | { |
617 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
651 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
618 | max_idle = nthreads <= 0 ? 1 : nthreads; |
652 | max_idle = nthreads; |
619 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
653 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
620 | } |
654 | } |
621 | |
655 | |
|
|
656 | static void ecb_cold |
|
|
657 | etp_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 | |
|
|
664 | static void ecb_cold |
622 | static void etp_set_min_parallel (unsigned int nthreads) |
665 | etp_set_min_parallel (unsigned int nthreads) |
623 | { |
666 | { |
624 | if (wanted < nthreads) |
667 | if (wanted < nthreads) |
625 | wanted = nthreads; |
668 | wanted = nthreads; |
626 | } |
669 | } |
627 | |
670 | |
|
|
671 | static void ecb_cold |
628 | static void etp_set_max_parallel (unsigned int nthreads) |
672 | etp_set_max_parallel (unsigned int nthreads) |
629 | { |
673 | { |
630 | if (wanted > nthreads) |
674 | if (wanted > nthreads) |
631 | wanted = nthreads; |
675 | wanted = nthreads; |
632 | |
676 | |
633 | while (started > wanted) |
677 | while (started > wanted) |
634 | etp_end_thread (); |
678 | etp_end_thread (); |
635 | } |
679 | } |
636 | |
680 | |
637 | /*****************************************************************************/ |
681 | /*****************************************************************************/ |
638 | |
682 | |
|
|
683 | static void |
639 | static void grp_try_feed (eio_req *grp) |
684 | grp_try_feed (eio_req *grp) |
640 | { |
685 | { |
641 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
686 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
642 | { |
687 | { |
643 | grp->flags &= ~EIO_FLAG_GROUPADD; |
688 | grp->flags &= ~EIO_FLAG_GROUPADD; |
644 | |
689 | |
… | |
… | |
651 | break; |
696 | break; |
652 | } |
697 | } |
653 | } |
698 | } |
654 | } |
699 | } |
655 | |
700 | |
|
|
701 | static int |
656 | static int grp_dec (eio_req *grp) |
702 | grp_dec (eio_req *grp) |
657 | { |
703 | { |
658 | --grp->size; |
704 | --grp->size; |
659 | |
705 | |
660 | /* call feeder, if applicable */ |
706 | /* call feeder, if applicable */ |
661 | grp_try_feed (grp); |
707 | grp_try_feed (grp); |
… | |
… | |
665 | return eio_finish (grp); |
711 | return eio_finish (grp); |
666 | else |
712 | else |
667 | return 0; |
713 | return 0; |
668 | } |
714 | } |
669 | |
715 | |
|
|
716 | void |
670 | void eio_destroy (eio_req *req) |
717 | eio_destroy (eio_req *req) |
671 | { |
718 | { |
672 | if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); |
719 | if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); |
673 | if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); |
720 | if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); |
674 | |
721 | |
675 | EIO_DESTROY (req); |
722 | EIO_DESTROY (req); |
676 | } |
723 | } |
677 | |
724 | |
|
|
725 | static int |
678 | static int eio_finish (eio_req *req) |
726 | eio_finish (eio_req *req) |
679 | { |
727 | { |
680 | int res = EIO_FINISH (req); |
728 | int res = EIO_FINISH (req); |
681 | |
729 | |
682 | if (req->grp) |
730 | if (req->grp) |
683 | { |
731 | { |
… | |
… | |
700 | eio_destroy (req); |
748 | eio_destroy (req); |
701 | |
749 | |
702 | return res; |
750 | return res; |
703 | } |
751 | } |
704 | |
752 | |
|
|
753 | void |
705 | void eio_grp_cancel (eio_req *grp) |
754 | eio_grp_cancel (eio_req *grp) |
706 | { |
755 | { |
707 | for (grp = grp->grp_first; grp; grp = grp->grp_next) |
756 | for (grp = grp->grp_first; grp; grp = grp->grp_next) |
708 | eio_cancel (grp); |
757 | eio_cancel (grp); |
709 | } |
758 | } |
710 | |
759 | |
|
|
760 | void |
711 | void eio_cancel (eio_req *req) |
761 | eio_cancel (eio_req *req) |
712 | { |
762 | { |
713 | etp_cancel (req); |
763 | etp_cancel (req); |
714 | } |
764 | } |
715 | |
765 | |
|
|
766 | void |
716 | void eio_submit (eio_req *req) |
767 | eio_submit (eio_req *req) |
717 | { |
768 | { |
718 | etp_submit (req); |
769 | etp_submit (req); |
719 | } |
770 | } |
720 | |
771 | |
721 | unsigned int eio_nreqs (void) |
772 | unsigned int |
|
|
773 | eio_nreqs (void) |
722 | { |
774 | { |
723 | return etp_nreqs (); |
775 | return etp_nreqs (); |
724 | } |
776 | } |
725 | |
777 | |
726 | unsigned int eio_nready (void) |
778 | unsigned int |
|
|
779 | eio_nready (void) |
727 | { |
780 | { |
728 | return etp_nready (); |
781 | return etp_nready (); |
729 | } |
782 | } |
730 | |
783 | |
731 | unsigned int eio_npending (void) |
784 | unsigned int |
|
|
785 | eio_npending (void) |
732 | { |
786 | { |
733 | return etp_npending (); |
787 | return etp_npending (); |
734 | } |
788 | } |
735 | |
789 | |
736 | unsigned int eio_nthreads (void) |
790 | unsigned int ecb_cold |
|
|
791 | eio_nthreads (void) |
737 | { |
792 | { |
738 | return etp_nthreads (); |
793 | return etp_nthreads (); |
739 | } |
794 | } |
740 | |
795 | |
|
|
796 | void ecb_cold |
741 | void eio_set_max_poll_time (double nseconds) |
797 | eio_set_max_poll_time (double nseconds) |
742 | { |
798 | { |
743 | etp_set_max_poll_time (nseconds); |
799 | etp_set_max_poll_time (nseconds); |
744 | } |
800 | } |
745 | |
801 | |
|
|
802 | void ecb_cold |
746 | void eio_set_max_poll_reqs (unsigned int maxreqs) |
803 | eio_set_max_poll_reqs (unsigned int maxreqs) |
747 | { |
804 | { |
748 | etp_set_max_poll_reqs (maxreqs); |
805 | etp_set_max_poll_reqs (maxreqs); |
749 | } |
806 | } |
750 | |
807 | |
|
|
808 | void ecb_cold |
751 | void eio_set_max_idle (unsigned int nthreads) |
809 | eio_set_max_idle (unsigned int nthreads) |
752 | { |
810 | { |
753 | etp_set_max_idle (nthreads); |
811 | etp_set_max_idle (nthreads); |
754 | } |
812 | } |
755 | |
813 | |
|
|
814 | void ecb_cold |
|
|
815 | eio_set_idle_timeout (unsigned int seconds) |
|
|
816 | { |
|
|
817 | etp_set_idle_timeout (seconds); |
|
|
818 | } |
|
|
819 | |
|
|
820 | void ecb_cold |
756 | void eio_set_min_parallel (unsigned int nthreads) |
821 | eio_set_min_parallel (unsigned int nthreads) |
757 | { |
822 | { |
758 | etp_set_min_parallel (nthreads); |
823 | etp_set_min_parallel (nthreads); |
759 | } |
824 | } |
760 | |
825 | |
|
|
826 | void ecb_cold |
761 | void eio_set_max_parallel (unsigned int nthreads) |
827 | eio_set_max_parallel (unsigned int nthreads) |
762 | { |
828 | { |
763 | etp_set_max_parallel (nthreads); |
829 | etp_set_max_parallel (nthreads); |
764 | } |
830 | } |
765 | |
831 | |
766 | int eio_poll (void) |
832 | int eio_poll (void) |
… | |
… | |
808 | |
874 | |
809 | return res; |
875 | return res; |
810 | } |
876 | } |
811 | #endif |
877 | #endif |
812 | |
878 | |
813 | #ifndef HAVE_FUTIMES |
879 | #ifndef HAVE_UTIMES |
814 | |
880 | |
815 | # undef utimes |
881 | # undef utimes |
816 | # undef futimes |
|
|
817 | # define utimes(path,times) eio__utimes (path, times) |
882 | # define utimes(path,times) eio__utimes (path, times) |
818 | # define futimes(fd,times) eio__futimes (fd, times) |
|
|
819 | |
883 | |
820 | static int |
884 | static int |
821 | eio__utimes (const char *filename, const struct timeval times[2]) |
885 | eio__utimes (const char *filename, const struct timeval times[2]) |
822 | { |
886 | { |
823 | if (times) |
887 | if (times) |
… | |
… | |
831 | } |
895 | } |
832 | else |
896 | else |
833 | return utime (filename, 0); |
897 | return utime (filename, 0); |
834 | } |
898 | } |
835 | |
899 | |
|
|
900 | #endif |
|
|
901 | |
|
|
902 | #ifndef HAVE_FUTIMES |
|
|
903 | |
|
|
904 | # undef futimes |
|
|
905 | # define futimes(fd,times) eio__futimes (fd, times) |
|
|
906 | |
|
|
907 | static int |
836 | static int eio__futimes (int fd, const struct timeval tv[2]) |
908 | eio__futimes (int fd, const struct timeval tv[2]) |
837 | { |
909 | { |
838 | errno = ENOSYS; |
910 | errno = ENOSYS; |
839 | return -1; |
911 | return -1; |
840 | } |
912 | } |
841 | |
913 | |
… | |
… | |
845 | # undef fdatasync |
917 | # undef fdatasync |
846 | # define fdatasync(fd) fsync (fd) |
918 | # define fdatasync(fd) fsync (fd) |
847 | #endif |
919 | #endif |
848 | |
920 | |
849 | /* sync_file_range always needs emulation */ |
921 | /* sync_file_range always needs emulation */ |
850 | int |
922 | static int |
851 | eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) |
923 | eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) |
852 | { |
924 | { |
853 | #if HAVE_SYNC_FILE_RANGE |
925 | #if HAVE_SYNC_FILE_RANGE |
854 | int res; |
926 | int res; |
855 | |
927 | |
… | |
… | |
901 | |
973 | |
902 | /* sendfile always needs emulation */ |
974 | /* sendfile always needs emulation */ |
903 | static ssize_t |
975 | static ssize_t |
904 | eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) |
976 | eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) |
905 | { |
977 | { |
|
|
978 | ssize_t written = 0; |
906 | ssize_t res; |
979 | ssize_t res; |
907 | |
980 | |
908 | if (!count) |
981 | if (!count) |
909 | return 0; |
982 | return 0; |
910 | |
983 | |
|
|
984 | for (;;) |
|
|
985 | { |
911 | #if HAVE_SENDFILE |
986 | #if HAVE_SENDFILE |
912 | # if __linux |
987 | # if __linux |
|
|
988 | off_t soffset = offset; |
913 | res = sendfile (ofd, ifd, &offset, count); |
989 | res = sendfile (ofd, ifd, &soffset, count); |
914 | |
990 | |
915 | # elif __FreeBSD__ |
991 | # elif __FreeBSD__ |
916 | /* |
992 | /* |
917 | * Of course, the freebsd sendfile is a dire hack with no thoughts |
993 | * Of course, the freebsd sendfile is a dire hack with no thoughts |
918 | * wasted on making it similar to other I/O functions. |
994 | * wasted on making it similar to other I/O functions. |
919 | */ |
995 | */ |
920 | { |
|
|
921 | off_t sbytes; |
996 | off_t sbytes; |
922 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
997 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
923 | |
998 | |
|
|
999 | #if 0 /* according to the manpage, this is correct, but broken behaviour */ |
924 | /* freebsd' sendfile will return 0 on success */ |
1000 | /* freebsd' sendfile will return 0 on success */ |
925 | /* 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 */ |
926 | /* not on e.g. EIO or EPIPE - sounds broken */ |
1002 | /* not on e.g. EIO or EPIPE - sounds broken */ |
927 | if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0) |
1003 | if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0) |
928 | res = sbytes; |
1004 | res = sbytes; |
929 | } |
1005 | #endif |
|
|
1006 | |
|
|
1007 | /* according to source inspection, this is correct, and useful behaviour */ |
|
|
1008 | if (sbytes) |
|
|
1009 | res = sbytes; |
930 | |
1010 | |
931 | # elif defined (__APPLE__) |
1011 | # elif defined (__APPLE__) |
932 | |
|
|
933 | { |
|
|
934 | off_t sbytes = count; |
1012 | off_t sbytes = count; |
935 | res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); |
1013 | res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); |
936 | |
1014 | |
937 | /* according to the manpage, sbytes is always valid */ |
1015 | /* according to the manpage, sbytes is always valid */ |
938 | if (sbytes) |
1016 | if (sbytes) |
939 | res = sbytes; |
1017 | res = sbytes; |
940 | } |
|
|
941 | |
1018 | |
942 | # elif __hpux |
1019 | # elif __hpux |
943 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
1020 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
944 | |
1021 | |
945 | # elif __solaris |
1022 | # elif __solaris |
946 | { |
|
|
947 | struct sendfilevec vec; |
1023 | struct sendfilevec vec; |
948 | size_t sbytes; |
1024 | size_t sbytes; |
949 | |
1025 | |
950 | vec.sfv_fd = ifd; |
1026 | vec.sfv_fd = ifd; |
951 | vec.sfv_flag = 0; |
1027 | vec.sfv_flag = 0; |
952 | vec.sfv_off = offset; |
1028 | vec.sfv_off = offset; |
953 | vec.sfv_len = count; |
1029 | vec.sfv_len = count; |
954 | |
1030 | |
955 | res = sendfilev (ofd, &vec, 1, &sbytes); |
1031 | res = sendfilev (ofd, &vec, 1, &sbytes); |
956 | |
1032 | |
957 | if (res < 0 && sbytes) |
1033 | if (res < 0 && sbytes) |
958 | res = sbytes; |
1034 | res = sbytes; |
959 | } |
|
|
960 | |
1035 | |
961 | # endif |
1036 | # endif |
962 | |
1037 | |
963 | #elif defined (_WIN32) |
1038 | #elif defined (_WIN32) |
964 | |
|
|
965 | /* 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 */ |
966 | { |
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 */ |
967 | HANDLE h = TO_SOCKET (ifd); |
1043 | HANDLE h = TO_SOCKET (ifd); |
968 | SetFilePointer (h, offset, 0, FILE_BEGIN); |
1044 | SetFilePointer (h, offset, 0, FILE_BEGIN); |
969 | res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); |
1045 | res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); |
970 | } |
|
|
971 | |
1046 | |
972 | #else |
1047 | #else |
973 | res = -1; |
1048 | res = -1; |
974 | errno = ENOSYS; |
1049 | errno = ENOSYS; |
975 | #endif |
1050 | #endif |
976 | |
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 | |
977 | if (res < 0 |
1075 | if (res < 0 |
978 | && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK |
1076 | && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK |
979 | /* BSDs */ |
1077 | /* BSDs */ |
980 | #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... */ |
981 | || errno == ENOTSUP |
1079 | || errno == ENOTSUP |
982 | #endif |
1080 | #endif |
… | |
… | |
1022 | } |
1120 | } |
1023 | |
1121 | |
1024 | static signed char |
1122 | static signed char |
1025 | eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) |
1123 | eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) |
1026 | { |
1124 | { |
1027 | 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 */ |
1028 | : 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; |
1029 | } |
1129 | } |
1030 | |
1130 | |
1031 | #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 |
1032 | |
1132 | |
1033 | #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 */ |
… | |
… | |
1039 | unsigned char bits [9 + sizeof (ino_t) * 8]; |
1139 | unsigned char bits [9 + sizeof (ino_t) * 8]; |
1040 | unsigned char *bit = bits; |
1140 | unsigned char *bit = bits; |
1041 | |
1141 | |
1042 | assert (CHAR_BIT == 8); |
1142 | assert (CHAR_BIT == 8); |
1043 | assert (sizeof (eio_dirent) * 8 < 256); |
1143 | assert (sizeof (eio_dirent) * 8 < 256); |
1044 | assert (offsetof (eio_dirent, inode)); /* we use 0 as sentinel */ |
1144 | assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */ |
1045 | assert (offsetof (eio_dirent, score)); /* we use 0 as sentinel */ |
1145 | assert (offsetof (eio_dirent, score)); /* we use bit #0 as sentinel */ |
1046 | |
1146 | |
1047 | if (size <= EIO_SORT_FAST) |
1147 | if (size <= EIO_SORT_FAST) |
1048 | return; |
1148 | return; |
1049 | |
1149 | |
1050 | /* 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 */ |
… | |
… | |
1205 | flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); |
1305 | flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); |
1206 | |
1306 | |
1207 | X_LOCK (wrklock); |
1307 | X_LOCK (wrklock); |
1208 | /* the corresponding closedir is in ETP_WORKER_CLEAR */ |
1308 | /* the corresponding closedir is in ETP_WORKER_CLEAR */ |
1209 | self->dirp = dirp = opendir (req->ptr1); |
1309 | self->dirp = dirp = opendir (req->ptr1); |
|
|
1310 | |
1210 | req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; |
1311 | req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; |
1211 | req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; |
1312 | req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; |
1212 | req->ptr2 = names = malloc (namesalloc); |
1313 | req->ptr2 = names = malloc (namesalloc); |
1213 | X_UNLOCK (wrklock); |
1314 | X_UNLOCK (wrklock); |
1214 | |
1315 | |
… | |
… | |
1226 | /* sort etc. */ |
1327 | /* sort etc. */ |
1227 | req->int1 = flags; |
1328 | req->int1 = flags; |
1228 | req->result = dentoffs; |
1329 | req->result = dentoffs; |
1229 | |
1330 | |
1230 | if (flags & EIO_READDIR_STAT_ORDER) |
1331 | if (flags & EIO_READDIR_STAT_ORDER) |
1231 | eio_dent_sort (dents, dentoffs, 0, inode_bits); /* sort by inode exclusively */ |
1332 | eio_dent_sort (dents, dentoffs, flags & EIO_READDIR_DIRS_FIRST ? 7 : 0, inode_bits); |
1232 | else if (flags & EIO_READDIR_DIRS_FIRST) |
1333 | else if (flags & EIO_READDIR_DIRS_FIRST) |
1233 | if (flags & EIO_READDIR_FOUND_UNKNOWN) |
1334 | if (flags & EIO_READDIR_FOUND_UNKNOWN) |
1234 | eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */ |
1335 | eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */ |
1235 | else |
1336 | else |
1236 | { |
1337 | { |
… | |
… | |
1238 | eio_dirent *oth = dents + dentoffs; |
1339 | eio_dirent *oth = dents + dentoffs; |
1239 | eio_dirent *dir = dents; |
1340 | eio_dirent *dir = dents; |
1240 | |
1341 | |
1241 | /* now partition dirs to the front, and non-dirs to the back */ |
1342 | /* now partition dirs to the front, and non-dirs to the back */ |
1242 | /* by walking from both sides and swapping if necessary */ |
1343 | /* by walking from both sides and swapping if necessary */ |
1243 | /* also clear score, so it doesn't influence sorting */ |
|
|
1244 | while (oth > dir) |
1344 | while (oth > dir) |
1245 | { |
1345 | { |
1246 | if (dir->type == EIO_DT_DIR) |
1346 | if (dir->type == EIO_DT_DIR) |
1247 | ++dir; |
1347 | ++dir; |
1248 | else if ((--oth)->type == EIO_DT_DIR) |
1348 | else if ((--oth)->type == EIO_DT_DIR) |
… | |
… | |
1251 | |
1351 | |
1252 | ++dir; |
1352 | ++dir; |
1253 | } |
1353 | } |
1254 | } |
1354 | } |
1255 | |
1355 | |
1256 | /* now sort the dirs only */ |
1356 | /* now sort the dirs only (dirs all have the same score) */ |
1257 | eio_dent_sort (dents, dir - dents, 0, inode_bits); |
1357 | eio_dent_sort (dents, dir - dents, 0, inode_bits); |
1258 | } |
1358 | } |
1259 | |
1359 | |
1260 | break; |
1360 | break; |
1261 | } |
1361 | } |
… | |
… | |
1266 | /* skip . and .. entries */ |
1366 | /* skip . and .. entries */ |
1267 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
1367 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
1268 | { |
1368 | { |
1269 | int len = D_NAMLEN (entp) + 1; |
1369 | int len = D_NAMLEN (entp) + 1; |
1270 | |
1370 | |
1271 | while (expect_false (namesoffs + len > namesalloc)) |
1371 | while (ecb_expect_false (namesoffs + len > namesalloc)) |
1272 | { |
1372 | { |
1273 | namesalloc *= 2; |
1373 | namesalloc *= 2; |
1274 | X_LOCK (wrklock); |
1374 | X_LOCK (wrklock); |
1275 | req->ptr2 = names = realloc (names, namesalloc); |
1375 | req->ptr2 = names = realloc (names, namesalloc); |
1276 | X_UNLOCK (wrklock); |
1376 | X_UNLOCK (wrklock); |
… | |
… | |
1283 | |
1383 | |
1284 | if (dents) |
1384 | if (dents) |
1285 | { |
1385 | { |
1286 | struct eio_dirent *ent; |
1386 | struct eio_dirent *ent; |
1287 | |
1387 | |
1288 | if (expect_false (dentoffs == dentalloc)) |
1388 | if (ecb_expect_false (dentoffs == dentalloc)) |
1289 | { |
1389 | { |
1290 | dentalloc *= 2; |
1390 | dentalloc *= 2; |
1291 | X_LOCK (wrklock); |
1391 | X_LOCK (wrklock); |
1292 | req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); |
1392 | req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); |
1293 | X_UNLOCK (wrklock); |
1393 | X_UNLOCK (wrklock); |
… | |
… | |
1381 | break; |
1481 | break; |
1382 | } |
1482 | } |
1383 | } |
1483 | } |
1384 | } |
1484 | } |
1385 | |
1485 | |
|
|
1486 | #ifdef PAGESIZE |
|
|
1487 | # define eio_pagesize() PAGESIZE |
|
|
1488 | #else |
|
|
1489 | static intptr_t |
|
|
1490 | eio_pagesize (void) |
|
|
1491 | { |
|
|
1492 | static intptr_t page; |
|
|
1493 | |
|
|
1494 | if (!page) |
|
|
1495 | page = sysconf (_SC_PAGESIZE); |
|
|
1496 | |
|
|
1497 | return page; |
|
|
1498 | } |
|
|
1499 | #endif |
|
|
1500 | |
|
|
1501 | static void |
|
|
1502 | eio_page_align (void **addr, size_t *length) |
|
|
1503 | { |
|
|
1504 | intptr_t mask = eio_pagesize () - 1; |
|
|
1505 | |
|
|
1506 | /* round down addr */ |
|
|
1507 | intptr_t adj = mask & (intptr_t)*addr; |
|
|
1508 | |
|
|
1509 | *addr = (void *)((intptr_t)*addr - adj); |
|
|
1510 | *length += adj; |
|
|
1511 | |
|
|
1512 | /* round up length */ |
|
|
1513 | *length = (*length + mask) & ~mask; |
|
|
1514 | } |
|
|
1515 | |
|
|
1516 | #if !_POSIX_MEMLOCK |
|
|
1517 | # define eio__mlockall(a) ((errno = ENOSYS), -1) |
|
|
1518 | #else |
|
|
1519 | |
|
|
1520 | static int |
|
|
1521 | eio__mlockall (int flags) |
|
|
1522 | { |
|
|
1523 | #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 |
|
|
1524 | extern int mallopt (int, int); |
|
|
1525 | mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ |
|
|
1526 | #endif |
|
|
1527 | |
|
|
1528 | if (EIO_MCL_CURRENT != MCL_CURRENT |
|
|
1529 | || EIO_MCL_FUTURE != MCL_FUTURE) |
|
|
1530 | { |
|
|
1531 | flags = 0 |
|
|
1532 | | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0) |
|
|
1533 | | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0); |
|
|
1534 | } |
|
|
1535 | |
|
|
1536 | return mlockall (flags); |
|
|
1537 | } |
|
|
1538 | #endif |
|
|
1539 | |
|
|
1540 | #if !_POSIX_MEMLOCK_RANGE |
|
|
1541 | # define eio__mlock(a,b) ((errno = ENOSYS), -1) |
|
|
1542 | #else |
|
|
1543 | |
|
|
1544 | static int |
|
|
1545 | eio__mlock (void *addr, size_t length) |
|
|
1546 | { |
|
|
1547 | eio_page_align (&addr, &length); |
|
|
1548 | |
|
|
1549 | return mlock (addr, length); |
|
|
1550 | } |
|
|
1551 | |
|
|
1552 | #endif |
|
|
1553 | |
1386 | #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) |
1554 | #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) |
1387 | # define eio__msync(a,b,c) ((errno = ENOSYS), -1) |
1555 | # define eio__msync(a,b,c) ((errno = ENOSYS), -1) |
1388 | #else |
1556 | #else |
1389 | |
1557 | |
1390 | int |
1558 | static int |
1391 | eio__msync (void *mem, size_t len, int flags) |
1559 | eio__msync (void *mem, size_t len, int flags) |
1392 | { |
1560 | { |
|
|
1561 | eio_page_align (&mem, &len); |
|
|
1562 | |
1393 | if (EIO_MS_ASYNC != MS_SYNC |
1563 | if (EIO_MS_ASYNC != MS_SYNC |
1394 | || EIO_MS_INVALIDATE != MS_INVALIDATE |
1564 | || EIO_MS_INVALIDATE != MS_INVALIDATE |
1395 | || EIO_MS_SYNC != MS_SYNC) |
1565 | || EIO_MS_SYNC != MS_SYNC) |
1396 | { |
1566 | { |
1397 | flags = 0 |
1567 | flags = 0 |
… | |
… | |
1403 | return msync (mem, len, flags); |
1573 | return msync (mem, len, flags); |
1404 | } |
1574 | } |
1405 | |
1575 | |
1406 | #endif |
1576 | #endif |
1407 | |
1577 | |
1408 | int |
1578 | static int |
1409 | eio__mtouch (void *mem, size_t len, int flags) |
1579 | eio__mtouch (eio_req *req) |
1410 | { |
1580 | { |
|
|
1581 | void *mem = req->ptr2; |
|
|
1582 | size_t len = req->size; |
|
|
1583 | int flags = req->int1; |
|
|
1584 | |
|
|
1585 | eio_page_align (&mem, &len); |
|
|
1586 | |
|
|
1587 | { |
1411 | intptr_t addr = (intptr_t)mem; |
1588 | intptr_t addr = (intptr_t)mem; |
1412 | intptr_t end = addr + len; |
1589 | intptr_t end = addr + len; |
1413 | #ifdef PAGESIZE |
1590 | intptr_t page = eio_pagesize (); |
1414 | const intptr_t page = PAGESIZE; |
|
|
1415 | #else |
|
|
1416 | static intptr_t page; |
|
|
1417 | |
1591 | |
1418 | if (!page) |
|
|
1419 | page = sysconf (_SC_PAGESIZE); |
|
|
1420 | #endif |
|
|
1421 | |
|
|
1422 | /* round down to start of page, although this is probably useless */ |
|
|
1423 | addr &= ~(page - 1); /* assume page size is always a power of two */ |
|
|
1424 | |
|
|
1425 | if (addr < end) |
1592 | if (addr < end) |
1426 | if (flags & EIO_MT_MODIFY) /* modify */ |
1593 | if (flags & EIO_MT_MODIFY) /* modify */ |
1427 | do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len); |
1594 | do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req)); |
1428 | else |
1595 | else |
1429 | do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len); |
1596 | do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req)); |
|
|
1597 | } |
1430 | |
1598 | |
1431 | return 0; |
1599 | return 0; |
1432 | } |
1600 | } |
1433 | |
1601 | |
1434 | /*****************************************************************************/ |
1602 | /*****************************************************************************/ |
… | |
… | |
1468 | if (req) |
1636 | if (req) |
1469 | break; |
1637 | break; |
1470 | |
1638 | |
1471 | ++idle; |
1639 | ++idle; |
1472 | |
1640 | |
1473 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1641 | ts.tv_sec = time (0) + idle_timeout; |
1474 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) |
1642 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) |
1475 | { |
1643 | { |
1476 | if (idle > max_idle) |
1644 | if (idle > max_idle) |
1477 | { |
1645 | { |
1478 | --idle; |
1646 | --idle; |
… | |
… | |
1521 | return 0; |
1689 | return 0; |
1522 | } |
1690 | } |
1523 | |
1691 | |
1524 | /*****************************************************************************/ |
1692 | /*****************************************************************************/ |
1525 | |
1693 | |
|
|
1694 | int ecb_cold |
1526 | int eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1695 | eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1527 | { |
1696 | { |
1528 | return etp_init (want_poll, done_poll); |
1697 | return etp_init (want_poll, done_poll); |
1529 | } |
1698 | } |
1530 | |
1699 | |
|
|
1700 | ECB_INLINE void |
1531 | static void eio_api_destroy (eio_req *req) |
1701 | eio_api_destroy (eio_req *req) |
1532 | { |
1702 | { |
1533 | free (req); |
1703 | free (req); |
1534 | } |
1704 | } |
1535 | |
1705 | |
1536 | #define REQ(rtype) \ |
1706 | #define REQ(rtype) \ |
… | |
… | |
1555 | { \ |
1725 | { \ |
1556 | eio_api_destroy (req); \ |
1726 | eio_api_destroy (req); \ |
1557 | return 0; \ |
1727 | return 0; \ |
1558 | } |
1728 | } |
1559 | |
1729 | |
|
|
1730 | static void |
1560 | static void eio_execute (etp_worker *self, eio_req *req) |
1731 | eio_execute (etp_worker *self, eio_req *req) |
1561 | { |
1732 | { |
1562 | errno = 0; |
|
|
1563 | |
|
|
1564 | switch (req->type) |
1733 | switch (req->type) |
1565 | { |
1734 | { |
1566 | case EIO_READ: ALLOC (req->size); |
1735 | case EIO_READ: ALLOC (req->size); |
1567 | req->result = req->offs >= 0 |
1736 | req->result = req->offs >= 0 |
1568 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
1737 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
… | |
… | |
1600 | case EIO_RMDIR: req->result = rmdir (req->ptr1); break; |
1769 | case EIO_RMDIR: req->result = rmdir (req->ptr1); break; |
1601 | case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break; |
1770 | case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break; |
1602 | case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; |
1771 | case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; |
1603 | case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; |
1772 | case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; |
1604 | case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; |
1773 | case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; |
1605 | case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->int3); break; |
1774 | case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
1606 | |
1775 | |
1607 | case EIO_READLINK: ALLOC (NAME_MAX); |
1776 | case EIO_READLINK: ALLOC (PATH_MAX); |
1608 | req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break; |
1777 | req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break; |
1609 | |
1778 | |
1610 | case EIO_SYNC: req->result = 0; sync (); break; |
1779 | case EIO_SYNC: req->result = 0; sync (); break; |
1611 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
1780 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
1612 | case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; |
1781 | case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; |
1613 | case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; |
1782 | case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; |
|
|
1783 | case EIO_MTOUCH: req->result = eio__mtouch (req); break; |
1614 | case EIO_MTOUCH: req->result = eio__mtouch (req->ptr2, req->size, req->int1); break; |
1784 | case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; |
|
|
1785 | case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; |
1615 | case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; |
1786 | case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; |
1616 | |
1787 | |
1617 | case EIO_READDIR: eio__scandir (req, self); break; |
1788 | case EIO_READDIR: eio__scandir (req, self); break; |
1618 | |
1789 | |
1619 | case EIO_BUSY: |
1790 | case EIO_BUSY: |
1620 | #ifdef _WIN32 |
1791 | #ifdef _WIN32 |
1621 | Sleep (req->nv1 * 1000.); |
1792 | Sleep (req->nv1 * 1e3); |
1622 | #else |
1793 | #else |
1623 | { |
1794 | { |
1624 | struct timeval tv; |
1795 | struct timeval tv; |
1625 | |
1796 | |
1626 | tv.tv_sec = req->nv1; |
1797 | tv.tv_sec = req->nv1; |
1627 | tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; |
1798 | tv.tv_usec = (req->nv1 - tv.tv_sec) * 1e6; |
1628 | |
1799 | |
1629 | req->result = select (0, 0, 0, 0, &tv); |
1800 | req->result = select (0, 0, 0, 0, &tv); |
1630 | } |
1801 | } |
1631 | #endif |
1802 | #endif |
1632 | break; |
1803 | break; |
… | |
… | |
1647 | times = tv; |
1818 | times = tv; |
1648 | } |
1819 | } |
1649 | else |
1820 | else |
1650 | times = 0; |
1821 | times = 0; |
1651 | |
1822 | |
1652 | |
|
|
1653 | req->result = req->type == EIO_FUTIME |
1823 | req->result = req->type == EIO_FUTIME |
1654 | ? futimes (req->int1, times) |
1824 | ? futimes (req->int1, times) |
1655 | : utimes (req->ptr1, times); |
1825 | : utimes (req->ptr1, times); |
1656 | } |
1826 | } |
1657 | break; |
1827 | break; |
… | |
… | |
1662 | case EIO_NOP: |
1832 | case EIO_NOP: |
1663 | req->result = 0; |
1833 | req->result = 0; |
1664 | break; |
1834 | break; |
1665 | |
1835 | |
1666 | case EIO_CUSTOM: |
1836 | case EIO_CUSTOM: |
1667 | ((void (*)(eio_req *))req->feed) (req); |
1837 | req->feed (req); |
1668 | break; |
1838 | break; |
1669 | |
1839 | |
1670 | default: |
1840 | default: |
|
|
1841 | errno = ENOSYS; |
1671 | req->result = -1; |
1842 | req->result = -1; |
1672 | break; |
1843 | break; |
1673 | } |
1844 | } |
1674 | |
1845 | |
1675 | req->errorno = errno; |
1846 | req->errorno = errno; |
… | |
… | |
1705 | eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) |
1876 | eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) |
1706 | { |
1877 | { |
1707 | REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; |
1878 | REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; |
1708 | } |
1879 | } |
1709 | |
1880 | |
|
|
1881 | eio_req *eio_mlock (void *addr, size_t length, int pri, eio_cb cb, void *data) |
|
|
1882 | { |
|
|
1883 | REQ (EIO_MLOCK); req->ptr2 = addr; req->size = length; SEND; |
|
|
1884 | } |
|
|
1885 | |
|
|
1886 | eio_req *eio_mlockall (int flags, int pri, eio_cb cb, void *data) |
|
|
1887 | { |
|
|
1888 | REQ (EIO_MLOCKALL); req->int1 = flags; SEND; |
|
|
1889 | } |
|
|
1890 | |
1710 | eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) |
1891 | eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) |
1711 | { |
1892 | { |
1712 | REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; |
1893 | REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; |
1713 | } |
1894 | } |
1714 | |
1895 | |
… | |
… | |
1848 | REQ (EIO_READDIR); PATH; req->int1 = flags; SEND; |
2029 | REQ (EIO_READDIR); PATH; req->int1 = flags; SEND; |
1849 | } |
2030 | } |
1850 | |
2031 | |
1851 | eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) |
2032 | eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) |
1852 | { |
2033 | { |
1853 | REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND; |
2034 | REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->offs = (off_t)dev; SEND; |
1854 | } |
2035 | } |
1855 | |
2036 | |
1856 | static eio_req * |
2037 | static eio_req * |
1857 | eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
2038 | eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1858 | { |
2039 | { |
… | |
… | |
1882 | eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
2063 | eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
1883 | { |
2064 | { |
1884 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
2065 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
1885 | } |
2066 | } |
1886 | |
2067 | |
1887 | eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) |
2068 | eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data) |
1888 | { |
2069 | { |
1889 | REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND; |
2070 | REQ (EIO_CUSTOM); req->feed = execute; SEND; |
1890 | } |
2071 | } |
1891 | |
2072 | |
1892 | #endif |
2073 | #endif |
1893 | |
2074 | |
1894 | eio_req *eio_grp (eio_cb cb, void *data) |
2075 | eio_req *eio_grp (eio_cb cb, void *data) |
… | |
… | |
1903 | #undef SEND |
2084 | #undef SEND |
1904 | |
2085 | |
1905 | /*****************************************************************************/ |
2086 | /*****************************************************************************/ |
1906 | /* grp functions */ |
2087 | /* grp functions */ |
1907 | |
2088 | |
|
|
2089 | void |
1908 | void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) |
2090 | eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) |
1909 | { |
2091 | { |
1910 | grp->int2 = limit; |
2092 | grp->int2 = limit; |
1911 | grp->feed = feed; |
2093 | grp->feed = feed; |
1912 | |
2094 | |
1913 | grp_try_feed (grp); |
2095 | grp_try_feed (grp); |
1914 | } |
2096 | } |
1915 | |
2097 | |
|
|
2098 | void |
1916 | void eio_grp_limit (eio_req *grp, int limit) |
2099 | eio_grp_limit (eio_req *grp, int limit) |
1917 | { |
2100 | { |
1918 | grp->int2 = limit; |
2101 | grp->int2 = limit; |
1919 | |
2102 | |
1920 | grp_try_feed (grp); |
2103 | grp_try_feed (grp); |
1921 | } |
2104 | } |
1922 | |
2105 | |
|
|
2106 | void |
1923 | void eio_grp_add (eio_req *grp, eio_req *req) |
2107 | eio_grp_add (eio_req *grp, eio_req *req) |
1924 | { |
2108 | { |
1925 | assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); |
2109 | assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); |
1926 | |
2110 | |
1927 | grp->flags |= EIO_FLAG_GROUPADD; |
2111 | grp->flags |= EIO_FLAG_GROUPADD; |
1928 | |
2112 | |
… | |
… | |
1939 | } |
2123 | } |
1940 | |
2124 | |
1941 | /*****************************************************************************/ |
2125 | /*****************************************************************************/ |
1942 | /* misc garbage */ |
2126 | /* misc garbage */ |
1943 | |
2127 | |
|
|
2128 | ssize_t |
1944 | ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) |
2129 | eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) |
1945 | { |
2130 | { |
1946 | etp_worker wrk; |
2131 | etp_worker wrk; |
1947 | ssize_t ret; |
2132 | ssize_t ret; |
1948 | |
2133 | |
1949 | wrk.dbuf = 0; |
2134 | wrk.dbuf = 0; |