Enhance scheduler, cleanup lib

This commit is contained in:
lxsang
2020-08-25 16:40:24 +02:00
parent 2041ee2ba0
commit 776bd017e2
21 changed files with 165 additions and 134 deletions

View File

@ -1,4 +1,8 @@
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "scheduler.h"
#include "utils.h"
static void enqueue(antd_task_queue_t* q, antd_task_t* task)
{
@ -94,6 +98,11 @@ static void execute_callback(antd_scheduler_t* scheduler, antd_task_t* task)
// call the first come call back
task->handle = cb->handle;
task->callback = task->callback->next;
task->priority = task->priority + 1;
if(task->priority > N_PRIORITY - 1)
{
task->priority = N_PRIORITY - 1;
}
free(cb);
antd_add_task(scheduler, task);
}
@ -150,7 +159,7 @@ static void* work(antd_worker_t* worker)
init the main scheduler
*/
void antd_scheduler_init(antd_scheduler_t* scheduler, int n)
int antd_scheduler_init(antd_scheduler_t* scheduler, int n)
{
scheduler->n_workers = n;
scheduler->status = 1;
@ -163,13 +172,13 @@ void antd_scheduler_init(antd_scheduler_t* scheduler, int n)
if (scheduler->scheduler_sem == SEM_FAILED)
{
ERROR("Cannot open semaphore for scheduler");
exit(-1);
return -1;
}
scheduler->worker_sem = sem_open("worker", O_CREAT, 0600, 0);
if (!scheduler->worker_sem)
{
ERROR("Cannot open semaphore for workers");
exit(-1);
return -1;
}
// init lock
pthread_mutex_init(&scheduler->scheduler_lock,NULL);
@ -183,7 +192,7 @@ void antd_scheduler_init(antd_scheduler_t* scheduler, int n)
if(!scheduler->workers)
{
ERROR("Cannot allocate memory for worker");
exit(-1);
return -1;
}
for(int i = 0; i < scheduler->n_workers;i++)
{
@ -191,7 +200,8 @@ void antd_scheduler_init(antd_scheduler_t* scheduler, int n)
scheduler->workers[i].manager = (void*)scheduler;
if (pthread_create(&scheduler->workers[i].tid, NULL,(void *(*)(void *))work, (void*)&scheduler->workers[i]) != 0)
{
perror("pthread_create: cannot create worker\n");
ERROR("pthread_create: cannot create worker: %s", strerror(errno));
return -1;
}
else
{
@ -200,6 +210,7 @@ void antd_scheduler_init(antd_scheduler_t* scheduler, int n)
}
}
LOG("Antd scheduler initialized with %d worker", scheduler->n_workers);
return 0;
}
/*
destroy all pending task
@ -227,7 +238,7 @@ antd_task_t* antd_create_task(void* (*handle)(void*), void *data, void* (*callba
task->data = data;
task->handle = handle;
task->callback = callback_of(callback);
task->priority = NORMAL_PRIORITY;
task->priority = HIGH_PRIORITY;
task->type = HEAVY;
//task->type = LIGHT;
task->access_time = atime;
@ -258,6 +269,7 @@ void antd_execute_task(antd_scheduler_t* scheduler, antd_task_item_t taski)
if(!taski)
return;
// execute the task
LOG("Execute task with priority: %d", taski->task->priority);
void *ret = (*(taski->task->handle))(taski->task->data);
// check the return data if it is a new task
if(!ret)
@ -289,6 +301,11 @@ void antd_execute_task(antd_scheduler_t* scheduler, antd_task_item_t taski)
}
else
{
rtask->priority = taski->task->priority + 1;
if(rtask->priority > N_PRIORITY - 1)
{
rtask->priority = N_PRIORITY - 1;
}
antd_add_task(scheduler, rtask);
free(taski->task);
free(taski);
@ -324,10 +341,10 @@ int antd_task_schedule(antd_scheduler_t* scheduler)
}
// has the task now
// validate the task
if(scheduler->validate_data && difftime( time(NULL), it->task->access_time) > MAX_VALIDITY_INTERVAL)
if(scheduler->validate_data && difftime( time(NULL), it->task->access_time) > MAX_VALIDITY_INTERVAL && it->task->priority == N_PRIORITY - 1)
{
// data task is not valid
LOG("Task data is not valid, task will be killed");
// LOG("Task is no longer valid and will be killed");
if(scheduler->destroy_data)
scheduler->destroy_data(it->task->data);
if(it->task->callback)