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

Comparing cvsroot/libeio/eio.c (file contents):
Revision 1.15 by root, Sat May 17 12:26:57 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
177static mutex_t wrklock = X_MUTEX_INIT; 188static mutex_t wrklock = X_MUTEX_INIT;
178static mutex_t reslock = X_MUTEX_INIT; 189static mutex_t reslock = X_MUTEX_INIT;
179static mutex_t reqlock = X_MUTEX_INIT; 190static mutex_t reqlock = X_MUTEX_INIT;
180static cond_t reqwait = X_COND_INIT; 191static 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 */
199static mutex_t preadwritelock = X_MUTEX_INIT;
200#endif
201
182typedef struct etp_worker 202typedef 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
314 X_LOCK (reqlock); 334 X_LOCK (reqlock);
315 X_LOCK (reslock); 335 X_LOCK (reslock);
316#if !HAVE_PREADWRITE 336#if !HAVE_PREADWRITE
317 X_LOCK (preadwritelock); 337 X_LOCK (preadwritelock);
318#endif 338#endif
319#if !HAVE_READDIR_R
320 X_LOCK (readdirlock);
321#endif
322} 339}
323 340
324static void etp_atfork_parent (void) 341static void etp_atfork_parent (void)
325{ 342{
326#if !HAVE_READDIR_R
327 X_UNLOCK (readdirlock);
328#endif
329#if !HAVE_PREADWRITE 343#if !HAVE_PREADWRITE
330 X_UNLOCK (preadwritelock); 344 X_UNLOCK (preadwritelock);
331#endif 345#endif
332 X_UNLOCK (reslock); 346 X_UNLOCK (reslock);
333 X_UNLOCK (reqlock); 347 X_UNLOCK (reqlock);
336 350
337static void etp_atfork_child (void) 351static void etp_atfork_child (void)
338{ 352{
339 ETP_REQ *prv; 353 ETP_REQ *prv;
340 354
341 while (prv = reqq_shift (&req_queue)) 355 while ((prv = reqq_shift (&req_queue)))
342 ETP_DESTROY (prv); 356 ETP_DESTROY (prv);
343 357
344 while (prv = reqq_shift (&res_queue)) 358 while ((prv = reqq_shift (&res_queue)))
345 ETP_DESTROY (prv); 359 ETP_DESTROY (prv);
346 360
347 while (wrk_first.next != &wrk_first) 361 while (wrk_first.next != &wrk_first)
348 { 362 {
349 etp_worker *wrk = wrk_first.next; 363 etp_worker *wrk = wrk_first.next;
377 391
378 pthread_once (&doinit, etp_once_init); 392 pthread_once (&doinit, etp_once_init);
379 393
380 want_poll_cb = want_poll; 394 want_poll_cb = want_poll;
381 done_poll_cb = done_poll; 395 done_poll_cb = done_poll;
396
397 return 0;
382} 398}
383 399
384X_THREAD_PROC (etp_proc); 400X_THREAD_PROC (etp_proc);
385 401
386static void etp_start_thread (void) 402static void etp_start_thread (void)
406 X_UNLOCK (wrklock); 422 X_UNLOCK (wrklock);
407} 423}
408 424
409static void etp_maybe_start_thread (void) 425static void etp_maybe_start_thread (void)
410{ 426{
411 if (etp_nthreads () >= wanted) 427 if (expect_true (etp_nthreads () >= wanted))
412 return; 428 return;
413 429
414 /* todo: maybe use idle here, but might be less exact */ 430 /* todo: maybe use idle here, but might be less exact */
415 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 ()))
416 return; 432 return;
417 433
418 etp_start_thread (); 434 etp_start_thread ();
419} 435}
420 436
473 489
474 X_LOCK (reqlock); 490 X_LOCK (reqlock);
475 --nreqs; 491 --nreqs;
476 X_UNLOCK (reqlock); 492 X_UNLOCK (reqlock);
477 493
478 if (req->type == EIO_GROUP && req->size) 494 if (expect_false (req->type == EIO_GROUP && req->size))
479 { 495 {
480 req->int1 = 1; /* mark request as delayed */ 496 req->int1 = 1; /* mark request as delayed */
481 continue; 497 continue;
482 } 498 }
483 else 499 else
484 { 500 {
485 int res = ETP_FINISH (req); 501 int res = ETP_FINISH (req);
486 if (res) 502 if (expect_false (res))
487 return res; 503 return res;
488 } 504 }
489 505
490 if (maxreqs && !--maxreqs) 506 if (expect_false (maxreqs && !--maxreqs))
491 break; 507 break;
492 508
493 if (maxtime) 509 if (maxtime)
494 { 510 {
495 gettimeofday (&tv_now, 0); 511 gettimeofday (&tv_now, 0);
514 530
515static void etp_submit (ETP_REQ *req) 531static void etp_submit (ETP_REQ *req)
516{ 532{
517 req->pri -= ETP_PRI_MIN; 533 req->pri -= ETP_PRI_MIN;
518 534
519 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;
520 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;
521 537
538 if (expect_false (req->type == EIO_GROUP))
539 {
540 /* I hope this is worth it :/ */
522 X_LOCK (reqlock); 541 X_LOCK (reqlock);
523 ++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;
524 ++nready; 558 ++nready;
525 reqq_push (&req_queue, req); 559 reqq_push (&req_queue, req);
526 X_COND_SIGNAL (reqwait); 560 X_COND_SIGNAL (reqwait);
527 X_UNLOCK (reqlock); 561 X_UNLOCK (reqlock);
528 562
529 etp_maybe_start_thread (); 563 etp_maybe_start_thread ();
564 }
530} 565}
531 566
532static void etp_set_max_poll_time (double nseconds) 567static void etp_set_max_poll_time (double nseconds)
533{ 568{
534 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 569 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
704 739
705#if !HAVE_PREADWRITE 740#if !HAVE_PREADWRITE
706# define pread eio__pread 741# define pread eio__pread
707# define pwrite eio__pwrite 742# define pwrite eio__pwrite
708 743
709/*
710 * make our pread/pwrite safe against themselves, but not against
711 * normal read/write by using a mutex. slows down execution a lot,
712 * but that's your problem, not mine.
713 */
714static mutex_t preadwritelock = X_MUTEX_INIT;
715
716static ssize_t 744static ssize_t
717eio__pread (int fd, void *buf, size_t count, off_t offset) 745eio__pread (int fd, void *buf, size_t count, off_t offset)
718{ 746{
719 ssize_t res; 747 ssize_t res;
720 off_t ooffset; 748 off_t ooffset;
781 809
782#if !HAVE_READAHEAD 810#if !HAVE_READAHEAD
783# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) 811# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self)
784 812
785static ssize_t 813static ssize_t
786eio__readahead (int fd, off_t offset, size_t count, worker *self) 814eio__readahead (int fd, off_t offset, size_t count, etp_worker *self)
787{ 815{
788 size_t todo = count; 816 size_t todo = count;
789 dBUF; 817 dBUF;
790 818
791 while (todo > 0) 819 while (todo > 0)
799 827
800 errno = 0; 828 errno = 0;
801 return count; 829 return count;
802} 830}
803 831
804#endif
805
806#if !HAVE_READDIR_R
807# define readdir_r eio__readdir_r
808
809static mutex_t readdirlock = X_MUTEX_INIT;
810
811static int
812eio__readdir_r (DIR *dirp, EIO_STRUCT_DIRENT *ent, EIO_STRUCT_DIRENT **res)
813{
814 EIO_STRUCT_DIRENT *e;
815 int errorno;
816
817 X_LOCK (readdirlock);
818
819 e = readdir (dirp);
820 errorno = errno;
821
822 if (e)
823 {
824 *res = ent;
825 strcpy (ent->d_name, e->d_name);
826 }
827 else
828 *res = 0;
829
830 X_UNLOCK (readdirlock);
831
832 errno = errorno;
833 return e ? 0 : -1;
834}
835#endif 832#endif
836 833
837/* sendfile always needs emulation */ 834/* sendfile always needs emulation */
838static ssize_t 835static ssize_t
839eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) 836eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self)
931/* read a full directory */ 928/* read a full directory */
932static void 929static void
933eio__scandir (eio_req *req, etp_worker *self) 930eio__scandir (eio_req *req, etp_worker *self)
934{ 931{
935 DIR *dirp; 932 DIR *dirp;
936 union
937 {
938 EIO_STRUCT_DIRENT d;
939 char b [offsetof (EIO_STRUCT_DIRENT, d_name) + NAME_MAX + 1];
940 } *u;
941 EIO_STRUCT_DIRENT *entp; 933 EIO_STRUCT_DIRENT *entp;
942 char *name, *names; 934 char *name, *names;
943 int memlen = 4096; 935 int memlen = 4096;
944 int memofs = 0; 936 int memofs = 0;
945 int res = 0; 937 int res = 0;
946 938
947 X_LOCK (wrklock); 939 X_LOCK (wrklock);
940 /* the corresponding closedir is in ETP_WORKER_CLEAR */
948 self->dirp = dirp = opendir (req->ptr1); 941 self->dirp = dirp = opendir (req->ptr1);
949 self->dbuf = u = malloc (sizeof (*u));
950 req->flags |= EIO_FLAG_PTR2_FREE; 942 req->flags |= EIO_FLAG_PTR2_FREE;
951 req->ptr2 = names = malloc (memlen); 943 req->ptr2 = names = malloc (memlen);
952 X_UNLOCK (wrklock); 944 X_UNLOCK (wrklock);
953 945
954 if (dirp && u && names) 946 if (dirp && names)
955 for (;;) 947 for (;;)
956 { 948 {
957 errno = 0; 949 errno = 0;
958 readdir_r (dirp, &u->d, &entp); 950 entp = readdir (dirp);
959 951
960 if (!entp) 952 if (!entp)
961 break; 953 break;
962 954
963 name = entp->d_name; 955 name = entp->d_name;
986 978
987 if (errno) 979 if (errno)
988 res = -1; 980 res = -1;
989 981
990 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;
991} 1012}
992 1013
993/*****************************************************************************/ 1014/*****************************************************************************/
994 1015
995#define ALLOC(len) \ 1016#define ALLOC(len) \
1082 1103
1083/*****************************************************************************/ 1104/*****************************************************************************/
1084 1105
1085int eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1106int eio_init (void (*want_poll)(void), void (*done_poll)(void))
1086{ 1107{
1087 etp_init (want_poll, done_poll); 1108 return etp_init (want_poll, done_poll);
1088} 1109}
1089 1110
1090static void eio_api_destroy (eio_req *req) 1111static void eio_api_destroy (eio_req *req)
1091{ 1112{
1092 free (req); 1113 free (req);
1154 case EIO_RMDIR: req->result = rmdir (req->ptr1); break; 1175 case EIO_RMDIR: req->result = rmdir (req->ptr1); break;
1155 case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break; 1176 case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break;
1156 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; 1177 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break;
1157 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; 1178 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break;
1158 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; 1179 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break;
1159 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; 1180 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->int3); break;
1160 1181
1161 case EIO_READLINK: ALLOC (NAME_MAX); 1182 case EIO_READLINK: ALLOC (NAME_MAX);
1162 req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break; 1183 req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break;
1163 1184
1164 case EIO_SYNC: req->result = 0; sync (); break; 1185 case EIO_SYNC: req->result = 0; sync (); break;
1165 case EIO_FSYNC: req->result = fsync (req->int1); break; 1186 case EIO_FSYNC: req->result = fsync (req->int1); break;
1166 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;
1167 1190
1168 case EIO_READDIR: eio__scandir (req, self); break; 1191 case EIO_READDIR: eio__scandir (req, self); break;
1169 1192
1170 case EIO_BUSY: 1193 case EIO_BUSY:
1171#ifdef _WIN32 1194#ifdef _WIN32
1203 1226
1204 req->result = req->type == EIO_FUTIME 1227 req->result = req->type == EIO_FUTIME
1205 ? futimes (req->int1, times) 1228 ? futimes (req->int1, times)
1206 : utimes (req->ptr1, times); 1229 : utimes (req->ptr1, times);
1207 } 1230 }
1231 break;
1208 1232
1209 case EIO_GROUP: 1233 case EIO_GROUP:
1234 abort (); /* handled in eio_request */
1235
1210 case EIO_NOP: 1236 case EIO_NOP:
1211 req->result = 0; 1237 req->result = 0;
1212 break; 1238 break;
1213 1239
1214 case EIO_CUSTOM: 1240 case EIO_CUSTOM:
1215 req->feed (req); 1241 ((void (*)(eio_req *))req->feed) (req);
1216 break; 1242 break;
1217 1243
1218 default: 1244 default:
1219 req->result = -1; 1245 req->result = -1;
1220 break; 1246 break;
1243eio_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)
1244{ 1270{
1245 REQ (EIO_FSYNC); req->int1 = fd; SEND; 1271 REQ (EIO_FSYNC); req->int1 = fd; SEND;
1246} 1272}
1247 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;
1282}
1283
1248eio_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)
1249{ 1285{
1250 REQ (EIO_FDATASYNC); req->int1 = fd; SEND; 1286 REQ (EIO_FDATASYNC); req->int1 = fd; SEND;
1251} 1287}
1252 1288
1371 return eio__1path (EIO_READDIR, path, pri, cb, data); 1407 return eio__1path (EIO_READDIR, path, pri, cb, data);
1372} 1408}
1373 1409
1374eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) 1410eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data)
1375{ 1411{
1376 REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int2 = (long)dev; SEND; 1412 REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND;
1377} 1413}
1378 1414
1379static eio_req * 1415static eio_req *
1380eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data) 1416eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1381{ 1417{
1407 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); 1443 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
1408} 1444}
1409 1445
1410eio_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)
1411{ 1447{
1412 REQ (EIO_CUSTOM); req->feed = execute; SEND; 1448 REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND;
1413} 1449}
1414 1450
1415#endif 1451#endif
1416 1452
1417eio_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