add data validity to scheduler

This commit is contained in:
lxsang
2019-07-31 15:11:59 +02:00
parent 5155e9e0f5
commit 52ede859bd
7 changed files with 111 additions and 115 deletions

View File

@ -193,6 +193,8 @@ int antd_send(void *src, const void* data, int len)
{
antd_close(src);
}*/
if(written > 0)
time(&source->last_io);
return written;
}
int antd_recv(void *src, void* data, int len)
@ -340,6 +342,8 @@ int antd_recv(void *src, void* data, int len)
{
antd_close(src);
}*/
if(read > 0)
time(&source->last_io);
return read;
}
void set_nonblock(int socket) {

View File

@ -26,7 +26,7 @@
#define __RESULT__ "{\"result\":%d,\"msg\":\"%s\"}"
#define FORM_URL_ENCODE "application/x-www-form-urlencoded"
#define FORM_MULTI_PART "multipart/form-data"
#define MAX_WAIT_S 20 // 1/3 minute
#define MAX_WAIT_S 2 // 1/3 minute
#ifdef USE_OPENSSL
int __attribute__((weak)) usessl();
#endif
@ -38,7 +38,7 @@ typedef struct{
#ifdef USE_OPENSSL
int status;
#endif
time_t last_wait;
time_t last_io;
} antd_client_t;
typedef struct {

View File

@ -154,6 +154,8 @@ void antd_scheduler_init(antd_scheduler_t* scheduler, int n)
scheduler->status = 1;
scheduler->workers_queue = NULL;
scheduler->pending_task = 0 ;
scheduler->validate_data = 0;
scheduler->destroy_data = NULL;
// init semaphore
scheduler->scheduler_sem = sem_open("scheduler", O_CREAT, 0600, 0);
if (scheduler->scheduler_sem == SEM_FAILED)
@ -216,7 +218,7 @@ void antd_scheduler_destroy(antd_scheduler_t* scheduler)
/*
create a task
*/
antd_task_t* antd_create_task(void* (*handle)(void*), void *data, void* (*callback)(void*))
antd_task_t* antd_create_task(void* (*handle)(void*), void *data, void* (*callback)(void*), time_t atime)
{
antd_task_t* task = (antd_task_t*)malloc(sizeof *task);
task->stamp = (unsigned long)time(NULL);
@ -225,7 +227,7 @@ antd_task_t* antd_create_task(void* (*handle)(void*), void *data, void* (*callba
task->callback = callback_of(callback);
task->priority = NORMAL_PRIORITY;
task->type = LIGHT;
task->status = NOSTATUS;
task->access_time = atime;
return task;
}
@ -312,11 +314,26 @@ int antd_task_schedule(antd_scheduler_t* scheduler)
break;
}
pthread_mutex_unlock(&scheduler->scheduler_lock);
// no task
if(!it)
{
return 0;
}
// has the task now
// validate the task
if(scheduler->validate_data && difftime( time(NULL), it->task->access_time) > MAX_VALIDITY_INTERVAL)
{
// data task is not valid
LOG("Task data is not valid \n");
if(scheduler->destroy_data)
scheduler->destroy_data(it->task->data);
if(it->task->callback)
free_callback(it->task->callback);
free(it->task);
free(it);
return 0;
}
// check the type of task
if(it->task->type == LIGHT || scheduler->n_workers <= 0)
{

View File

@ -9,63 +9,69 @@
#define NORMAL_PRIORITY ((int)((N_PRIORITY - 1) / 2))
#define LOW_PRIORITY (N_PRIORITY - 1)
#define HIGH_PRIORITY 0
#define NOSTATUS 0x00
typedef enum { LIGHT, HEAVY } antd_task_type_t;
#define MAX_VALIDITY_INTERVAL 20 // 10 s for task validity
typedef enum
{
LIGHT,
HEAVY
} antd_task_type_t;
// callback definition
typedef struct __callback_t{
void* (*handle)(void*);
struct __callback_t * next;
typedef struct __callback_t
{
void *(*handle)(void *);
struct __callback_t *next;
} antd_callback_t;
// task definition
typedef struct {
typedef struct
{
/*
creation time of a task
*/
unsigned long stamp;
/*
Last access time of
task data
*/
time_t access_time;
/*
priority from 0 to N_PRIORITY - 1
higher value is lower priority
*/
uint8_t priority;
/*
The status of a task
should be set by the application
default value 0x00 means that
there is no status set
*/
uint8_t status;
/*
the callback
*/
void* (*handle)(void*);
antd_callback_t* callback;
/*
void *(*handle)(void *);
antd_callback_t *callback;
/*
user data if any
*/
void * data;
void *data;
/*
type of a task
light tasks are executed directly
heavy tasks are delegated to workers
*/
antd_task_type_t type;
antd_task_type_t type;
} antd_task_t;
typedef struct __task_item_t{
antd_task_t* task;
struct __task_item_t* next;
}* antd_task_item_t;
typedef struct __task_item_t
{
antd_task_t *task;
struct __task_item_t *next;
} * antd_task_item_t;
typedef antd_task_item_t antd_task_queue_t;
typedef struct {
typedef struct
{
int id;
pthread_t tid;
void* manager;
void *manager;
} antd_worker_t;
typedef struct {
typedef struct
{
// data lock
pthread_mutex_t scheduler_lock;
pthread_mutex_t worker_lock;
@ -77,44 +83,56 @@ typedef struct {
antd_task_queue_t task_queue[N_PRIORITY];
antd_task_queue_t workers_queue;
uint8_t status; // 0 stop, 1 working
antd_worker_t* workers;
antd_worker_t *workers;
int n_workers;
int pending_task;
/*
function pointer that free data in a task if
the task is not valid
default to NULL
*/
void* (*destroy_data)(void*);
int validate_data;
} antd_scheduler_t;
/*
init the main scheduler
*/
void antd_scheduler_init(antd_scheduler_t*, int);
void antd_scheduler_init(antd_scheduler_t *, int);
/*
destroy all pending task
*/
void antd_scheduler_destroy(antd_scheduler_t*);
void antd_scheduler_destroy(antd_scheduler_t *);
/*
create a task
parameter:
- handle
- data
- callback
- last data access time
*/
antd_task_t* antd_create_task(void* (*handle)(void*), void *data, void* (*callback)(void*));
antd_task_t *antd_create_task(void *(*handle)(void *), void *data, void *(*callback)(void *), time_t);
/*
add a task
*/
void antd_add_task(antd_scheduler_t*, antd_task_t*);
void antd_add_task(antd_scheduler_t *, antd_task_t *);
/*
execute and free a task a task
*/
void antd_execute_task(antd_scheduler_t*, antd_task_item_t);
void antd_execute_task(antd_scheduler_t *, antd_task_item_t);
/*
scheduler status
*/
int antd_scheduler_busy(antd_scheduler_t*);
int antd_scheduler_busy(antd_scheduler_t *);
/*
schedule a task
*/
int antd_task_schedule(antd_scheduler_t*);
int antd_task_schedule(antd_scheduler_t *);
/*
wait for event
*/
void antd_wait(antd_scheduler_t*);
void antd_wait(antd_scheduler_t *);
#endif