From 792e4da4c23f30c582be90e4b04fabf39ee9eb9d Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 29 Jan 2019 08:38:12 +0000 Subject: [PATCH] changed directory error number setting to mio_seterrxxx() calls removed mio->renew_watch and moved it to each device by using the MIO_DEV_RENEW_REQUIRED bit --- mio/Makefile.in | 9 ++-- mio/configure | 14 +----- mio/lib/Makefile.in | 1 - mio/lib/main.c | 11 ++--- mio/lib/mio-pro.c | 83 ++++++++++++++++---------------- mio/lib/mio.c | 93 ++++++++++++++---------------------- mio/lib/mio.h | 8 ++-- mio/lib/sys-log.c | 4 -- mio/lib/sys-mux.c | 112 +++++++++++++++----------------------------- 9 files changed, 128 insertions(+), 207 deletions(-) diff --git a/mio/Makefile.in b/mio/Makefile.in index 2fe5d77..e8308fe 100644 --- a/mio/Makefile.in +++ b/mio/Makefile.in @@ -347,7 +347,6 @@ pdfdir = @pdfdir@ prefix = @prefix@ program_transform_name = @program_transform_name@ psdir = @psdir@ -runstatedir = @runstatedir@ sbindir = @sbindir@ sharedstatedir = @sharedstatedir@ srcdir = @srcdir@ @@ -578,7 +577,7 @@ distdir: $(DISTFILES) ! -type d ! -perm -444 -exec $(install_sh) -c -m a+r {} {} \; \ || chmod -R a+r "$(distdir)" dist-gzip: distdir - tardir=$(distdir) && $(am__tar) | GZIP=$(GZIP_ENV) gzip -c >$(distdir).tar.gz + tardir=$(distdir) && $(am__tar) | eval GZIP= gzip $(GZIP_ENV) -c >$(distdir).tar.gz $(am__post_remove_distdir) dist-bzip2: distdir @@ -604,7 +603,7 @@ dist-shar: distdir @echo WARNING: "Support for shar distribution archives is" \ "deprecated." >&2 @echo WARNING: "It will be removed altogether in Automake 2.0" >&2 - shar $(distdir) | GZIP=$(GZIP_ENV) gzip -c >$(distdir).shar.gz + shar $(distdir) | eval GZIP= gzip $(GZIP_ENV) -c >$(distdir).shar.gz $(am__post_remove_distdir) dist-zip: distdir @@ -622,7 +621,7 @@ dist dist-all: distcheck: dist case '$(DIST_ARCHIVES)' in \ *.tar.gz*) \ - GZIP=$(GZIP_ENV) gzip -dc $(distdir).tar.gz | $(am__untar) ;;\ + eval GZIP= gzip $(GZIP_ENV) -dc $(distdir).tar.gz | $(am__untar) ;;\ *.tar.bz2*) \ bzip2 -dc $(distdir).tar.bz2 | $(am__untar) ;;\ *.tar.lz*) \ @@ -632,7 +631,7 @@ distcheck: dist *.tar.Z*) \ uncompress -c $(distdir).tar.Z | $(am__untar) ;;\ *.shar.gz*) \ - GZIP=$(GZIP_ENV) gzip -dc $(distdir).shar.gz | unshar ;;\ + eval GZIP= gzip $(GZIP_ENV) -dc $(distdir).shar.gz | unshar ;;\ *.zip*) \ unzip $(distdir).zip ;;\ esac diff --git a/mio/configure b/mio/configure index c6a5b5a..37efc64 100755 --- a/mio/configure +++ b/mio/configure @@ -771,7 +771,6 @@ infodir docdir oldincludedir includedir -runstatedir localstatedir sharedstatedir sysconfdir @@ -862,7 +861,6 @@ datadir='${datarootdir}' sysconfdir='${prefix}/etc' sharedstatedir='${prefix}/com' localstatedir='${prefix}/var' -runstatedir='${localstatedir}/run' includedir='${prefix}/include' oldincludedir='/usr/include' docdir='${datarootdir}/doc/${PACKAGE_TARNAME}' @@ -1115,15 +1113,6 @@ do | -silent | --silent | --silen | --sile | --sil) silent=yes ;; - -runstatedir | --runstatedir | --runstatedi | --runstated \ - | --runstate | --runstat | --runsta | --runst | --runs \ - | --run | --ru | --r) - ac_prev=runstatedir ;; - -runstatedir=* | --runstatedir=* | --runstatedi=* | --runstated=* \ - | --runstate=* | --runstat=* | --runsta=* | --runst=* | --runs=* \ - | --run=* | --ru=* | --r=*) - runstatedir=$ac_optarg ;; - -sbindir | --sbindir | --sbindi | --sbind | --sbin | --sbi | --sb) ac_prev=sbindir ;; -sbindir=* | --sbindir=* | --sbindi=* | --sbind=* | --sbin=* \ @@ -1261,7 +1250,7 @@ fi for ac_var in exec_prefix prefix bindir sbindir libexecdir datarootdir \ datadir sysconfdir sharedstatedir localstatedir includedir \ oldincludedir docdir infodir htmldir dvidir pdfdir psdir \ - libdir localedir mandir runstatedir + libdir localedir mandir do eval ac_val=\$$ac_var # Remove trailing slashes. @@ -1414,7 +1403,6 @@ Fine tuning of the installation directories: --sysconfdir=DIR read-only single-machine data [PREFIX/etc] --sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com] --localstatedir=DIR modifiable single-machine data [PREFIX/var] - --runstatedir=DIR modifiable per-process data [LOCALSTATEDIR/run] --libdir=DIR object code libraries [EPREFIX/lib] --includedir=DIR C header files [PREFIX/include] --oldincludedir=DIR C header files for non-gcc [/usr/include] diff --git a/mio/lib/Makefile.in b/mio/lib/Makefile.in index 41988ee..68eb2a3 100644 --- a/mio/lib/Makefile.in +++ b/mio/lib/Makefile.in @@ -365,7 +365,6 @@ pdfdir = @pdfdir@ prefix = @prefix@ program_transform_name = @program_transform_name@ psdir = @psdir@ -runstatedir = @runstatedir@ sbindir = @sbindir@ sharedstatedir = @sharedstatedir@ srcdir = @srcdir@ diff --git a/mio/lib/main.c b/mio/lib/main.c index a703313..50bb2f7 100644 --- a/mio/lib/main.c +++ b/mio/lib/main.c @@ -249,19 +249,18 @@ static int x = 0; mio_ntime_t tmout; static char a ='A'; - char* xxx = malloc (1000000); - memset (xxx, a++ ,1000000); + static char xxx[1000000]; + memset (xxx, a++ , MIO_SIZEOF(xxx)); - MIO_INFO2 (tcp->mio, "TCP_SCK_ON_READ(%d) >>> REQUESTING to write data of %d bytes\n", (int)tcp->sck, 1000000); + MIO_INFO2 (tcp->mio, "TCP_SCK_ON_READ(%d) >>> REQUESTING to write data of %d bytes\n", (int)tcp->sck, MIO_SIZEOF(xxx)); //return mio_dev_sck_write (tcp, "HELLO", 5, MIO_NULL); MIO_INIT_NTIME (&tmout, 5, 0); - n = mio_dev_sck_timedwrite(tcp, xxx, 1000000, &tmout, MIO_NULL, MIO_NULL); - free (xxx); + n = mio_dev_sck_timedwrite(tcp, xxx, MIO_SIZEOF(xxx), &tmout, MIO_NULL, MIO_NULL); if (n <= -1) return -1; } - MIO_INFO1 (tcp->mio, "TCP_SCK_ON_READ(%d) - DISABLING READ\n", (int)tcp->sck); + MIO_INFO1 (tcp->mio, "TCP_SCK_ON_READ(%d) - REQUESTING TO STOP READ\n", (int)tcp->sck); mio_dev_sck_read (tcp, 0); #if 0 diff --git a/mio/lib/mio-pro.c b/mio/lib/mio-pro.c index 13adf33..123c5f6 100644 --- a/mio/lib/mio-pro.c +++ b/mio/lib/mio-pro.c @@ -59,8 +59,8 @@ typedef struct param_t param_t; static void free_param (mio_t* mio, param_t* param) { if (param->argv && param->argv != param->fixed_argv) - MIO_MMGR_FREE (mio->mmgr, param->argv); - if (param->mcmd) MIO_MMGR_FREE (mio->mmgr, param->mcmd); + mio_freemem (mio, param->argv); + if (param->mcmd) mio_freemem (mio, param->mcmd); MIO_MEMSET (param, 0, MIO_SIZEOF(*param)); } @@ -94,7 +94,7 @@ static int make_param (mio_t* mio, const mio_bch_t* cmd, int flags, param_t* par if (fcnt <= 0) { /* no field or an error */ - mio->errnum = MIO_EINVAL; + mio_seterrnum (mio, MIO_EINVAL); goto oops; } @@ -104,12 +104,8 @@ static int make_param (mio_t* mio, const mio_bch_t* cmd, int flags, param_t* par } else { - param->argv = MIO_MMGR_ALLOC (mio->mmgr, (fcnt + 1) * MIO_SIZEOF(argv[0])); - if (param->argv == MIO_NULL) - { - mio->errnum = MIO_ESYSMEM; - goto oops; - } + param->argv = mio_allocmem(mio, (fcnt + 1) * MIO_SIZEOF(argv[0])); + if (!param->argv) goto oops; } mcmdptr = mcmd; @@ -126,7 +122,7 @@ static int make_param (mio_t* mio, const mio_bch_t* cmd, int flags, param_t* par return 0; oops: - if (mcmd && mcmd != cmd) MIO_MMGR_FREE (mio->mmgr, mcmd); + if (mcmd && mcmd != cmd) mio_freemem (mio, mcmd); return -1; } @@ -223,6 +219,7 @@ static pid_t standard_fork_and_exec (mio_t* mio, int pfds[], int flags, param_t* static int dev_pro_make_master (mio_dev_t* dev, void* ctx) { + mio_t* mio = dev->mio; mio_dev_pro_t* rdev = (mio_dev_pro_t*)dev; mio_dev_pro_make_t* info = (mio_dev_pro_make_t*)ctx; mio_syshnd_t pfds[6]; @@ -234,7 +231,7 @@ static int dev_pro_make_master (mio_dev_t* dev, void* ctx) { if (pipe(&pfds[0]) == -1) { - mio_seterrwithsyserr (dev->mio, 0, errno); + mio_seterrwithsyserr (mio, 0, errno); goto oops; } minidx = 0; maxidx = 1; @@ -244,7 +241,7 @@ static int dev_pro_make_master (mio_dev_t* dev, void* ctx) { if (pipe(&pfds[2]) == -1) { - mio_seterrwithsyserr (dev->mio, 0, errno); + mio_seterrwithsyserr (mio, 0, errno); goto oops; } if (minidx == -1) minidx = 2; @@ -255,7 +252,7 @@ static int dev_pro_make_master (mio_dev_t* dev, void* ctx) { if (pipe(&pfds[4]) == -1) { - mio_seterrwithsyserr (dev->mio, 0, errno); + mio_seterrwithsyserr (mio, 0, errno); goto oops; } if (minidx == -1) minidx = 4; @@ -264,21 +261,21 @@ static int dev_pro_make_master (mio_dev_t* dev, void* ctx) if (maxidx == -1) { - mio_seterrnum (dev->mio, MIO_EINVAL); + mio_seterrnum (mio, MIO_EINVAL); goto oops; } - if (make_param (rdev->mio, info->cmd, info->flags, ¶m) <= -1) goto oops; + if (make_param(mio, info->cmd, info->flags, ¶m) <= -1) goto oops; /* TODO: more advanced fork and exec .. */ - pid = standard_fork_and_exec (rdev->mio, pfds, info->flags, ¶m); + pid = standard_fork_and_exec(mio, pfds, info->flags, ¶m); if (pid <= -1) { - free_param (rdev->mio, ¶m); + free_param (mio, ¶m); goto oops; } - free_param (rdev->mio, ¶m); + free_param (mio, ¶m); rdev->child_pid = pid; /* this is the parent process */ @@ -293,7 +290,7 @@ static int dev_pro_make_master (mio_dev_t* dev, void* ctx) close (pfds[0]); pfds[0] = MIO_SYSHND_INVALID; - if (mio_makesyshndasync (dev->mio, pfds[1]) <= -1) goto oops; + if (mio_makesyshndasync(mio, pfds[1]) <= -1) goto oops; } if (info->flags & MIO_DEV_PRO_READOUT) @@ -307,7 +304,7 @@ static int dev_pro_make_master (mio_dev_t* dev, void* ctx) close (pfds[3]); pfds[3] = MIO_SYSHND_INVALID; - if (mio_makesyshndasync (dev->mio, pfds[2]) <= -1) goto oops; + if (mio_makesyshndasync(mio, pfds[2]) <= -1) goto oops; } if (info->flags & MIO_DEV_PRO_READERR) @@ -321,7 +318,7 @@ static int dev_pro_make_master (mio_dev_t* dev, void* ctx) close (pfds[5]); pfds[5] = MIO_SYSHND_INVALID; - if (mio_makesyshndasync (dev->mio, pfds[4]) <= -1) goto oops; + if (mio_makesyshndasync(mio, pfds[4]) <= -1) goto oops; } if (pfds[1] != MIO_SYSHND_INVALID) @@ -334,7 +331,7 @@ static int dev_pro_make_master (mio_dev_t* dev, void* ctx) si.dev_capa = MIO_DEV_CAPA_OUT | MIO_DEV_CAPA_OUT_QUEUED | MIO_DEV_CAPA_STREAM; si.id = MIO_DEV_PRO_IN; - rdev->slave[MIO_DEV_PRO_IN] = make_slave (dev->mio, &si); + rdev->slave[MIO_DEV_PRO_IN] = make_slave(mio, &si); if (!rdev->slave[MIO_DEV_PRO_IN]) goto oops; pfds[1] = MIO_SYSHND_INVALID; @@ -351,7 +348,7 @@ static int dev_pro_make_master (mio_dev_t* dev, void* ctx) si.dev_capa = MIO_DEV_CAPA_IN | MIO_DEV_CAPA_STREAM; si.id = MIO_DEV_PRO_OUT; - rdev->slave[MIO_DEV_PRO_OUT] = make_slave (dev->mio, &si); + rdev->slave[MIO_DEV_PRO_OUT] = make_slave(mio, &si); if (!rdev->slave[MIO_DEV_PRO_OUT]) goto oops; pfds[2] = MIO_SYSHND_INVALID; @@ -368,7 +365,7 @@ static int dev_pro_make_master (mio_dev_t* dev, void* ctx) si.dev_capa = MIO_DEV_CAPA_IN | MIO_DEV_CAPA_STREAM; si.id = MIO_DEV_PRO_ERR; - rdev->slave[MIO_DEV_PRO_ERR] = make_slave (dev->mio, &si); + rdev->slave[MIO_DEV_PRO_ERR] = make_slave(mio, &si); if (!rdev->slave[MIO_DEV_PRO_ERR]) goto oops; pfds[4] = MIO_SYSHND_INVALID; @@ -395,8 +392,8 @@ oops: if (rdev->mcmd) { - MIO_MMGR_FREE (rdev->mio->mmgr, rdev->mcmd); - free_param (rdev->mio, ¶m); + mio_freemem (mio, rdev->mcmd); + free_param (mio, ¶m); } for (i = MIO_COUNTOF(rdev->slave); i > 0; ) @@ -404,7 +401,7 @@ oops: i--; if (rdev->slave[i]) { - mio_killdev (rdev->mio, (mio_dev_t*)rdev->slave[i]); + mio_killdev (mio, (mio_dev_t*)rdev->slave[i]); rdev->slave[i] = MIO_NULL; } } @@ -429,6 +426,7 @@ static int dev_pro_make_slave (mio_dev_t* dev, void* ctx) static int dev_pro_kill_master (mio_dev_t* dev, int force) { + mio_t* mio = dev->mio; mio_dev_pro_t* rdev = (mio_dev_pro_t*)dev; int i, status; pid_t wpid; @@ -447,7 +445,7 @@ static int dev_pro_kill_master (mio_dev_t* dev, int force) * self-initiated termination or master-driven termination */ rdev->slave[i] = MIO_NULL; - mio_killdev (rdev->mio, (mio_dev_t*)sdev); + mio_killdev (mio, (mio_dev_t*)sdev); } } } @@ -474,7 +472,7 @@ static int dev_pro_kill_master (mio_dev_t* dev, int force) else { /* child process is still alive */ - rdev->mio->errnum = MIO_EAGAIN; + mio_seterrnum (mio, MIO_EAGAIN); return -1; /* call me again */ } } @@ -486,7 +484,7 @@ static int dev_pro_kill_master (mio_dev_t* dev, int force) */ } -printf (">>>>>>>>>>>>>>>>>>> REAPED CHILD %d\n", (int)rdev->child_pid); + MIO_DEBUG1 (mio, ">>>>>>>>>>>>>>>>>>> REAPED CHILD %d\n", (int)rdev->child_pid); rdev->child_pid = -1; } @@ -496,6 +494,7 @@ printf (">>>>>>>>>>>>>>>>>>> REAPED CHILD %d\n", (int)rdev->child_pid); static int dev_pro_kill_slave (mio_dev_t* dev, int force) { + mio_t* mio = dev->mio; mio_dev_pro_slave_t* rdev = (mio_dev_pro_slave_t*)dev; if (rdev->master) @@ -508,7 +507,7 @@ static int dev_pro_kill_slave (mio_dev_t* dev, int force) /* indicate EOF */ if (master->on_close) master->on_close (master, rdev->id); - MIO_ASSERT (dev->mio, master->slave_count > 0); + MIO_ASSERT (mio, master->slave_count > 0); master->slave_count--; if (master->slave[rdev->id]) @@ -517,7 +516,7 @@ static int dev_pro_kill_slave (mio_dev_t* dev, int force) * if this is the last slave, kill the master also */ if (master->slave_count <= 0) { - mio_killdev (rdev->mio, (mio_dev_t*)master); + mio_killdev (mio, (mio_dev_t*)master); /* the master pointer is not valid from this point onwards * as the actual master device object is freed in mio_killdev() */ } @@ -589,6 +588,7 @@ static mio_syshnd_t dev_pro_getsyshnd_slave (mio_dev_t* dev) static int dev_pro_ioctl (mio_dev_t* dev, int cmd, void* arg) { + mio_t* mio = dev->mio; mio_dev_pro_t* rdev = (mio_dev_pro_t*)dev; switch (cmd) @@ -599,7 +599,7 @@ static int dev_pro_ioctl (mio_dev_t* dev, int cmd, void* arg) if (sid < MIO_DEV_PRO_IN || sid > MIO_DEV_PRO_ERR) { - rdev->mio->errnum = MIO_EINVAL; + mio_seterrnum (mio, MIO_EINVAL); return -1; } @@ -608,7 +608,7 @@ static int dev_pro_ioctl (mio_dev_t* dev, int cmd, void* arg) /* unlike dev_pro_kill_master(), i don't nullify rdev->slave[sid]. * so i treat the closing ioctl as if it's a kill request * initiated by the slave device itself. */ - mio_killdev (rdev->mio, (mio_dev_t*)rdev->slave[sid]); + mio_killdev (mio, (mio_dev_t*)rdev->slave[sid]); } return 0; } @@ -618,7 +618,7 @@ static int dev_pro_ioctl (mio_dev_t* dev, int cmd, void* arg) { if (kill (rdev->child_pid, SIGKILL) == -1) { - mio_seterrwithsyserr (rdev->mio, 0, errno); + mio_seterrwithsyserr (mio, 0, errno); return -1; } } @@ -626,7 +626,7 @@ static int dev_pro_ioctl (mio_dev_t* dev, int cmd, void* arg) return 0; default: - dev->mio->errnum = MIO_EINVAL; + mio_seterrnum (mio, MIO_EINVAL); return -1; } } @@ -658,21 +658,21 @@ static mio_dev_mth_t dev_pro_methods_slave = static int pro_ready (mio_dev_t* dev, int events) { /* virtual device. no I/O */ - dev->mio->errnum = MIO_EINTERN; + mio_seterrnum (dev->mio, MIO_EINTERN); return -1; } static int pro_on_read (mio_dev_t* dev, const void* data, mio_iolen_t len, const mio_devaddr_t* srcaddr) { /* virtual device. no I/O */ - dev->mio->errnum = MIO_EINTERN; + mio_seterrnum (dev->mio, MIO_EINTERN); return -1; } static int pro_on_write (mio_dev_t* dev, mio_iolen_t wrlen, void* wrctx, const mio_devaddr_t* dstaddr) { /* virtual device. no I/O */ - dev->mio->errnum = MIO_EINTERN; + mio_seterrnum (dev->mio, MIO_EINTERN); return -1; } @@ -687,11 +687,12 @@ static mio_dev_evcb_t dev_pro_event_callbacks = static int pro_ready_slave (mio_dev_t* dev, int events) { + mio_t* mio = dev->mio; mio_dev_pro_t* pro = (mio_dev_pro_t*)dev; if (events & MIO_DEV_EVENT_ERR) { - pro->mio->errnum = MIO_EDEVERR; + mio_seterrnum (mio, MIO_EDEVERR); return -1; } @@ -703,7 +704,7 @@ static int pro_ready_slave (mio_dev_t* dev, int events) return 1; } - pro->mio->errnum = MIO_EDEVHUP; + mio_seterrnum (mio, MIO_EDEVHUP); return -1; } diff --git a/mio/lib/mio.c b/mio/lib/mio.c index 7dcf771..5621ca7 100644 --- a/mio/lib/mio.c +++ b/mio/lib/mio.c @@ -276,7 +276,7 @@ static MIO_INLINE void handle_event (mio_t* mio, mio_dev_t* dev, int events, int { MIO_ASSERT (mio, mio == dev->mio); - mio->renew_watch = 0; + dev->dev_capa &= ~MIO_DEV_RENEW_REQUIRED; MIO_ASSERT (mio, mio == dev->mio); @@ -353,7 +353,7 @@ static MIO_INLINE void handle_event (mio_t* mio, mio_dev_t* dev, int events, int /* it was a zero-length write request. * for a stream, it is to close the output. */ dev->dev_capa |= MIO_DEV_CAPA_OUT_CLOSED; - mio->renew_watch = 1; + dev->dev_capa |= MIO_DEV_RENEW_REQUIRED; out_closed = 1; } @@ -396,7 +396,7 @@ static MIO_INLINE void handle_event (mio_t* mio, mio_dev_t* dev, int events, int } else { - mio->renew_watch = 1; + dev->dev_capa |= MIO_DEV_RENEW_REQUIRED; } } } @@ -447,14 +447,12 @@ static MIO_INLINE void handle_event (mio_t* mio, mio_dev_t* dev, int events, int } else /*if (x >= 1) */ { - if (len <= 0 && (dev->dev_capa & MIO_DEV_CAPA_STREAM)) { /* EOF received. for a stream device, a zero-length * read is interpreted as EOF. */ dev->dev_capa |= MIO_DEV_CAPA_IN_CLOSED; - mio->renew_watch = 1; - + dev->dev_capa |= MIO_DEV_RENEW_REQUIRED; /* call the on_read callback to report EOF */ if (dev->dev_evcb->on_read(dev, mio->bigbuf, len, &srcaddr) <= -1 || @@ -503,7 +501,7 @@ static MIO_INLINE void handle_event (mio_t* mio, mio_dev_t* dev, int events, int * EPOLLIN or EPOLLOUT check because EPOLLERR or EPOLLHUP * can be set together with EPOLLIN or EPOLLOUT. */ dev->dev_capa |= MIO_DEV_CAPA_IN_CLOSED | MIO_DEV_CAPA_OUT_CLOSED; - mio->renew_watch = 1; + dev->dev_capa |= MIO_DEV_RENEW_REQUIRED; } else if (dev && rdhup) { @@ -515,7 +513,7 @@ static MIO_INLINE void handle_event (mio_t* mio, mio_dev_t* dev, int events, int else { dev->dev_capa |= MIO_DEV_CAPA_IN_CLOSED | MIO_DEV_CAPA_OUT_CLOSED; - mio->renew_watch = 1; + dev->dev_capa |= MIO_DEV_RENEW_REQUIRED; } } @@ -528,21 +526,17 @@ static MIO_INLINE void handle_event (mio_t* mio, mio_dev_t* dev, int events, int } skip_evcb: - if (dev && mio->renew_watch && mio_dev_watch(dev, MIO_DEV_WATCH_RENEW, 0) <= -1) + if (dev && (dev->dev_capa & MIO_DEV_RENEW_REQUIRED) && mio_dev_watch(dev, MIO_DEV_WATCH_RENEW, 0) <= -1) { mio_dev_halt (dev); dev = MIO_NULL; } } -static MIO_INLINE int __exec (mio_t* mio) +int mio_exec (mio_t* mio) { - mio_ntime_t tmout; - mio_sys_mux_t* mux; int ret = 0; - /*if (!mio->actdev.head) return 0;*/ - /* execute callbacks for completed write operations */ while (!MIO_CWQ_ISEMPTY(&mio->cwq)) { @@ -572,17 +566,23 @@ static MIO_INLINE int __exec (mio_t* mio) * multiplexer. the scheduled jobs can safely destroy the devices */ mio_firetmrjobs (mio, MIO_NULL, MIO_NULL); - if (mio_gettmrtmout(mio, MIO_NULL, &tmout) <= -1) + if (mio->actdev.head) { - /* defaults to 1 second if timeout can't be acquired */ - tmout.sec = 1; /* TODO: make the default timeout configurable */ - tmout.nsec = 0; - } + /* wait on the multiplexer only if there is at least 1 active device */ + mio_ntime_t tmout; - if (mio_sys_waitmux(mio, &tmout, handle_event) <= -1) - { - MIO_DEBUG0 (mio, "WARNING - Failed to wait on mutiplexer\n"); - ret = -1; + if (mio_gettmrtmout(mio, MIO_NULL, &tmout) <= -1) + { + /* defaults to 1 second if timeout can't be acquired */ + tmout.sec = 1; /* TODO: make the default timeout configurable */ + tmout.nsec = 0; + } + + if (mio_sys_waitmux(mio, &tmout, handle_event) <= -1) + { + MIO_DEBUG0 (mio, "WARNING - Failed to wait on mutiplexer\n"); + ret = -1; + } } /* kill all halted devices */ @@ -596,17 +596,6 @@ static MIO_INLINE int __exec (mio_t* mio) return ret; } -int mio_exec (mio_t* mio) -{ - int n; - - mio->in_exec = 1; - n = __exec (mio); - mio->in_exec = 0; - - return n; -} - void mio_stop (mio_t* mio, mio_stopreq_t stopreq) { mio->stopreq = stopreq; @@ -617,7 +606,6 @@ int mio_loop (mio_t* mio) if (!mio->actdev.head) return 0; mio->stopreq = MIO_STOPREQ_NONE; - mio->renew_watch = 0; if (mio_prologue(mio) <= -1) return -1; @@ -637,7 +625,7 @@ mio_dev_t* mio_makedev (mio_t* mio, mio_oow_t dev_size, mio_dev_mth_t* dev_mth, if (dev_size < MIO_SIZEOF(mio_dev_t)) { - mio->errnum = MIO_EINVAL; + mio_seterrnum (mio, MIO_EINVAL); return MIO_NULL; } @@ -675,15 +663,7 @@ mio_dev_t* mio_makedev (mio_t* mio, mio_oow_t dev_size, mio_dev_mth_t* dev_mth, if (!(dev->dev_capa & MIO_DEV_CAPA_IN)) dev->dev_capa |= MIO_DEV_CAPA_IN_CLOSED; if (!(dev->dev_capa & MIO_DEV_CAPA_OUT)) dev->dev_capa |= MIO_DEV_CAPA_OUT_CLOSED; -#if defined(_WIN32) - if (CreateIoCompletionPort((HANDLE)dev->dev_mth->getsyshnd(dev), mio->iocp, MIO_IOCP_KEY, 0) == NULL) - { - /* TODO: set errnum from GetLastError()... */ - goto oops_after_make; - } -#else if (mio_dev_watch(dev, MIO_DEV_WATCH_START, 0) <= -1) goto oops_after_make; -#endif /* and place the new device object at the back of the active device list */ APPEND_DEVICE_TO_LIST (&mio->actdev, dev); @@ -926,8 +906,7 @@ int mio_dev_watch (mio_dev_t* dev, mio_dev_watch_cmd_t cmd, int events) switch (cmd) { case MIO_DEV_WATCH_START: - /* upon start, only input watching is requested */ - events = MIO_DEV_EVENT_IN; + events = 0; /* MIO_DEV_EVENT_IN if you want input watching to be enabled upon device creation */ mux_cmd = MIO_SYS_MUX_CMD_INSERT; break; @@ -949,7 +928,7 @@ int mio_dev_watch (mio_dev_t* dev, mio_dev_watch_cmd_t cmd, int events) break; default: - dev->mio->errnum = MIO_EINVAL; + mio_seterrnum (dev->mio, MIO_EINVAL); return -1; } @@ -980,7 +959,7 @@ int mio_dev_watch (mio_dev_t* dev, mio_dev_watch_cmd_t cmd, int events) } else { - if (mio_sys_ctrlmux(dev->mio, mux_cmd, dev, dev_capa) <= -1) return -1; + if (mio_sys_ctrlmux(mio, mux_cmd, dev, dev_capa) <= -1) return -1; } dev->dev_capa = dev_capa; @@ -994,7 +973,7 @@ static void on_read_timeout (mio_t* mio, const mio_ntime_t* now, mio_tmrjob_t* j dev = (mio_dev_t*)job->ctx; - dev->mio->errnum = MIO_ETMOUT; + mio_seterrnum (mio, MIO_ETMOUT); x = dev->dev_evcb->on_read(dev, MIO_NULL, -1, MIO_NULL); MIO_ASSERT (mio, dev->rtmridx == MIO_TMRIDX_INVALID); @@ -1015,15 +994,15 @@ static int __dev_read (mio_dev_t* dev, int enabled, const mio_ntime_t* tmout, vo if (enabled) { dev->dev_capa &= ~MIO_DEV_CAPA_IN_DISABLED; - if (/*!mio->in_exec && */!(dev->dev_capa & MIO_DEV_CAPA_IN_WATCHED)) goto renew_watch_now; + if (!(dev->dev_capa & MIO_DEV_CAPA_IN_WATCHED)) goto renew_watch_now; } else { dev->dev_capa |= MIO_DEV_CAPA_IN_DISABLED; - if (/*!mio->in_exec && */(dev->dev_capa & MIO_DEV_CAPA_IN_WATCHED)) goto renew_watch_now; + if ((dev->dev_capa & MIO_DEV_CAPA_IN_WATCHED)) goto renew_watch_now; } - mio->renew_watch = 1; + dev->dev_capa |= MIO_DEV_RENEW_REQUIRED; goto update_timer; renew_watch_now: @@ -1079,7 +1058,7 @@ static void on_write_timeout (mio_t* mio, const mio_ntime_t* now, mio_tmrjob_t* q = (mio_wq_t*)job->ctx; dev = q->dev; - dev->mio->errnum = MIO_ETMOUT; + mio_seterrnum (mio, MIO_ETMOUT); x = dev->dev_evcb->on_write(dev, -1, q->ctx, &q->dstaddr); MIO_ASSERT (mio, q->tmridx == MIO_TMRIDX_INVALID); @@ -1176,7 +1155,7 @@ enqueue_data: if (!(dev->dev_capa & MIO_DEV_CAPA_OUT_QUEUED)) { /* writing queuing is not requested. so return failure */ - dev->mio->errnum = MIO_ENOCAPA; + mio_seterrbfmt (mio, MIO_ENOCAPA, "device incapable of queuing"); return -1; } @@ -1224,7 +1203,7 @@ enqueue_data: } MIO_WQ_ENQ (&dev->wq, q); - if (!mio->in_exec && !(dev->dev_capa & MIO_DEV_CAPA_OUT_WATCHED)) + if (!(dev->dev_capa & MIO_DEV_CAPA_OUT_WATCHED)) { /* if output is not being watched, arrange to do so */ if (mio_dev_watch(dev, MIO_DEV_WATCH_RENEW, 0) <= -1) @@ -1234,10 +1213,6 @@ enqueue_data: return -1; } } - else - { - mio->renew_watch = 1; - } return 0; /* request pused to a write queue. */ diff --git a/mio/lib/mio.h b/mio/lib/mio.h index 45bbe23..5e853cb 100644 --- a/mio/lib/mio.h +++ b/mio/lib/mio.h @@ -353,7 +353,10 @@ enum mio_dev_capa_t MIO_DEV_CAPA_ACTIVE = (1 << 15), MIO_DEV_CAPA_HALTED = (1 << 16), - MIO_DEV_CAPA_ZOMBIE = (1 << 17) + MIO_DEV_CAPA_ZOMBIE = (1 << 17), + + /* internal use only */ + MIO_DEV_RENEW_REQUIRED = (1 << 20) }; typedef enum mio_dev_capa_t mio_dev_capa_t; @@ -534,9 +537,6 @@ struct mio_t mio_uint8_t bigbuf[65535]; /* TODO: make this dynamic depending on devices added. device may indicate a buffer size required??? */ - unsigned int renew_watch: 1; - unsigned int in_exec: 1; - struct { mio_oow_t capa; diff --git a/mio/lib/sys-log.c b/mio/lib/sys-log.c index aef6940..f1f63c2 100644 --- a/mio/lib/sys-log.c +++ b/mio/lib/sys-log.c @@ -91,10 +91,6 @@ struct mio_sys_log_t } out; }; -typedef mio_sys_log_t xtn_t; -#define GET_XTN(mio) ((mio)->sys.log) - - enum logfd_flag_t { LOGFD_TTY = (1 << 0), diff --git a/mio/lib/sys-mux.c b/mio/lib/sys-mux.c index 6655ea6..0dc89be 100644 --- a/mio/lib/sys-mux.c +++ b/mio/lib/sys-mux.c @@ -63,6 +63,15 @@ struct mio_sys_mux_t } pd; /* poll data */ }; +#elif defined(USE_EPOLL) + +struct mio_sys_mux_t +{ + int hnd; + struct epoll_event revs[100]; /* TODO: is it a good size? */ +}; + +#endif int mio_sys_initmux (mio_t* mio) { @@ -71,6 +80,16 @@ int mio_sys_initmux (mio_t* mio) mux = (mio_sys_mux_t*)mio_callocmem(mio, MIO_SIZEOF(*mux)); if (!mux) return -1; +#if defined(USE_EPOLL) + mux->hnd = epoll_create(1000); /* TODO: choose proper initial size? */ + if (mux->hnd == -1) + { + mio_seterrwithsyserr (mio, 0, errno); + mio_freemem (mio, mux); + return -1; + } +#endif + mio->sys.mux = mux; return 0; } @@ -79,6 +98,9 @@ void mio_sys_finimux (mio_t* mio) { if (mio->sys.mux) { +#if defined(USE_EPOLL) + close (mio->sys.mux->hnd); +#endif mio_freemem (mio, mio->sys.mux); mio->sys.mux = MIO_NULL; } @@ -86,31 +108,28 @@ void mio_sys_finimux (mio_t* mio) int mio_sys_ctrlmux (mio_t* mio, mio_sys_mux_cmd_t cmd, mio_dev_t* dev, int dev_capa) { +#if defined(USE_POLL) mio_t* mio = dev->mio; mio_sys_mux_t* mux; mio_oow_t idx; mux = (mio_sys_mux_t*)mio->sys.mux; - if (hnd >= mux->map.capa) + if (dev->hnd >= mux->map.capa) { mio_oow_t new_capa; mio_oow_t* tmp; if (cmd != MIO_SYS_MUX_CMD_INSERT) { - mio->errnum = MIO_ENOENT; + mio_seterrnum (moo, MIO_ENOENT); return -1; } new_capa = MIO_ALIGN_POW2((hnd + 1), 256); - tmp = MIO_MMGR_REALLOC(mio->mmgr, mux->map.ptr, new_capa * MIO_SIZEOF(*tmp)); - if (!tmp) - { - mio->errnum = MIO_ESYSMEM; - return -1; - } + tmp = mio_reallocmem(mio, mux->map.ptr, new_capa * MIO_SIZEOF(*tmp)); + if (!tmp) return -1; for (idx = mux->map.capa; idx < new_capa; idx++) tmp[idx] = MUX_INDEX_INVALID; @@ -124,7 +143,7 @@ int mio_sys_ctrlmux (mio_t* mio, mio_sys_mux_cmd_t cmd, mio_dev_t* dev, int dev_ { if (cmd == MIO_SYS_MUX_CMD_INSERT) { - mio->errnum = MIO_EEXIST; + mio_seterrnum (mio, MIO_EEXIST); return -1; } } @@ -132,7 +151,7 @@ int mio_sys_ctrlmux (mio_t* mio, mio_sys_mux_cmd_t cmd, mio_dev_t* dev, int dev_ { if (cmd != MIO_SYS_MUX_CMD_INSERT) { - mio->errnum = MIO_ENOENT; + mio_seterrnum (mio, MIO_ENOENT); return -1; } } @@ -149,18 +168,13 @@ int mio_sys_ctrlmux (mio_t* mio, mio_sys_mux_cmd_t cmd, mio_dev_t* dev, int dev_ new_capa = MIO_ALIGN_POW2(mux->pd.size + 1, 256); - tmp1 = MIO_MMGR_REALLOC(mio->mmgr, mux->pd.pfd, new_capa * MIO_SIZEOF(*tmp1)); - if (!tmp1) - { - mio->errnum = MIO_ESYSMEM; - return -1; - } + tmp1 = mio_reallocmem(mio, mux->pd.pfd, new_capa * MIO_SIZEOF(*tmp1)); + if (!tmp1) return -1; - tmp2 = MIO_MMGR_REALLOC (mio->mmgr, mux->pd.dptr, new_capa * MIO_SIZEOF(*tmp2)); + tmp2 = mio_reallocmem(mio, mux->pd.dptr, new_capa * MIO_SIZEOF(*tmp2)); if (!tmp2) { - MIO_MMGR_FREE (mio->mmgr, tmp1); - mio->errnum = MIO_ESYSMEM; + mio_freemem (mio, tmp1); return -1; } @@ -217,50 +231,10 @@ int mio_sys_ctrlmux (mio_t* mio, mio_sys_mux_cmd_t cmd, mio_dev_t* dev, int dev_ return 0; default: - mio->errnum = MIO_EINVAL; + mio_seterrnum (mio, MIO_EINVAL); return -1; } -} - #elif defined(USE_EPOLL) - -struct mio_sys_mux_t -{ - int hnd; - struct epoll_event revs[100]; /* TODO: is it a good size? */ -}; - -int mio_sys_initmux (mio_t* mio) -{ - mio_sys_mux_t* mux; - - mux = (mio_sys_mux_t*)mio_callocmem(mio, MIO_SIZEOF(*mux)); - if (!mux) return -1; - - mux->hnd = epoll_create (1000); - if (mux->hnd == -1) - { - mio_seterrwithsyserr (mio, 0, errno); - mio_freemem (mio, mux); - return -1; - } - - mio->sys.mux = mux; - return 0; -} - -void mio_sys_finimux (mio_t* mio) -{ - if (mio->sys.mux) - { - close (mio->sys.mux->hnd); - mio_freemem (mio, mio->sys.mux); - mio->sys.mux = MIO_NULL; - } -} - -int mio_sys_ctrlmux (mio_t* mio, mio_sys_mux_cmd_t cmd, mio_dev_t* dev, int dev_capa) -{ static int epoll_cmd[] = { EPOLL_CTL_ADD, EPOLL_CTL_MOD, EPOLL_CTL_DEL }; struct epoll_event ev; @@ -287,24 +261,14 @@ int mio_sys_ctrlmux (mio_t* mio, mio_sys_mux_cmd_t cmd, mio_dev_t* dev, int dev_ } return 0; -} +#else +# error NO SUPPORTED MULTIPLEXER #endif +} int mio_sys_waitmux (mio_t* mio, const mio_ntime_t* tmout, mio_sys_mux_evtcb_t event_handler) { -#if defined(_WIN32) -/* - if (GetQueuedCompletionStatusEx (mio->iocp, mio->ovls, MIO_COUNTOF(mio->ovls), &nentries, timeout, FALSE) == FALSE) - { - // TODO: set errnum - return -1; - } - - for (i = 0; i < nentries; i++) - { - } -*/ -#elif defined(USE_POLL) +#if defined(USE_POLL) int nentries; mux = (mio_sys_mux_t*)mio->sys.mux;