2016-01-30 05:24:23 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "stio-prv.h"
|
|
|
|
|
|
|
|
#define HEAP_PARENT(x) (((x) - 1) / 2)
|
|
|
|
#define HEAP_LEFT(x) ((x) * 2 + 1)
|
|
|
|
#define HEAP_RIGHT(x) ((x) * 2 + 2)
|
|
|
|
|
|
|
|
#define YOUNGER_THAN(x,y) (stio_cmptime(&(x)->when, &(y)->when) < 0)
|
|
|
|
|
|
|
|
void stio_cleartmrjobs (stio_t* stio)
|
|
|
|
{
|
|
|
|
while (stio->tmr.size > 0) stio_deltmrjob (stio, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static stio_tmridx_t sift_up (stio_t* stio, stio_tmridx_t index, int notify)
|
|
|
|
{
|
2016-03-04 12:36:44 +00:00
|
|
|
stio_tmridx_t parent;
|
2016-01-30 05:24:23 +00:00
|
|
|
|
|
|
|
parent = HEAP_PARENT(index);
|
|
|
|
if (index > 0 && YOUNGER_THAN(&stio->tmr.jobs[index], &stio->tmr.jobs[parent]))
|
|
|
|
{
|
|
|
|
stio_tmrjob_t item;
|
2016-02-04 15:06:20 +00:00
|
|
|
#if defined(STIO_USE_TMRJOB_IDXPTR)
|
|
|
|
/* nothing */
|
|
|
|
#else
|
2016-03-04 12:36:44 +00:00
|
|
|
stio_tmridx_t old_index;
|
2016-02-04 15:06:20 +00:00
|
|
|
#endif
|
2016-01-30 05:24:23 +00:00
|
|
|
|
|
|
|
item = stio->tmr.jobs[index];
|
2016-02-04 15:06:20 +00:00
|
|
|
#if defined(STIO_USE_TMRJOB_IDXPTR)
|
|
|
|
/* nothing */
|
|
|
|
#else
|
2016-01-30 05:24:23 +00:00
|
|
|
old_index = index;
|
2016-02-04 15:06:20 +00:00
|
|
|
#endif
|
2016-01-30 05:24:23 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* move down the parent to my current position */
|
|
|
|
stio->tmr.jobs[index] = stio->tmr.jobs[parent];
|
2016-01-31 05:39:12 +00:00
|
|
|
#if defined(STIO_USE_TMRJOB_IDXPTR)
|
|
|
|
if (stio->tmr.jobs[index].idxptr) *stio->tmr.jobs[index].idxptr = index;
|
|
|
|
#else
|
2016-01-30 05:24:23 +00:00
|
|
|
stio->tmr.jobs[index].updater (stio, parent, index, &stio->tmr.jobs[index]);
|
2016-01-31 05:39:12 +00:00
|
|
|
#endif
|
2016-01-30 05:24:23 +00:00
|
|
|
|
|
|
|
/* traverse up */
|
|
|
|
index = parent;
|
|
|
|
parent = HEAP_PARENT(parent);
|
|
|
|
}
|
|
|
|
while (index > 0 && YOUNGER_THAN(&item, &stio->tmr.jobs[parent]));
|
|
|
|
|
2016-01-31 05:39:12 +00:00
|
|
|
stio->tmr.jobs[index] = item;
|
|
|
|
#if defined(STIO_USE_TMRJOB_IDXPTR)
|
|
|
|
if (stio->tmr.jobs[index].idxptr) *stio->tmr.jobs[index].idxptr = index;
|
|
|
|
#else
|
2016-01-30 05:24:23 +00:00
|
|
|
/* we send no notification if the item is added with stio_instmrjob()
|
|
|
|
* or updated with stio_updtmrjob(). the caller of these functions
|
|
|
|
* must rely on the return value. */
|
|
|
|
if (notify && index != old_index)
|
|
|
|
stio->tmr.jobs[index].updater (stio, old_index, index, &stio->tmr.jobs[index]);
|
2016-01-31 05:39:12 +00:00
|
|
|
#endif
|
2016-01-30 05:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
static stio_tmridx_t sift_down (stio_t* stio, stio_tmridx_t index, int notify)
|
|
|
|
{
|
|
|
|
stio_size_t base = stio->tmr.size / 2;
|
|
|
|
|
2016-03-23 13:54:15 +00:00
|
|
|
if (index < base) /* at least 1 child is under the 'index' position */
|
2016-01-30 05:24:23 +00:00
|
|
|
{
|
|
|
|
stio_tmrjob_t item;
|
2016-02-04 15:06:20 +00:00
|
|
|
#if defined(STIO_USE_TMRJOB_IDXPTR)
|
|
|
|
/* nothing */
|
|
|
|
#else
|
2016-03-04 12:36:44 +00:00
|
|
|
stio_tmridx_t old_index;
|
2016-02-04 15:06:20 +00:00
|
|
|
#endif
|
2016-01-30 05:24:23 +00:00
|
|
|
|
|
|
|
item = stio->tmr.jobs[index];
|
2016-02-04 15:06:20 +00:00
|
|
|
|
|
|
|
#if defined(STIO_USE_TMRJOB_IDXPTR)
|
|
|
|
/* nothing */
|
|
|
|
#else
|
2016-01-30 05:24:23 +00:00
|
|
|
old_index = index;
|
2016-02-04 15:06:20 +00:00
|
|
|
#endif
|
2016-01-30 05:24:23 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2016-03-04 12:36:44 +00:00
|
|
|
stio_tmridx_t left, right, younger;
|
2016-01-30 05:24:23 +00:00
|
|
|
|
2016-03-04 12:36:44 +00:00
|
|
|
left = HEAP_LEFT(index);
|
2016-01-30 05:24:23 +00:00
|
|
|
right = HEAP_RIGHT(index);
|
|
|
|
|
|
|
|
if (right < stio->tmr.size && YOUNGER_THAN(&stio->tmr.jobs[right], &stio->tmr.jobs[left]))
|
|
|
|
{
|
|
|
|
younger = right;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
younger = left;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (YOUNGER_THAN(&item, &stio->tmr.jobs[younger])) break;
|
|
|
|
|
|
|
|
stio->tmr.jobs[index] = stio->tmr.jobs[younger];
|
2016-01-31 05:39:12 +00:00
|
|
|
#if defined(STIO_USE_TMRJOB_IDXPTR)
|
|
|
|
if (stio->tmr.jobs[index].idxptr) *stio->tmr.jobs[index].idxptr = index;
|
|
|
|
#else
|
2016-01-30 05:24:23 +00:00
|
|
|
stio->tmr.jobs[index].updater (stio, younger, index, &stio->tmr.jobs[index]);
|
2016-01-31 05:39:12 +00:00
|
|
|
#endif
|
2016-01-30 05:24:23 +00:00
|
|
|
|
|
|
|
index = younger;
|
|
|
|
}
|
|
|
|
while (index < base);
|
|
|
|
|
|
|
|
stio->tmr.jobs[index] = item;
|
2016-01-31 05:39:12 +00:00
|
|
|
#if defined(STIO_USE_TMRJOB_IDXPTR)
|
|
|
|
if (stio->tmr.jobs[index].idxptr) *stio->tmr.jobs[index].idxptr = index;
|
|
|
|
#else
|
2016-01-30 05:24:23 +00:00
|
|
|
if (notify && index != old_index)
|
|
|
|
stio->tmr.jobs[index].updater (stio, old_index, index, &stio->tmr.jobs[index]);
|
2016-01-31 05:39:12 +00:00
|
|
|
#endif
|
2016-01-30 05:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stio_deltmrjob (stio_t* stio, stio_tmridx_t index)
|
|
|
|
{
|
|
|
|
stio_tmrjob_t item;
|
|
|
|
|
|
|
|
STIO_ASSERT (index < stio->tmr.size);
|
|
|
|
|
|
|
|
item = stio->tmr.jobs[index];
|
2016-01-31 05:39:12 +00:00
|
|
|
#if defined(STIO_USE_TMRJOB_IDXPTR)
|
|
|
|
if (stio->tmr.jobs[index].idxptr) *stio->tmr.jobs[index].idxptr = STIO_TMRIDX_INVALID;
|
|
|
|
#else
|
2016-01-30 05:24:23 +00:00
|
|
|
stio->tmr.jobs[index].updater (stio, index, STIO_TMRIDX_INVALID, &stio->tmr.jobs[index]);
|
2016-01-31 05:39:12 +00:00
|
|
|
#endif
|
2016-01-30 05:24:23 +00:00
|
|
|
|
|
|
|
stio->tmr.size = stio->tmr.size - 1;
|
|
|
|
if (stio->tmr.size > 0 && index != stio->tmr.size)
|
|
|
|
{
|
|
|
|
stio->tmr.jobs[index] = stio->tmr.jobs[stio->tmr.size];
|
2016-01-31 05:39:12 +00:00
|
|
|
#if defined(STIO_USE_TMRJOB_IDXPTR)
|
|
|
|
if (stio->tmr.jobs[index].idxptr) *stio->tmr.jobs[index].idxptr = index;
|
|
|
|
#else
|
2016-01-30 05:24:23 +00:00
|
|
|
stio->tmr.jobs[index].updater (stio, stio->tmr.size, index, &stio->tmr.jobs[index]);
|
2016-01-31 05:39:12 +00:00
|
|
|
#endif
|
2016-01-30 05:24:23 +00:00
|
|
|
YOUNGER_THAN(&stio->tmr.jobs[index], &item)? sift_up(stio, index, 1): sift_down(stio, index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stio_tmridx_t stio_instmrjob (stio_t* stio, const stio_tmrjob_t* job)
|
|
|
|
{
|
|
|
|
stio_tmridx_t index = stio->tmr.size;
|
|
|
|
|
|
|
|
if (index >= stio->tmr.capa)
|
|
|
|
{
|
|
|
|
stio_tmrjob_t* tmp;
|
|
|
|
stio_size_t new_capa;
|
|
|
|
|
|
|
|
STIO_ASSERT (stio->tmr.capa >= 1);
|
|
|
|
new_capa = stio->tmr.capa * 2;
|
|
|
|
tmp = (stio_tmrjob_t*)STIO_MMGR_REALLOC (stio->mmgr, stio->tmr.jobs, new_capa * STIO_SIZEOF(*tmp));
|
|
|
|
if (tmp == STIO_NULL)
|
|
|
|
{
|
|
|
|
stio->errnum = STIO_ENOMEM;
|
|
|
|
return STIO_TMRIDX_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
stio->tmr.jobs = tmp;
|
|
|
|
stio->tmr.capa = new_capa;
|
|
|
|
}
|
|
|
|
|
|
|
|
stio->tmr.size = stio->tmr.size + 1;
|
|
|
|
stio->tmr.jobs[index] = *job;
|
2016-01-31 05:39:12 +00:00
|
|
|
#if defined(STIO_USE_TMRJOB_IDXPTR)
|
|
|
|
if (stio->tmr.jobs[index].idxptr) *stio->tmr.jobs[index].idxptr = index;
|
|
|
|
#endif
|
2016-01-30 05:24:23 +00:00
|
|
|
return sift_up (stio, index, 0);
|
|
|
|
}
|
|
|
|
|
2016-03-04 12:36:44 +00:00
|
|
|
stio_tmridx_t stio_updtmrjob (stio_t* stio, stio_tmridx_t index, const stio_tmrjob_t* job)
|
2016-01-30 05:24:23 +00:00
|
|
|
{
|
|
|
|
stio_tmrjob_t item;
|
|
|
|
item = stio->tmr.jobs[index];
|
|
|
|
stio->tmr.jobs[index] = *job;
|
2016-01-31 05:39:12 +00:00
|
|
|
#if defined(STIO_USE_TMRJOB_IDXPTR)
|
|
|
|
if (stio->tmr.jobs[index].idxptr) *stio->tmr.jobs[index].idxptr = index;
|
|
|
|
#endif
|
2016-01-30 05:24:23 +00:00
|
|
|
return YOUNGER_THAN(job, &item)? sift_up (stio, index, 0): sift_down (stio, index, 0);
|
|
|
|
}
|
|
|
|
|
2016-01-30 19:08:28 +00:00
|
|
|
void stio_firetmrjobs (stio_t* stio, const stio_ntime_t* tm, stio_size_t* firecnt)
|
2016-01-30 05:24:23 +00:00
|
|
|
{
|
|
|
|
stio_ntime_t now;
|
2016-01-31 05:39:12 +00:00
|
|
|
stio_tmrjob_t tmrjob;
|
2016-01-30 19:08:28 +00:00
|
|
|
stio_size_t count = 0;
|
2016-01-30 05:24:23 +00:00
|
|
|
|
|
|
|
/* if the current time is not specified, get it from the system */
|
|
|
|
if (tm) now = *tm;
|
2016-01-30 19:08:28 +00:00
|
|
|
else stio_gettime (&now);
|
2016-01-30 05:24:23 +00:00
|
|
|
|
|
|
|
while (stio->tmr.size > 0)
|
|
|
|
{
|
|
|
|
if (stio_cmptime(&stio->tmr.jobs[0].when, &now) > 0) break;
|
|
|
|
|
2016-01-31 05:39:12 +00:00
|
|
|
tmrjob = stio->tmr.jobs[0]; /* copy the scheduled job */
|
|
|
|
stio_deltmrjob (stio, 0); /* deschedule the job */
|
2016-01-30 05:24:23 +00:00
|
|
|
|
2016-01-30 19:08:28 +00:00
|
|
|
count++;
|
2016-01-31 05:39:12 +00:00
|
|
|
tmrjob.handler (stio, &now, &tmrjob); /* then fire the job */
|
2016-01-30 05:24:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-30 19:08:28 +00:00
|
|
|
if (firecnt) *firecnt = count;
|
2016-01-30 05:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int stio_gettmrtmout (stio_t* stio, const stio_ntime_t* tm, stio_ntime_t* tmout)
|
|
|
|
{
|
|
|
|
stio_ntime_t now;
|
|
|
|
|
2016-01-30 19:08:28 +00:00
|
|
|
/* time-out can't be calculated when there's no job scheduled */
|
2016-01-30 05:24:23 +00:00
|
|
|
if (stio->tmr.size <= 0)
|
|
|
|
{
|
|
|
|
stio->errnum = STIO_ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if the current time is not specified, get it from the system */
|
|
|
|
if (tm) now = *tm;
|
2016-01-30 19:08:28 +00:00
|
|
|
else stio_gettime (&now);
|
2016-01-30 05:24:23 +00:00
|
|
|
|
|
|
|
stio_subtime (&stio->tmr.jobs[0].when, &now, tmout);
|
|
|
|
if (tmout->sec < 0) stio_cleartime (tmout);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
stio_tmrjob_t* stio_gettmrjob (stio_t* stio, stio_tmridx_t index)
|
|
|
|
{
|
|
|
|
return (index < 0 || index >= stio->tmr.size)? STIO_NULL: &stio->tmr.jobs[index];
|
|
|
|
}
|