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

Comparing libeio/eio.c (file contents):
Revision 1.22 by root, Fri Jul 11 10:54:50 2008 UTC vs.
Revision 1.25 by root, Tue Sep 30 17:23:24 2008 UTC

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
128struct etp_worker; 139struct etp_worker;
129 140
411 X_UNLOCK (wrklock); 422 X_UNLOCK (wrklock);
412} 423}
413 424
414static void etp_maybe_start_thread (void) 425static void etp_maybe_start_thread (void)
415{ 426{
416 if (etp_nthreads () >= wanted) 427 if (expect_true (etp_nthreads () >= wanted))
417 return; 428 return;
418 429
419 /* todo: maybe use idle here, but might be less exact */ 430 /* todo: maybe use idle here, but might be less exact */
420 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 ()))
421 return; 432 return;
422 433
423 etp_start_thread (); 434 etp_start_thread ();
424} 435}
425 436
478 489
479 X_LOCK (reqlock); 490 X_LOCK (reqlock);
480 --nreqs; 491 --nreqs;
481 X_UNLOCK (reqlock); 492 X_UNLOCK (reqlock);
482 493
483 if (req->type == EIO_GROUP && req->size) 494 if (expect_false (req->type == EIO_GROUP && req->size))
484 { 495 {
485 req->int1 = 1; /* mark request as delayed */ 496 req->int1 = 1; /* mark request as delayed */
486 continue; 497 continue;
487 } 498 }
488 else 499 else
489 { 500 {
490 int res = ETP_FINISH (req); 501 int res = ETP_FINISH (req);
491 if (res) 502 if (expect_false (res))
492 return res; 503 return res;
493 } 504 }
494 505
495 if (maxreqs && !--maxreqs) 506 if (expect_false (maxreqs && !--maxreqs))
496 break; 507 break;
497 508
498 if (maxtime) 509 if (maxtime)
499 { 510 {
500 gettimeofday (&tv_now, 0); 511 gettimeofday (&tv_now, 0);
519 530
520static void etp_submit (ETP_REQ *req) 531static void etp_submit (ETP_REQ *req)
521{ 532{
522 req->pri -= ETP_PRI_MIN; 533 req->pri -= ETP_PRI_MIN;
523 534
524 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;
525 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;
526 537
538 if (expect_false (req->type == EIO_GROUP))
539 {
540 /* I hope this is worth it :/ */
527 X_LOCK (reqlock); 541 X_LOCK (reqlock);
528 ++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;
529 ++nready; 558 ++nready;
530 reqq_push (&req_queue, req); 559 reqq_push (&req_queue, req);
531 X_COND_SIGNAL (reqwait); 560 X_COND_SIGNAL (reqwait);
532 X_UNLOCK (reqlock); 561 X_UNLOCK (reqlock);
533 562
534 etp_maybe_start_thread (); 563 etp_maybe_start_thread ();
564 }
535} 565}
536 566
537static void etp_set_max_poll_time (double nseconds) 567static void etp_set_max_poll_time (double nseconds)
538{ 568{
539 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 569 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
948 978
949 if (errno) 979 if (errno)
950 res = -1; 980 res = -1;
951 981
952 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
989int
990eio__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;
953} 1012}
954 1013
955/*****************************************************************************/ 1014/*****************************************************************************/
956 1015
957#define ALLOC(len) \ 1016#define ALLOC(len) \
1124 req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break; 1183 req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break;
1125 1184
1126 case EIO_SYNC: req->result = 0; sync (); break; 1185 case EIO_SYNC: req->result = 0; sync (); break;
1127 case EIO_FSYNC: req->result = fsync (req->int1); break; 1186 case EIO_FSYNC: req->result = fsync (req->int1); break;
1128 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;
1129 1190
1130 case EIO_READDIR: eio__scandir (req, self); break; 1191 case EIO_READDIR: eio__scandir (req, self); break;
1131 1192
1132 case EIO_BUSY: 1193 case EIO_BUSY:
1133#ifdef _WIN32 1194#ifdef _WIN32
1165 1226
1166 req->result = req->type == EIO_FUTIME 1227 req->result = req->type == EIO_FUTIME
1167 ? futimes (req->int1, times) 1228 ? futimes (req->int1, times)
1168 : utimes (req->ptr1, times); 1229 : utimes (req->ptr1, times);
1169 } 1230 }
1231 break;
1170 1232
1171 case EIO_GROUP: 1233 case EIO_GROUP:
1234 abort (); /* handled in eio_request */
1235
1172 case EIO_NOP: 1236 case EIO_NOP:
1173 req->result = 0; 1237 req->result = 0;
1174 break; 1238 break;
1175 1239
1176 case EIO_CUSTOM: 1240 case EIO_CUSTOM:
1177 req->feed (req); 1241 ((void (*)(eio_req *))req->feed) (req);
1178 break; 1242 break;
1179 1243
1180 default: 1244 default:
1181 req->result = -1; 1245 req->result = -1;
1182 break; 1246 break;
1203} 1267}
1204 1268
1205eio_req *eio_fsync (int fd, int pri, eio_cb cb, void *data) 1269eio_req *eio_fsync (int fd, int pri, eio_cb cb, void *data)
1206{ 1270{
1207 REQ (EIO_FSYNC); req->int1 = fd; SEND; 1271 REQ (EIO_FSYNC); req->int1 = fd; SEND;
1272}
1273
1274eio_req *eio_msync (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data)
1275{
1276 REQ (EIO_MSYNC); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND;
1277}
1278
1279eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data)
1280{
1281 REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND;
1208} 1282}
1209 1283
1210eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) 1284eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data)
1211{ 1285{
1212 REQ (EIO_FDATASYNC); req->int1 = fd; SEND; 1286 REQ (EIO_FDATASYNC); req->int1 = fd; SEND;
1369 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); 1443 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
1370} 1444}
1371 1445
1372eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) 1446eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data)
1373{ 1447{
1374 REQ (EIO_CUSTOM); req->feed = execute; SEND; 1448 REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND;
1375} 1449}
1376 1450
1377#endif 1451#endif
1378 1452
1379eio_req *eio_grp (eio_cb cb, void *data) 1453eio_req *eio_grp (eio_cb cb, void *data)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines