mirror of
https://github.com/troglobit/libuev.git
synced 2026-11-20 19:52:16 +00:00
Major cleanup, use Linux coding style, remove CamelCase.
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
0bba080660
commit
7e87644bd1
4 files changed
+524
-564
No files matched your search
@@ -34,459 +34,327 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "llist.h"
|
|
||||||
#include "libuev.h"
|
#include "libuev.h"
|
||||||
|
|
||||||
#ifdef DEBUG
|
|
||||||
#define Debug(f, args...) printf(":LUE: " f "\n", ##args)
|
|
||||||
#else
|
|
||||||
#define Debug(f, args...)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static clock_t clock_tick = 0;
|
static clock_t clock_tick = 0;
|
||||||
|
|
||||||
/// Enumerate file handle direction
|
uev_io_t *uev_io_create(uev_t *ctx, int fd, uev_dir_t dir, uev_io_cb_t handler, void *data)
|
||||||
enum LUEFhDirectionT {LUE_FHIN = 0, LUE_FHOUT = 1};
|
|
||||||
|
|
||||||
struct LUEFileEventH;
|
|
||||||
struct LUEFileEventRem
|
|
||||||
{
|
{
|
||||||
LListField(struct LUEFileEventRem);
|
uev_io_t *w;
|
||||||
struct LUEFileEventH *ent;
|
|
||||||
};
|
|
||||||
|
|
||||||
/// File event handle
|
w = (uev_io_t *)malloc(sizeof(*w));
|
||||||
struct LUEFileEventH
|
if (!w)
|
||||||
{
|
return NULL;
|
||||||
LListField(struct LUEFileEventH); ///< Elements for linked list
|
|
||||||
struct LUEFileEventRem rem; ///< For postponed removal
|
|
||||||
|
|
||||||
int fd; ///< File descriptor
|
w->rem.ent = w;
|
||||||
enum LUEFhDirectionT direction; ///< Direction: in or out
|
w->fd = fd;
|
||||||
int pollIx; ///< This file handle index into list of file handles to poll
|
w->dir = dir;
|
||||||
LUEFileHandler handler; ///< File handler callback
|
w->handler = (void *)handler;
|
||||||
void *data; ///< File handler callback parameter
|
w->data = data;
|
||||||
};
|
w->index = -1;
|
||||||
|
|
||||||
/// Timer context object
|
lListAppend(&ctx->io_list, w);
|
||||||
struct LUETimerH
|
|
||||||
{
|
|
||||||
LListField(struct LUETimerH); ///< Linked list elements
|
|
||||||
|
|
||||||
int dueTime; ///< Epoch timestamp
|
return w;
|
||||||
LUETimerHandler handler; ///< Timer callback
|
|
||||||
void *data; ///< Timer callback parameter
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @cond doxygen does not like this construction
|
|
||||||
*/
|
|
||||||
typedef LList(struct LUETimerH) LUETimerHandlerDL;
|
|
||||||
typedef LList(struct LUEFileEventH) LUEFileEventDL;
|
|
||||||
typedef LList(struct LUEFileEventRem) LUEFileEventRemL;
|
|
||||||
/**
|
|
||||||
* @endcond
|
|
||||||
*/
|
|
||||||
|
|
||||||
/// Main lue context object
|
|
||||||
struct LUECtxt
|
|
||||||
{
|
|
||||||
LUEFileEventDL fileHandlers; ///< File handlers
|
|
||||||
LUETimerHandlerDL timeoutHandlers; ///< Timer handlers
|
|
||||||
|
|
||||||
LUEFileEventRemL fileRem; ///< List of file handles to be garbage collected
|
|
||||||
LUETimerHandlerDL timeRem; ///< List of timer handles to be garbage collected
|
|
||||||
|
|
||||||
clock_t baseTime; ///< Time at last timer tick
|
|
||||||
clock_t setTime; ///< Next timer tick epoch
|
|
||||||
int timeSet; ///< True if next timer epoch is set
|
|
||||||
int exiting; ///< True if the application is terminating
|
|
||||||
};
|
|
||||||
|
|
||||||
static
|
|
||||||
struct LUEFileEventH *lueAddIO(struct LUECtxt * ctxt,
|
|
||||||
int fd, LUEFileHandler handler, void *data,
|
|
||||||
enum LUEFhDirectionT direction)
|
|
||||||
{
|
|
||||||
struct LUEFileEventH *ent;
|
|
||||||
|
|
||||||
ent = (struct LUEFileEventH *) malloc(sizeof(*ent));
|
|
||||||
Debug("Add IO %d %p", fd, ent);
|
|
||||||
assert(ent);
|
|
||||||
ent->rem.ent = ent;
|
|
||||||
ent->fd = fd;
|
|
||||||
ent->direction = direction;
|
|
||||||
ent->handler = handler;
|
|
||||||
ent->data = data;
|
|
||||||
ent->pollIx = -1;
|
|
||||||
lListAppend(&ctxt->fileHandlers, ent);
|
|
||||||
|
|
||||||
return ent;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
int uev_io_delete(uev_t *ctx, uev_io_t *w)
|
||||||
* Add an input file handler
|
|
||||||
*/
|
|
||||||
struct LUEFileEventH *lueAddInput(struct LUECtxt *ctxt,
|
|
||||||
int fd, LUEFileHandler handler, void *data)
|
|
||||||
{
|
{
|
||||||
return lueAddIO(ctxt, fd, handler, data, LUE_FHIN);
|
uev_io_rem_t *ent;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
if (!ctx || !w) {
|
||||||
* Add an output file handler
|
errno = EINVAL;
|
||||||
*/
|
return -1;
|
||||||
struct LUEFileEventH *lueAddOutput(struct LUECtxt *ctxt,
|
}
|
||||||
int fd, LUEFileHandler handler, void *data)
|
|
||||||
{
|
|
||||||
return lueAddIO(ctxt, fd, handler, data, LUE_FHOUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
static
|
lListForeachIn(ent, &ctx->io_delist) {
|
||||||
int lueRemIO(struct LUECtxt *ctxt, struct LUEFileEventH *hdl)
|
if (ent->ent == w) {
|
||||||
{
|
errno = EALREADY;
|
||||||
struct LUEFileEventRem *ent;
|
return -1; /* Already removed */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Debug("Rem IO %p", hdl);
|
w->handler = NULL;
|
||||||
lListForeachIn (ent, &ctxt->fileRem)
|
lListAppend(&ctx->io_delist, &w->rem);
|
||||||
if (ent->ent == hdl)
|
|
||||||
return -1; // Already removed
|
|
||||||
|
|
||||||
Debug("Rem FD %d", hdl->fd);
|
return 0;
|
||||||
hdl->handler = NULL;
|
|
||||||
lListAppend(&ctxt->fileRem, &(hdl->rem));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove all handlers for an fd
|
|
||||||
*/
|
|
||||||
int lueRemFd(struct LUECtxt *ctxt, int fd)
|
|
||||||
{
|
|
||||||
struct LUEFileEventH *ent;
|
|
||||||
struct LUEFileEventRem *rem;
|
|
||||||
|
|
||||||
Debug("Remfd FD %d", ent->fd);
|
|
||||||
lListForeachIn(ent, &ctxt->fileHandlers)
|
|
||||||
{
|
|
||||||
if (ent->fd != fd)
|
|
||||||
continue;
|
|
||||||
lListForeachIn(rem, &ctxt->fileRem)
|
|
||||||
if (rem->ent == ent)
|
|
||||||
return -1; // Already removed
|
|
||||||
Debug("Remfd ent %p", ent);
|
|
||||||
ent->handler = NULL;
|
|
||||||
lListAppend(&ctxt->fileRem, &(ent->rem));
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove an input file handler
|
|
||||||
*/
|
|
||||||
int lueRemInput(struct LUECtxt *ctxt, struct LUEFileEventH *hdl)
|
|
||||||
{
|
|
||||||
return lueRemIO(ctxt, hdl);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove an output file handler
|
|
||||||
*/
|
|
||||||
int lueRemOutput(struct LUECtxt *ctxt, struct LUEFileEventH *hdl)
|
|
||||||
{
|
|
||||||
return lueRemIO(ctxt, hdl);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove a timer event
|
|
||||||
*/
|
|
||||||
int lueRemTimer(struct LUECtxt *ctxt, struct LUETimerH *hdl)
|
|
||||||
{
|
|
||||||
if (hdl)
|
|
||||||
{
|
|
||||||
struct LUETimerH *ent;
|
|
||||||
|
|
||||||
lListForeachIn (ent, &ctxt->timeRem)
|
|
||||||
if (ent == hdl)
|
|
||||||
return -1; // Already removed
|
|
||||||
|
|
||||||
Debug("Remtimer %p", hdl);
|
|
||||||
lListRemove(&ctxt->timeoutHandlers, hdl);
|
|
||||||
lListAppend(&ctxt->timeRem, hdl); // Remember to free him (at a safe time)
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a timer event
|
* Add a timer event
|
||||||
*/
|
*/
|
||||||
struct LUETimerH *lueAddTimer(struct LUECtxt * ctxt, int periodMs,
|
uev_timer_t *uev_timer_create(uev_t *ctx, int msec, uev_timer_cb_t handler, void *data)
|
||||||
LUETimerHandler handler, void *data)
|
|
||||||
{
|
{
|
||||||
int timeDiff;
|
int diff;
|
||||||
clock_t curTime;
|
clock_t now;
|
||||||
struct LUETimerH *nent,
|
uev_timer_t *w, *ret, *ent;
|
||||||
*ret,
|
|
||||||
*ent;
|
|
||||||
|
|
||||||
nent = (struct LUETimerH *) malloc(sizeof(*ent));
|
if (!ctx) {
|
||||||
assert(nent);
|
errno = EINVAL;
|
||||||
nent->dueTime = periodMs;
|
return NULL;
|
||||||
nent->handler = handler;
|
}
|
||||||
nent->data = data;
|
|
||||||
|
|
||||||
if (0 == periodMs)
|
w = (uev_timer_t *)malloc(sizeof(*ent));
|
||||||
{
|
if (!w)
|
||||||
// Zero delay is a special case: A oneshot workproc: Must go first
|
return NULL;
|
||||||
lListInsertFirst(&ctxt->timeoutHandlers, nent);
|
|
||||||
return nent;
|
|
||||||
|
|
||||||
}
|
w->due = msec;
|
||||||
|
w->handler = (void *)handler;
|
||||||
|
w->data = data;
|
||||||
|
|
||||||
// Adjust timers.
|
if (0 == msec) {
|
||||||
// Jiffie wraparound is OK since we only care about diff time
|
/* Zero delay is a special case: A oneshot workproc: Must go first */
|
||||||
curTime = ctxt->timeSet ? ctxt->setTime : times(NULL);
|
lListInsertFirst(&ctx->timer_list, w);
|
||||||
timeDiff = (curTime - ctxt->baseTime) / clock_tick * 1000
|
|
||||||
+ ((curTime - ctxt->baseTime) % clock_tick) * 1000 / clock_tick;
|
|
||||||
|
|
||||||
ret = nent;
|
return w;
|
||||||
lListForeachIn(ent, &ctxt->timeoutHandlers)
|
}
|
||||||
{
|
|
||||||
if (ent->dueTime > timeDiff)
|
|
||||||
ent->dueTime -= timeDiff;
|
|
||||||
else
|
|
||||||
ent->dueTime = 0;
|
|
||||||
if (nent && ent->dueTime > periodMs)
|
|
||||||
{
|
|
||||||
Debug("Timer Insert %d before %d %p", periodMs, ent->dueTime, nent);
|
|
||||||
lListInsert(&ctxt->timeoutHandlers, nent, ent);
|
|
||||||
nent = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (nent)
|
|
||||||
{
|
|
||||||
Debug("Timer Append %d at end %p", periodMs, nent);
|
|
||||||
lListAppend(&ctxt->timeoutHandlers, nent);
|
|
||||||
}
|
|
||||||
ctxt->baseTime = curTime;
|
|
||||||
|
|
||||||
return ret;
|
/* Adjust timers.
|
||||||
|
* Jiffie wraparound is OK since we only care about diff time */
|
||||||
|
now = ctx->use_next ? ctx->next_time : times(NULL);
|
||||||
|
diff = (now - ctx->base_time) / clock_tick * 1000 +
|
||||||
|
((now - ctx->base_time) % clock_tick) * 1000 / clock_tick;
|
||||||
|
|
||||||
|
ret = w;
|
||||||
|
lListForeachIn(ent, &ctx->timer_list) {
|
||||||
|
if (ent->due > diff)
|
||||||
|
ent->due -= diff;
|
||||||
|
else
|
||||||
|
ent->due = 0;
|
||||||
|
|
||||||
|
if (w && ent->due > msec) {
|
||||||
|
lListInsert(&ctx->timer_list, w, ent);
|
||||||
|
w = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (w)
|
||||||
|
lListAppend(&ctx->timer_list, w);
|
||||||
|
|
||||||
|
ctx->base_time = now;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a timer event
|
||||||
|
*/
|
||||||
|
int uev_timer_delete(uev_t *ctx, uev_timer_t *w)
|
||||||
|
{
|
||||||
|
uev_timer_t *ent;
|
||||||
|
|
||||||
|
if (!ctx || !w) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
lListForeachIn(ent, &ctx->timer_delist) {
|
||||||
|
if (ent == w) {
|
||||||
|
errno = EALREADY;
|
||||||
|
return -1; /* Already removed */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lListRemove(&ctx->timer_list, w);
|
||||||
|
lListAppend(&ctx->timer_delist, w); /* Remember to free it (at a safe time) */
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an application context
|
* Create an application context
|
||||||
*/
|
*/
|
||||||
struct LUECtxt *lueCtxtCreate()
|
uev_t *uev_create(void)
|
||||||
{
|
{
|
||||||
struct LUECtxt *lue;
|
uev_t *ctx;
|
||||||
|
|
||||||
lue = (struct LUECtxt *) malloc(sizeof(*lue));
|
ctx = (uev_t *)malloc(sizeof(*ctx));
|
||||||
assert(lue);
|
if (!ctx)
|
||||||
lListInit(&lue->fileHandlers);
|
return NULL;
|
||||||
lListInit(&lue->timeoutHandlers);
|
|
||||||
lListInit(&lue->fileRem);
|
|
||||||
lListInit(&lue->timeRem);
|
|
||||||
|
|
||||||
lue->baseTime = times(NULL);
|
lListInit(&ctx->io_list);
|
||||||
lue->setTime = 0;
|
lListInit(&ctx->timer_list);
|
||||||
lue->timeSet = false;
|
lListInit(&ctx->io_delist);
|
||||||
lue->exiting = false;
|
lListInit(&ctx->timer_delist);
|
||||||
|
|
||||||
if (0 == clock_tick)
|
ctx->base_time = times(NULL);
|
||||||
clock_tick = sysconf(_SC_CLK_TCK);
|
ctx->next_time = 0;
|
||||||
|
ctx->use_next = false;
|
||||||
return lue;
|
|
||||||
|
ctx->exiting = false;
|
||||||
|
|
||||||
|
if (0 == clock_tick)
|
||||||
|
clock_tick = sysconf(_SC_CLK_TCK);
|
||||||
|
|
||||||
|
return ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destroy an application context
|
* Destroy an application context
|
||||||
*/
|
*/
|
||||||
void lueCtxtDestroy(struct LUECtxt *lue)
|
void uev_delete(uev_t *ctx)
|
||||||
{
|
{
|
||||||
lListPurge(&lue->fileHandlers);
|
lListPurge(&ctx->io_list);
|
||||||
lListPurge(&lue->timeoutHandlers);
|
lListPurge(&ctx->timer_list);
|
||||||
free(lue);
|
free(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Drive the timeouts ourself (from now on until an increment of -1)
|
* Drive the timeouts ourself (from now on until an increment of -1)
|
||||||
* This is intended for use in testing frameworks
|
* This is intended for use in testing frameworks
|
||||||
* Caveat emptor: Currently, interactions with the scheduler (agent) API is undefined.
|
* Caveat emptor: Currently, interactions with the scheduler (agent) API is undefined.
|
||||||
* Not for use in lues that use the agent scheduler
|
* Not for use in uevs that use the agent scheduler
|
||||||
* @param ctxt LUElication context object
|
* @param ctx UEVlication context object
|
||||||
* @param mSecs Ms to advance. Use -1 to return to realtime
|
* @param msec Ms to advance. Use -1 to return to realtime
|
||||||
*/
|
*/
|
||||||
void lueTimeAdvance(struct LUECtxt *ctxt, int mSecs)
|
void uev_run_tick(uev_t *ctx, int msec)
|
||||||
{
|
{
|
||||||
// Obviously we are executing a callback right now (arent we always)
|
// Obviously we are executing a callback right now (arent we always)
|
||||||
// The time leap is taken into account before falling back into poll() below
|
// The time leap is taken into account before falling back into poll() below
|
||||||
if (mSecs >= 0)
|
if (msec >= 0) {
|
||||||
{
|
if (!ctx->use_next) {
|
||||||
if (!ctxt->timeSet)
|
ctx->use_next = true;
|
||||||
{
|
ctx->next_time = times(NULL) + msec;
|
||||||
ctxt->timeSet = true;
|
} else {
|
||||||
ctxt->setTime = times(NULL) + mSecs;
|
ctx->next_time += msec;
|
||||||
}
|
}
|
||||||
else
|
} else if (msec == -1) {
|
||||||
{
|
/* Go back to realtime */
|
||||||
ctxt->setTime += mSecs;
|
ctx->use_next = false;
|
||||||
}
|
ctx->base_time += times(NULL) - ctx->next_time;
|
||||||
}
|
ctx->next_time = 0;
|
||||||
else if (mSecs == -1)
|
}
|
||||||
{
|
|
||||||
// Go back to realtime
|
|
||||||
ctxt->timeSet = false;
|
|
||||||
ctxt->baseTime += times(NULL) - ctxt->setTime;
|
|
||||||
ctxt->setTime = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process pending events
|
* Process pending events
|
||||||
*/
|
*/
|
||||||
static int _lueProcessPending(struct LUECtxt *ctxt, int immediate)
|
static int do_run_pending(uev_t *ctx, int immediate)
|
||||||
{
|
{
|
||||||
struct LUETimerH *timer;
|
int result, timeout = 0;
|
||||||
int pollRet;
|
clock_t now;
|
||||||
clock_t curTime;
|
uev_timer_t *timer;
|
||||||
int curPeriod = 0;
|
|
||||||
|
|
||||||
// Do due timers
|
/* Do due timers */
|
||||||
if (lListHead(&ctxt->timeoutHandlers))
|
if (lListHead(&ctx->timer_list)) {
|
||||||
{
|
now = ctx->use_next ? ctx->next_time : times(NULL);
|
||||||
curTime = ctxt->timeSet ? ctxt->setTime : times(NULL);
|
timeout = (now - ctx->base_time) / clock_tick * 1000
|
||||||
curPeriod = (curTime - ctxt->baseTime) / clock_tick * 1000
|
+ ((now - ctx->base_time) % clock_tick) * 1000 / clock_tick;
|
||||||
+ ((curTime - ctxt->baseTime) % clock_tick) * 1000 / clock_tick;
|
while ((timer = lListHead(&ctx->timer_list)) && timer->due <= timeout) {
|
||||||
Debug("Period for timer execution %d", curPeriod);
|
timer->handler((struct uev *)ctx, timer, timer->data);
|
||||||
while((timer = lListHead(&ctxt->timeoutHandlers)) &&
|
uev_timer_delete(ctx, timer);
|
||||||
timer->dueTime <= curPeriod)
|
|
||||||
{
|
|
||||||
Debug("Timer execution %d", timer->dueTime);
|
|
||||||
timer->handler(ctxt, timer, timer->data);
|
|
||||||
lueRemTimer(ctxt, timer);
|
|
||||||
|
|
||||||
// baseTime changes for every lueAddTimer
|
/* base_time changes for every uev_timer_create */
|
||||||
curPeriod = (curTime - ctxt->baseTime) / clock_tick * 1000
|
timeout = (now - ctx->base_time) / clock_tick * 1000
|
||||||
+ ((curTime - ctxt->baseTime) % clock_tick) * 1000 / clock_tick;
|
+ ((now - ctx->base_time) % clock_tick) * 1000 / clock_tick;
|
||||||
}
|
}
|
||||||
lListPurge(&ctxt->timeRem); // Reap timer zombies
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate waiting time
|
lListPurge(&ctx->timer_delist); /* Reap timer zombies */
|
||||||
if (ctxt->exiting)
|
}
|
||||||
curPeriod = 0;
|
|
||||||
else if ((timer = lListHead(&ctxt->timeoutHandlers)) &&
|
|
||||||
timer->dueTime > curPeriod)
|
|
||||||
{
|
|
||||||
Debug("Period from timer head %d - %d -> %d",
|
|
||||||
timer->dueTime, curPeriod, timer->dueTime - curPeriod);
|
|
||||||
curPeriod = timer->dueTime - curPeriod;
|
|
||||||
}
|
|
||||||
else if (NULL != timer)
|
|
||||||
curPeriod = 0;
|
|
||||||
else
|
|
||||||
curPeriod = -1;
|
|
||||||
|
|
||||||
// Do pipe handlers
|
/* Calculate waiting time */
|
||||||
if (lListHead(&ctxt->fileHandlers) && !ctxt->exiting)
|
if (ctx->exiting)
|
||||||
{
|
timeout = 0;
|
||||||
struct pollfd polls[lListLength(&ctxt->fileHandlers)];
|
else if ((timer = lListHead(&ctx->timer_list)) && timer->due > timeout)
|
||||||
struct LUEFileEventH *ent;
|
timeout = timer->due - timeout;
|
||||||
|
else if (NULL != timer)
|
||||||
|
timeout = 0;
|
||||||
|
else
|
||||||
|
timeout = -1;
|
||||||
|
|
||||||
int ix = 0;
|
/* Do pipe handlers */
|
||||||
lListForeachIn(ent, &ctxt->fileHandlers)
|
if (lListHead(&ctx->io_list) && !ctx->exiting) {
|
||||||
{
|
int i = 0;
|
||||||
ent->pollIx = ix;
|
struct pollfd polls[lListLength(&ctx->io_list)];
|
||||||
polls[ix].fd = ent->fd;
|
uev_io_t *w;
|
||||||
polls[ix].events = LUE_FHIN == ent->direction ? POLLIN : POLLOUT;
|
|
||||||
polls[ix].revents = 0;
|
|
||||||
ix++;
|
|
||||||
}
|
|
||||||
Debug("Polling %d fds in %d ms", ix, curPeriod);
|
|
||||||
pollRet = poll(polls, ix, immediate ? 0 : curPeriod);
|
|
||||||
if (pollRet < 0)
|
|
||||||
{
|
|
||||||
if (EINTR == errno)
|
|
||||||
return 0;
|
|
||||||
else if (EBADF == errno)
|
|
||||||
{
|
|
||||||
lListForeachIn(ent, &ctxt->fileHandlers)
|
|
||||||
{
|
|
||||||
if (fcntl(ent->fd, F_GETFD) == -1 && EBADF == errno)
|
|
||||||
{
|
|
||||||
ent->handler(ctxt, ent, ent->fd, ent->data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Error in poll. Cannot continue
|
|
||||||
assert(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (pollRet > 0)
|
|
||||||
{
|
|
||||||
lListForeachIn(ent, &ctxt->fileHandlers)
|
|
||||||
{
|
|
||||||
if (ent->handler && ent->pollIx >= 0 && polls[ent->pollIx].revents)
|
|
||||||
{
|
|
||||||
ent->handler(ctxt, ent, ent->fd, ent->data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (lListHead(&ctxt->fileRem))
|
|
||||||
{
|
|
||||||
struct LUEFileEventRem *ent;
|
|
||||||
|
|
||||||
while (NULL != (ent = lListHead(&ctxt->fileRem)))
|
lListForeachIn(w, &ctx->io_list) {
|
||||||
{
|
w->index = i;
|
||||||
lListRemove(&ctxt->fileRem, ent);
|
polls[i].fd = w->fd;
|
||||||
lListRemove(&ctxt->fileHandlers, ent->ent);
|
polls[i].events = UEV_FHIN == w->dir ? POLLIN : POLLOUT;
|
||||||
free(ent->ent);
|
polls[i].revents = 0;
|
||||||
}
|
i++;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else if (curPeriod > 0)
|
|
||||||
{
|
|
||||||
if (!immediate)
|
|
||||||
poll(NULL, 0, curPeriod);
|
|
||||||
}
|
|
||||||
else if (curPeriod == -1)
|
|
||||||
curPeriod = -2; // Nothing more to do
|
|
||||||
|
|
||||||
return curPeriod;
|
// fprintf(stderr, "Polling %d sec\n", timeout);
|
||||||
|
result = poll(polls, i, immediate ? 0 : timeout);
|
||||||
|
if (result < 0) {
|
||||||
|
if (EINTR == errno)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (EBADF == errno) {
|
||||||
|
lListForeachIn(w, &ctx->io_list) {
|
||||||
|
if (fcntl(w->fd, F_GETFD) == -1 && EBADF == errno)
|
||||||
|
w->handler((struct uev *)ctx, w, w->fd, w->data);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/* Error in poll. Cannot continue */
|
||||||
|
assert(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else if (result > 0) {
|
||||||
|
lListForeachIn(w, &ctx->io_list) {
|
||||||
|
// fprintf(stderr, "I/O event!\n");
|
||||||
|
if (w->handler && w->index >= 0 && polls[w->index].revents) {
|
||||||
|
w->handler((struct uev *)ctx, w, w->fd, w->data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lListHead(&ctx->io_delist)) {
|
||||||
|
uev_io_rem_t *w;
|
||||||
|
|
||||||
|
while ((w = lListHead(&ctx->io_delist))) {
|
||||||
|
lListRemove(&ctx->io_delist, w);
|
||||||
|
lListRemove(&ctx->io_list, w->ent);
|
||||||
|
free(w->ent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (timeout > 0) {
|
||||||
|
if (!immediate)
|
||||||
|
poll(NULL, 0, timeout);
|
||||||
|
} else if (timeout == -1)
|
||||||
|
timeout = -2; /* Nothing more to do */
|
||||||
|
|
||||||
|
return timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process pending events
|
* Process pending events
|
||||||
*/
|
*/
|
||||||
int lueProcessPending(struct LUECtxt *ctxt)
|
int uev_run_pending(uev_t *ctx)
|
||||||
{
|
{
|
||||||
return _lueProcessPending(ctxt, 1);
|
return do_run_pending(ctx, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run the application
|
* Run the application
|
||||||
*/
|
*/
|
||||||
void lueRun(struct LUECtxt *ctxt)
|
void uev_run(uev_t *ctx)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret = 0;
|
||||||
|
|
||||||
do {
|
if (!ctx)
|
||||||
ret = _lueProcessPending(ctxt, 0);
|
return;
|
||||||
} while (!ctxt->exiting && ret != -2);
|
|
||||||
ctxt->exiting = false;
|
while (!ctx->exiting && ret != -2)
|
||||||
Debug("LUE exiting - bye");
|
ret = do_run_pending(ctx, 0);
|
||||||
|
|
||||||
|
ctx->exiting = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Terminate the application
|
* Terminate the application
|
||||||
*/
|
*/
|
||||||
void lueTerminate(struct LUECtxt *ctxt)
|
void uev_exit(uev_t *ctx)
|
||||||
{
|
{
|
||||||
ctxt->exiting = true;
|
ctx->exiting = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// vim: set ts=8 sw=3 sts=3 et:
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* version-control: t
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -23,46 +23,100 @@
|
|||||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef LIBUEV_H
|
#ifndef LIBUEV_H_
|
||||||
#define LIBUEV_H
|
#define LIBUEV_H_
|
||||||
|
|
||||||
struct LUETimerH;
|
#include "llist.h"
|
||||||
struct LUEFileEventH;
|
|
||||||
struct LUECtxt;
|
struct uev;
|
||||||
|
struct uev_io;
|
||||||
|
struct uev_timer;
|
||||||
|
|
||||||
|
/* I/O direction */
|
||||||
|
typedef enum {
|
||||||
|
UEV_FHIN = 0,
|
||||||
|
UEV_FHOUT = 1
|
||||||
|
} uev_dir_t;
|
||||||
|
|
||||||
|
/* I/O event type */
|
||||||
|
typedef struct uev_io_rem {
|
||||||
|
LListField(struct uev_io_rem);
|
||||||
|
struct uev_io *ent;
|
||||||
|
} uev_io_rem_t;
|
||||||
|
|
||||||
|
typedef struct uev_io {
|
||||||
|
LListField(struct uev_io); ///< Elements for linked list
|
||||||
|
uev_io_rem_t rem; ///< For postponed removal
|
||||||
|
|
||||||
|
int fd; ///< File descriptor
|
||||||
|
uev_dir_t dir; ///< Direction: in or out
|
||||||
|
int index; ///< Index in poll array
|
||||||
|
|
||||||
|
int (*handler)(struct uev *, struct uev_io *, int, void *);
|
||||||
|
void *data; ///< Callback optional parameter
|
||||||
|
} uev_io_t;
|
||||||
|
|
||||||
|
/* Timer event type */
|
||||||
|
typedef struct uev_timer {
|
||||||
|
LListField(struct uev_timer); ///< Linked list elements
|
||||||
|
|
||||||
|
int due; ///< Epoch timestamp
|
||||||
|
|
||||||
|
int (*handler)(struct uev *, struct uev_timer *, void *);
|
||||||
|
void *data; ///< Timer callback parameter
|
||||||
|
} uev_timer_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Typedef file handler callbacks
|
* @cond doxygen does not like this construction
|
||||||
*/
|
*/
|
||||||
typedef int (*LUEFileHandler)(struct LUECtxt *ctxt,
|
typedef LList(uev_timer_t) uev_timer_list_t;
|
||||||
struct LUEFileEventH *handle,
|
typedef LList(uev_io_t) uev_io_list_t;
|
||||||
int fd,
|
typedef LList(uev_io_rem_t) uev_io_rem_list_t;
|
||||||
void *data);
|
|
||||||
/**
|
/**
|
||||||
* Typedef timer handler callbacks
|
* @endcond
|
||||||
*/
|
*/
|
||||||
typedef int (*LUETimerHandler)(struct LUECtxt *ctxt,
|
|
||||||
struct LUETimerH *handle,
|
|
||||||
void *data);
|
|
||||||
|
|
||||||
struct LUEFileEventH *lueAddInput(struct LUECtxt *ctxt,
|
/* Main libuev context type */
|
||||||
int fd, LUEFileHandler handler, void *data);
|
typedef struct {
|
||||||
int lueRemInput(struct LUECtxt *ctxt, struct LUEFileEventH *hdl);
|
uev_io_list_t io_list; ///< File handlers
|
||||||
struct LUEFileEventH *lueAddOutput(struct LUECtxt *ctxt,
|
uev_timer_list_t timer_list; ///< Timer handlers
|
||||||
int fd, LUEFileHandler handler, void *data);
|
|
||||||
int lueRemOutput(struct LUECtxt *ctxt, struct LUEFileEventH *hdl);
|
|
||||||
int lueRemFd(struct LUECtxt *ctxt, int fd);
|
|
||||||
|
|
||||||
struct LUETimerH *lueAddTimer(struct LUECtxt *ctxt, int periodMs,
|
uev_io_rem_list_t io_delist; ///< List of file handles to be garbage collected
|
||||||
LUETimerHandler handler, void *data);
|
uev_timer_list_t timer_delist; ///< List of timer handles to be garbage collected
|
||||||
int lueRemTimer(struct LUECtxt *ctxt, struct LUETimerH *hdl);
|
|
||||||
|
|
||||||
struct LUECtxt *lueCtxtCreate();
|
clock_t base_time; ///< Time at last timer tick
|
||||||
void lueCtxtDestroy(struct LUECtxt *lue);
|
clock_t next_time; ///< Next timer tick epoch
|
||||||
void lueTimeAdvance(struct LUECtxt *ctxt, int mSecs);
|
int use_next; ///< True if next timer epoch is set
|
||||||
void lueRun(struct LUECtxt *ctxt);
|
|
||||||
int lueProcessPending(struct LUECtxt *ctxt);
|
|
||||||
void lueTerminate(struct LUECtxt *ctxt);
|
|
||||||
|
|
||||||
#endif /* LIBUEV_H */
|
int exiting; ///< True if the application is terminating
|
||||||
|
} uev_t;
|
||||||
|
|
||||||
// vim: set ts=8 sw=3 sts=3 et:
|
/* Callbacks for different watchers */
|
||||||
|
typedef int (*uev_io_cb_t) (uev_t *ctx, uev_io_t *w, int fd, void *data);
|
||||||
|
typedef int (*uev_timer_cb_t)(uev_t *ctx, uev_timer_t *w, void *data);
|
||||||
|
|
||||||
|
/* Public interface */
|
||||||
|
uev_io_t *uev_io_create (uev_t *ctx, int fd, uev_dir_t dir, uev_io_cb_t handler, void *data);
|
||||||
|
int uev_io_delete (uev_t *ctx, uev_io_t *io);
|
||||||
|
|
||||||
|
uev_timer_t *uev_timer_create (uev_t *ctx, int msec, uev_timer_cb_t handler, void *data);
|
||||||
|
int uev_timer_delete (uev_t *ctx, uev_timer_t *hdl);
|
||||||
|
|
||||||
|
uev_t *uev_create (void);
|
||||||
|
void uev_delete (uev_t *uev);
|
||||||
|
|
||||||
|
void uev_run_tick (uev_t *ctx, int msec);
|
||||||
|
int uev_run_pending (uev_t *ctx);
|
||||||
|
|
||||||
|
void uev_run (uev_t *ctx);
|
||||||
|
void uev_exit (uev_t *ctx);
|
||||||
|
|
||||||
|
#endif /* LIBUEV_H_ */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* version-control: t
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
@@ -1,150 +1,159 @@
|
|||||||
/**
|
/* libuev - (Doubly) Linked lists anchored at both ends
|
||||||
* @file llist.h
|
|
||||||
* (Doubly) Linked lists anchored at both ends
|
|
||||||
*
|
*
|
||||||
* $Id$
|
* Copyright (c) 2012 Flemming Madsen <flemming!madsen()madsensoft!dk>
|
||||||
|
* Copyright (c) 2013 Joachim Nilsson <troglobit()gmail!com>
|
||||||
*
|
*
|
||||||
* (C) Copyright 2012 flemming.madsen at madsensoft.dk. Notice at end of file
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
* a copy of this software and associated documentation files (the
|
||||||
|
* "Software"), to deal in the Software without restriction, including
|
||||||
|
* without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
* permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
* the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be
|
||||||
|
* included in all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||||
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||||
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||||
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/// Struct mix-in. Use this inside your struct declaration
|
/* Struct mix-in. Use this inside your struct declaration */
|
||||||
#define LListField(tn) tn * _llNext; tn * _llPrev
|
#define LListField(tn) tn * _llNext; tn * _llPrev
|
||||||
|
|
||||||
/// Anchor Declaration. Use like: LList(typename) varname;
|
/* Anchor Declaration. Use like: LList(typename) varname; */
|
||||||
#define LList(tn) struct { tn *head; tn *tail; int numElm; }
|
#define LList(tn) struct { tn *head; tn *tail; int numElm; }
|
||||||
|
|
||||||
/// List init
|
/* List init */
|
||||||
#define lListInit(a) ((a)->head = (a)->tail = NULL, (a)->numElm = 0, (a))
|
#define lListInit(a) ((a)->head = (a)->tail = NULL, (a)->numElm = 0, (a))
|
||||||
|
|
||||||
/// List empty
|
/* List empty */
|
||||||
#define lListEmpty(a) ((a)->head == NULL)
|
#define lListEmpty(a) ((a)->head == NULL)
|
||||||
|
|
||||||
/// List head
|
/* List head */
|
||||||
#define lListHead(a) ((a)->head)
|
#define lListHead(a) ((a)->head)
|
||||||
|
|
||||||
/// List tail
|
/* List tail */
|
||||||
#define lListTail(a) ((a)->tail)
|
#define lListTail(a) ((a)->tail)
|
||||||
|
|
||||||
/// List length
|
/* List length */
|
||||||
#define lListLength(a) ((a)->numElm)
|
#define lListLength(a) ((a)->numElm)
|
||||||
|
|
||||||
/// List element next
|
/* List element next */
|
||||||
#define lListNext(e) ((e)->_llNext)
|
#define lListNext(e) ((e)->_llNext)
|
||||||
|
|
||||||
/// List element prev
|
/* List element prev */
|
||||||
#define lListPrev(e) ((e)->_llPrev)
|
#define lListPrev(e) ((e)->_llPrev)
|
||||||
|
|
||||||
/// Append element e to list e
|
/* Append element e to list e */
|
||||||
#define lListAppend(a, e) \
|
#define lListAppend(a, e) \
|
||||||
{ \
|
{ \
|
||||||
if (lListEmpty(a)) (a)->head = (e); \
|
if (lListEmpty(a)) \
|
||||||
else (a)->tail->_llNext = (e); \
|
(a)->head = (e); \
|
||||||
(e)->_llPrev = (a)->tail; \
|
else \
|
||||||
(a)->tail = (e); \
|
(a)->tail->_llNext = (e); \
|
||||||
(e)->_llNext = NULL; \
|
(e)->_llPrev = (a)->tail; \
|
||||||
(a)->numElm++; \
|
(a)->tail = (e); \
|
||||||
}
|
(e)->_llNext = NULL; \
|
||||||
|
(a)->numElm++; \
|
||||||
|
}
|
||||||
|
|
||||||
/// Insert element n before element e in list a
|
/* Insert element n before element e in list a */
|
||||||
#define lListInsertFirst(a, n) lListInsert(a, n, (a)->head)
|
#define lListInsertFirst(a, n) lListInsert(a, n, (a)->head)
|
||||||
|
|
||||||
/// Insert element n before element e in list a
|
/* Insert element n before element e in list a */
|
||||||
#define lListInsert(a, n, e) \
|
#define lListInsert(a, n, e) \
|
||||||
{ \
|
{ \
|
||||||
if (!(e) || (a)->head == (e)) { \
|
if (!(e) || (a)->head == (e)) { \
|
||||||
n->_llNext = (a)->head; \
|
n->_llNext = (a)->head; \
|
||||||
n->_llPrev = NULL; \
|
n->_llPrev = NULL; \
|
||||||
(a)->head = (n); \
|
(a)->head = (n); \
|
||||||
} else { \
|
} else { \
|
||||||
n->_llNext = (e); \
|
n->_llNext = (e); \
|
||||||
n->_llPrev = (e)->_llPrev; \
|
n->_llPrev = (e)->_llPrev; \
|
||||||
(e)->_llPrev->_llNext = n; \
|
(e)->_llPrev->_llNext = n; \
|
||||||
} \
|
} \
|
||||||
if (n->_llNext) n->_llNext->_llPrev = n; \
|
if (n->_llNext) n->_llNext->_llPrev = n; \
|
||||||
(a)->numElm++; \
|
(a)->numElm++; \
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove element e from list a. Caller must free element.
|
/* Remove element e from list a. Caller must free element. */
|
||||||
#define lListRemove(a, e) \
|
#define lListRemove(a, e) \
|
||||||
{ \
|
{ \
|
||||||
if ((a)->head) { \
|
if ((a)->head) { \
|
||||||
if ((e) == (a)->head) (a)->head = (e)->_llNext; \
|
if ((e) == (a)->head) \
|
||||||
else (e)->_llPrev->_llNext = (e)->_llNext; \
|
(a)->head = (e)->_llNext; \
|
||||||
if ((e) == (a)->tail) (a)->tail = (e)->_llPrev; \
|
else \
|
||||||
else (e)->_llNext->_llPrev = (e)->_llPrev; \
|
(e)->_llPrev->_llNext = (e)->_llNext; \
|
||||||
(a)->numElm--; \
|
if ((e) == (a)->tail) \
|
||||||
} \
|
(a)->tail = (e)->_llPrev; \
|
||||||
}
|
else \
|
||||||
|
(e)->_llNext->_llPrev = (e)->_llPrev; \
|
||||||
|
(a)->numElm--; \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
/// Purge list a elements with free()
|
/* Purge list a elements with free() */
|
||||||
#define lListPurgeT(a, T) \
|
#define lListPurgeT(a, T) \
|
||||||
{ \
|
{ \
|
||||||
T p1, p2; \
|
T p1, p2; \
|
||||||
for (p1 = (a)->head; p1; ) { \
|
for (p1 = (a)->head; p1; ) { \
|
||||||
p2 = p1; p1 = p1->_llNext; free(p2); \
|
p2 = p1; \
|
||||||
} \
|
p1 = p1->_llNext; free(p2); \
|
||||||
lListInit(a); \
|
} \
|
||||||
}
|
lListInit(a); \
|
||||||
|
}
|
||||||
|
|
||||||
/// Remove last element from list a
|
/* Remove last element from list a */
|
||||||
#define lListRemoveTailT(a, T) \
|
#define lListRemoveTailT(a, T) \
|
||||||
{ \
|
{ \
|
||||||
T p; \
|
T p; \
|
||||||
if ((a)->tail) { \
|
if ((a)->tail) { \
|
||||||
p = (a)->tail; \
|
p = (a)->tail; \
|
||||||
(a)->tail = (a)->tail->_llPrev; \
|
(a)->tail = (a)->tail->_llPrev; \
|
||||||
if ((a)->tail) (a)->tail->_llNext = NULL; \
|
if ((a)->tail) \
|
||||||
else (a)->head = NULL; \
|
(a)->tail->_llNext = NULL; \
|
||||||
free(p); \
|
else \
|
||||||
(a)->numElm--; \
|
(a)->head = NULL; \
|
||||||
} \
|
free(p); \
|
||||||
}
|
(a)->numElm--; \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
/// Remove first element from list a
|
/* Remove first element from list a */
|
||||||
#define lListRemoveHeadT(a, T) \
|
#define lListRemoveHeadT(a, T) \
|
||||||
{ \
|
{ \
|
||||||
T p; \
|
T p; \
|
||||||
if ((a)->head) { \
|
if ((a)->head) { \
|
||||||
p = (a)->head; \
|
p = (a)->head; \
|
||||||
(a)->head = (a)->head->_llNext; \
|
(a)->head = (a)->head->_llNext; \
|
||||||
if ((a)->head) (a)->head->_llPrev = NULL; \
|
if ((a)->head) \
|
||||||
else (a)->tail = NULL; \
|
(a)->head->_llPrev = NULL; \
|
||||||
free(p); \
|
else \
|
||||||
(a)->numElm--; \
|
(a)->tail = NULL; \
|
||||||
} \
|
free(p); \
|
||||||
}
|
(a)->numElm--; \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
// Convenience
|
/* Convenience
|
||||||
// Only use if you have typeof() eg. gcc
|
* Only use if you have typeof() eg. gcc */
|
||||||
#define lListPurge(a) lListPurgeT(a, typeof((a)->head))
|
#define lListPurge(a) lListPurgeT(a, typeof((a)->head))
|
||||||
#define lListRemoveTail(a) lListRemoveTailT(a, typeof((a)->head))
|
#define lListRemoveTail(a) lListRemoveTailT(a, typeof((a)->head))
|
||||||
#define lListRemoveHead(a) lListRemoveHeadT(a, typeof((a)->head))
|
#define lListRemoveHead(a) lListRemoveHeadT(a, typeof((a)->head))
|
||||||
|
|
||||||
/// Traverse the list foreach e in a
|
/* Traverse the list foreach e in a */
|
||||||
#define lListForeachIn(e, a) for (e = (a)->head; e; e = e->_llNext)
|
#define lListForeachIn(e, a) for (e = (a)->head; e; e = e->_llNext)
|
||||||
|
|
||||||
|
/**
|
||||||
/******************************************************************************
|
* Local Variables:
|
||||||
* Copyright (C) 2012 flemming.madsen at madsensoft.dk. All rights reserved.
|
* version-control: t
|
||||||
*
|
* indent-tabs-mode: t
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining
|
* c-file-style: "linux"
|
||||||
* a copy of this software and associated documentation files (the
|
* End:
|
||||||
* "Software"), to deal in the Software without restriction, including
|
*/
|
||||||
* without limitation the rights to use, copy, modify, merge, publish,
|
|
||||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
||||||
* permit persons to whom the Software is furnished to do so, subject to
|
|
||||||
* the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be
|
|
||||||
* included in all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
||||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
||||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
||||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
||||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
// vim: set sw=3 sts=3 et:
|
|
||||||
@@ -25,76 +25,105 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h> /* intptr_t */
|
#include <unistd.h> /* intptr_t */
|
||||||
|
|
||||||
#include "libuev.h"
|
#include "libuev.h"
|
||||||
|
|
||||||
static period;
|
static int in, out;
|
||||||
static client_fd, server_fd, service_fd;
|
static int period = 0;
|
||||||
static struct LUETimerH *timeOut;
|
static uev_timer_t *timer = NULL;
|
||||||
int readFd, writeFd;
|
|
||||||
|
|
||||||
static timeOutHdl(struct LUECtxt *ctxt, struct LUETimerH *handle, void *data)
|
static int lifetime_cb(uev_t *ctx, uev_timer_t *w, void *data)
|
||||||
{
|
{
|
||||||
timeOut = NULL;
|
fprintf(stderr, "\nLifetime exceeded %p\n", data);
|
||||||
printf("Timeout exceeded %p\n", data);
|
uev_exit(ctx);
|
||||||
lueRemFd(ctxt, readFd);
|
|
||||||
lueTerminate(ctxt);
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static appTimeout(struct LUECtxt *ctxt, struct LUETimerH *handle, void *data)
|
/* The pipe watchdog, if it triggers we haven't received data in time. */
|
||||||
|
static int timeout_cb(uev_t *ctx, uev_timer_t *w, void *data)
|
||||||
{
|
{
|
||||||
printf("Lifetime exceeded %p\n", data);
|
timer = NULL;
|
||||||
exit(1);
|
fprintf(stderr, "\nTimeout exceeded %p\n", data);
|
||||||
|
|
||||||
|
uev_timer_delete(ctx, w);
|
||||||
|
uev_exit(ctx);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int readCb(struct LUECtxt *ctxt, struct LUEFileEventH *handle, int fd, void *data)
|
static int pipe_read_cb(uev_t *ctx, uev_io_t *w, int fd, void *data)
|
||||||
{
|
{
|
||||||
if (timeOut) {
|
int cnt;
|
||||||
lueRemTimer(ctxt, timeOut);
|
char msg[50];
|
||||||
timeOut = lueAddTimer(ctxt, 950, timeOutHdl, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
char msg[50];
|
/* Kick watchdog */
|
||||||
int cnt = read(readFd, msg, sizeof(msg));
|
if (timer) {
|
||||||
//printf("READ %.*s %d\n", cnt, msg, cnt);
|
uev_timer_delete(ctx, timer);
|
||||||
printf("%.*s.%d ", cnt, msg, cnt); fflush(stdout);
|
timer = uev_timer_create(ctx, 1000, timeout_cb, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
cnt = read(in, msg, sizeof(msg));
|
||||||
|
// fprintf(stderr, "READ %.*s %d\n", cnt, msg, cnt);
|
||||||
|
fprintf(stderr, "%.*s.%d ", cnt, msg, cnt);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static writeThread(struct LUECtxt *ctxt, struct LUETimerH *handle, void *data)
|
static int pipe_write_cb(uev_t *ctx, uev_timer_t *handle, void *data)
|
||||||
{
|
{
|
||||||
int cnt = (int)(intptr_t)data;
|
int cnt = (int)(intptr_t)data;
|
||||||
char *msg = "TESTING";
|
char *msg = "TESTING";
|
||||||
|
|
||||||
write(writeFd, msg, cnt);
|
write(out, msg, cnt);
|
||||||
//printf("WRITE %.*s %d\n", cnt, msg, cnt);
|
// fprintf(stderr, "WRITE %.*s %d\n", cnt, msg, cnt);
|
||||||
printf("%d ", cnt); fflush(stdout);
|
fprintf(stderr, "%d ", cnt);
|
||||||
period = cnt + 5;
|
period = cnt + 5;
|
||||||
lueAddTimer(ctxt, period * 100, writeThread, (void *)(intptr_t)(cnt + 1));
|
|
||||||
|
uev_timer_create(ctx, period * 100, pipe_write_cb, (void *)(intptr_t)(cnt + 1));
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
int main(void)
|
||||||
{
|
{
|
||||||
struct LUECtxt *ctxt = lueCtxtCreate();
|
int fd[2];
|
||||||
|
uev_t *ctx = uev_create();
|
||||||
|
|
||||||
timeOut = lueAddTimer(ctxt, 950, timeOutHdl, (void *)1);
|
/* Total program execution time */
|
||||||
lueAddTimer(ctxt, 5000, appTimeout, (void *)2);
|
uev_timer_create(ctx, 4000, lifetime_cb, (void *)(intptr_t)2);
|
||||||
|
|
||||||
lueAddTimer(ctxt, 500, writeThread, (void *) 1);
|
/* Work load, one timer callback writes to a pipe periodically,
|
||||||
|
* and one I/O watcher that is called every time the pipe has
|
||||||
|
* some data. */
|
||||||
|
pipe(fd);
|
||||||
|
in = fd[0];
|
||||||
|
out = fd[1];
|
||||||
|
uev_timer_create(ctx, 400, pipe_write_cb, (void *)(intptr_t)1);
|
||||||
|
uev_io_create(ctx, in, UEV_FHIN, pipe_read_cb, NULL);
|
||||||
|
|
||||||
int fd[2];
|
/* Watchdog for the above timer callback, if it doesn't wake up
|
||||||
pipe(fd);
|
* and write to the pipe within a given deadline it will bark. */
|
||||||
readFd = fd[0];
|
timer = uev_timer_create(ctx, 950, timeout_cb, (void *)(intptr_t)1);
|
||||||
writeFd = fd[1];
|
|
||||||
lueAddInput(ctxt, readFd, readCb, NULL);
|
|
||||||
|
|
||||||
lueRun(ctxt);
|
/* Start event loop */
|
||||||
lueCtxtDestroy(ctxt);
|
uev_run(ctx);
|
||||||
|
|
||||||
printf("Period is %d must be 10: %s\n", period, 10 == period ? "OK" : "ERROR!");
|
/* Tear down event context */
|
||||||
exit(10 == period ? 0 : 1);
|
uev_delete(ctx);
|
||||||
|
|
||||||
|
fprintf(stderr, "Period is %d must be 10: %s\n", period, 10 == period ? "OK" : "ERROR!");
|
||||||
|
if (10 != period)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// vim: set sw=3 sts=3 et:
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* version-control: t
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
Reference in new issue
Block a user