ant-http/libs/scheduler.h

112 lines
2.3 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
2018-09-27 15:00:19 +02:00
typedef enum { LIGHT, HEAVY } antd_task_type_t;
2018-09-25 17:51:56 +02:00
// callback definition
typedef struct __callback_t{
void* (*handle)(void*);
struct __callback_t * next;
} antd_callback_t;
// task definition
typedef struct {
/*
creation time of a task
*/
unsigned long stamp;
/*
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
*/
void* (*handle)(void*);
antd_callback_t* callback;
/*
user data if any
*/
2018-09-27 15:00:19 +02:00
void * data;
/*
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
*/
antd_task_type_t type;
2018-09-25 17:51:56 +02:00
} antd_task_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 {
2018-10-10 12:42:47 +02:00
int id;
pthread_t tid;
void* manager;
} antd_worker_t;
typedef struct {
// 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
2018-10-10 12:42:47 +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;
2018-09-25 17:51:56 +02:00
} antd_scheduler_t;
/*
init the main scheduler
*/
2018-10-01 22:49:20 +02:00
void antd_scheduler_init(antd_scheduler_t*, int);
2018-09-25 17:51:56 +02:00
/*
destroy all pending task
*/
2018-10-01 22:49:20 +02:00
void antd_scheduler_destroy(antd_scheduler_t*);
2018-09-25 17:51:56 +02:00
/*
create a task
*/
antd_task_t* antd_create_task(void* (*handle)(void*), void *data, void* (*callback)(void*));
/*
2018-10-01 22:49:20 +02:00
add a task
2018-09-25 17:51:56 +02:00
*/
2018-10-01 22:49:20 +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
*/
2018-10-01 22:49:20 +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
*/
2018-10-01 22:49:20 +02:00
int antd_scheduler_busy(antd_scheduler_t*);
/*
schedule a task
*/
2018-10-09 17:24:00 +02:00
int antd_task_schedule(antd_scheduler_t*);
2018-10-10 12:42:47 +02:00
/*
wait for event
*/
void antd_wait(antd_scheduler_t*);
2018-09-25 17:51:56 +02:00
#endif