1
0
mirror of https://github.com/lxsang/ant-http synced 2024-07-09 15:39:48 +02:00
ant-http/lib/scheduler.h

138 lines
2.7 KiB
C
Raw Normal View History

2018-09-25 17:51:56 +02:00
#ifndef ANT_SCHEDULER
#define ANT_SCHEDULER
#include "utils.h"
#include <pthread.h>
2018-10-10 12:42:47 +02:00
#include <semaphore.h>
2018-10-14 11:25:13 +02:00
#include <fcntl.h>
2018-09-25 17:51:56 +02:00
#define N_PRIORITY 10
#define NORMAL_PRIORITY ((int)((N_PRIORITY - 1) / 2))
#define LOW_PRIORITY (N_PRIORITY - 1)
#define HIGH_PRIORITY 0
2019-07-31 15:11:59 +02:00
#define MAX_VALIDITY_INTERVAL 20 // 10 s for task validity
typedef enum
{
LIGHT,
HEAVY
} antd_task_type_t;
2018-09-25 17:51:56 +02:00
// callback definition
2019-07-31 15:11:59 +02:00
typedef struct __callback_t
{
void *(*handle)(void *);
struct __callback_t *next;
2018-09-25 17:51:56 +02:00
} antd_callback_t;
// task definition
2019-07-31 15:11:59 +02:00
typedef struct
{
2018-09-25 17:51:56 +02:00
/*
creation time of a task
*/
unsigned long stamp;
2019-07-31 15:11:59 +02:00
/*
Last access time of
task data
*/
time_t access_time;
2018-09-25 17:51:56 +02:00
/*
2018-10-01 22:49:20 +02:00
priority from 0 to N_PRIORITY - 1
2018-09-25 17:51:56 +02:00
higher value is lower priority
*/
uint8_t priority;
/*
the callback
*/
2019-07-31 15:11:59 +02:00
void *(*handle)(void *);
antd_callback_t *callback;
/*
2018-09-25 17:51:56 +02:00
user data if any
*/
2019-07-31 15:11:59 +02:00
void *data;
2018-09-27 15:00:19 +02:00
/*
type of a task
2018-10-01 22:49:20 +02:00
light tasks are executed directly
heavy tasks are delegated to workers
2018-09-27 15:00:19 +02:00
*/
2019-07-31 15:11:59 +02:00
antd_task_type_t type;
2018-09-25 17:51:56 +02:00
} antd_task_t;
2019-07-31 15:11:59 +02:00
typedef struct __task_item_t
{
antd_task_t *task;
struct __task_item_t *next;
} * antd_task_item_t;
2018-09-25 17:51:56 +02:00
typedef antd_task_item_t antd_task_queue_t;
2019-07-31 15:11:59 +02:00
typedef struct
{
2018-10-10 12:42:47 +02:00
int id;
pthread_t tid;
2019-07-31 15:11:59 +02:00
void *manager;
2018-10-10 12:42:47 +02:00
} antd_worker_t;
2019-07-31 15:11:59 +02:00
typedef struct
{
2018-10-10 12:42:47 +02:00
// data lock
2018-09-27 15:00:19 +02:00
pthread_mutex_t scheduler_lock;
pthread_mutex_t worker_lock;
2018-10-01 22:49:20 +02:00
pthread_mutex_t pending_lock;
2018-10-10 12:42:47 +02:00
// event handle
2018-10-14 10:57:09 +02:00
sem_t *scheduler_sem;
sem_t *worker_sem;
2018-10-10 12:42:47 +02:00
// worker and data
2018-09-25 17:51:56 +02:00
antd_task_queue_t task_queue[N_PRIORITY];
2018-09-27 15:00:19 +02:00
antd_task_queue_t workers_queue;
2018-09-25 17:51:56 +02:00
uint8_t status; // 0 stop, 1 working
2019-07-31 15:11:59 +02:00
antd_worker_t *workers;
2018-09-25 17:51:56 +02:00
int n_workers;
2018-10-01 22:49:20 +02:00
int pending_task;
2019-07-31 15:11:59 +02:00
/*
function pointer that free data in a task if
the task is not valid
default to NULL
*/
void* (*destroy_data)(void*);
int validate_data;
2018-09-25 17:51:56 +02:00
} antd_scheduler_t;
/*
init the main scheduler
*/
2019-07-31 15:11:59 +02:00
void antd_scheduler_init(antd_scheduler_t *, int);
2018-09-25 17:51:56 +02:00
/*
destroy all pending task
*/
2019-07-31 15:11:59 +02:00
void antd_scheduler_destroy(antd_scheduler_t *);
2018-09-25 17:51:56 +02:00
/*
create a task
2019-07-31 15:11:59 +02:00
parameter:
- handle
- data
- callback
- last data access time
2018-09-25 17:51:56 +02:00
*/
2019-07-31 15:11:59 +02:00
antd_task_t *antd_create_task(void *(*handle)(void *), void *data, void *(*callback)(void *), time_t);
2018-09-25 17:51:56 +02:00
/*
2018-10-01 22:49:20 +02:00
add a task
2018-09-25 17:51:56 +02:00
*/
2019-07-31 15:11:59 +02:00
void antd_add_task(antd_scheduler_t *, antd_task_t *);
2018-09-25 17:51:56 +02:00
/*
2018-10-01 22:49:20 +02:00
execute and free a task a task
2018-09-25 17:51:56 +02:00
*/
2019-07-31 15:11:59 +02:00
void antd_execute_task(antd_scheduler_t *, antd_task_item_t);
2018-09-25 17:51:56 +02:00
/*
2018-10-01 22:49:20 +02:00
scheduler status
2018-09-25 17:51:56 +02:00
*/
2019-07-31 15:11:59 +02:00
int antd_scheduler_busy(antd_scheduler_t *);
2018-10-01 22:49:20 +02:00
/*
schedule a task
*/
2019-07-31 15:11:59 +02:00
int antd_task_schedule(antd_scheduler_t *);
2018-10-10 12:42:47 +02:00
/*
wait for event
*/
2019-07-31 15:11:59 +02:00
void antd_wait(antd_scheduler_t *);
2018-09-25 17:51:56 +02:00
#endif