2016-01-26 16:07:52 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
Copyright (c) 2015-2016 Chung, Hyung-Hwan. All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided 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 reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided 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.
|
|
|
|
*/
|
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
#include "stio-prv.h"
|
|
|
|
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
#define DEV_CAPA_ALL_WATCHED (STIO_DEV_CAPA_IN_WATCHED | STIO_DEV_CAPA_OUT_WATCHED | STIO_DEV_CAPA_PRI_WATCHED)
|
|
|
|
|
2016-04-12 13:56:59 +00:00
|
|
|
|
2016-04-16 16:05:57 +00:00
|
|
|
static int schedule_kill_zombie_job (stio_dev_t* dev);
|
|
|
|
static int kill_and_free_device (stio_dev_t* dev, int force);
|
|
|
|
|
2016-04-17 17:00:58 +00:00
|
|
|
#define APPEND_DEVICE_TO_LIST(list,dev) do { \
|
|
|
|
if ((list)->tail) (list)->tail->dev_next = (dev); \
|
|
|
|
else (list)->head = (dev); \
|
|
|
|
(dev)->dev_prev = (list)->tail; \
|
|
|
|
(dev)->dev_next = STIO_NULL; \
|
|
|
|
(list)->tail = (dev); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define UNLINK_DEVICE_FROM_LIST(list,dev) do { \
|
|
|
|
if ((dev)->dev_prev) (dev)->dev_prev->dev_next = (dev)->dev_next; \
|
|
|
|
else (list)->head = (dev)->dev_next; \
|
|
|
|
if ((dev)->dev_next) (dev)->dev_next->dev_prev = (dev)->dev_prev; \
|
|
|
|
else (list)->tail = (dev)->dev_prev; \
|
|
|
|
} while (0)
|
|
|
|
|
2016-01-30 05:24:23 +00:00
|
|
|
stio_t* stio_open (stio_mmgr_t* mmgr, stio_size_t xtnsize, stio_size_t tmrcapa, stio_errnum_t* errnum)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
|
|
|
stio_t* stio;
|
|
|
|
|
2016-01-30 05:24:23 +00:00
|
|
|
stio = STIO_MMGR_ALLOC (mmgr, STIO_SIZEOF(stio_t) + xtnsize);
|
|
|
|
if (stio)
|
|
|
|
{
|
|
|
|
if (stio_init (stio, mmgr, tmrcapa) <= -1)
|
|
|
|
{
|
|
|
|
if (errnum) *errnum = stio->errnum;
|
|
|
|
STIO_MMGR_FREE (mmgr, stio);
|
|
|
|
stio = STIO_NULL;
|
|
|
|
}
|
|
|
|
else STIO_MEMSET (stio + 1, 0, xtnsize);
|
|
|
|
}
|
|
|
|
else
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
|
|
|
if (errnum) *errnum = STIO_ENOMEM;
|
|
|
|
}
|
|
|
|
|
2016-01-30 05:24:23 +00:00
|
|
|
return stio;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stio_close (stio_t* stio)
|
|
|
|
{
|
|
|
|
stio_fini (stio);
|
|
|
|
STIO_MMGR_FREE (stio->mmgr, stio);
|
|
|
|
}
|
|
|
|
|
|
|
|
int stio_init (stio_t* stio, stio_mmgr_t* mmgr, stio_size_t tmrcapa)
|
|
|
|
{
|
2016-01-26 16:02:20 +00:00
|
|
|
STIO_MEMSET (stio, 0, STIO_SIZEOF(*stio));
|
|
|
|
stio->mmgr = mmgr;
|
|
|
|
|
2016-01-30 05:24:23 +00:00
|
|
|
/* intialize the multiplexer object */
|
2016-01-26 16:02:20 +00:00
|
|
|
stio->mux = epoll_create (1000);
|
|
|
|
if (stio->mux == -1)
|
|
|
|
{
|
2016-01-31 02:05:39 +00:00
|
|
|
stio->errnum = stio_syserrtoerrnum(errno);
|
2016-01-30 05:24:23 +00:00
|
|
|
return -1;
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2016-01-30 05:24:23 +00:00
|
|
|
/* initialize the timer object */
|
|
|
|
if (tmrcapa <= 0) tmrcapa = 1;
|
|
|
|
stio->tmr.jobs = STIO_MMGR_ALLOC (stio->mmgr, tmrcapa * STIO_SIZEOF(stio_tmrjob_t));
|
|
|
|
if (!stio->tmr.jobs)
|
|
|
|
{
|
|
|
|
stio->errnum = STIO_ENOMEM;
|
|
|
|
close (stio->mux);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
stio->tmr.capa = tmrcapa;
|
|
|
|
|
|
|
|
return 0;
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2016-01-30 05:24:23 +00:00
|
|
|
void stio_fini (stio_t* stio)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2016-04-17 17:00:58 +00:00
|
|
|
stio_dev_t* dev, * next_dev;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
stio_dev_t* head;
|
|
|
|
stio_dev_t* tail;
|
|
|
|
} diehard;
|
2016-01-30 05:24:23 +00:00
|
|
|
|
2016-04-16 16:05:57 +00:00
|
|
|
/* kill all registered devices */
|
2016-04-17 17:00:58 +00:00
|
|
|
while (stio->actdev.head)
|
2016-04-16 16:05:57 +00:00
|
|
|
{
|
2016-04-17 17:00:58 +00:00
|
|
|
stio_killdev (stio, stio->actdev.head);
|
2016-04-16 16:05:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-17 17:00:58 +00:00
|
|
|
while (stio->hltdev.head)
|
2016-04-16 16:05:57 +00:00
|
|
|
{
|
2016-04-17 17:00:58 +00:00
|
|
|
stio_killdev (stio, stio->hltdev.head);
|
2016-04-16 16:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* clean up all zombie devices */
|
2016-04-17 17:00:58 +00:00
|
|
|
STIO_MEMSET (&diehard, 0, STIO_SIZEOF(diehard));
|
2016-04-16 16:05:57 +00:00
|
|
|
for (dev = stio->zmbdev.head; dev; )
|
|
|
|
{
|
|
|
|
kill_and_free_device (dev, 1);
|
2016-04-17 17:00:58 +00:00
|
|
|
if (stio->zmbdev.head == dev)
|
|
|
|
{
|
|
|
|
/* the deive has not been freed. go on to the next one */
|
|
|
|
next_dev = dev->dev_next;
|
|
|
|
|
|
|
|
/* remove the device from the zombie device list */
|
|
|
|
UNLINK_DEVICE_FROM_LIST (&stio->zmbdev, dev);
|
|
|
|
dev->dev_capa &= ~STIO_DEV_CAPA_ZOMBIE;
|
|
|
|
|
|
|
|
/* put it to a private list for aborting */
|
|
|
|
APPEND_DEVICE_TO_LIST (&diehard, dev);
|
|
|
|
|
|
|
|
dev = next_dev;
|
|
|
|
}
|
2016-04-16 16:05:57 +00:00
|
|
|
else dev = stio->zmbdev.head;
|
|
|
|
}
|
|
|
|
|
2016-04-17 17:00:58 +00:00
|
|
|
while (diehard.head)
|
2016-04-16 16:05:57 +00:00
|
|
|
{
|
|
|
|
/* if the kill method returns failure, it can leak some resource
|
|
|
|
* because the device is freed regardless of the failure when 2
|
|
|
|
* is given to kill_and_free_device(). */
|
2016-04-17 17:00:58 +00:00
|
|
|
dev = diehard.head;
|
|
|
|
STIO_ASSERT (!(dev->dev_capa & (STIO_DEV_CAPA_ACTIVE | STIO_DEV_CAPA_HALTED | STIO_DEV_CAPA_ZOMBIE)));
|
|
|
|
UNLINK_DEVICE_FROM_LIST (&diehard, dev);
|
|
|
|
kill_and_free_device (dev, 2);
|
2016-04-16 16:05:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-17 17:00:58 +00:00
|
|
|
/* purge scheduled timer jobs and kill the timer */
|
|
|
|
stio_cleartmrjobs (stio);
|
|
|
|
STIO_MMGR_FREE (stio->mmgr, stio->tmr.jobs);
|
|
|
|
|
2016-01-30 05:24:23 +00:00
|
|
|
/* close the multiplexer */
|
2016-01-26 16:02:20 +00:00
|
|
|
close (stio->mux);
|
|
|
|
}
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
|
|
|
|
int stio_prologue (stio_t* stio)
|
|
|
|
{
|
|
|
|
/* TODO: */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stio_epilogue (stio_t* stio)
|
|
|
|
{
|
|
|
|
/* TODO: */
|
|
|
|
}
|
|
|
|
|
2016-02-05 13:25:59 +00:00
|
|
|
static STIO_INLINE void unlink_wq (stio_t* stio, stio_wq_t* q)
|
|
|
|
{
|
|
|
|
if (q->tmridx != STIO_TMRIDX_INVALID)
|
|
|
|
{
|
|
|
|
stio_deltmrjob (stio, q->tmridx);
|
|
|
|
STIO_ASSERT (q->tmridx == STIO_TMRIDX_INVALID);
|
|
|
|
}
|
|
|
|
STIO_WQ_UNLINK (q);
|
|
|
|
}
|
|
|
|
|
|
|
|
static STIO_INLINE void handle_event (stio_t* stio, stio_size_t i)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
|
|
|
stio_dev_t* dev;
|
|
|
|
|
2016-02-05 13:25:59 +00:00
|
|
|
stio->renew_watch = 0;
|
2016-02-04 15:06:20 +00:00
|
|
|
dev = stio->revs[i].data.ptr;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-02-05 13:25:59 +00:00
|
|
|
STIO_ASSERT (stio == dev->stio);
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
if (dev->dev_evcb->ready)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2016-02-04 15:06:20 +00:00
|
|
|
int x, events = 0;
|
|
|
|
|
|
|
|
if (stio->revs[i].events & EPOLLIN) events |= STIO_DEV_EVENT_IN;
|
|
|
|
if (stio->revs[i].events & EPOLLOUT) events |= STIO_DEV_EVENT_OUT;
|
|
|
|
if (stio->revs[i].events & EPOLLPRI) events |= STIO_DEV_EVENT_PRI;
|
|
|
|
if (stio->revs[i].events & EPOLLERR) events |= STIO_DEV_EVENT_ERR;
|
|
|
|
if (stio->revs[i].events & EPOLLHUP) events |= STIO_DEV_EVENT_HUP;
|
|
|
|
#if defined(EPOLLRDHUP)
|
|
|
|
else if (stio->revs[i].events & EPOLLRDHUP) events |= STIO_DEV_EVENT_HUP;
|
|
|
|
#endif
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
/* return value of ready()
|
|
|
|
* <= -1 - failure. kill the device.
|
|
|
|
* == 0 - ok. but don't invoke recv() or send().
|
|
|
|
* >= 1 - everything is ok. */
|
|
|
|
x = dev->dev_evcb->ready (dev, events);
|
|
|
|
if (x <= -1)
|
|
|
|
{
|
|
|
|
stio_dev_halt (dev);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (x == 0) goto skip_evcb;
|
|
|
|
}
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
if (dev && stio->revs[i].events & EPOLLPRI)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2016-02-04 15:06:20 +00:00
|
|
|
/* urgent data */
|
|
|
|
/* TODO: urgent data.... */
|
2016-02-05 13:25:59 +00:00
|
|
|
/*x = dev->dev_mth->urgread (dev, stio->bugbuf, &len);*/
|
2016-02-04 15:06:20 +00:00
|
|
|
printf ("has urgent data...\n");
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
if (dev && stio->revs[i].events & EPOLLOUT)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2016-02-04 15:06:20 +00:00
|
|
|
/* write pending requests */
|
|
|
|
while (!STIO_WQ_ISEMPTY(&dev->wq))
|
|
|
|
{
|
|
|
|
stio_wq_t* q;
|
|
|
|
const stio_uint8_t* uptr;
|
2016-04-02 15:25:35 +00:00
|
|
|
stio_iolen_t urem, ulen;
|
2016-02-04 15:06:20 +00:00
|
|
|
int x;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
q = STIO_WQ_HEAD(&dev->wq);
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
uptr = q->ptr;
|
|
|
|
urem = q->len;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
send_leftover:
|
|
|
|
ulen = urem;
|
2016-04-18 14:21:23 +00:00
|
|
|
printf ("&&&&&&&&&&&&&&&&&&&&&&&&&&&&7WRITING..........................\n");
|
|
|
|
x = dev->dev_mth->write (dev, uptr, &ulen, &q->dstaddr);
|
2016-02-04 15:06:20 +00:00
|
|
|
if (x <= -1)
|
|
|
|
{
|
|
|
|
stio_dev_halt (dev);
|
|
|
|
dev = STIO_NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (x == 0)
|
|
|
|
{
|
|
|
|
/* keep the left-over */
|
|
|
|
STIO_MEMMOVE (q->ptr, uptr, urem);
|
|
|
|
q->len = urem;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uptr += ulen;
|
|
|
|
urem -= ulen;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
if (urem <= 0)
|
|
|
|
{
|
|
|
|
/* finished writing a single write request */
|
|
|
|
int y, out_closed = 0;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
if (q->len <= 0 && (dev->dev_capa & STIO_DEV_CAPA_STREAM))
|
|
|
|
{
|
|
|
|
/* it was a zero-length write request.
|
|
|
|
* for a stream, it is to close the output. */
|
|
|
|
dev->dev_capa |= STIO_DEV_CAPA_OUT_CLOSED;
|
|
|
|
stio->renew_watch = 1;
|
|
|
|
out_closed = 1;
|
|
|
|
}
|
2016-01-28 16:44:47 +00:00
|
|
|
|
2016-02-05 13:25:59 +00:00
|
|
|
unlink_wq (stio, q);
|
2016-04-18 14:21:23 +00:00
|
|
|
y = dev->dev_evcb->on_write (dev, q->olen, q->ctx, &q->dstaddr);
|
2016-02-05 13:25:59 +00:00
|
|
|
STIO_MMGR_FREE (stio->mmgr, q);
|
2016-01-28 16:44:47 +00:00
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
if (y <= -1)
|
|
|
|
{
|
|
|
|
stio_dev_halt (dev);
|
|
|
|
dev = STIO_NULL;
|
|
|
|
break;
|
|
|
|
}
|
2016-01-28 16:44:47 +00:00
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
if (out_closed)
|
|
|
|
{
|
|
|
|
/* drain all pending requests.
|
|
|
|
* callbacks are skipped for drained requests */
|
|
|
|
while (!STIO_WQ_ISEMPTY(&dev->wq))
|
|
|
|
{
|
|
|
|
q = STIO_WQ_HEAD(&dev->wq);
|
2016-02-05 13:25:59 +00:00
|
|
|
unlink_wq (stio, q);
|
2016-02-04 15:06:20 +00:00
|
|
|
STIO_MMGR_FREE (dev->stio->mmgr, q);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else goto send_leftover;
|
|
|
|
}
|
|
|
|
}
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
if (dev && STIO_WQ_ISEMPTY(&dev->wq))
|
|
|
|
{
|
|
|
|
/* no pending request to write */
|
|
|
|
if ((dev->dev_capa & STIO_DEV_CAPA_IN_CLOSED) &&
|
|
|
|
(dev->dev_capa & STIO_DEV_CAPA_OUT_CLOSED))
|
|
|
|
{
|
|
|
|
stio_dev_halt (dev);
|
|
|
|
dev = STIO_NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stio->renew_watch = 1;
|
|
|
|
}
|
|
|
|
}
|
2016-02-02 07:51:17 +00:00
|
|
|
}
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
if (dev && stio->revs[i].events & EPOLLIN)
|
|
|
|
{
|
2016-04-18 14:21:23 +00:00
|
|
|
stio_devaddr_t srcaddr;
|
2016-04-02 15:25:35 +00:00
|
|
|
stio_iolen_t len;
|
2016-02-04 15:06:20 +00:00
|
|
|
int x;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
/* the devices are all non-blocking. read as much as possible
|
|
|
|
* if on_read callback returns 1 or greater. read only once
|
|
|
|
* if the on_read calllback returns 0. */
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
len = STIO_COUNTOF(stio->bigbuf);
|
2016-04-18 14:21:23 +00:00
|
|
|
x = dev->dev_mth->read (dev, stio->bigbuf, &len, &srcaddr);
|
2016-02-04 15:06:20 +00:00
|
|
|
if (x <= -1)
|
|
|
|
{
|
|
|
|
stio_dev_halt (dev);
|
|
|
|
dev = STIO_NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (x == 0)
|
|
|
|
{
|
|
|
|
/* no data is available - EWOULDBLOCK or something similar */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (x >= 1)
|
|
|
|
{
|
|
|
|
if (len <= 0 && (dev->dev_capa & STIO_DEV_CAPA_STREAM))
|
|
|
|
{
|
|
|
|
/* EOF received. for a stream device, a zero-length
|
|
|
|
* read is interpreted as EOF. */
|
|
|
|
dev->dev_capa |= STIO_DEV_CAPA_IN_CLOSED;
|
|
|
|
stio->renew_watch = 1;
|
|
|
|
|
|
|
|
/* call the on_read callback to report EOF */
|
2016-04-18 14:21:23 +00:00
|
|
|
if (dev->dev_evcb->on_read (dev, stio->bigbuf, len, &srcaddr) <= -1 ||
|
2016-02-04 15:06:20 +00:00
|
|
|
(dev->dev_capa & STIO_DEV_CAPA_OUT_CLOSED))
|
|
|
|
{
|
|
|
|
/* 1. input ended and its reporting failed or
|
|
|
|
* 2. input ended and no writing is possible */
|
|
|
|
stio_dev_halt (dev);
|
|
|
|
dev = STIO_NULL;
|
|
|
|
}
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
/* since EOF is received, reading can't be greedy */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int y;
|
|
|
|
/* TODO: for a stream device, merge received data if bigbuf isn't full and fire the on_read callback
|
|
|
|
* when x == 0 or <= -1. you can */
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
/* data available */
|
2016-04-18 14:21:23 +00:00
|
|
|
y = dev->dev_evcb->on_read (dev, stio->bigbuf, len, &srcaddr);
|
2016-02-04 15:06:20 +00:00
|
|
|
if (y <= -1)
|
|
|
|
{
|
|
|
|
stio_dev_halt (dev);
|
|
|
|
dev = STIO_NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (y == 0)
|
|
|
|
{
|
|
|
|
/* don't be greedy. read only once
|
|
|
|
* for this loop iteration */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-02 07:51:17 +00:00
|
|
|
|
2016-02-05 13:25:59 +00:00
|
|
|
if (dev)
|
2016-02-04 15:06:20 +00:00
|
|
|
{
|
2016-02-05 13:25:59 +00:00
|
|
|
if (stio->revs[i].events & (EPOLLERR | EPOLLHUP))
|
|
|
|
{
|
|
|
|
/* if error or hangup has been reported on the device,
|
|
|
|
* halt the device. this check is performed after
|
|
|
|
* EPOLLIN or EPOLLOUT check because EPOLLERR or EPOLLHUP
|
|
|
|
* can be set together with EPOLLIN or EPOLLOUT. */
|
|
|
|
dev->dev_capa |= STIO_DEV_CAPA_IN_CLOSED | STIO_DEV_CAPA_OUT_CLOSED;
|
|
|
|
stio->renew_watch = 1;
|
|
|
|
}
|
|
|
|
#if defined(EPOLLRDHUP)
|
|
|
|
else if (dev && stio->revs[i].events & EPOLLRDHUP)
|
|
|
|
{
|
|
|
|
if (stio->revs[i].events & (EPOLLIN | EPOLLOUT | EPOLLPRI))
|
|
|
|
{
|
|
|
|
/* it may be a half-open state. don't do anything here
|
|
|
|
* to let the next read detect EOF */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dev->dev_capa |= STIO_DEV_CAPA_IN_CLOSED | STIO_DEV_CAPA_OUT_CLOSED;
|
|
|
|
stio->renew_watch = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2016-02-02 07:51:17 +00:00
|
|
|
|
2016-02-05 13:25:59 +00:00
|
|
|
if ((dev->dev_capa & STIO_DEV_CAPA_IN_CLOSED) &&
|
|
|
|
(dev->dev_capa & STIO_DEV_CAPA_OUT_CLOSED))
|
|
|
|
{
|
|
|
|
stio_dev_halt (dev);
|
|
|
|
dev = STIO_NULL;
|
|
|
|
}
|
2016-02-04 15:06:20 +00:00
|
|
|
}
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
skip_evcb:
|
|
|
|
if (dev && stio->renew_watch && stio_dev_watch (dev, STIO_DEV_WATCH_RENEW, 0) <= -1)
|
|
|
|
{
|
|
|
|
stio_dev_halt (dev);
|
|
|
|
dev = STIO_NULL;
|
|
|
|
}
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 13:25:59 +00:00
|
|
|
static STIO_INLINE int __exec (stio_t* stio)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2016-01-30 05:24:23 +00:00
|
|
|
stio_ntime_t tmout;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
ULONG nentries, i;
|
|
|
|
#else
|
|
|
|
int nentries, i;
|
|
|
|
#endif
|
|
|
|
|
2016-04-17 17:00:58 +00:00
|
|
|
/*if (!stio->actdev.head) return 0;*/
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-01-30 19:08:28 +00:00
|
|
|
/* execute the scheduled jobs before checking devices with the
|
|
|
|
* multiplexer. the scheduled jobs can safely destroy the devices */
|
|
|
|
stio_firetmrjobs (stio, STIO_NULL, STIO_NULL);
|
|
|
|
|
2016-01-30 05:24:23 +00:00
|
|
|
if (stio_gettmrtmout (stio, STIO_NULL, &tmout) <= -1)
|
|
|
|
{
|
|
|
|
/* defaults to 1 second if timeout can't be acquired */
|
2016-01-30 19:08:28 +00:00
|
|
|
tmout.sec = 1; /* TODO: make the default timeout configurable */
|
2016-01-30 05:24:23 +00:00
|
|
|
tmout.nsec = 0;
|
|
|
|
}
|
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
/*
|
|
|
|
if (GetQueuedCompletionStatusEx (stio->iocp, stio->ovls, STIO_COUNTOF(stio->ovls), &nentries, timeout, FALSE) == FALSE)
|
|
|
|
{
|
|
|
|
// TODO: set errnum
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nentries; i++)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#else
|
2016-01-30 05:24:23 +00:00
|
|
|
nentries = epoll_wait (stio->mux, stio->revs, STIO_COUNTOF(stio->revs), STIO_SECNSEC_TO_MSEC(tmout.sec, tmout.nsec));
|
2016-01-26 16:02:20 +00:00
|
|
|
if (nentries <= -1)
|
|
|
|
{
|
2016-01-30 05:24:23 +00:00
|
|
|
if (errno == EINTR) return 0; /* it's actually ok */
|
|
|
|
/* other errors are critical - EBADF, EFAULT, EINVAL */
|
2016-01-31 02:05:39 +00:00
|
|
|
stio->errnum = stio_syserrtoerrnum(errno);
|
2016-01-26 16:02:20 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-01-30 05:24:23 +00:00
|
|
|
/* TODO: merge events??? for the same descriptor */
|
2016-02-04 15:06:20 +00:00
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
for (i = 0; i < nentries; i++)
|
|
|
|
{
|
2016-02-04 15:06:20 +00:00
|
|
|
handle_event (stio, i);
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
/* kill all halted devices */
|
2016-04-17 17:00:58 +00:00
|
|
|
while (stio->hltdev.head)
|
2016-04-16 16:05:57 +00:00
|
|
|
{
|
2016-04-17 17:00:58 +00:00
|
|
|
printf (">>>>>>>>>>>>>> KILLING HALTED DEVICE %p\n", stio->hltdev.head);
|
|
|
|
stio_killdev (stio, stio->hltdev.head);
|
2016-04-16 16:05:57 +00:00
|
|
|
}
|
2016-04-17 17:00:58 +00:00
|
|
|
STIO_ASSERT (stio->hltdev.tail == STIO_NULL);
|
2016-02-02 07:51:17 +00:00
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-05 13:25:59 +00:00
|
|
|
int stio_exec (stio_t* stio)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
stio->in_exec = 1;
|
|
|
|
n = __exec (stio);
|
|
|
|
stio->in_exec = 0;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
void stio_stop (stio_t* stio)
|
|
|
|
{
|
|
|
|
stio->stopreq = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int stio_loop (stio_t* stio)
|
|
|
|
{
|
2016-04-17 17:00:58 +00:00
|
|
|
if (!stio->actdev.head) return 0;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
|
|
|
stio->stopreq = 0;
|
2016-02-04 15:06:20 +00:00
|
|
|
stio->renew_watch = 0;
|
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
if (stio_prologue (stio) <= -1) return -1;
|
|
|
|
|
2016-04-17 17:00:58 +00:00
|
|
|
while (!stio->stopreq && stio->actdev.head)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
|
|
|
if (stio_exec (stio) <= -1) break;
|
|
|
|
/* you can do other things here */
|
|
|
|
}
|
|
|
|
|
|
|
|
stio_epilogue (stio);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
stio_dev_t* stio_makedev (stio_t* stio, stio_size_t dev_size, stio_dev_mth_t* dev_mth, stio_dev_evcb_t* dev_evcb, void* make_ctx)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2016-02-04 15:06:20 +00:00
|
|
|
stio_dev_t* dev;
|
|
|
|
|
|
|
|
if (dev_size < STIO_SIZEOF(stio_dev_t))
|
|
|
|
{
|
|
|
|
stio->errnum = STIO_EINVAL;
|
|
|
|
return STIO_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev = STIO_MMGR_ALLOC (stio->mmgr, dev_size);
|
|
|
|
if (!dev)
|
|
|
|
{
|
|
|
|
stio->errnum = STIO_ENOMEM;
|
|
|
|
return STIO_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
STIO_MEMSET (dev, 0, dev_size);
|
|
|
|
dev->stio = stio;
|
|
|
|
dev->dev_size = dev_size;
|
|
|
|
/* default capability. dev->dev_mth->make() can change this.
|
|
|
|
* stio_dev_watch() is affected by the capability change. */
|
|
|
|
dev->dev_capa = STIO_DEV_CAPA_IN | STIO_DEV_CAPA_OUT;
|
|
|
|
dev->dev_mth = dev_mth;
|
|
|
|
dev->dev_evcb = dev_evcb;
|
|
|
|
STIO_WQ_INIT(&dev->wq);
|
|
|
|
|
|
|
|
/* call the callback function first */
|
|
|
|
stio->errnum = STIO_ENOERR;
|
|
|
|
if (dev->dev_mth->make (dev, make_ctx) <= -1)
|
|
|
|
{
|
|
|
|
if (stio->errnum == STIO_ENOERR) stio->errnum = STIO_EDEVMAKE;
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2016-04-02 15:25:35 +00:00
|
|
|
/* the make callback must not change these fields */
|
|
|
|
STIO_ASSERT (dev->dev_mth == dev_mth);
|
|
|
|
STIO_ASSERT (dev->dev_evcb == dev_evcb);
|
|
|
|
STIO_ASSERT (dev->dev_prev == STIO_NULL);
|
|
|
|
STIO_ASSERT (dev->dev_next == STIO_NULL);
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
/* set some internal capability bits according to the capabilities
|
|
|
|
* removed by the device making callback for convenience sake. */
|
|
|
|
if (!(dev->dev_capa & STIO_DEV_CAPA_IN)) dev->dev_capa |= STIO_DEV_CAPA_IN_CLOSED;
|
|
|
|
if (!(dev->dev_capa & STIO_DEV_CAPA_OUT)) dev->dev_capa |= STIO_DEV_CAPA_OUT_CLOSED;
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
if (CreateIoCompletionPort ((HANDLE)dev->dev_mth->getsyshnd(dev), stio->iocp, STIO_IOCP_KEY, 0) == NULL)
|
|
|
|
{
|
|
|
|
/* TODO: set errnum from GetLastError()... */
|
|
|
|
goto oops_after_make;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (stio_dev_watch (dev, STIO_DEV_WATCH_START, 0) <= -1) goto oops_after_make;
|
|
|
|
#endif
|
|
|
|
|
2016-04-17 17:00:58 +00:00
|
|
|
/* and place the new device object at the back of the active device list */
|
|
|
|
APPEND_DEVICE_TO_LIST (&stio->actdev, dev);
|
|
|
|
dev->dev_capa |= STIO_DEV_CAPA_ACTIVE;
|
2016-02-04 15:06:20 +00:00
|
|
|
|
|
|
|
return dev;
|
|
|
|
|
|
|
|
oops_after_make:
|
2016-04-16 16:05:57 +00:00
|
|
|
if (kill_and_free_device (dev, 0) <= -1)
|
|
|
|
{
|
|
|
|
/* schedule a timer job that reattempts to destroy the device */
|
|
|
|
if (schedule_kill_zombie_job (dev) <= -1)
|
|
|
|
{
|
|
|
|
/* job scheduling failed. i have no choice but to
|
|
|
|
* destroy the device now.
|
|
|
|
*
|
|
|
|
* NOTE: this while loop can block the process
|
|
|
|
* if the kill method keep returning failure */
|
|
|
|
while (kill_and_free_device (dev, 1) <= -1)
|
|
|
|
{
|
|
|
|
if (stio->stopreq)
|
|
|
|
{
|
|
|
|
/* i can't wait until destruction attempt gets
|
|
|
|
* fully successful. there is a chance that some
|
|
|
|
* resources can leak inside the device */
|
|
|
|
kill_and_free_device (dev, 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return STIO_NULL;
|
|
|
|
}
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
oops:
|
|
|
|
STIO_MMGR_FREE (stio->mmgr, dev);
|
|
|
|
return STIO_NULL;
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-16 16:05:57 +00:00
|
|
|
static int kill_and_free_device (stio_dev_t* dev, int force)
|
|
|
|
{
|
|
|
|
stio_t* stio;
|
|
|
|
|
2016-04-17 17:00:58 +00:00
|
|
|
STIO_ASSERT (!(dev->dev_capa & STIO_DEV_CAPA_ACTIVE));
|
|
|
|
STIO_ASSERT (!(dev->dev_capa & STIO_DEV_CAPA_HALTED));
|
|
|
|
|
2016-04-16 16:05:57 +00:00
|
|
|
stio = dev->stio;
|
|
|
|
|
|
|
|
if (dev->dev_mth->kill(dev, force) <= -1)
|
|
|
|
{
|
|
|
|
if (force >= 2) goto free_device;
|
|
|
|
|
|
|
|
if (!(dev->dev_capa & STIO_DEV_CAPA_ZOMBIE))
|
|
|
|
{
|
2016-04-17 17:00:58 +00:00
|
|
|
APPEND_DEVICE_TO_LIST (&stio->zmbdev, dev);
|
2016-04-16 16:05:57 +00:00
|
|
|
dev->dev_capa |= STIO_DEV_CAPA_ZOMBIE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free_device:
|
|
|
|
if (dev->dev_capa & STIO_DEV_CAPA_ZOMBIE)
|
|
|
|
{
|
|
|
|
/* detach it from the zombie device list */
|
2016-04-17 17:00:58 +00:00
|
|
|
UNLINK_DEVICE_FROM_LIST (&stio->zmbdev, dev);
|
|
|
|
dev->dev_capa &= ~STIO_DEV_CAPA_ZOMBIE;
|
2016-04-16 16:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
STIO_MMGR_FREE (stio->mmgr, dev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kill_zombie_job_handler (stio_t* stio, const stio_ntime_t* now, stio_tmrjob_t* job)
|
|
|
|
{
|
|
|
|
stio_dev_t* dev = (stio_dev_t*)job->ctx;
|
|
|
|
|
|
|
|
STIO_ASSERT (dev->dev_capa & STIO_DEV_CAPA_ZOMBIE);
|
|
|
|
|
|
|
|
if (kill_and_free_device (dev, 0) <= -1)
|
|
|
|
{
|
|
|
|
if (schedule_kill_zombie_job (dev) <= -1)
|
|
|
|
{
|
|
|
|
/* i have to choice but to free up the devide by force */
|
|
|
|
while (kill_and_free_device (dev, 1) <= -1)
|
|
|
|
{
|
|
|
|
if (stio->stopreq)
|
|
|
|
{
|
|
|
|
/* i can't wait until destruction attempt gets
|
|
|
|
* fully successful. there is a chance that some
|
|
|
|
* resources can leak inside the device */
|
|
|
|
kill_and_free_device (dev, 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int schedule_kill_zombie_job (stio_dev_t* dev)
|
|
|
|
{
|
|
|
|
stio_tmrjob_t kill_zombie_job;
|
|
|
|
stio_ntime_t tmout;
|
|
|
|
|
|
|
|
stio_inittime (&tmout, 3, 0); /* TODO: take it from configuration */
|
|
|
|
|
|
|
|
STIO_MEMSET (&kill_zombie_job, 0, STIO_SIZEOF(kill_zombie_job));
|
|
|
|
kill_zombie_job.ctx = dev;
|
|
|
|
stio_gettime (&kill_zombie_job.when);
|
|
|
|
stio_addtime (&kill_zombie_job.when, &tmout, &kill_zombie_job.when);
|
|
|
|
kill_zombie_job.handler = kill_zombie_job_handler;
|
|
|
|
/*kill_zombie_job.idxptr = &rdev->tmridx_kill_zombie;*/
|
|
|
|
|
|
|
|
return stio_instmrjob (dev->stio, &kill_zombie_job) == STIO_TMRIDX_INVALID? -1: 0;
|
|
|
|
}
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
void stio_killdev (stio_t* stio, stio_dev_t* dev)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2016-02-04 15:06:20 +00:00
|
|
|
STIO_ASSERT (stio == dev->stio);
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-04-16 16:05:57 +00:00
|
|
|
if (dev->dev_capa & STIO_DEV_CAPA_ZOMBIE)
|
2016-04-12 13:56:59 +00:00
|
|
|
{
|
|
|
|
STIO_ASSERT (STIO_WQ_ISEMPTY(&dev->wq));
|
|
|
|
goto kill_device;
|
|
|
|
}
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
/* clear pending send requests */
|
|
|
|
while (!STIO_WQ_ISEMPTY(&dev->wq))
|
|
|
|
{
|
2016-02-05 13:25:59 +00:00
|
|
|
stio_wq_t* q;
|
|
|
|
q = STIO_WQ_HEAD(&dev->wq);
|
|
|
|
unlink_wq (stio, q);
|
|
|
|
STIO_MMGR_FREE (stio->mmgr, q);
|
2016-02-04 15:06:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 13:56:59 +00:00
|
|
|
if (dev->dev_capa & STIO_DEV_CAPA_HALTED)
|
|
|
|
{
|
2016-04-16 16:05:57 +00:00
|
|
|
/* this device is in the halted state.
|
|
|
|
* unlink it from the halted device list */
|
2016-04-17 17:00:58 +00:00
|
|
|
UNLINK_DEVICE_FROM_LIST (&stio->hltdev, dev);
|
|
|
|
dev->dev_capa &= ~STIO_DEV_CAPA_HALTED;
|
2016-04-12 13:56:59 +00:00
|
|
|
}
|
|
|
|
else
|
2016-02-02 07:51:17 +00:00
|
|
|
{
|
2016-04-17 17:00:58 +00:00
|
|
|
STIO_ASSERT (dev->dev_capa & STIO_DEV_CAPA_ACTIVE);
|
|
|
|
UNLINK_DEVICE_FROM_LIST (&stio->actdev, dev);
|
|
|
|
dev->dev_capa &= ~STIO_DEV_CAPA_ACTIVE;
|
2016-02-02 07:51:17 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
stio_dev_watch (dev, STIO_DEV_WATCH_STOP, 0);
|
|
|
|
|
2016-04-12 13:56:59 +00:00
|
|
|
kill_device:
|
2016-04-16 16:05:57 +00:00
|
|
|
if (kill_and_free_device(dev, 0) <= -1)
|
2016-04-12 13:56:59 +00:00
|
|
|
{
|
2016-04-16 16:05:57 +00:00
|
|
|
STIO_ASSERT (dev->dev_capa & STIO_DEV_CAPA_ZOMBIE);
|
|
|
|
if (schedule_kill_zombie_job (dev) <= -1)
|
2016-04-12 13:56:59 +00:00
|
|
|
{
|
2016-04-16 16:05:57 +00:00
|
|
|
/* i have to choice but to free up the devide by force */
|
|
|
|
while (kill_and_free_device (dev, 1) <= -1)
|
|
|
|
{
|
|
|
|
if (stio->stopreq)
|
|
|
|
{
|
|
|
|
/* i can't wait until destruction attempt gets
|
|
|
|
* fully successful. there is a chance that some
|
|
|
|
* resources can leak inside the device */
|
|
|
|
kill_and_free_device (dev, 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-04-12 13:56:59 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-04 15:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void stio_dev_halt (stio_dev_t* dev)
|
|
|
|
{
|
2016-04-17 17:00:58 +00:00
|
|
|
if (dev->dev_capa & STIO_DEV_CAPA_ACTIVE)
|
2016-02-02 07:51:17 +00:00
|
|
|
{
|
2016-02-04 15:06:20 +00:00
|
|
|
stio_t* stio;
|
|
|
|
|
|
|
|
stio = dev->stio;
|
|
|
|
|
2016-04-17 17:00:58 +00:00
|
|
|
/* delink the device object from the active device list */
|
|
|
|
UNLINK_DEVICE_FROM_LIST (&stio->actdev, dev);
|
|
|
|
dev->dev_capa &= ~STIO_DEV_CAPA_ACTIVE;
|
2016-02-04 15:06:20 +00:00
|
|
|
|
2016-04-12 13:56:59 +00:00
|
|
|
/* place it at the back of the halted device list */
|
2016-04-17 17:00:58 +00:00
|
|
|
APPEND_DEVICE_TO_LIST (&stio->hltdev, dev);
|
2016-02-04 15:06:20 +00:00
|
|
|
dev->dev_capa |= STIO_DEV_CAPA_HALTED;
|
2016-02-02 07:51:17 +00:00
|
|
|
}
|
2016-02-04 15:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int stio_dev_ioctl (stio_dev_t* dev, int cmd, void* arg)
|
|
|
|
{
|
|
|
|
if (dev->dev_mth->ioctl) return dev->dev_mth->ioctl (dev, cmd, arg);
|
|
|
|
dev->stio->errnum = STIO_ENOSUP; /* TODO: different error code ? */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int stio_dev_watch (stio_dev_t* dev, stio_dev_watch_cmd_t cmd, int events)
|
|
|
|
{
|
|
|
|
struct epoll_event ev;
|
|
|
|
int epoll_op;
|
|
|
|
int dev_capa;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-04-12 13:56:59 +00:00
|
|
|
/* the virtual device doesn't perform actual I/O.
|
|
|
|
* it's different from not hanving STIO_DEV_CAPA_IN and STIO_DEV_CAPA_OUT.
|
|
|
|
* a non-virtual device without the capabilities still gets attention
|
|
|
|
* of the system multiplexer for hangup and error. */
|
|
|
|
if (dev->dev_capa & STIO_DEV_CAPA_VIRTUAL) return 0;
|
|
|
|
|
2016-02-02 07:51:17 +00:00
|
|
|
ev.data.ptr = dev;
|
2016-01-26 16:02:20 +00:00
|
|
|
switch (cmd)
|
|
|
|
{
|
2016-02-02 07:51:17 +00:00
|
|
|
case STIO_DEV_WATCH_START:
|
2016-02-04 15:06:20 +00:00
|
|
|
/* upon start, only input watching is requested */
|
|
|
|
events = STIO_DEV_EVENT_IN;
|
2016-01-26 16:02:20 +00:00
|
|
|
epoll_op = EPOLL_CTL_ADD;
|
|
|
|
break;
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
case STIO_DEV_WATCH_RENEW:
|
|
|
|
/* auto-renwal mode. input watching is requested all the time.
|
|
|
|
* output watching is requested only if there're enqueued
|
|
|
|
* data for writing. */
|
|
|
|
events = STIO_DEV_EVENT_IN;
|
|
|
|
if (!STIO_WQ_ISEMPTY(&dev->wq)) events |= STIO_DEV_EVENT_OUT;
|
2016-04-17 17:00:58 +00:00
|
|
|
/* fall through */
|
2016-02-02 07:51:17 +00:00
|
|
|
case STIO_DEV_WATCH_UPDATE:
|
2016-02-04 15:06:20 +00:00
|
|
|
/* honor event watching requests as given by the caller */
|
2016-01-26 16:02:20 +00:00
|
|
|
epoll_op = EPOLL_CTL_MOD;
|
|
|
|
break;
|
|
|
|
|
2016-02-02 07:51:17 +00:00
|
|
|
case STIO_DEV_WATCH_STOP:
|
2016-02-04 15:06:20 +00:00
|
|
|
events = 0; /* override events */
|
2016-01-30 05:24:23 +00:00
|
|
|
epoll_op = EPOLL_CTL_DEL;
|
2016-01-26 16:02:20 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
dev->stio->errnum = STIO_EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
dev_capa = dev->dev_capa;
|
|
|
|
dev_capa &= ~(DEV_CAPA_ALL_WATCHED);
|
|
|
|
|
|
|
|
/* this function honors STIO_DEV_EVENT_IN and STIO_DEV_EVENT_OUT only
|
|
|
|
* as valid input event bits. it intends to provide simple abstraction
|
|
|
|
* by reducing the variety of event bits that the caller has to handle. */
|
|
|
|
ev.events = EPOLLHUP | EPOLLERR /*| EPOLLET*/;
|
|
|
|
|
|
|
|
if ((events & STIO_DEV_EVENT_IN) && !(dev->dev_capa & (STIO_DEV_CAPA_IN_CLOSED | STIO_DEV_CAPA_IN_DISABLED)))
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2016-02-04 15:06:20 +00:00
|
|
|
if (dev->dev_capa & STIO_DEV_CAPA_IN)
|
|
|
|
{
|
|
|
|
ev.events |= EPOLLIN;
|
|
|
|
#if defined(EPOLLRDHUP)
|
|
|
|
ev.events |= EPOLLRDHUP;
|
|
|
|
#endif
|
|
|
|
if (dev->dev_capa & STIO_DEV_CAPA_PRI)
|
|
|
|
{
|
|
|
|
ev.events |= EPOLLPRI;
|
|
|
|
dev_capa |= STIO_DEV_CAPA_PRI_WATCHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_capa |= STIO_DEV_CAPA_IN_WATCHED;
|
|
|
|
}
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
if ((events & STIO_DEV_EVENT_OUT) && !(dev->dev_capa & STIO_DEV_CAPA_OUT_CLOSED))
|
|
|
|
{
|
|
|
|
if (dev->dev_capa & STIO_DEV_CAPA_OUT)
|
|
|
|
{
|
|
|
|
ev.events |= EPOLLOUT;
|
|
|
|
dev_capa |= STIO_DEV_CAPA_OUT_WATCHED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (epoll_op == EPOLL_CTL_MOD && (dev_capa & DEV_CAPA_ALL_WATCHED) == (dev->dev_capa & DEV_CAPA_ALL_WATCHED))
|
|
|
|
{
|
2016-04-18 14:21:23 +00:00
|
|
|
/* no change in the device capacity. skip calling epoll_ctl */
|
2016-02-04 15:06:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (epoll_ctl (dev->stio->mux, epoll_op, dev->dev_mth->getsyshnd(dev), &ev) == -1)
|
|
|
|
{
|
|
|
|
dev->stio->errnum = stio_syserrtoerrnum(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->dev_capa = dev_capa;
|
2016-01-26 16:02:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-01-28 16:44:47 +00:00
|
|
|
|
2016-02-05 13:25:59 +00:00
|
|
|
int stio_dev_read (stio_dev_t* dev, int enabled)
|
2016-02-04 15:06:20 +00:00
|
|
|
{
|
2016-02-05 13:25:59 +00:00
|
|
|
if (dev->dev_capa & STIO_DEV_CAPA_IN_CLOSED)
|
|
|
|
{
|
|
|
|
dev->stio->errnum = STIO_ENOCAPA;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
if (enabled)
|
2016-02-05 13:25:59 +00:00
|
|
|
{
|
2016-02-04 15:06:20 +00:00
|
|
|
dev->dev_capa &= ~STIO_DEV_CAPA_IN_DISABLED;
|
2016-02-05 13:25:59 +00:00
|
|
|
if (!dev->stio->in_exec && (dev->dev_capa & STIO_DEV_CAPA_IN_WATCHED)) goto renew_watch_now;
|
|
|
|
}
|
2016-02-04 15:06:20 +00:00
|
|
|
else
|
2016-02-05 13:25:59 +00:00
|
|
|
{
|
2016-02-04 15:06:20 +00:00
|
|
|
dev->dev_capa |= STIO_DEV_CAPA_IN_DISABLED;
|
2016-02-05 13:25:59 +00:00
|
|
|
if (!dev->stio->in_exec && !(dev->dev_capa & STIO_DEV_CAPA_IN_WATCHED)) goto renew_watch_now;
|
|
|
|
}
|
2016-02-04 15:06:20 +00:00
|
|
|
|
|
|
|
dev->stio->renew_watch = 1;
|
2016-02-05 13:25:59 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
renew_watch_now:
|
|
|
|
if (stio_dev_watch (dev, STIO_DEV_WATCH_RENEW, 0) <= -1) return -1;
|
|
|
|
return 0;
|
2016-02-04 15:06:20 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 13:25:59 +00:00
|
|
|
static void on_write_timeout (stio_t* stio, const stio_ntime_t* now, stio_tmrjob_t* job)
|
|
|
|
{
|
|
|
|
stio_wq_t* q;
|
|
|
|
stio_dev_t* dev;
|
|
|
|
int x;
|
|
|
|
|
|
|
|
q = (stio_wq_t*)job->ctx;
|
|
|
|
dev = q->dev;
|
|
|
|
|
|
|
|
dev->stio->errnum = STIO_ETMOUT;
|
2016-04-18 14:21:23 +00:00
|
|
|
x = dev->dev_evcb->on_write (dev, -1, q->ctx, &q->dstaddr);
|
2016-02-05 13:25:59 +00:00
|
|
|
|
|
|
|
STIO_ASSERT (q->tmridx == STIO_TMRIDX_INVALID);
|
|
|
|
STIO_WQ_UNLINK(q);
|
|
|
|
STIO_MMGR_FREE (stio->mmgr, q);
|
|
|
|
|
|
|
|
if (x <= -1) stio_dev_halt (dev);
|
|
|
|
}
|
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
static int __dev_write (stio_dev_t* dev, const void* data, stio_iolen_t len, const stio_ntime_t* tmout, void* wrctx, const stio_devaddr_t* dstaddr)
|
2016-01-28 16:44:47 +00:00
|
|
|
{
|
|
|
|
const stio_uint8_t* uptr;
|
2016-04-02 15:25:35 +00:00
|
|
|
stio_iolen_t urem, ulen;
|
2016-02-02 07:51:17 +00:00
|
|
|
stio_wq_t* q;
|
2016-02-04 15:06:20 +00:00
|
|
|
int x;
|
2016-02-02 07:51:17 +00:00
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
if (dev->dev_capa & STIO_DEV_CAPA_OUT_CLOSED)
|
2016-02-02 07:51:17 +00:00
|
|
|
{
|
|
|
|
dev->stio->errnum = STIO_ENOCAPA;
|
|
|
|
return -1;
|
|
|
|
}
|
2016-01-28 16:44:47 +00:00
|
|
|
|
|
|
|
uptr = data;
|
|
|
|
urem = len;
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
if (!STIO_WQ_ISEMPTY(&dev->wq))
|
|
|
|
{
|
|
|
|
/* the writing queue is not empty.
|
|
|
|
* enqueue this request immediately */
|
|
|
|
goto enqueue_data;
|
|
|
|
}
|
2016-02-02 07:51:17 +00:00
|
|
|
|
2016-02-05 13:25:59 +00:00
|
|
|
if (dev->dev_capa & STIO_DEV_CAPA_STREAM)
|
2016-01-28 16:44:47 +00:00
|
|
|
{
|
2016-02-05 13:25:59 +00:00
|
|
|
/* use the do..while() loop to be able to send a zero-length data */
|
|
|
|
do
|
2016-02-04 15:06:20 +00:00
|
|
|
{
|
2016-02-05 13:25:59 +00:00
|
|
|
ulen = urem;
|
2016-04-18 14:21:23 +00:00
|
|
|
x = dev->dev_mth->write (dev, data, &ulen, dstaddr);
|
2016-02-05 13:25:59 +00:00
|
|
|
if (x <= -1) return -1;
|
|
|
|
else if (x == 0)
|
|
|
|
{
|
|
|
|
/* [NOTE]
|
|
|
|
* the write queue is empty at this moment. a zero-length
|
|
|
|
* request for a stream device can still get enqueued if the
|
|
|
|
* write callback returns 0 though i can't figure out if there
|
|
|
|
* is a compelling reason to do so
|
|
|
|
*/
|
|
|
|
goto enqueue_data; /* enqueue remaining data */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
urem -= ulen;
|
|
|
|
uptr += ulen;
|
|
|
|
}
|
2016-02-04 15:06:20 +00:00
|
|
|
}
|
2016-02-05 13:25:59 +00:00
|
|
|
while (urem > 0);
|
|
|
|
|
|
|
|
if (len <= 0) /* original length */
|
2016-01-28 16:44:47 +00:00
|
|
|
{
|
2016-02-05 13:25:59 +00:00
|
|
|
/* a zero-length writing request is to close the writing end */
|
|
|
|
dev->dev_capa |= STIO_DEV_CAPA_OUT_CLOSED;
|
2016-01-28 16:44:47 +00:00
|
|
|
}
|
2016-02-04 15:06:20 +00:00
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
if (dev->dev_evcb->on_write (dev, len, wrctx, dstaddr) <= -1) return -1;
|
2016-02-05 13:25:59 +00:00
|
|
|
}
|
|
|
|
else
|
2016-02-04 15:06:20 +00:00
|
|
|
{
|
2016-02-05 13:25:59 +00:00
|
|
|
ulen = urem;
|
2016-04-02 15:25:35 +00:00
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
x = dev->dev_mth->write (dev, data, &ulen, dstaddr);
|
2016-02-05 13:25:59 +00:00
|
|
|
if (x <= -1) return -1;
|
|
|
|
else if (x == 0) goto enqueue_data;
|
|
|
|
|
|
|
|
/* partial writing is still considered ok for a non-stream device */
|
2016-04-18 14:21:23 +00:00
|
|
|
if (dev->dev_evcb->on_write (dev, ulen, wrctx, dstaddr) <= -1) return -1;
|
2016-02-04 15:06:20 +00:00
|
|
|
}
|
2016-01-28 16:44:47 +00:00
|
|
|
|
2016-02-05 13:25:59 +00:00
|
|
|
return 1; /* written immediately and called on_write callback */
|
2016-01-28 16:44:47 +00:00
|
|
|
|
2016-02-02 07:51:17 +00:00
|
|
|
enqueue_data:
|
2016-03-29 15:02:01 +00:00
|
|
|
if (!(dev->dev_capa & STIO_DEV_CAPA_OUT_QUEUED))
|
|
|
|
{
|
|
|
|
/* writing queuing is not requested. so return failure */
|
2016-04-18 14:21:23 +00:00
|
|
|
dev->stio->errnum = STIO_ENOCAPA;
|
2016-03-29 15:02:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-02-02 07:51:17 +00:00
|
|
|
/* queue the remaining data*/
|
2016-04-18 14:21:23 +00:00
|
|
|
q = (stio_wq_t*)STIO_MMGR_ALLOC (dev->stio->mmgr, STIO_SIZEOF(*q) + (dstaddr? dstaddr->len: 0) + urem);
|
2016-02-02 07:51:17 +00:00
|
|
|
if (!q)
|
|
|
|
{
|
|
|
|
dev->stio->errnum = STIO_ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
2016-01-28 16:44:47 +00:00
|
|
|
|
2016-02-05 13:25:59 +00:00
|
|
|
q->tmridx = STIO_TMRIDX_INVALID;
|
|
|
|
q->dev = dev;
|
2016-02-02 15:40:09 +00:00
|
|
|
q->ctx = wrctx;
|
2016-03-29 15:02:01 +00:00
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
if (dstaddr)
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
2016-04-18 14:21:23 +00:00
|
|
|
q->dstaddr.ptr = (stio_uint8_t*)(q + 1);
|
|
|
|
q->dstaddr.len = dstaddr->len;
|
|
|
|
STIO_MEMCPY (q->dstaddr.ptr, dstaddr->ptr, dstaddr->len);
|
2016-03-29 15:02:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-18 14:21:23 +00:00
|
|
|
q->dstaddr.len = 0;
|
2016-03-29 15:02:01 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
q->ptr = (stio_uint8_t*)(q + 1) + q->dstaddr.len;
|
2016-02-02 07:51:17 +00:00
|
|
|
q->len = urem;
|
2016-02-05 13:25:59 +00:00
|
|
|
q->olen = len;
|
2016-02-02 07:51:17 +00:00
|
|
|
STIO_MEMCPY (q->ptr, uptr, urem);
|
2016-01-28 16:44:47 +00:00
|
|
|
|
2016-04-20 15:59:11 +00:00
|
|
|
if (tmout && stio_ispostime(tmout))
|
2016-02-05 13:25:59 +00:00
|
|
|
{
|
|
|
|
stio_tmrjob_t tmrjob;
|
2016-02-04 15:06:20 +00:00
|
|
|
|
2016-02-05 13:25:59 +00:00
|
|
|
STIO_MEMSET (&tmrjob, 0, STIO_SIZEOF(tmrjob));
|
|
|
|
tmrjob.ctx = q;
|
|
|
|
stio_gettime (&tmrjob.when);
|
|
|
|
stio_addtime (&tmrjob.when, tmout, &tmrjob.when);
|
|
|
|
tmrjob.handler = on_write_timeout;
|
|
|
|
tmrjob.idxptr = &q->tmridx;
|
|
|
|
|
|
|
|
q->tmridx = stio_instmrjob (dev->stio, &tmrjob);
|
|
|
|
if (q->tmridx == STIO_TMRIDX_INVALID)
|
|
|
|
{
|
|
|
|
STIO_MMGR_FREE (dev->stio->mmgr, q);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STIO_WQ_ENQ (&dev->wq, q);
|
|
|
|
if (!dev->stio->in_exec && !(dev->dev_capa & STIO_DEV_CAPA_OUT_WATCHED))
|
2016-02-02 07:51:17 +00:00
|
|
|
{
|
2016-02-04 15:06:20 +00:00
|
|
|
/* if output is not being watched, arrange to do so */
|
|
|
|
if (stio_dev_watch (dev, STIO_DEV_WATCH_RENEW, 0) <= -1)
|
2016-01-28 16:44:47 +00:00
|
|
|
{
|
2016-02-05 13:25:59 +00:00
|
|
|
unlink_wq (dev->stio, q);
|
2016-02-02 07:51:17 +00:00
|
|
|
STIO_MMGR_FREE (dev->stio->mmgr, q);
|
|
|
|
return -1;
|
2016-01-28 16:44:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-05 13:25:59 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
dev->stio->renew_watch = 1;
|
|
|
|
}
|
2016-01-28 16:44:47 +00:00
|
|
|
|
2016-02-05 13:25:59 +00:00
|
|
|
return 0; /* request pused to a write queue. */
|
|
|
|
}
|
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
int stio_dev_write (stio_dev_t* dev, const void* data, stio_iolen_t len, void* wrctx, const stio_devaddr_t* dstaddr)
|
2016-02-05 13:25:59 +00:00
|
|
|
{
|
2016-04-18 14:21:23 +00:00
|
|
|
return __dev_write (dev, data, len, STIO_NULL, wrctx, dstaddr);
|
2016-02-05 13:25:59 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
int stio_dev_timedwrite (stio_dev_t* dev, const void* data, stio_iolen_t len, const stio_ntime_t* tmout, void* wrctx, const stio_devaddr_t* dstaddr)
|
2016-02-05 13:25:59 +00:00
|
|
|
{
|
2016-04-18 14:21:23 +00:00
|
|
|
return __dev_write (dev, data, len, tmout, wrctx, dstaddr);
|
2016-01-28 16:44:47 +00:00
|
|
|
}
|
2016-01-31 02:05:39 +00:00
|
|
|
|
2016-02-02 15:40:09 +00:00
|
|
|
int stio_makesyshndasync (stio_t* stio, stio_syshnd_t hnd)
|
|
|
|
{
|
|
|
|
#if defined(F_GETFL) && defined(F_SETFL) && defined(O_NONBLOCK)
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
if ((flags = fcntl (hnd, F_GETFL)) <= -1 ||
|
|
|
|
(flags = fcntl (hnd, F_SETFL, flags | O_NONBLOCK)) <= -1)
|
|
|
|
{
|
|
|
|
stio->errnum = stio_syserrtoerrnum (errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
stio->errnum = STIO_ENOSUP;
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-01-31 02:05:39 +00:00
|
|
|
stio_errnum_t stio_syserrtoerrnum (int no)
|
|
|
|
{
|
|
|
|
switch (no)
|
|
|
|
{
|
|
|
|
case ENOMEM:
|
|
|
|
return STIO_ENOMEM;
|
|
|
|
|
|
|
|
case EINVAL:
|
|
|
|
return STIO_EINVAL;
|
|
|
|
|
|
|
|
case ENOENT:
|
|
|
|
return STIO_ENOENT;
|
|
|
|
|
|
|
|
case EMFILE:
|
|
|
|
return STIO_EMFILE;
|
|
|
|
|
|
|
|
#if defined(ENFILE)
|
|
|
|
case ENFILE:
|
|
|
|
return STIO_ENFILE;
|
|
|
|
#endif
|
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
#if defined(EWOULDBLOCK) && defined(EAGAIN) && (EWOULDBLOCK != EAGAIN)
|
2016-04-16 16:05:57 +00:00
|
|
|
case EAGAIN:
|
2016-04-18 14:21:23 +00:00
|
|
|
case EWOULDBLOCK:
|
|
|
|
return STIO_EAGAIN;
|
|
|
|
#elif defined(EAGAIN)
|
|
|
|
case EAGAIN:
|
|
|
|
return STIO_EAGAIN;
|
|
|
|
#elif defined(EWOULDBLOCK)
|
|
|
|
case EWOULDBLOCK:
|
2016-04-16 16:05:57 +00:00
|
|
|
return STIO_EAGAIN;
|
|
|
|
#endif
|
|
|
|
|
2016-02-01 07:42:49 +00:00
|
|
|
#if defined(ECONNREFUSED)
|
|
|
|
case ECONNREFUSED:
|
|
|
|
return STIO_ECONRF;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(ECONNRESETD)
|
|
|
|
case ECONNRESET:
|
|
|
|
return STIO_ECONRS;
|
|
|
|
#endif
|
|
|
|
|
2016-01-31 02:05:39 +00:00
|
|
|
default:
|
|
|
|
return STIO_ESYSERR;
|
|
|
|
}
|
|
|
|
}
|