2023-05-13 17:30:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
|
|
*
|
|
|
|
* 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 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.
|
|
|
|
*/
|
|
|
|
#include <Cron.h>
|
|
|
|
|
|
|
|
#include <Array.h>
|
|
|
|
#include <Memory.h>
|
|
|
|
#include <Util.h>
|
|
|
|
|
2024-01-13 22:13:45 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2023-05-13 17:30:09 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
struct Cron
|
|
|
|
{
|
2024-01-13 22:13:45 +00:00
|
|
|
uint64_t tick;
|
2023-05-13 17:30:09 +00:00
|
|
|
Array *jobs;
|
|
|
|
pthread_mutex_t lock;
|
|
|
|
pthread_t thread;
|
2024-01-13 22:13:45 +00:00
|
|
|
volatile bool stop;
|
2023-05-13 17:30:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct Job
|
|
|
|
{
|
2024-01-13 22:13:45 +00:00
|
|
|
uint64_t interval;
|
|
|
|
uint64_t lastExec;
|
2023-05-13 17:30:09 +00:00
|
|
|
JobFunc *func;
|
|
|
|
void *args;
|
|
|
|
} Job;
|
|
|
|
|
|
|
|
static Job *
|
2024-01-13 22:13:45 +00:00
|
|
|
JobCreate(uint64_t interval, JobFunc * func, void *args)
|
2023-05-13 17:30:09 +00:00
|
|
|
{
|
|
|
|
Job *job;
|
|
|
|
|
|
|
|
if (!func)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
job = Malloc(sizeof(Job));
|
|
|
|
if (!job)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-01-13 22:13:45 +00:00
|
|
|
job->interval = interval;
|
|
|
|
job->lastExec = 0;
|
2023-05-13 17:30:09 +00:00
|
|
|
job->func = func;
|
|
|
|
job->args = args;
|
|
|
|
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
CronThread(void *args)
|
|
|
|
{
|
|
|
|
Cron *cron = args;
|
|
|
|
|
|
|
|
while (!cron->stop)
|
|
|
|
{
|
|
|
|
size_t i;
|
2024-01-13 22:13:45 +00:00
|
|
|
uint64_t ts; /* tick start */
|
|
|
|
uint64_t te; /* tick end */
|
2023-05-13 17:30:09 +00:00
|
|
|
|
|
|
|
pthread_mutex_lock(&cron->lock);
|
|
|
|
|
2024-01-13 22:13:45 +00:00
|
|
|
ts = UtilTsMillis();
|
2023-05-13 17:30:09 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ArraySize(cron->jobs); i++)
|
|
|
|
{
|
|
|
|
Job *job = ArrayGet(cron->jobs, i);
|
|
|
|
|
2024-01-13 22:13:45 +00:00
|
|
|
if ((ts - job->lastExec) > job->interval)
|
2023-05-13 17:30:09 +00:00
|
|
|
{
|
|
|
|
job->func(job->args);
|
|
|
|
job->lastExec = ts;
|
|
|
|
}
|
|
|
|
|
2024-01-13 22:13:45 +00:00
|
|
|
if (!job->interval)
|
2023-05-13 17:30:09 +00:00
|
|
|
{
|
|
|
|
ArrayDelete(cron->jobs, i);
|
|
|
|
Free(job);
|
|
|
|
}
|
|
|
|
}
|
2024-01-13 22:13:45 +00:00
|
|
|
te = UtilTsMillis();
|
2023-05-13 17:30:09 +00:00
|
|
|
|
|
|
|
pthread_mutex_unlock(&cron->lock);
|
|
|
|
|
2024-01-13 22:13:45 +00:00
|
|
|
// Only sleep if the jobs didn't overrun the tick
|
|
|
|
if (cron->tick > (te - ts))
|
2023-05-13 17:30:09 +00:00
|
|
|
{
|
2024-01-13 22:13:45 +00:00
|
|
|
const uint64_t microTick = 100;
|
2023-08-12 23:02:06 +00:00
|
|
|
|
2024-01-13 22:13:45 +00:00
|
|
|
uint64_t remainingTick = cron->tick - (te - ts);
|
2023-05-13 17:30:09 +00:00
|
|
|
|
|
|
|
/* Only sleep for microTick ms at a time because if the job
|
|
|
|
* scheduler is supposed to stop before the tick is up, we
|
|
|
|
* don't want to be stuck in a long sleep */
|
2024-01-13 22:13:45 +00:00
|
|
|
while (remainingTick >= microTick && !cron->stop)
|
2023-05-13 17:30:09 +00:00
|
|
|
{
|
|
|
|
UtilSleepMillis(microTick);
|
2023-08-12 23:02:06 +00:00
|
|
|
|
2024-01-13 22:13:45 +00:00
|
|
|
remainingTick -= microTick;
|
2023-05-13 17:30:09 +00:00
|
|
|
}
|
|
|
|
|
2024-01-13 22:13:45 +00:00
|
|
|
if (remainingTick && !cron->stop)
|
2023-05-13 17:30:09 +00:00
|
|
|
{
|
|
|
|
UtilSleepMillis(remainingTick);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cron *
|
2024-01-13 22:13:45 +00:00
|
|
|
CronCreate(uint64_t tick)
|
2023-05-13 17:30:09 +00:00
|
|
|
{
|
|
|
|
Cron *cron = Malloc(sizeof(Cron));
|
|
|
|
|
|
|
|
if (!cron)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cron->jobs = ArrayCreate();
|
|
|
|
if (!cron->jobs)
|
|
|
|
{
|
|
|
|
Free(cron);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-01-13 22:13:45 +00:00
|
|
|
cron->tick = tick;
|
|
|
|
cron->stop = true;
|
2023-05-13 17:30:09 +00:00
|
|
|
|
|
|
|
pthread_mutex_init(&cron->lock, NULL);
|
|
|
|
|
|
|
|
return cron;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CronOnce(Cron * cron, JobFunc * func, void *args)
|
|
|
|
{
|
|
|
|
Job *job;
|
|
|
|
|
|
|
|
if (!cron || !func)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
job = JobCreate(0, func, args);
|
|
|
|
if (!job)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&cron->lock);
|
|
|
|
ArrayAdd(cron->jobs, job);
|
|
|
|
pthread_mutex_unlock(&cron->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2024-01-13 22:13:45 +00:00
|
|
|
CronEvery(Cron * cron, uint64_t interval, JobFunc * func, void *args)
|
2023-05-13 17:30:09 +00:00
|
|
|
{
|
|
|
|
Job *job;
|
|
|
|
|
|
|
|
if (!cron || !func)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
job = JobCreate(interval, func, args);
|
|
|
|
if (!job)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&cron->lock);
|
|
|
|
ArrayAdd(cron->jobs, job);
|
|
|
|
pthread_mutex_unlock(&cron->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CronStart(Cron * cron)
|
|
|
|
{
|
|
|
|
if (!cron || !cron->stop)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-13 22:13:45 +00:00
|
|
|
cron->stop = false;
|
2023-05-13 17:30:09 +00:00
|
|
|
|
|
|
|
pthread_create(&cron->thread, NULL, CronThread, cron);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CronStop(Cron * cron)
|
|
|
|
{
|
|
|
|
if (!cron || cron->stop)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-13 22:13:45 +00:00
|
|
|
cron->stop = true;
|
2023-05-13 17:30:09 +00:00
|
|
|
|
|
|
|
pthread_join(cron->thread, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CronFree(Cron * cron)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!cron)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CronStop(cron);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&cron->lock);
|
|
|
|
for (i = 0; i < ArraySize(cron->jobs); i++)
|
|
|
|
{
|
|
|
|
Free(ArrayGet(cron->jobs, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayFree(cron->jobs);
|
|
|
|
pthread_mutex_unlock(&cron->lock);
|
|
|
|
pthread_mutex_destroy(&cron->lock);
|
|
|
|
|
|
|
|
Free(cron);
|
|
|
|
}
|