hio/lib/tmr.c

250 lines
6.9 KiB
C
Raw Normal View History

2018-12-12 13:15:54 +00:00
/*
Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved.
2018-12-12 13:15:54 +00:00
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
2022-06-11 05:32:01 +00:00
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2018-12-12 13:15:54 +00:00
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.
*/
2021-07-22 07:30:20 +00:00
#include "hio-prv.h"
2018-12-12 13:15:54 +00:00
#define HEAP_PARENT(x) (((x) - 1) / 2)
#define HEAP_LEFT(x) ((x) * 2 + 1)
#define HEAP_RIGHT(x) ((x) * 2 + 2)
2021-07-22 07:30:20 +00:00
#define YOUNGER_THAN(x,y) (HIO_CMP_NTIME(&(x)->when, &(y)->when) < 0)
2018-12-12 13:15:54 +00:00
2021-07-22 07:30:20 +00:00
void hio_cleartmrjobs (hio_t* hio)
2018-12-12 13:15:54 +00:00
{
2021-07-22 07:30:20 +00:00
while (hio->tmr.size > 0) hio_deltmrjob (hio, 0);
2018-12-12 13:15:54 +00:00
}
2021-07-22 07:30:20 +00:00
static hio_tmridx_t sift_up (hio_t* hio, hio_tmridx_t index)
2018-12-12 13:15:54 +00:00
{
2021-07-22 07:30:20 +00:00
hio_tmridx_t parent;
2018-12-12 13:15:54 +00:00
parent = HEAP_PARENT(index);
2021-07-22 07:30:20 +00:00
if (index > 0 && YOUNGER_THAN(&hio->tmr.jobs[index], &hio->tmr.jobs[parent]))
2018-12-12 13:15:54 +00:00
{
2021-07-22 07:30:20 +00:00
hio_tmrjob_t item;
2018-12-12 13:15:54 +00:00
2021-07-22 07:30:20 +00:00
item = hio->tmr.jobs[index];
2018-12-12 13:15:54 +00:00
do
{
/* move down the parent to my current position */
2021-07-22 07:30:20 +00:00
hio->tmr.jobs[index] = hio->tmr.jobs[parent];
if (hio->tmr.jobs[index].idxptr) *hio->tmr.jobs[index].idxptr = index;
2018-12-12 13:15:54 +00:00
/* traverse up */
index = parent;
parent = HEAP_PARENT(parent);
}
2021-07-22 07:30:20 +00:00
while (index > 0 && YOUNGER_THAN(&item, &hio->tmr.jobs[parent]));
2018-12-12 13:15:54 +00:00
2021-07-22 07:30:20 +00:00
hio->tmr.jobs[index] = item;
if (hio->tmr.jobs[index].idxptr) *hio->tmr.jobs[index].idxptr = index;
2018-12-12 13:15:54 +00:00
}
return index;
}
2021-07-22 07:30:20 +00:00
static hio_tmridx_t sift_down (hio_t* hio, hio_tmridx_t index)
2018-12-12 13:15:54 +00:00
{
2021-07-22 07:30:20 +00:00
hio_oow_t base = hio->tmr.size / 2;
2018-12-12 13:15:54 +00:00
if (index < base) /* at least 1 child is under the 'index' position */
{
2021-07-22 07:30:20 +00:00
hio_tmrjob_t item;
2018-12-12 13:15:54 +00:00
2021-07-22 07:30:20 +00:00
item = hio->tmr.jobs[index];
2018-12-12 13:15:54 +00:00
do
{
2021-07-22 07:30:20 +00:00
hio_tmridx_t left, right, younger;
2018-12-12 13:15:54 +00:00
left = HEAP_LEFT(index);
right = HEAP_RIGHT(index);
2021-07-22 07:30:20 +00:00
if (right < hio->tmr.size && YOUNGER_THAN(&hio->tmr.jobs[right], &hio->tmr.jobs[left]))
2018-12-12 13:15:54 +00:00
{
younger = right;
}
else
{
younger = left;
}
2021-07-22 07:30:20 +00:00
if (YOUNGER_THAN(&item, &hio->tmr.jobs[younger])) break;
2018-12-12 13:15:54 +00:00
2021-07-22 07:30:20 +00:00
hio->tmr.jobs[index] = hio->tmr.jobs[younger];
if (hio->tmr.jobs[index].idxptr) *hio->tmr.jobs[index].idxptr = index;
2018-12-12 13:15:54 +00:00
index = younger;
}
while (index < base);
2021-07-22 07:30:20 +00:00
hio->tmr.jobs[index] = item;
if (hio->tmr.jobs[index].idxptr) *hio->tmr.jobs[index].idxptr = index;
2018-12-12 13:15:54 +00:00
}
return index;
}
2021-07-22 07:30:20 +00:00
void hio_deltmrjob (hio_t* hio, hio_tmridx_t index)
2018-12-12 13:15:54 +00:00
{
2021-07-22 07:30:20 +00:00
hio_tmrjob_t item;
2018-12-12 13:15:54 +00:00
2021-07-22 07:30:20 +00:00
HIO_ASSERT (hio, index < hio->tmr.size);
2018-12-12 13:15:54 +00:00
2021-07-22 07:30:20 +00:00
item = hio->tmr.jobs[index];
if (hio->tmr.jobs[index].idxptr) *hio->tmr.jobs[index].idxptr = HIO_TMRIDX_INVALID;
2018-12-12 13:15:54 +00:00
2021-07-22 07:30:20 +00:00
hio->tmr.size = hio->tmr.size - 1;
if (hio->tmr.size > 0 && index != hio->tmr.size)
2018-12-12 13:15:54 +00:00
{
2021-07-22 07:30:20 +00:00
hio->tmr.jobs[index] = hio->tmr.jobs[hio->tmr.size];
if (hio->tmr.jobs[index].idxptr) *hio->tmr.jobs[index].idxptr = index;
YOUNGER_THAN(&hio->tmr.jobs[index], &item)? sift_up(hio, index): sift_down(hio, index);
2018-12-12 13:15:54 +00:00
}
}
2021-07-22 07:30:20 +00:00
hio_tmridx_t hio_instmrjob (hio_t* hio, const hio_tmrjob_t* job)
2018-12-12 13:15:54 +00:00
{
2021-07-22 07:30:20 +00:00
hio_tmridx_t index = hio->tmr.size;
2018-12-12 13:15:54 +00:00
2021-07-22 07:30:20 +00:00
if (index >= hio->tmr.capa)
2018-12-12 13:15:54 +00:00
{
2021-07-22 07:30:20 +00:00
hio_tmrjob_t* tmp;
hio_oow_t new_capa;
2018-12-12 13:15:54 +00:00
2021-07-22 07:30:20 +00:00
HIO_ASSERT (hio, hio->tmr.capa >= 1);
new_capa = hio->tmr.capa * 2;
tmp = (hio_tmrjob_t*)hio_reallocmem(hio, hio->tmr.jobs, new_capa * HIO_SIZEOF(*tmp));
if (!tmp) return HIO_TMRIDX_INVALID;
2018-12-12 13:15:54 +00:00
2021-07-22 07:30:20 +00:00
hio->tmr.jobs = tmp;
hio->tmr.capa = new_capa;
2018-12-12 13:15:54 +00:00
}
2021-07-22 07:30:20 +00:00
hio->tmr.size = hio->tmr.size + 1;
hio->tmr.jobs[index] = *job;
if (hio->tmr.jobs[index].idxptr) *hio->tmr.jobs[index].idxptr = index;
return sift_up(hio, index);
2018-12-12 13:15:54 +00:00
}
2021-07-22 07:30:20 +00:00
hio_tmridx_t hio_updtmrjob (hio_t* hio, hio_tmridx_t index, const hio_tmrjob_t* job)
2018-12-12 13:15:54 +00:00
{
2021-07-22 07:30:20 +00:00
hio_tmrjob_t item;
item = hio->tmr.jobs[index];
hio->tmr.jobs[index] = *job;
if (hio->tmr.jobs[index].idxptr) *hio->tmr.jobs[index].idxptr = index;
return YOUNGER_THAN(job, &item)? sift_up(hio, index): sift_down(hio, index);
2018-12-12 13:15:54 +00:00
}
2021-07-22 07:30:20 +00:00
void hio_firetmrjobs (hio_t* hio, const hio_ntime_t* tm, hio_oow_t* firecnt)
2018-12-12 13:15:54 +00:00
{
2021-07-22 07:30:20 +00:00
hio_ntime_t now;
hio_tmrjob_t tmrjob;
hio_oow_t count = 0;
2018-12-12 13:15:54 +00:00
/* if the current time is not specified, get it from the system */
if (tm) now = *tm;
2021-07-22 07:30:20 +00:00
else hio_gettime (hio, &now);
2018-12-12 13:15:54 +00:00
2021-07-22 07:30:20 +00:00
while (hio->tmr.size > 0)
2018-12-12 13:15:54 +00:00
{
2021-07-22 07:30:20 +00:00
if (HIO_CMP_NTIME(&hio->tmr.jobs[0].when, &now) > 0) break;
2018-12-12 13:15:54 +00:00
2021-07-22 07:30:20 +00:00
tmrjob = hio->tmr.jobs[0]; /* copy the scheduled job */
hio_deltmrjob (hio, 0); /* deschedule the job */
2018-12-12 13:15:54 +00:00
count++;
2021-07-22 07:30:20 +00:00
tmrjob.handler (hio, &now, &tmrjob); /* then fire the job */
2018-12-12 13:15:54 +00:00
}
if (firecnt) *firecnt = count;
}
2021-07-22 07:30:20 +00:00
int hio_gettmrtmout (hio_t* hio, const hio_ntime_t* tm, hio_ntime_t* tmout)
2018-12-12 13:15:54 +00:00
{
2021-07-22 07:30:20 +00:00
hio_ntime_t now;
2018-12-12 13:15:54 +00:00
/* time-out can't be calculated when there's no job scheduled */
2021-07-22 07:30:20 +00:00
if (hio->tmr.size <= 0) return 0; /* no scheduled job */
2018-12-12 13:15:54 +00:00
/* if the current time is not specified, get it from the system */
if (tm) now = *tm;
2021-07-22 07:30:20 +00:00
else hio_gettime (hio, &now);
2018-12-12 13:15:54 +00:00
2021-07-22 07:30:20 +00:00
HIO_SUB_NTIME (tmout, &hio->tmr.jobs[0].when, &now);
if (tmout->sec < 0) HIO_CLEAR_NTIME (tmout);
return 1; /* tmout is set */
2018-12-12 13:15:54 +00:00
}
2021-07-22 07:30:20 +00:00
hio_tmrjob_t* hio_gettmrjob (hio_t* hio, hio_tmridx_t index)
2018-12-12 13:15:54 +00:00
{
2021-07-22 07:30:20 +00:00
if (index < 0 || index >= hio->tmr.size)
2018-12-12 13:15:54 +00:00
{
2021-07-22 07:30:20 +00:00
hio_seterrbfmt (hio, HIO_ENOENT, "unable to get timer job as the given index is out of range");
return HIO_NULL;
2018-12-12 13:15:54 +00:00
}
2021-07-22 07:30:20 +00:00
return &hio->tmr.jobs[index];
2018-12-12 13:15:54 +00:00
}
2021-07-22 07:30:20 +00:00
int hio_gettmrjobdeadline (hio_t* hio, hio_tmridx_t index, hio_ntime_t* deadline)
2018-12-12 13:15:54 +00:00
{
2021-07-22 07:30:20 +00:00
if (index < 0 || index >= hio->tmr.size)
2018-12-12 13:15:54 +00:00
{
2021-07-22 07:30:20 +00:00
hio_seterrbfmt (hio, HIO_ENOENT, "unable to get timer job deadline as the given index is out of range");
2018-12-12 13:15:54 +00:00
return -1;
}
2021-07-22 07:30:20 +00:00
*deadline = hio->tmr.jobs[index].when;
2018-12-12 13:15:54 +00:00
return 0;
}
2021-07-22 07:30:20 +00:00
int hio_schedtmrjobat (hio_t* hio, const hio_ntime_t* fire_at, hio_tmrjob_handler_t handler, hio_tmridx_t* tmridx, void* ctx)
{
2021-07-22 07:30:20 +00:00
hio_tmrjob_t tmrjob;
2021-07-22 07:30:20 +00:00
HIO_MEMSET (&tmrjob, 0, HIO_SIZEOF(tmrjob));
tmrjob.ctx = ctx;
if (fire_at) tmrjob.when = *fire_at;
tmrjob.handler = handler;
tmrjob.idxptr = tmridx;
2021-07-22 07:30:20 +00:00
return hio_instmrjob(hio, &tmrjob) == HIO_TMRIDX_INVALID? -1: 0;
}
2021-07-22 07:30:20 +00:00
int hio_schedtmrjobafter (hio_t* hio, const hio_ntime_t* fire_after, hio_tmrjob_handler_t handler, hio_tmridx_t* tmridx, void* ctx)
{
2021-07-22 07:30:20 +00:00
hio_ntime_t fire_at;
2021-07-22 07:30:20 +00:00
HIO_ASSERT (hio, HIO_IS_POS_NTIME(fire_after));
2021-07-22 07:30:20 +00:00
hio_gettime (hio, &fire_at);
HIO_ADD_NTIME (&fire_at, &fire_at, fire_after);
2021-07-22 07:30:20 +00:00
return hio_schedtmrjobat(hio, &fire_at, handler, tmridx, ctx);
}