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

Comparing libeio/eio.c (file contents):
Revision 1.135 by root, Thu Jun 25 16:39:29 2015 UTC vs.
Revision 1.143 by root, Sat Dec 3 16:33:46 2016 UTC

1/* 1/*
2 * libeio implementation 2 * libeio implementation
3 * 3 *
4 * Copyright (c) 2007,2008,2009,2010,2011,2012,2013 Marc Alexander Lehmann <libeio@schmorp.de> 4 * Copyright (c) 2007,2008,2009,2010,2011,2012,2013,2016 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 *
42#endif 42#endif
43 43
44#include "eio.h" 44#include "eio.h"
45#include "ecb.h" 45#include "ecb.h"
46 46
47#ifdef EIO_STACKSIZE
48# define X_STACKSIZE EIO_STACKSIZE
49#endif
50#include "xthread.h"
51
52#include <errno.h> 47#include <errno.h>
53#include <stddef.h> 48#include <stddef.h>
54#include <stdlib.h> 49#include <stdlib.h>
55#include <string.h> 50#include <string.h>
56#include <errno.h> 51#include <errno.h>
122 #define chmod(path,mode) _chmod (path, mode) 117 #define chmod(path,mode) _chmod (path, mode)
123 #define dup(fd) _dup (fd) 118 #define dup(fd) _dup (fd)
124 #define dup2(fd1,fd2) _dup2 (fd1, fd2) 119 #define dup2(fd1,fd2) _dup2 (fd1, fd2)
125 #define pipe(fds) _pipe (fds, 4096, O_BINARY) 120 #define pipe(fds) _pipe (fds, 4096, O_BINARY)
126 121
122 #define fcntl(fd,cmd,arg) EIO_ENOSYS ()
123 #define ioctl(fd,cmd,arg) EIO_ENOSYS ()
127 #define fchmod(fd,mode) EIO_ENOSYS () 124 #define fchmod(fd,mode) EIO_ENOSYS ()
128 #define chown(path,uid,gid) EIO_ENOSYS () 125 #define chown(path,uid,gid) EIO_ENOSYS ()
129 #define fchown(fd,uid,gid) EIO_ENOSYS () 126 #define fchown(fd,uid,gid) EIO_ENOSYS ()
130 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ 127 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */
131 #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ 128 #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */
204 /* we could even stat and see if it exists */ 201 /* we could even stat and see if it exists */
205 static int 202 static int
206 symlink (const char *old, const char *neu) 203 symlink (const char *old, const char *neu)
207 { 204 {
208 #if WINVER >= 0x0600 205 #if WINVER >= 0x0600
206 int flags;
207
208 /* This tries out all combinations of SYMBOLIC_LINK_FLAG_DIRECTORY
209 * and SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE,
210 * with directory first.
211 */
212 for (flags = 3; flags >= 0; --flags)
209 if (CreateSymbolicLink (neu, old, 1)) 213 if (CreateSymbolicLink (neu, old, flags))
210 return 0; 214 return 0;
211
212 if (CreateSymbolicLink (neu, old, 0))
213 return 0;
214 #endif 215 #endif
215 216
216 return EIO_ERRNO (ENOENT, -1); 217 return EIO_ERRNO (ENOENT, -1);
217 } 218 }
218 219
281# include <utime.h> 282# include <utime.h>
282#endif 283#endif
283 284
284#if HAVE_SYS_SYSCALL_H 285#if HAVE_SYS_SYSCALL_H
285# include <sys/syscall.h> 286# include <sys/syscall.h>
286#endif
287
288#if HAVE_SYS_PRCTL_H
289# include <sys/prctl.h>
290#endif 287#endif
291 288
292#if HAVE_SENDFILE 289#if HAVE_SENDFILE
293# if __linux 290# if __linux
294# include <sys/sendfile.h> 291# include <sys/sendfile.h>
366#define ETP_PRI_MAX EIO_PRI_MAX 363#define ETP_PRI_MAX EIO_PRI_MAX
367 364
368#define ETP_TYPE_QUIT -1 365#define ETP_TYPE_QUIT -1
369#define ETP_TYPE_GROUP EIO_GROUP 366#define ETP_TYPE_GROUP EIO_GROUP
370 367
368static void eio_nop_callback (void) { }
369static void (*eio_want_poll_cb)(void) = eio_nop_callback;
370static void (*eio_done_poll_cb)(void) = eio_nop_callback;
371
372#define ETP_WANT_POLL(pool) eio_want_poll_cb ()
373#define ETP_DONE_POLL(pool) eio_done_poll_cb ()
374
371struct etp_worker; 375struct etp_worker;
372
373#define ETP_REQ eio_req 376#define ETP_REQ eio_req
374#define ETP_DESTROY(req) eio_destroy (req) 377#define ETP_DESTROY(req) eio_destroy (req)
375static int eio_finish (eio_req *req); 378static int eio_finish (eio_req *req);
376#define ETP_FINISH(req) eio_finish (req) 379#define ETP_FINISH(req) eio_finish (req)
377static void eio_execute (struct etp_worker *self, eio_req *req); 380static void eio_execute (struct etp_worker *self, eio_req *req);
378#define ETP_EXECUTE(wrk,req) eio_execute (wrk, req) 381#define ETP_EXECUTE(wrk,req) eio_execute (wrk, req)
379 382
380#include "etp.c" 383#include "etp.c"
381 384
385static struct etp_pool eio_pool;
386#define EIO_POOL (&eio_pool)
387
382/*****************************************************************************/ 388/*****************************************************************************/
383 389
384static void 390static void
385grp_try_feed (eio_req *grp) 391grp_try_feed (eio_req *grp)
386{ 392{
387 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 393 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
388 { 394 {
389 grp->flags &= ~EIO_FLAG_GROUPADD; 395 grp->flags &= ~ETP_FLAG_GROUPADD;
390 396
391 EIO_FEED (grp); 397 EIO_FEED (grp);
392 398
393 /* stop if no progress has been made */ 399 /* stop if no progress has been made */
394 if (!(grp->flags & EIO_FLAG_GROUPADD)) 400 if (!(grp->flags & ETP_FLAG_GROUPADD))
395 { 401 {
396 grp->feed = 0; 402 grp->feed = 0;
397 break; 403 break;
398 } 404 }
399 } 405 }
406 412
407 /* call feeder, if applicable */ 413 /* call feeder, if applicable */
408 grp_try_feed (grp); 414 grp_try_feed (grp);
409 415
410 /* finish, if done */ 416 /* finish, if done */
411 if (!grp->size && grp->int1) 417 if (!grp->size && grp->flags & ETP_FLAG_DELAYED)
412 return eio_finish (grp); 418 return eio_finish (grp);
413 else 419 else
414 return 0; 420 return 0;
415} 421}
416 422
452} 458}
453 459
454void 460void
455eio_grp_cancel (eio_req *grp) 461eio_grp_cancel (eio_req *grp)
456{ 462{
457 etp_grp_cancel (grp); 463 etp_grp_cancel (EIO_POOL, grp);
458} 464}
459 465
460void 466void
461eio_cancel (eio_req *req) 467eio_cancel (eio_req *req)
462{ 468{
463 etp_cancel (req); 469 etp_cancel (EIO_POOL, req);
464} 470}
465 471
466void 472void
467eio_submit (eio_req *req) 473eio_submit (eio_req *req)
468{ 474{
469 etp_submit (req); 475 etp_submit (EIO_POOL, req);
470} 476}
471 477
472unsigned int 478unsigned int
473eio_nreqs (void) 479eio_nreqs (void)
474{ 480{
475 return etp_nreqs (); 481 return etp_nreqs (EIO_POOL);
476} 482}
477 483
478unsigned int 484unsigned int
479eio_nready (void) 485eio_nready (void)
480{ 486{
481 return etp_nready (); 487 return etp_nready (EIO_POOL);
482} 488}
483 489
484unsigned int 490unsigned int
485eio_npending (void) 491eio_npending (void)
486{ 492{
487 return etp_npending (); 493 return etp_npending (EIO_POOL);
488} 494}
489 495
490unsigned int ecb_cold 496unsigned int ecb_cold
491eio_nthreads (void) 497eio_nthreads (void)
492{ 498{
493 return etp_nthreads (); 499 return etp_nthreads (EIO_POOL);
494} 500}
495 501
496void ecb_cold 502void ecb_cold
497eio_set_max_poll_time (double nseconds) 503eio_set_max_poll_time (double nseconds)
498{ 504{
499 etp_set_max_poll_time (nseconds); 505 etp_set_max_poll_time (EIO_POOL, nseconds);
500} 506}
501 507
502void ecb_cold 508void ecb_cold
503eio_set_max_poll_reqs (unsigned int maxreqs) 509eio_set_max_poll_reqs (unsigned int maxreqs)
504{ 510{
505 etp_set_max_poll_reqs (maxreqs); 511 etp_set_max_poll_reqs (EIO_POOL, maxreqs);
506} 512}
507 513
508void ecb_cold 514void ecb_cold
509eio_set_max_idle (unsigned int nthreads) 515eio_set_max_idle (unsigned int nthreads)
510{ 516{
511 etp_set_max_idle (nthreads); 517 etp_set_max_idle (EIO_POOL, nthreads);
512} 518}
513 519
514void ecb_cold 520void ecb_cold
515eio_set_idle_timeout (unsigned int seconds) 521eio_set_idle_timeout (unsigned int seconds)
516{ 522{
517 etp_set_idle_timeout (seconds); 523 etp_set_idle_timeout (EIO_POOL, seconds);
518} 524}
519 525
520void ecb_cold 526void ecb_cold
521eio_set_min_parallel (unsigned int nthreads) 527eio_set_min_parallel (unsigned int nthreads)
522{ 528{
523 etp_set_min_parallel (nthreads); 529 etp_set_min_parallel (EIO_POOL, nthreads);
524} 530}
525 531
526void ecb_cold 532void ecb_cold
527eio_set_max_parallel (unsigned int nthreads) 533eio_set_max_parallel (unsigned int nthreads)
528{ 534{
529 etp_set_max_parallel (nthreads); 535 etp_set_max_parallel (EIO_POOL, nthreads);
530} 536}
531 537
532int eio_poll (void) 538int eio_poll (void)
533{ 539{
534 return etp_poll (); 540 return etp_poll (EIO_POOL);
535} 541}
536 542
537/*****************************************************************************/ 543/*****************************************************************************/
538/* work around various missing functions */ 544/* work around various missing functions */
539 545
1703/*****************************************************************************/ 1709/*****************************************************************************/
1704 1710
1705#define ALLOC(len) \ 1711#define ALLOC(len) \
1706 if (!req->ptr2) \ 1712 if (!req->ptr2) \
1707 { \ 1713 { \
1708 X_LOCK (wrklock); \ 1714 X_LOCK (EIO_POOL->wrklock); \
1709 req->flags |= EIO_FLAG_PTR2_FREE; \ 1715 req->flags |= EIO_FLAG_PTR2_FREE; \
1710 X_UNLOCK (wrklock); \ 1716 X_UNLOCK (EIO_POOL->wrklock); \
1711 req->ptr2 = malloc (len); \ 1717 req->ptr2 = malloc (len); \
1712 if (!req->ptr2) \ 1718 if (!req->ptr2) \
1713 { \ 1719 { \
1714 errno = ENOMEM; \ 1720 errno = ENOMEM; \
1715 req->result = -1; \ 1721 req->result = -1; \
1716 break; \ 1722 break; \
1717 } \ 1723 } \
1718 } 1724 }
1719 1725
1720static void ecb_noinline ecb_cold
1721etp_proc_init (void)
1722{
1723#if HAVE_PRCTL_SET_NAME
1724 /* provide a more sensible "thread name" */
1725 char name[16 + 1];
1726 const int namelen = sizeof (name) - 1;
1727 int len;
1728
1729 prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0);
1730 name [namelen] = 0;
1731 len = strlen (name);
1732 strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio");
1733 prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0);
1734#endif
1735}
1736
1737/* TODO: move somehow to etp.c */
1738X_THREAD_PROC (etp_proc)
1739{
1740 ETP_REQ *req;
1741 struct timespec ts;
1742 etp_worker *self = (etp_worker *)thr_arg;
1743
1744 etp_proc_init ();
1745
1746 /* try to distribute timeouts somewhat evenly */
1747 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1748
1749 for (;;)
1750 {
1751 ts.tv_sec = 0;
1752
1753 X_LOCK (reqlock);
1754
1755 for (;;)
1756 {
1757 req = reqq_shift (&req_queue);
1758
1759 if (req)
1760 break;
1761
1762 if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */
1763 {
1764 X_UNLOCK (reqlock);
1765 X_LOCK (wrklock);
1766 --started;
1767 X_UNLOCK (wrklock);
1768 goto quit;
1769 }
1770
1771 ++idle;
1772
1773 if (idle <= max_idle)
1774 /* we are allowed to idle, so do so without any timeout */
1775 X_COND_WAIT (reqwait, reqlock);
1776 else
1777 {
1778 /* initialise timeout once */
1779 if (!ts.tv_sec)
1780 ts.tv_sec = time (0) + idle_timeout;
1781
1782 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
1783 ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */
1784 }
1785
1786 --idle;
1787 }
1788
1789 --nready;
1790
1791 X_UNLOCK (reqlock);
1792
1793 if (req->type == ETP_TYPE_QUIT)
1794 goto quit;
1795
1796 ETP_EXECUTE (self, req);
1797
1798 X_LOCK (reslock);
1799
1800 ++npending;
1801
1802 if (!reqq_push (&res_queue, req) && want_poll_cb)
1803 want_poll_cb ();
1804
1805 etp_worker_clear (self);
1806
1807 X_UNLOCK (reslock);
1808 }
1809
1810quit:
1811 free (req);
1812
1813 X_LOCK (wrklock);
1814 etp_worker_free (self);
1815 X_UNLOCK (wrklock);
1816
1817 return 0;
1818}
1819
1820/*****************************************************************************/ 1726/*****************************************************************************/
1821 1727
1822int ecb_cold 1728int ecb_cold
1823eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1729eio_init (void (*want_poll)(void), void (*done_poll)(void))
1824{ 1730{
1825 return etp_init (want_poll, done_poll); 1731 eio_want_poll_cb = want_poll;
1732 eio_done_poll_cb = done_poll;
1733
1734 return etp_init (EIO_POOL, 0, 0, 0);
1826} 1735}
1827 1736
1828ecb_inline void 1737ecb_inline void
1829eio_api_destroy (eio_req *req) 1738eio_api_destroy (eio_req *req)
1830{ 1739{
1903 ? pread (req->int1, req->ptr2, req->size, req->offs) 1812 ? pread (req->int1, req->ptr2, req->size, req->offs)
1904 : read (req->int1, req->ptr2, req->size); break; 1813 : read (req->int1, req->ptr2, req->size); break;
1905 case EIO_WRITE: req->result = req->offs >= 0 1814 case EIO_WRITE: req->result = req->offs >= 0
1906 ? pwrite (req->int1, req->ptr2, req->size, req->offs) 1815 ? pwrite (req->int1, req->ptr2, req->size, req->offs)
1907 : write (req->int1, req->ptr2, req->size); break; 1816 : write (req->int1, req->ptr2, req->size); break;
1817
1818 case EIO_FCNTL: req->result = fcntl (req->int1, (int) req->int2, req->ptr2); break;
1819 case EIO_IOCTL: req->result = ioctl (req->int1, (unsigned long)req->int2, req->ptr2); break;
1908 1820
1909 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; 1821 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
1910 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; 1822 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break;
1911 1823
1912#if HAVE_AT 1824#if HAVE_AT
2173eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) 2085eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2174{ 2086{
2175 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; 2087 REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;
2176} 2088}
2177 2089
2090eio_req *eio_fcntl (int fd, int cmd, void *arg, int pri, eio_cb cb, void *data)
2091{
2092 REQ (EIO_IOCTL); req->int1 = fd; req->int2 = cmd; req->ptr2 = arg; SEND;
2093}
2094
2095eio_req *eio_ioctl (int fd, unsigned long request, void *buf, int pri, eio_cb cb, void *data)
2096{
2097 REQ (EIO_IOCTL); req->int1 = fd; req->int2 = request; req->ptr2 = buf; SEND;
2098}
2099
2178eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) 2100eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data)
2179{ 2101{
2180 REQ (EIO_FSTAT); req->int1 = fd; SEND; 2102 REQ (EIO_FSTAT); req->int1 = fd; SEND;
2181} 2103}
2182 2104
2368void 2290void
2369eio_grp_add (eio_req *grp, eio_req *req) 2291eio_grp_add (eio_req *grp, eio_req *req)
2370{ 2292{
2371 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); 2293 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
2372 2294
2373 grp->flags |= EIO_FLAG_GROUPADD; 2295 grp->flags |= ETP_FLAG_GROUPADD;
2374 2296
2375 ++grp->size; 2297 ++grp->size;
2376 req->grp = grp; 2298 req->grp = grp;
2377 2299
2378 req->grp_prev = 0; 2300 req->grp_prev = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines