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

Comparing libeio/eio.c (file contents):
Revision 1.133 by root, Thu Jun 25 13:42:18 2015 UTC vs.
Revision 1.139 by root, Thu Jun 25 18:14:19 2015 UTC

336#define FUBd \ 336#define FUBd \
337 free (eio_buf) 337 free (eio_buf)
338 338
339/*****************************************************************************/ 339/*****************************************************************************/
340 340
341struct tmpbuf
342{
343 void *ptr;
344 int len;
345};
346
347static void *
348tmpbuf_get (struct tmpbuf *buf, int len)
349{
350 if (buf->len < len)
351 {
352 free (buf->ptr);
353 buf->ptr = malloc (buf->len = len);
354 }
355
356 return buf->ptr;
357}
358
359struct tmpbuf; 341struct etp_tmpbuf;
360 342
361#if _POSIX_VERSION >= 200809L 343#if _POSIX_VERSION >= 200809L
362 #define HAVE_AT 1 344 #define HAVE_AT 1
363 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) 345 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD)
364 #ifndef O_SEARCH 346 #ifndef O_SEARCH
365 #define O_SEARCH O_RDONLY 347 #define O_SEARCH O_RDONLY
366 #endif 348 #endif
367#else 349#else
368 #define HAVE_AT 0 350 #define HAVE_AT 0
369 static const char *wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path); 351 static const char *wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path);
370#endif 352#endif
371 353
372struct eio_pwd 354struct eio_pwd
373{ 355{
374#if HAVE_AT 356#if HAVE_AT
384#define ETP_PRI_MAX EIO_PRI_MAX 366#define ETP_PRI_MAX EIO_PRI_MAX
385 367
386#define ETP_TYPE_QUIT -1 368#define ETP_TYPE_QUIT -1
387#define ETP_TYPE_GROUP EIO_GROUP 369#define ETP_TYPE_GROUP EIO_GROUP
388 370
371static void eio_nop_callback (void) { }
372static void (*eio_want_poll_cb)(void) = eio_nop_callback;
373static void (*eio_done_poll_cb)(void) = eio_nop_callback;
374
375#define ETP_WANT_POLL(pool) eio_want_poll_cb ()
376#define ETP_DONE_POLL(pool) eio_done_poll_cb ()
377
389struct etp_worker; 378struct etp_worker;
390
391#define ETP_REQ eio_req 379#define ETP_REQ eio_req
392#define ETP_DESTROY(req) eio_destroy (req) 380#define ETP_DESTROY(req) eio_destroy (req)
393static int eio_finish (eio_req *req); 381static int eio_finish (eio_req *req);
394#define ETP_FINISH(req) eio_finish (req) 382#define ETP_FINISH(req) eio_finish (req)
395static void eio_execute (struct etp_worker *self, eio_req *req); 383static void eio_execute (struct etp_worker *self, eio_req *req);
396#define ETP_EXECUTE(wrk,req) eio_execute (wrk, req) 384#define ETP_EXECUTE(wrk,req) eio_execute (wrk, req)
397 385
398#include "etp.c" 386#include "etp.c"
399 387
388static struct etp_pool eio_pool;
389#define EIO_POOL (&eio_pool)
390
400/*****************************************************************************/ 391/*****************************************************************************/
401 392
402static void 393static void
403grp_try_feed (eio_req *grp) 394grp_try_feed (eio_req *grp)
404{ 395{
405 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 396 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
406 { 397 {
407 grp->flags &= ~EIO_FLAG_GROUPADD; 398 grp->flags &= ~ETP_FLAG_GROUPADD;
408 399
409 EIO_FEED (grp); 400 EIO_FEED (grp);
410 401
411 /* stop if no progress has been made */ 402 /* stop if no progress has been made */
412 if (!(grp->flags & EIO_FLAG_GROUPADD)) 403 if (!(grp->flags & ETP_FLAG_GROUPADD))
413 { 404 {
414 grp->feed = 0; 405 grp->feed = 0;
415 break; 406 break;
416 } 407 }
417 } 408 }
424 415
425 /* call feeder, if applicable */ 416 /* call feeder, if applicable */
426 grp_try_feed (grp); 417 grp_try_feed (grp);
427 418
428 /* finish, if done */ 419 /* finish, if done */
429 if (!grp->size && grp->int1) 420 if (!grp->size && grp->flags & ETP_FLAG_DELAYED)
430 return eio_finish (grp); 421 return eio_finish (grp);
431 else 422 else
432 return 0; 423 return 0;
433} 424}
434 425
470} 461}
471 462
472void 463void
473eio_grp_cancel (eio_req *grp) 464eio_grp_cancel (eio_req *grp)
474{ 465{
475 etp_grp_cancel (grp); 466 etp_grp_cancel (EIO_POOL, grp);
476} 467}
477 468
478void 469void
479eio_cancel (eio_req *req) 470eio_cancel (eio_req *req)
480{ 471{
481 etp_cancel (req); 472 etp_cancel (EIO_POOL, req);
482} 473}
483 474
484void 475void
485eio_submit (eio_req *req) 476eio_submit (eio_req *req)
486{ 477{
487 etp_submit (req); 478 etp_submit (EIO_POOL, req);
488} 479}
489 480
490unsigned int 481unsigned int
491eio_nreqs (void) 482eio_nreqs (void)
492{ 483{
493 return etp_nreqs (); 484 return etp_nreqs (EIO_POOL);
494} 485}
495 486
496unsigned int 487unsigned int
497eio_nready (void) 488eio_nready (void)
498{ 489{
499 return etp_nready (); 490 return etp_nready (EIO_POOL);
500} 491}
501 492
502unsigned int 493unsigned int
503eio_npending (void) 494eio_npending (void)
504{ 495{
505 return etp_npending (); 496 return etp_npending (EIO_POOL);
506} 497}
507 498
508unsigned int ecb_cold 499unsigned int ecb_cold
509eio_nthreads (void) 500eio_nthreads (void)
510{ 501{
511 return etp_nthreads (); 502 return etp_nthreads (EIO_POOL);
512} 503}
513 504
514void ecb_cold 505void ecb_cold
515eio_set_max_poll_time (double nseconds) 506eio_set_max_poll_time (double nseconds)
516{ 507{
517 etp_set_max_poll_time (nseconds); 508 etp_set_max_poll_time (EIO_POOL, nseconds);
518} 509}
519 510
520void ecb_cold 511void ecb_cold
521eio_set_max_poll_reqs (unsigned int maxreqs) 512eio_set_max_poll_reqs (unsigned int maxreqs)
522{ 513{
523 etp_set_max_poll_reqs (maxreqs); 514 etp_set_max_poll_reqs (EIO_POOL, maxreqs);
524} 515}
525 516
526void ecb_cold 517void ecb_cold
527eio_set_max_idle (unsigned int nthreads) 518eio_set_max_idle (unsigned int nthreads)
528{ 519{
529 etp_set_max_idle (nthreads); 520 etp_set_max_idle (EIO_POOL, nthreads);
530} 521}
531 522
532void ecb_cold 523void ecb_cold
533eio_set_idle_timeout (unsigned int seconds) 524eio_set_idle_timeout (unsigned int seconds)
534{ 525{
535 etp_set_idle_timeout (seconds); 526 etp_set_idle_timeout (EIO_POOL, seconds);
536} 527}
537 528
538void ecb_cold 529void ecb_cold
539eio_set_min_parallel (unsigned int nthreads) 530eio_set_min_parallel (unsigned int nthreads)
540{ 531{
541 etp_set_min_parallel (nthreads); 532 etp_set_min_parallel (EIO_POOL, nthreads);
542} 533}
543 534
544void ecb_cold 535void ecb_cold
545eio_set_max_parallel (unsigned int nthreads) 536eio_set_max_parallel (unsigned int nthreads)
546{ 537{
547 etp_set_max_parallel (nthreads); 538 etp_set_max_parallel (EIO_POOL, nthreads);
548} 539}
549 540
550int eio_poll (void) 541int eio_poll (void)
551{ 542{
552 return etp_poll (); 543 return etp_poll (EIO_POOL);
553} 544}
554 545
555/*****************************************************************************/ 546/*****************************************************************************/
556/* work around various missing functions */ 547/* work around various missing functions */
557 548
967 req->result = req->offs == (off_t)-1 ? -1 : 0; 958 req->result = req->offs == (off_t)-1 ? -1 : 0;
968} 959}
969 960
970/* result will always end up in tmpbuf, there is always space for adding a 0-byte */ 961/* result will always end up in tmpbuf, there is always space for adding a 0-byte */
971static int 962static int
972eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 963eio__realpath (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
973{ 964{
974 char *res; 965 char *res;
975 const char *rel = path; 966 const char *rel = path;
976 char *tmp1, *tmp2; 967 char *tmp1, *tmp2;
977#if SYMLOOP_MAX > 32 968#if SYMLOOP_MAX > 32
986 977
987 errno = ENOENT; 978 errno = ENOENT;
988 if (!*rel) 979 if (!*rel)
989 return -1; 980 return -1;
990 981
991 res = tmpbuf_get (tmpbuf, PATH_MAX * 3); 982 res = etp_tmpbuf_get (tmpbuf, PATH_MAX * 3);
992#ifdef _WIN32 983#ifdef _WIN32
993 if (_access (rel, 4) != 0) 984 if (_access (rel, 4) != 0)
994 return -1; 985 return -1;
995 986
996 symlinks = GetFullPathName (rel, PATH_MAX * 3, res, 0); 987 symlinks = GetFullPathName (rel, PATH_MAX * 3, res, 0);
1605#if !HAVE_AT 1596#if !HAVE_AT
1606 1597
1607/* a bit like realpath, but usually faster because it doesn'T have to return */ 1598/* a bit like realpath, but usually faster because it doesn'T have to return */
1608/* an absolute or canonical path */ 1599/* an absolute or canonical path */
1609static const char * 1600static const char *
1610wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1601wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
1611{ 1602{
1612 if (!wd || *path == '/') 1603 if (!wd || *path == '/')
1613 return path; 1604 return path;
1614 1605
1615 if (path [0] == '.' && !path [1]) 1606 if (path [0] == '.' && !path [1])
1617 1608
1618 { 1609 {
1619 int l1 = wd->len; 1610 int l1 = wd->len;
1620 int l2 = strlen (path); 1611 int l2 = strlen (path);
1621 1612
1622 char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2); 1613 char *res = etp_tmpbuf_get (tmpbuf, l1 + l2 + 2);
1623 1614
1624 memcpy (res, wd->str, l1); 1615 memcpy (res, wd->str, l1);
1625 res [l1] = '/'; 1616 res [l1] = '/';
1626 memcpy (res + l1 + 1, path, l2 + 1); 1617 memcpy (res + l1 + 1, path, l2 + 1);
1627 1618
1630} 1621}
1631 1622
1632#endif 1623#endif
1633 1624
1634static eio_wd 1625static eio_wd
1635eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1626eio__wd_open_sync (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path)
1636{ 1627{
1637 int fd; 1628 int fd;
1638 eio_wd res; 1629 eio_wd res;
1639 int len = eio__realpath (tmpbuf, wd, path); 1630 int len = eio__realpath (tmpbuf, wd, path);
1640 1631
1662} 1653}
1663 1654
1664eio_wd 1655eio_wd
1665eio_wd_open_sync (eio_wd wd, const char *path) 1656eio_wd_open_sync (eio_wd wd, const char *path)
1666{ 1657{
1667 struct tmpbuf tmpbuf = { 0 }; 1658 struct etp_tmpbuf tmpbuf = { };
1668 wd = eio__wd_open_sync (&tmpbuf, wd, path); 1659 wd = eio__wd_open_sync (&tmpbuf, wd, path);
1669 free (tmpbuf.ptr); 1660 free (tmpbuf.ptr);
1670 1661
1671 return wd; 1662 return wd;
1672} 1663}
1721/*****************************************************************************/ 1712/*****************************************************************************/
1722 1713
1723#define ALLOC(len) \ 1714#define ALLOC(len) \
1724 if (!req->ptr2) \ 1715 if (!req->ptr2) \
1725 { \ 1716 { \
1726 X_LOCK (wrklock); \ 1717 X_LOCK (EIO_POOL->wrklock); \
1727 req->flags |= EIO_FLAG_PTR2_FREE; \ 1718 req->flags |= EIO_FLAG_PTR2_FREE; \
1728 X_UNLOCK (wrklock); \ 1719 X_UNLOCK (EIO_POOL->wrklock); \
1729 req->ptr2 = malloc (len); \ 1720 req->ptr2 = malloc (len); \
1730 if (!req->ptr2) \ 1721 if (!req->ptr2) \
1731 { \ 1722 { \
1732 errno = ENOMEM; \ 1723 errno = ENOMEM; \
1733 req->result = -1; \ 1724 req->result = -1; \
1734 break; \ 1725 break; \
1735 } \ 1726 } \
1736 } 1727 }
1737 1728
1738static void ecb_noinline ecb_cold
1739etp_proc_init (void)
1740{
1741#if HAVE_PRCTL_SET_NAME
1742 /* provide a more sensible "thread name" */
1743 char name[16 + 1];
1744 const int namelen = sizeof (name) - 1;
1745 int len;
1746
1747 prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0);
1748 name [namelen] = 0;
1749 len = strlen (name);
1750 strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio");
1751 prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0);
1752#endif
1753}
1754
1755/* TODO: move somehow to etp.c */
1756X_THREAD_PROC (etp_proc)
1757{
1758 ETP_REQ *req;
1759 struct timespec ts;
1760 etp_worker *self = (etp_worker *)thr_arg;
1761
1762 etp_proc_init ();
1763
1764 /* try to distribute timeouts somewhat evenly */
1765 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1766
1767 for (;;)
1768 {
1769 ts.tv_sec = 0;
1770
1771 X_LOCK (reqlock);
1772
1773 for (;;)
1774 {
1775 req = reqq_shift (&req_queue);
1776
1777 if (req)
1778 break;
1779
1780 if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */
1781 {
1782 X_UNLOCK (reqlock);
1783 X_LOCK (wrklock);
1784 --started;
1785 X_UNLOCK (wrklock);
1786 goto quit;
1787 }
1788
1789 ++idle;
1790
1791 if (idle <= max_idle)
1792 /* we are allowed to idle, so do so without any timeout */
1793 X_COND_WAIT (reqwait, reqlock);
1794 else
1795 {
1796 /* initialise timeout once */
1797 if (!ts.tv_sec)
1798 ts.tv_sec = time (0) + idle_timeout;
1799
1800 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
1801 ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */
1802 }
1803
1804 --idle;
1805 }
1806
1807 --nready;
1808
1809 X_UNLOCK (reqlock);
1810
1811 if (req->type == ETP_TYPE_QUIT)
1812 goto quit;
1813
1814 ETP_EXECUTE (self, req);
1815
1816 X_LOCK (reslock);
1817
1818 ++npending;
1819
1820 if (!reqq_push (&res_queue, req) && want_poll_cb)
1821 want_poll_cb ();
1822
1823 etp_worker_clear (self);
1824
1825 X_UNLOCK (reslock);
1826 }
1827
1828quit:
1829 free (req);
1830
1831 X_LOCK (wrklock);
1832 etp_worker_free (self);
1833 X_UNLOCK (wrklock);
1834
1835 return 0;
1836}
1837
1838/*****************************************************************************/ 1729/*****************************************************************************/
1839 1730
1840int ecb_cold 1731int ecb_cold
1841eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1732eio_init (void (*want_poll)(void), void (*done_poll)(void))
1842{ 1733{
1843 return etp_init (want_poll, done_poll); 1734 eio_want_poll_cb = want_poll;
1735 eio_done_poll_cb = done_poll;
1736
1737 return etp_init (EIO_POOL, 0, 0, 0);
1844} 1738}
1845 1739
1846ecb_inline void 1740ecb_inline void
1847eio_api_destroy (eio_req *req) 1741eio_api_destroy (eio_req *req)
1848{ 1742{
2073 req->result = select (0, 0, 0, 0, &tv); 1967 req->result = select (0, 0, 0, 0, &tv);
2074 } 1968 }
2075#endif 1969#endif
2076 break; 1970 break;
2077 1971
1972#if 0
2078 case EIO_GROUP: 1973 case EIO_GROUP:
2079 abort (); /* handled in eio_request */ 1974 abort (); /* handled in eio_request */
1975#endif
2080 1976
2081 case EIO_NOP: 1977 case EIO_NOP:
2082 req->result = 0; 1978 req->result = 0;
2083 break; 1979 break;
2084 1980
2384void 2280void
2385eio_grp_add (eio_req *grp, eio_req *req) 2281eio_grp_add (eio_req *grp, eio_req *req)
2386{ 2282{
2387 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); 2283 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
2388 2284
2389 grp->flags |= EIO_FLAG_GROUPADD; 2285 grp->flags |= ETP_FLAG_GROUPADD;
2390 2286
2391 ++grp->size; 2287 ++grp->size;
2392 req->grp = grp; 2288 req->grp = grp;
2393 2289
2394 req->grp_prev = 0; 2290 req->grp_prev = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines