diff --git a/mio/lib/Makefile.am b/mio/lib/Makefile.am index 4bb5f09..4339b70 100644 --- a/mio/lib/Makefile.am +++ b/mio/lib/Makefile.am @@ -37,6 +37,7 @@ include_HEADERS = \ mio-pro.h \ mio-sck.h \ mio-skad.h \ + mio-thr.h \ mio-upac.h \ mio-utl.h \ mio.h @@ -71,6 +72,7 @@ libmio_la_SOURCES = \ sys-mux.c \ sys-prv.h \ sys-tim.c \ + thr.c \ uch-case.h \ uch-prop.h \ tmr.c \ diff --git a/mio/lib/Makefile.in b/mio/lib/Makefile.in index 9324577..e83001f 100644 --- a/mio/lib/Makefile.in +++ b/mio/lib/Makefile.in @@ -146,8 +146,8 @@ am_libmio_la_OBJECTS = libmio_la-chr.lo libmio_la-dns.lo \ libmio_la-pipe.lo libmio_la-pro.lo libmio_la-sck.lo \ libmio_la-skad.lo libmio_la-sys.lo libmio_la-sys-ass.lo \ libmio_la-sys-err.lo libmio_la-sys-log.lo libmio_la-sys-mux.lo \ - libmio_la-sys-tim.lo libmio_la-tmr.lo libmio_la-utf8.lo \ - libmio_la-utl.lo + libmio_la-sys-tim.lo libmio_la-thr.lo libmio_la-tmr.lo \ + libmio_la-utf8.lo libmio_la-utl.lo libmio_la_OBJECTS = $(am_libmio_la_OBJECTS) AM_V_lt = $(am__v_lt_@AM_V@) am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) @@ -187,8 +187,9 @@ am__depfiles_remade = ./$(DEPDIR)/libmio_la-chr.Plo \ ./$(DEPDIR)/libmio_la-sys-log.Plo \ ./$(DEPDIR)/libmio_la-sys-mux.Plo \ ./$(DEPDIR)/libmio_la-sys-tim.Plo \ - ./$(DEPDIR)/libmio_la-sys.Plo ./$(DEPDIR)/libmio_la-tmr.Plo \ - ./$(DEPDIR)/libmio_la-utf8.Plo ./$(DEPDIR)/libmio_la-utl.Plo + ./$(DEPDIR)/libmio_la-sys.Plo ./$(DEPDIR)/libmio_la-thr.Plo \ + ./$(DEPDIR)/libmio_la-tmr.Plo ./$(DEPDIR)/libmio_la-utf8.Plo \ + ./$(DEPDIR)/libmio_la-utl.Plo am__mv = mv -f COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) @@ -423,6 +424,7 @@ include_HEADERS = \ mio-pro.h \ mio-sck.h \ mio-skad.h \ + mio-thr.h \ mio-upac.h \ mio-utl.h \ mio.h @@ -457,6 +459,7 @@ libmio_la_SOURCES = \ sys-mux.c \ sys-prv.h \ sys-tim.c \ + thr.c \ uch-case.h \ uch-prop.h \ tmr.c \ @@ -584,6 +587,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-sys-mux.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-sys-tim.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-sys.Plo@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-thr.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-tmr.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-utf8.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-utl.Plo@am__quote@ # am--include-marker @@ -786,6 +790,13 @@ libmio_la-sys-tim.lo: sys-tim.c @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libmio_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libmio_la-sys-tim.lo `test -f 'sys-tim.c' || echo '$(srcdir)/'`sys-tim.c +libmio_la-thr.lo: thr.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libmio_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libmio_la-thr.lo -MD -MP -MF $(DEPDIR)/libmio_la-thr.Tpo -c -o libmio_la-thr.lo `test -f 'thr.c' || echo '$(srcdir)/'`thr.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libmio_la-thr.Tpo $(DEPDIR)/libmio_la-thr.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='thr.c' object='libmio_la-thr.lo' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libmio_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libmio_la-thr.lo `test -f 'thr.c' || echo '$(srcdir)/'`thr.c + libmio_la-tmr.lo: tmr.c @am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libmio_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libmio_la-tmr.lo -MD -MP -MF $(DEPDIR)/libmio_la-tmr.Tpo -c -o libmio_la-tmr.lo `test -f 'tmr.c' || echo '$(srcdir)/'`tmr.c @am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libmio_la-tmr.Tpo $(DEPDIR)/libmio_la-tmr.Plo @@ -986,6 +997,7 @@ distclean: distclean-am -rm -f ./$(DEPDIR)/libmio_la-sys-mux.Plo -rm -f ./$(DEPDIR)/libmio_la-sys-tim.Plo -rm -f ./$(DEPDIR)/libmio_la-sys.Plo + -rm -f ./$(DEPDIR)/libmio_la-thr.Plo -rm -f ./$(DEPDIR)/libmio_la-tmr.Plo -rm -f ./$(DEPDIR)/libmio_la-utf8.Plo -rm -f ./$(DEPDIR)/libmio_la-utl.Plo @@ -1059,6 +1071,7 @@ maintainer-clean: maintainer-clean-am -rm -f ./$(DEPDIR)/libmio_la-sys-mux.Plo -rm -f ./$(DEPDIR)/libmio_la-sys-tim.Plo -rm -f ./$(DEPDIR)/libmio_la-sys.Plo + -rm -f ./$(DEPDIR)/libmio_la-thr.Plo -rm -f ./$(DEPDIR)/libmio_la-tmr.Plo -rm -f ./$(DEPDIR)/libmio_la-utf8.Plo -rm -f ./$(DEPDIR)/libmio_la-utl.Plo diff --git a/mio/lib/mio-thr.h b/mio/lib/mio-thr.h new file mode 100644 index 0000000..9538917 --- /dev/null +++ b/mio/lib/mio-thr.h @@ -0,0 +1,183 @@ +/* + * $Id$ + * + Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted thrvided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must rethrduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials thrvided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAfRRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _MIO_THR_H_ +#define _MIO_THR_H_ + +#include + +enum mio_dev_thr_sid_t +{ + MIO_DEV_THR_MASTER = -1, /* no io occurs on this. used only in on_close() */ + MIO_DEV_THR_IN = 0, /* input to the thread */ + MIO_DEV_THR_OUT = 1, /* output from the thread */ +}; +typedef enum mio_dev_thr_sid_t mio_dev_thr_sid_t; + +typedef struct mio_dev_thr_t mio_dev_thr_t; +typedef struct mio_dev_thr_slave_t mio_dev_thr_slave_t; + +typedef int (*mio_dev_thr_on_read_t) ( + mio_dev_thr_t* dev, + const void* data, + mio_iolen_t len +); + +typedef int (*mio_dev_thr_on_write_t) ( + mio_dev_thr_t* dev, + mio_iolen_t wrlen, + void* wrctx +); + +typedef void (*mio_dev_thr_on_close_t) ( + mio_dev_thr_t* dev, + mio_dev_thr_sid_t sid +); + +typedef int (*mio_dev_thr_func_t) ( + mio_dev_thr_t* dev, + mio_syshnd_t rfd, + mio_syshnd_t wfd, + void* ctx +); + +struct mio_dev_thr_t +{ + MIO_DEV_HEADER; + + int flags; + mio_syshnd_t thr_fd[2]; + mio_dev_thr_slave_t* slave[2]; + int slave_count; + + mio_dev_thr_func_t thr_func; + mio_dev_thr_on_read_t on_read; + mio_dev_thr_on_write_t on_write; + mio_dev_thr_on_close_t on_close; +}; + +struct mio_dev_thr_slave_t +{ + MIO_DEV_HEADER; + mio_dev_thr_sid_t id; + mio_syshnd_t pfd; + mio_dev_thr_t* master; /* parent device */ +}; + +enum mio_dev_thr_make_flag_t +{ + MIO_DEV_THR_WRITEIN = (1 << 0), + MIO_DEV_THR_READOUT = (1 << 1), +}; +typedef enum mio_dev_thr_make_flag_t mio_dev_thr_make_flag_t; + +typedef struct mio_dev_thr_make_t mio_dev_thr_make_t; +struct mio_dev_thr_make_t +{ + int flags; /**< bitwise-ORed of mio_dev_thr_make_flag_t enumerators */ + mio_dev_thr_func_t thr_func; + mio_dev_thr_on_write_t on_write; /* mandatory */ + mio_dev_thr_on_read_t on_read; /* mandatory */ + mio_dev_thr_on_close_t on_close; /* optional */ +}; + +enum mio_dev_thr_ioctl_cmd_t +{ + MIO_DEV_THR_CLOSE, + MIO_DEV_THR_KILL_CHILD +}; +typedef enum mio_dev_thr_ioctl_cmd_t mio_dev_thr_ioctl_cmd_t; + +#ifdef __cplusplus +extern "C" { +#endif + +MIO_EXPORT mio_dev_thr_t* mio_dev_thr_make ( + mio_t* mio, + mio_oow_t xtnsize, + const mio_dev_thr_make_t* data +); + +#if defined(MIO_HAVE_INLINE) +static MIO_INLINE mio_t* mio_dev_thr_getmio (mio_dev_thr_t* thr) { return mio_dev_getmio((mio_dev_t*)thr); } +#else +# define mio_dev_thr_getmio(thr) mio_dev_getmio(thr) +#endif + +#if defined(MIO_HAVE_INLINE) +static MIO_INLINE void* mio_dev_thr_getxtn (mio_dev_thr_t* thr) { return (void*)(thr + 1); } +#else +# define mio_dev_thr_getxtn(thr) ((void*)(((mio_dev_thr_t*)thr) + 1)) +#endif + +MIO_EXPORT void mio_dev_thr_kill ( + mio_dev_thr_t* thr +); + +MIO_EXPORT void mio_dev_thr_halt ( + mio_dev_thr_t* thr +); + +MIO_EXPORT int mio_dev_thr_read ( + mio_dev_thr_t* thr, + int enabled +); + +MIO_EXPORT int mio_dev_thr_timedread ( + mio_dev_thr_t* thr, + int enabled, + const mio_ntime_t* tmout +); + +MIO_EXPORT int mio_dev_thr_write ( + mio_dev_thr_t* thr, + const void* data, + mio_iolen_t len, + void* wrctx +); + +MIO_EXPORT int mio_dev_thr_timedwrite ( + mio_dev_thr_t* thr, + const void* data, + mio_iolen_t len, + const mio_ntime_t* tmout, + void* wrctx +); + +MIO_EXPORT int mio_dev_thr_close ( + mio_dev_thr_t* thr, + mio_dev_thr_sid_t sid +); + +MIO_EXPORT int mio_dev_thr_killthr ( + mio_dev_thr_t* thr +); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/mio/lib/thr.c b/mio/lib/thr.c new file mode 100644 index 0000000..96d5fdd --- /dev/null +++ b/mio/lib/thr.c @@ -0,0 +1,720 @@ +/* + * $Id$ + * + Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted thrvided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must rethrduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials thrvided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAfRRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "mio-thr.h" +#include "mio-prv.h" + +#include +#include +#include +#include + +/* ========================================================================= */ + +struct slave_info_t +{ + mio_dev_thr_make_t* mi; + mio_syshnd_t pfd; + int dev_cap; + mio_dev_thr_sid_t id; +}; + +typedef struct slave_info_t slave_info_t; + +static mio_dev_thr_slave_t* make_slave (mio_t* mio, slave_info_t* si); + +/* ========================================================================= */ + +struct thr_info_t +{ + mio_dev_thr_t* dev; + mio_dev_thr_func_t thr_func; + mio_syshnd_t rfd; + mio_syshnd_t wfd; +}; + +typedef struct thr_info_t thr_info_t; + +static void* run_thr_func (void* ctx) +{ + thr_info_t* ti = (thr_info_t*)ctx; + //ti->thr_func (ti->dev, ti->rfd, ti->wfd, ); + close (ti->rfd); + close (ti->wfd); + mio_freemem (ti->dev->mio, ti); + pthread_exit (MIO_NULL); + return MIO_NULL; +} + +static int dev_thr_make_master (mio_dev_t* dev, void* ctx) +{ + mio_t* mio = dev->mio; + mio_dev_thr_t* rdev = (mio_dev_thr_t*)dev; + mio_dev_thr_make_t* info = (mio_dev_thr_make_t*)ctx; + mio_syshnd_t pfds[4] = { MIO_SYSHND_INVALID, MIO_SYSHND_INVALID, MIO_SYSHND_INVALID, MIO_SYSHND_INVALID }; + int i, minidx = -1, maxidx = -1; + pid_t pid; + + if (info->flags & MIO_DEV_THR_WRITEIN) + { + if (pipe(&pfds[0]) == -1) + { + mio_seterrwithsyserr (mio, 0, errno); + goto oops; + } + minidx = 0; maxidx = 1; + } + + if (info->flags & MIO_DEV_THR_READOUT) + { + if (pipe(&pfds[2]) == -1) + { + mio_seterrwithsyserr (mio, 0, errno); + goto oops; + } + if (minidx == -1) minidx = 2; + maxidx = 3; + } + + if (maxidx == -1) + { + mio_seterrnum (mio, MIO_EINVAL); + goto oops; + } + + if (info->flags & MIO_DEV_THR_WRITEIN) + { + /* + * 0123 + * rw-- + * X + * WRITE => 1 + */ + if (mio_makesyshndasync(mio, pfds[1]) <= -1) goto oops; + } + + if (info->flags & MIO_DEV_THR_READOUT) + { + /* + * 0123 + * --rw + * X + * READ => 2 + */ + if (mio_makesyshndasync(mio, pfds[2]) <= -1) goto oops; + } + + if (pfds[1] != MIO_SYSHND_INVALID) + { + slave_info_t si; + + si.mi = info; + si.pfd = pfds[1]; + si.dev_cap = MIO_DEV_CAP_OUT | MIO_DEV_CAP_STREAM; + si.id = MIO_DEV_THR_IN; + + rdev->slave[MIO_DEV_THR_IN] = make_slave(mio, &si); + if (!rdev->slave[MIO_DEV_THR_IN]) goto oops; + + pfds[1] = MIO_SYSHND_INVALID; + rdev->slave_count++; + } + + if (pfds[2] != MIO_SYSHND_INVALID) + { + slave_info_t si; + + si.mi = info; + si.pfd = pfds[2]; + si.dev_cap = MIO_DEV_CAP_IN | MIO_DEV_CAP_STREAM; + si.id = MIO_DEV_THR_OUT; + + rdev->slave[MIO_DEV_THR_OUT] = make_slave(mio, &si); + if (!rdev->slave[MIO_DEV_THR_OUT]) goto oops; + + pfds[2] = MIO_SYSHND_INVALID; + rdev->slave_count++; + } + + for (i = 0; i < MIO_COUNTOF(rdev->slave); i++) + { + if (rdev->slave[i]) rdev->slave[i]->master = rdev; + } + + /* CREATE THREAD. DATA. CREATE THREAD .. */ +{ +pthread_t thr; +pthread_attr_t attr; +int n; + + thr_info_t* ti; + + ti = mio_callocmem(mio, MIO_SIZEOF(*ti)); + if (MIO_UNLIKELY(!ti)) goto oops; + + pthread_attr_init (&attr); + pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); + n = pthread_create(&thr, &attr, info->thr_func, ti); + pthread_attr_destroy (&attr); + if (n != 0) + { + mio_freemem (mio, ti); + goto oops; + } +} + + rdev->dev_cap = MIO_DEV_CAP_VIRTUAL; /* the master device doesn't perform I/O */ + rdev->flags = info->flags; + rdev->thr_func = info->thr_func; + rdev->on_read = info->on_read; + rdev->on_write = info->on_write; + rdev->on_close = info->on_close; + rdev->thr_fd[0] = pfds[0]; + rdev->thr_fd[1] = pfds[3]; + return 0; + +oops: + for (i = minidx; i < maxidx; i++) + { + if (pfds[i] != MIO_SYSHND_INVALID) close (pfds[i]); + } + + for (i = MIO_COUNTOF(rdev->slave); i > 0; ) + { + i--; + if (rdev->slave[i]) + { + mio_dev_kill ((mio_dev_t*)rdev->slave[i]); + rdev->slave[i] = MIO_NULL; + } + } + rdev->slave_count = 0; + + return -1; +} + +static int dev_thr_make_slave (mio_dev_t* dev, void* ctx) +{ + mio_dev_thr_slave_t* rdev = (mio_dev_thr_slave_t*)dev; + slave_info_t* si = (slave_info_t*)ctx; + + rdev->dev_cap = si->dev_cap; + rdev->id = si->id; + rdev->pfd = si->pfd; + /* keep rdev->master to MIO_NULL. it's set to the right master + * device in dev_thr_make() */ + + return 0; +} + +static int dev_thr_kill_master (mio_dev_t* dev, int force) +{ + mio_t* mio = dev->mio; + mio_dev_thr_t* rdev = (mio_dev_thr_t*)dev; + int i, status; + pid_t wpid; + + if (rdev->slave_count > 0) + { + for (i = 0; i < MIO_COUNTOF(rdev->slave); i++) + { + if (rdev->slave[i]) + { + mio_dev_thr_slave_t* sdev = rdev->slave[i]; + + /* nullify the pointer to the slave device + * before calling mio_dev_kill() on the slave device. + * the slave device can check this pointer to tell from + * self-initiated termination or master-driven termination */ + rdev->slave[i] = MIO_NULL; + + mio_dev_kill ((mio_dev_t*)sdev); + } + } + } + +/* TODO: may have to kill thread... */ + + if (rdev->on_close) rdev->on_close (rdev, MIO_DEV_THR_MASTER); + return 0; +} + +static int dev_thr_kill_slave (mio_dev_t* dev, int force) +{ + mio_t* mio = dev->mio; + mio_dev_thr_slave_t* rdev = (mio_dev_thr_slave_t*)dev; + + if (rdev->master) + { + mio_dev_thr_t* master; + + master = rdev->master; + rdev->master = MIO_NULL; + + /* indicate EOF */ + if (master->on_close) master->on_close (master, rdev->id); + + MIO_ASSERT (mio, master->slave_count > 0); + master->slave_count--; + + if (master->slave[rdev->id]) + { + /* this call is started by the slave device itself. */ + if (master->slave_count <= 0) + { + /* if this is the last slave, kill the master also */ + mio_dev_kill ((mio_dev_t*)master); + /* the master pointer is not valid from this point onwards + * as the actual master device object is freed in mio_dev_kill() */ + } + else + { + /* this call is initiated by this slave device itself. + * if it were by the master device, it would be MIO_NULL as + * nullified by the dev_thr_kill() */ + master->slave[rdev->id] = MIO_NULL; + } + } + } + + if (rdev->pfd != MIO_SYSHND_INVALID) + { + close (rdev->pfd); + rdev->pfd = MIO_SYSHND_INVALID; + } + + return 0; +} + +static int dev_thr_read_slave (mio_dev_t* dev, void* buf, mio_iolen_t* len, mio_devaddr_t* srcaddr) +{ + mio_dev_thr_slave_t* thr = (mio_dev_thr_slave_t*)dev; + ssize_t x; + + /* the read and write operation happens on different slave devices. + * the write EOF indication doesn't affect this device + if (MIO_UNLIKELY(thr->pfd == MIO_SYSHND_INVALID)) + { + mio_seterrnum (thr->mio, MIO_EBADHND); + return -1; + }*/ + MIO_ASSERT (thr->mio, thr->pfd != MIO_SYSHND_INVALID); /* use this assertion to check if my claim above is right */ + + x = read(thr->pfd, buf, *len); + if (x <= -1) + { + if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data available */ + if (errno == EINTR) return 0; + mio_seterrwithsyserr (thr->mio, 0, errno); + return -1; + } + + *len = x; + return 1; +} + +static int dev_thr_write_slave (mio_dev_t* dev, const void* data, mio_iolen_t* len, const mio_devaddr_t* dstaddr) +{ + mio_dev_thr_slave_t* thr = (mio_dev_thr_slave_t*)dev; + ssize_t x; + + /* this check is not needed because MIO_DEV_CAP_OUT_CLOSED is set on the device by the core + * when EOF indication is successful(return value 1 and *iovcnt 0). + * If MIO_DEV_CAP_OUT_CLOSED, the core doesn't invoke the write method + if (MIO_UNLIKELY(thr->pfd == MIO_SYSHND_INVALID)) + { + mio_seterrnum (thr->mio, MIO_EBADHND); + return -1; + }*/ + MIO_ASSERT (thr->mio, thr->pfd != MIO_SYSHND_INVALID); /* use this assertion to check if my claim above is right */ + + if (MIO_UNLIKELY(*len <= 0)) + { + /* this is an EOF indicator */ + /* It isn't apthrpriate to call mio_dev_halt(thr) or mio_dev_thr_close(thr->master, MIO_DEV_THR_IN) + * as those functions destroy the device itself */ + if (MIO_LIKELY(thr->pfd != MIO_SYSHND_INVALID)) + { + mio_dev_watch (dev, MIO_DEV_WATCH_STOP, 0); + close (thr->pfd); + thr->pfd = MIO_SYSHND_INVALID; + } + return 1; /* indicate that the operation got successful. the core will execute on_write() with the write length of 0. */ + } + + x = write(thr->pfd, data, *len); + if (x <= -1) + { + if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data can be written */ + if (errno == EINTR) return 0; + mio_seterrwithsyserr (thr->mio, 0, errno); + return -1; + } + + *len = x; + return 1; +} + +static int dev_thr_writev_slave (mio_dev_t* dev, const mio_iovec_t* iov, mio_iolen_t* iovcnt, const mio_devaddr_t* dstaddr) +{ + mio_dev_thr_slave_t* thr = (mio_dev_thr_slave_t*)dev; + ssize_t x; + + /* this check is not needed because MIO_DEV_CAP_OUT_CLOSED is set on the device by the core + * when EOF indication is successful(return value 1 and *iovcnt 0). + * If MIO_DEV_CAP_OUT_CLOSED, the core doesn't invoke the write method + if (MIO_UNLIKELY(thr->pfd == MIO_SYSHND_INVALID)) + { + mio_seterrnum (thr->mio, MIO_EBADHND); + return -1; + }*/ + MIO_ASSERT (thr->mio, thr->pfd != MIO_SYSHND_INVALID); /* use this assertion to check if my claim above is right */ + + if (MIO_UNLIKELY(*iovcnt <= 0)) + { + /* this is an EOF indicator */ + /* It isn't apthrpriate to call mio_dev_halt(thr) or mio_dev_thr_close(thr->master, MIO_DEV_THR_IN) + * as those functions destroy the device itself */ + if (MIO_LIKELY(thr->pfd != MIO_SYSHND_INVALID)) + { + mio_dev_watch (dev, MIO_DEV_WATCH_STOP, 0); + close (thr->pfd); + thr->pfd = MIO_SYSHND_INVALID; + } + return 1; /* indicate that the operation got successful. the core will execute on_write() with 0. */ + } + + x = writev(thr->pfd, iov, *iovcnt); + if (x <= -1) + { + if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data can be written */ + if (errno == EINTR) return 0; + mio_seterrwithsyserr (thr->mio, 0, errno); + return -1; + } + + *iovcnt = x; + return 1; +} + +static mio_syshnd_t dev_thr_getsyshnd (mio_dev_t* dev) +{ + return MIO_SYSHND_INVALID; +} + +static mio_syshnd_t dev_thr_getsyshnd_slave (mio_dev_t* dev) +{ + mio_dev_thr_slave_t* thr = (mio_dev_thr_slave_t*)dev; + return (mio_syshnd_t)thr->pfd; +} + +static int dev_thr_ioctl (mio_dev_t* dev, int cmd, void* arg) +{ + mio_t* mio = dev->mio; + mio_dev_thr_t* rdev = (mio_dev_thr_t*)dev; + + switch (cmd) + { + case MIO_DEV_THR_CLOSE: + { + mio_dev_thr_sid_t sid = *(mio_dev_thr_sid_t*)arg; + + if (MIO_UNLIKELY(sid != MIO_DEV_THR_IN && sid != MIO_DEV_THR_OUT)) + { + mio_seterrnum (mio, MIO_EINVAL); + return -1; + } + + if (rdev->slave[sid]) + { + /* unlike dev_thr_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_dev_kill ((mio_dev_t*)rdev->slave[sid]); + + /* if this is the last slave, the master is destroyed as well. + * therefore, using rdev is unsafe in the assertion below is unsafe. + *MIO_ASSERT (mio, rdev->slave[sid] == MIO_NULL); */ + } + + return 0; + } + +#if 0 + case MIO_DEV_THR_KILL_CHILD: + if (rdev->child_pid >= 0) + { + if (kill(rdev->child_pid, SIGKILL) == -1) + { + mio_seterrwithsyserr (mio, 0, errno); + return -1; + } + } +#endif + + return 0; + + default: + mio_seterrnum (mio, MIO_EINVAL); + return -1; + } +} + +static mio_dev_mth_t dev_thr_methods = +{ + dev_thr_make_master, + dev_thr_kill_master, + dev_thr_getsyshnd, + + MIO_NULL, + MIO_NULL, + MIO_NULL, + dev_thr_ioctl +}; + +static mio_dev_mth_t dev_thr_methods_slave = +{ + dev_thr_make_slave, + dev_thr_kill_slave, + dev_thr_getsyshnd_slave, + + dev_thr_read_slave, + dev_thr_write_slave, + dev_thr_writev_slave, + dev_thr_ioctl +}; + +/* ========================================================================= */ + +static int thr_ready (mio_dev_t* dev, int events) +{ + /* virtual device. no I/O */ + mio_seterrnum (dev->mio, MIO_EINTERN); + return -1; +} + +static int thr_on_read (mio_dev_t* dev, const void* data, mio_iolen_t len, const mio_devaddr_t* srcaddr) +{ + /* virtual device. no I/O */ + mio_seterrnum (dev->mio, MIO_EINTERN); + return -1; +} + +static int thr_on_write (mio_dev_t* dev, mio_iolen_t wrlen, void* wrctx, const mio_devaddr_t* dstaddr) +{ + /* virtual device. no I/O */ + mio_seterrnum (dev->mio, MIO_EINTERN); + return -1; +} + +static mio_dev_evcb_t dev_thr_event_callbacks = +{ + thr_ready, + thr_on_read, + thr_on_write +}; + +/* ========================================================================= */ + +static int thr_ready_slave (mio_dev_t* dev, int events) +{ + mio_t* mio = dev->mio; + /*mio_dev_thr_t* thr = (mio_dev_thr_t*)dev;*/ + + if (events & MIO_DEV_EVENT_ERR) + { + mio_seterrnum (mio, MIO_EDEVERR); + return -1; + } + + if (events & MIO_DEV_EVENT_HUP) + { + if (events & (MIO_DEV_EVENT_PRI | MIO_DEV_EVENT_IN | MIO_DEV_EVENT_OUT)) + { + /* thrbably half-open? */ + return 1; + } + + mio_seterrnum (mio, MIO_EDEVHUP); + return -1; + } + + return 1; /* the device is ok. carry on reading or writing */ +} + + +static int thr_on_read_slave (mio_dev_t* dev, const void* data, mio_iolen_t len, const mio_devaddr_t* srcaddr) +{ + mio_dev_thr_slave_t* thr = (mio_dev_thr_slave_t*)dev; + return thr->master->on_read(thr->master, data, len); +} + +static int thr_on_write_slave (mio_dev_t* dev, mio_iolen_t wrlen, void* wrctx, const mio_devaddr_t* dstaddr) +{ + mio_dev_thr_slave_t* thr = (mio_dev_thr_slave_t*)dev; + return thr->master->on_write(thr->master, wrlen, wrctx); +} + +static mio_dev_evcb_t dev_thr_event_callbacks_slave_in = +{ + thr_ready_slave, + MIO_NULL, + thr_on_write_slave +}; + +static mio_dev_evcb_t dev_thr_event_callbacks_slave_out = +{ + thr_ready_slave, + thr_on_read_slave, + MIO_NULL +}; + +/* ========================================================================= */ + +static mio_dev_thr_slave_t* make_slave (mio_t* mio, slave_info_t* si) +{ + switch (si->id) + { + case MIO_DEV_THR_IN: + return (mio_dev_thr_slave_t*)mio_dev_make( + mio, MIO_SIZEOF(mio_dev_thr_t), + &dev_thr_methods_slave, &dev_thr_event_callbacks_slave_in, si); + + case MIO_DEV_THR_OUT: + return (mio_dev_thr_slave_t*)mio_dev_make( + mio, MIO_SIZEOF(mio_dev_thr_t), + &dev_thr_methods_slave, &dev_thr_event_callbacks_slave_out, si); + + default: + mio_seterrnum (mio, MIO_EINVAL); + return MIO_NULL; + } +} + +mio_dev_thr_t* mio_dev_thr_make (mio_t* mio, mio_oow_t xtnsize, const mio_dev_thr_make_t* info) +{ + return (mio_dev_thr_t*)mio_dev_make( + mio, MIO_SIZEOF(mio_dev_thr_t) + xtnsize, + &dev_thr_methods, &dev_thr_event_callbacks, (void*)info); +} + +void mio_dev_thr_kill (mio_dev_thr_t* dev) +{ + mio_dev_kill ((mio_dev_t*)dev); +} + +void mio_dev_thr_halt (mio_dev_thr_t* dev) +{ + mio_dev_halt ((mio_dev_t*)dev); +} + +int mio_dev_thr_read (mio_dev_thr_t* dev, int enabled) +{ + mio_t* mio = dev->mio; + + if (dev->slave[MIO_DEV_THR_OUT]) + { + return mio_dev_read((mio_dev_t*)dev->slave[MIO_DEV_THR_OUT], enabled); + } + else + { + mio_seterrnum (dev->mio, MIO_ENOCAPA); /* TODO: is it the right error number? */ + return -1; + } +} + +int mio_dev_thr_timedread (mio_dev_thr_t* dev, int enabled, const mio_ntime_t* tmout) +{ + mio_t* mio = dev->mio; + + if (dev->slave[MIO_DEV_THR_OUT]) + { + return mio_dev_timedread((mio_dev_t*)dev->slave[MIO_DEV_THR_OUT], enabled, tmout); + } + else + { + mio_seterrnum (mio, MIO_ENOCAPA); /* TODO: is it the right error number? */ + return -1; + } +} + +int mio_dev_thr_write (mio_dev_thr_t* dev, const void* data, mio_iolen_t dlen, void* wrctx) +{ + if (dev->slave[MIO_DEV_THR_IN]) + { + return mio_dev_write((mio_dev_t*)dev->slave[MIO_DEV_THR_IN], data, dlen, wrctx, MIO_NULL); + } + else + { + mio_seterrnum (dev->mio, MIO_ENOCAPA); /* TODO: is it the right error number? */ + return -1; + } +} + +int mio_dev_thr_timedwrite (mio_dev_thr_t* dev, const void* data, mio_iolen_t dlen, const mio_ntime_t* tmout, void* wrctx) +{ + if (dev->slave[MIO_DEV_THR_IN]) + { + return mio_dev_timedwrite((mio_dev_t*)dev->slave[MIO_DEV_THR_IN], data, dlen, tmout, wrctx, MIO_NULL); + } + else + { + mio_seterrnum (dev->mio, MIO_ENOCAPA); /* TODO: is it the right error number? */ + return -1; + } +} + +int mio_dev_thr_close (mio_dev_thr_t* dev, mio_dev_thr_sid_t sid) +{ + return mio_dev_ioctl((mio_dev_t*)dev, MIO_DEV_THR_CLOSE, &sid); +} + +#if 0 +int mio_dev_thr_killchild (mio_dev_thr_t* dev) +{ + return mio_dev_ioctl((mio_dev_t*)dev, MIO_DEV_THR_KILL_CHILD, MIO_NULL); +} +#endif + +#if 0 +mio_dev_thr_t* mio_dev_thr_getdev (mio_dev_thr_t* thr, mio_dev_thr_sid_t sid) +{ + switch (type) + { + case MIO_DEV_THR_IN: + return XXX; + + case MIO_DEV_THR_OUT: + return XXX; + + case MIO_DEV_THR_ERR: + return XXX; + } + + thr->dev->mio = MIO_EINVAL; + return MIO_NULL; +} +#endif