… | |
… | |
120 | |
120 | |
121 | #define EIO_TICKS ((1000000 + 1023) >> 10) |
121 | #define EIO_TICKS ((1000000 + 1023) >> 10) |
122 | |
122 | |
123 | /*****************************************************************************/ |
123 | /*****************************************************************************/ |
124 | |
124 | |
|
|
125 | #if __GNUC__ >= 3 |
|
|
126 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
127 | #else |
|
|
128 | # define expect(expr,value) (expr) |
|
|
129 | #endif |
|
|
130 | |
|
|
131 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
132 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
133 | |
|
|
134 | /*****************************************************************************/ |
|
|
135 | |
125 | #define ETP_PRI_MIN EIO_PRI_MIN |
136 | #define ETP_PRI_MIN EIO_PRI_MIN |
126 | #define ETP_PRI_MAX EIO_PRI_MAX |
137 | #define ETP_PRI_MAX EIO_PRI_MAX |
127 | |
138 | |
128 | struct etp_worker; |
139 | struct etp_worker; |
129 | |
140 | |
… | |
… | |
177 | static mutex_t wrklock = X_MUTEX_INIT; |
188 | static mutex_t wrklock = X_MUTEX_INIT; |
178 | static mutex_t reslock = X_MUTEX_INIT; |
189 | static mutex_t reslock = X_MUTEX_INIT; |
179 | static mutex_t reqlock = X_MUTEX_INIT; |
190 | static mutex_t reqlock = X_MUTEX_INIT; |
180 | static cond_t reqwait = X_COND_INIT; |
191 | static cond_t reqwait = X_COND_INIT; |
181 | |
192 | |
|
|
193 | #if !HAVE_PREADWRITE |
|
|
194 | /* |
|
|
195 | * make our pread/pwrite emulation safe against themselves, but not against |
|
|
196 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
197 | * but that's your problem, not mine. |
|
|
198 | */ |
|
|
199 | static mutex_t preadwritelock = X_MUTEX_INIT; |
|
|
200 | #endif |
|
|
201 | |
182 | typedef struct etp_worker |
202 | typedef struct etp_worker |
183 | { |
203 | { |
184 | /* locked by wrklock */ |
204 | /* locked by wrklock */ |
185 | struct etp_worker *prev, *next; |
205 | struct etp_worker *prev, *next; |
186 | |
206 | |
… | |
… | |
330 | |
350 | |
331 | static void etp_atfork_child (void) |
351 | static void etp_atfork_child (void) |
332 | { |
352 | { |
333 | ETP_REQ *prv; |
353 | ETP_REQ *prv; |
334 | |
354 | |
335 | while (prv = reqq_shift (&req_queue)) |
355 | while ((prv = reqq_shift (&req_queue))) |
336 | ETP_DESTROY (prv); |
356 | ETP_DESTROY (prv); |
337 | |
357 | |
338 | while (prv = reqq_shift (&res_queue)) |
358 | while ((prv = reqq_shift (&res_queue))) |
339 | ETP_DESTROY (prv); |
359 | ETP_DESTROY (prv); |
340 | |
360 | |
341 | while (wrk_first.next != &wrk_first) |
361 | while (wrk_first.next != &wrk_first) |
342 | { |
362 | { |
343 | etp_worker *wrk = wrk_first.next; |
363 | etp_worker *wrk = wrk_first.next; |
… | |
… | |
402 | X_UNLOCK (wrklock); |
422 | X_UNLOCK (wrklock); |
403 | } |
423 | } |
404 | |
424 | |
405 | static void etp_maybe_start_thread (void) |
425 | static void etp_maybe_start_thread (void) |
406 | { |
426 | { |
407 | if (etp_nthreads () >= wanted) |
427 | if (expect_true (etp_nthreads () >= wanted)) |
408 | return; |
428 | return; |
409 | |
429 | |
410 | /* todo: maybe use idle here, but might be less exact */ |
430 | /* todo: maybe use idle here, but might be less exact */ |
411 | if (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()) |
431 | if (expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) |
412 | return; |
432 | return; |
413 | |
433 | |
414 | etp_start_thread (); |
434 | etp_start_thread (); |
415 | } |
435 | } |
416 | |
436 | |
… | |
… | |
469 | |
489 | |
470 | X_LOCK (reqlock); |
490 | X_LOCK (reqlock); |
471 | --nreqs; |
491 | --nreqs; |
472 | X_UNLOCK (reqlock); |
492 | X_UNLOCK (reqlock); |
473 | |
493 | |
474 | if (req->type == EIO_GROUP && req->size) |
494 | if (expect_false (req->type == EIO_GROUP && req->size)) |
475 | { |
495 | { |
476 | req->int1 = 1; /* mark request as delayed */ |
496 | req->int1 = 1; /* mark request as delayed */ |
477 | continue; |
497 | continue; |
478 | } |
498 | } |
479 | else |
499 | else |
480 | { |
500 | { |
481 | int res = ETP_FINISH (req); |
501 | int res = ETP_FINISH (req); |
482 | if (res) |
502 | if (expect_false (res)) |
483 | return res; |
503 | return res; |
484 | } |
504 | } |
485 | |
505 | |
486 | if (maxreqs && !--maxreqs) |
506 | if (expect_false (maxreqs && !--maxreqs)) |
487 | break; |
507 | break; |
488 | |
508 | |
489 | if (maxtime) |
509 | if (maxtime) |
490 | { |
510 | { |
491 | gettimeofday (&tv_now, 0); |
511 | gettimeofday (&tv_now, 0); |
… | |
… | |
510 | |
530 | |
511 | static void etp_submit (ETP_REQ *req) |
531 | static void etp_submit (ETP_REQ *req) |
512 | { |
532 | { |
513 | req->pri -= ETP_PRI_MIN; |
533 | req->pri -= ETP_PRI_MIN; |
514 | |
534 | |
515 | if (req->pri < ETP_PRI_MIN - ETP_PRI_MIN) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; |
535 | if (expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; |
516 | if (req->pri > ETP_PRI_MAX - ETP_PRI_MIN) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
536 | if (expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
517 | |
537 | |
|
|
538 | if (expect_false (req->type == EIO_GROUP)) |
|
|
539 | { |
|
|
540 | /* I hope this is worth it :/ */ |
518 | X_LOCK (reqlock); |
541 | X_LOCK (reqlock); |
519 | ++nreqs; |
542 | ++nreqs; |
|
|
543 | X_UNLOCK (reqlock); |
|
|
544 | |
|
|
545 | X_LOCK (reslock); |
|
|
546 | |
|
|
547 | ++npending; |
|
|
548 | |
|
|
549 | if (!reqq_push (&res_queue, req) && want_poll_cb) |
|
|
550 | want_poll_cb (); |
|
|
551 | |
|
|
552 | X_UNLOCK (reslock); |
|
|
553 | } |
|
|
554 | else |
|
|
555 | { |
|
|
556 | X_LOCK (reqlock); |
|
|
557 | ++nreqs; |
520 | ++nready; |
558 | ++nready; |
521 | reqq_push (&req_queue, req); |
559 | reqq_push (&req_queue, req); |
522 | X_COND_SIGNAL (reqwait); |
560 | X_COND_SIGNAL (reqwait); |
523 | X_UNLOCK (reqlock); |
561 | X_UNLOCK (reqlock); |
524 | |
562 | |
525 | etp_maybe_start_thread (); |
563 | etp_maybe_start_thread (); |
|
|
564 | } |
526 | } |
565 | } |
527 | |
566 | |
528 | static void etp_set_max_poll_time (double nseconds) |
567 | static void etp_set_max_poll_time (double nseconds) |
529 | { |
568 | { |
530 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
569 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
… | |
… | |
699 | /* work around various missing functions */ |
738 | /* work around various missing functions */ |
700 | |
739 | |
701 | #if !HAVE_PREADWRITE |
740 | #if !HAVE_PREADWRITE |
702 | # define pread eio__pread |
741 | # define pread eio__pread |
703 | # define pwrite eio__pwrite |
742 | # define pwrite eio__pwrite |
704 | |
|
|
705 | /* |
|
|
706 | * make our pread/pwrite safe against themselves, but not against |
|
|
707 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
708 | * but that's your problem, not mine. |
|
|
709 | */ |
|
|
710 | static mutex_t preadwritelock = X_MUTEX_INIT; |
|
|
711 | |
743 | |
712 | static ssize_t |
744 | static ssize_t |
713 | eio__pread (int fd, void *buf, size_t count, off_t offset) |
745 | eio__pread (int fd, void *buf, size_t count, off_t offset) |
714 | { |
746 | { |
715 | ssize_t res; |
747 | ssize_t res; |
… | |
… | |
946 | |
978 | |
947 | if (errno) |
979 | if (errno) |
948 | res = -1; |
980 | res = -1; |
949 | |
981 | |
950 | req->result = res; |
982 | req->result = res; |
|
|
983 | } |
|
|
984 | |
|
|
985 | #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) |
|
|
986 | # define msync(a,b,c) ENOSYS |
|
|
987 | #endif |
|
|
988 | |
|
|
989 | int |
|
|
990 | eio__mtouch (void *mem, size_t len, int flags) |
|
|
991 | { |
|
|
992 | intptr_t addr = (intptr_t)mem; |
|
|
993 | intptr_t end = addr + len; |
|
|
994 | #ifdef PAGESIZE |
|
|
995 | const intptr_t page = PAGESIZE; |
|
|
996 | #else |
|
|
997 | static intptr_t page; |
|
|
998 | |
|
|
999 | if (!page) |
|
|
1000 | page = sysconf (_SC_PAGESIZE); |
|
|
1001 | #endif |
|
|
1002 | |
|
|
1003 | addr &= ~(page - 1); /* assume page size is always a power of two */ |
|
|
1004 | |
|
|
1005 | if (addr < end) |
|
|
1006 | if (flags) /* modify */ |
|
|
1007 | do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len); |
|
|
1008 | else |
|
|
1009 | do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len); |
|
|
1010 | |
|
|
1011 | return 0; |
951 | } |
1012 | } |
952 | |
1013 | |
953 | /*****************************************************************************/ |
1014 | /*****************************************************************************/ |
954 | |
1015 | |
955 | #define ALLOC(len) \ |
1016 | #define ALLOC(len) \ |
… | |
… | |
1122 | req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break; |
1183 | req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break; |
1123 | |
1184 | |
1124 | case EIO_SYNC: req->result = 0; sync (); break; |
1185 | case EIO_SYNC: req->result = 0; sync (); break; |
1125 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
1186 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
1126 | case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; |
1187 | case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; |
|
|
1188 | case EIO_MSYNC: req->result = msync (req->ptr2, req->size, req->int1); break; |
|
|
1189 | case EIO_MTOUCH: req->result = eio__mtouch (req->ptr2, req->size, req->int1); break; |
1127 | |
1190 | |
1128 | case EIO_READDIR: eio__scandir (req, self); break; |
1191 | case EIO_READDIR: eio__scandir (req, self); break; |
1129 | |
1192 | |
1130 | case EIO_BUSY: |
1193 | case EIO_BUSY: |
1131 | #ifdef _WIN32 |
1194 | #ifdef _WIN32 |
… | |
… | |
1165 | ? futimes (req->int1, times) |
1228 | ? futimes (req->int1, times) |
1166 | : utimes (req->ptr1, times); |
1229 | : utimes (req->ptr1, times); |
1167 | } |
1230 | } |
1168 | |
1231 | |
1169 | case EIO_GROUP: |
1232 | case EIO_GROUP: |
|
|
1233 | abort (); /* handled in eio_request */ |
|
|
1234 | |
1170 | case EIO_NOP: |
1235 | case EIO_NOP: |
1171 | req->result = 0; |
1236 | req->result = 0; |
1172 | break; |
1237 | break; |
1173 | |
1238 | |
1174 | case EIO_CUSTOM: |
1239 | case EIO_CUSTOM: |
1175 | req->feed (req); |
1240 | ((void (*)(eio_req *))req->feed) (req); |
1176 | break; |
1241 | break; |
1177 | |
1242 | |
1178 | default: |
1243 | default: |
1179 | req->result = -1; |
1244 | req->result = -1; |
1180 | break; |
1245 | break; |
… | |
… | |
1201 | } |
1266 | } |
1202 | |
1267 | |
1203 | eio_req *eio_fsync (int fd, int pri, eio_cb cb, void *data) |
1268 | eio_req *eio_fsync (int fd, int pri, eio_cb cb, void *data) |
1204 | { |
1269 | { |
1205 | REQ (EIO_FSYNC); req->int1 = fd; SEND; |
1270 | REQ (EIO_FSYNC); req->int1 = fd; SEND; |
|
|
1271 | } |
|
|
1272 | |
|
|
1273 | eio_req *eio_msync (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) |
|
|
1274 | { |
|
|
1275 | REQ (EIO_MSYNC); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; |
|
|
1276 | } |
|
|
1277 | |
|
|
1278 | eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) |
|
|
1279 | { |
|
|
1280 | REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; |
1206 | } |
1281 | } |
1207 | |
1282 | |
1208 | eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) |
1283 | eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) |
1209 | { |
1284 | { |
1210 | REQ (EIO_FDATASYNC); req->int1 = fd; SEND; |
1285 | REQ (EIO_FDATASYNC); req->int1 = fd; SEND; |
… | |
… | |
1367 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
1442 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
1368 | } |
1443 | } |
1369 | |
1444 | |
1370 | eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) |
1445 | eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) |
1371 | { |
1446 | { |
1372 | REQ (EIO_CUSTOM); req->feed = execute; SEND; |
1447 | REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND; |
1373 | } |
1448 | } |
1374 | |
1449 | |
1375 | #endif |
1450 | #endif |
1376 | |
1451 | |
1377 | eio_req *eio_grp (eio_cb cb, void *data) |
1452 | eio_req *eio_grp (eio_cb cb, void *data) |