1
0
mirror of https://github.com/lxsang/ant-http synced 2024-07-06 06:29:46 +02:00
ant-http/lib/scheduler.h

119 lines
2.8 KiB
C
Raw Normal View History

2018-09-25 17:51:56 +02:00
#ifndef ANT_SCHEDULER
#define ANT_SCHEDULER
#include <pthread.h>
2018-10-10 12:42:47 +02:00
#include <semaphore.h>
2020-08-25 16:40:24 +02:00
#include <stdint.h>
2021-01-03 11:24:55 +01:00
2021-01-03 21:31:35 +01:00
// define the event
#define TASK_EVT_ALWAY_ON 0x01
#define TASK_EVT_ON_READABLE 0x02
#define TASK_EVT_ON_WRITABLE 0x04
#define TASK_EVT_ON_TIMEOUT 0x08
#define POLL_EVENT_TO 100 // ms
2021-01-03 11:24:55 +01:00
typedef struct _antd_scheduler_t antd_scheduler_t;
typedef struct _antd_callback_t antd_callback_t;
2021-01-03 21:31:35 +01:00
typedef struct _antd_queue_item_t* antd_task_evt_list_t;
2019-07-31 15:11:59 +02:00
typedef struct
{
2021-01-03 11:24:55 +01:00
/**
* task id
*/
int id;
/**
* creation time of a task
2018-09-25 17:51:56 +02:00
*/
unsigned long stamp;
2021-01-03 11:24:55 +01:00
/**
* Last access time of
* task data
2019-07-31 15:11:59 +02:00
*/
time_t access_time;
2021-01-03 11:24:55 +01:00
/**
* the handle and callback
2018-09-25 17:51:56 +02:00
*/
2019-07-31 15:11:59 +02:00
void *(*handle)(void *);
antd_callback_t *callback;
2021-01-03 11:24:55 +01:00
/**
* The task events
* each task must be binded to
* one or more event, otherwise it will be
* rejected by the scheduler
* */
2021-01-03 21:31:35 +01:00
antd_task_evt_list_t events;
2021-01-03 11:24:55 +01:00
/**
* user data if any
*/
2019-07-31 15:11:59 +02:00
void *data;
2018-09-25 17:51:56 +02:00
} antd_task_t;
/*
2021-01-03 11:24:55 +01:00
* nit the main scheduler
2018-09-25 17:51:56 +02:00
*/
2021-01-03 11:24:55 +01:00
antd_scheduler_t *antd_scheduler_init(int, const char *stat_name);
2018-09-25 17:51:56 +02:00
/*
2021-01-03 11:24:55 +01:00
* destroy all pending task
2018-09-25 17:51:56 +02:00
*/
2019-07-31 15:11:59 +02:00
void antd_scheduler_destroy(antd_scheduler_t *);
2018-09-25 17:51:56 +02:00
2021-01-03 11:24:55 +01:00
/**
* create a task
* parameter:
* - handle
* - data
* - callback
* - last data access time
2018-09-25 17:51:56 +02:00
*/
2021-01-03 21:31:35 +01: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
2021-01-03 21:31:35 +01:00
/**
* ALWAY_ON flag doest not need a file descriptor, it will be executed immediately by the scheduler
* ANY file descriptor should work with READABLE and WRITABLE flags, including timerfd for precision timeout
* Timeout flag (in seconds precision): val is the number of seconds
*
* File descriptor close operation is not handled by the scheduler
*
* */
void antd_task_bind_event(antd_task_t* task, int fd, int timeout, int flags);
2021-01-03 11:24:55 +01:00
/**
* add a task
2018-09-25 17:51:56 +02:00
*/
2021-01-03 11:24:55 +01:00
void antd_scheduler_add_task(antd_scheduler_t *, antd_task_t *);
/**
* check if scheduler is busy
2018-09-25 17:51:56 +02:00
*/
2019-07-31 15:11:59 +02:00
int antd_scheduler_busy(antd_scheduler_t *);
2021-01-03 11:24:55 +01:00
/**
* get scheduler status
* */
int antd_scheduler_ok(antd_scheduler_t *scheduler);
/**
*
* wait for event
2018-10-10 12:42:47 +02:00
*/
2021-01-03 21:31:35 +01:00
void *antd_scheduler_wait(void *);
2021-01-03 11:24:55 +01:00
/**
* lock the scheduler
* */
void antd_scheduler_lock(antd_scheduler_t *);
/**
* Get next valid task id
* */
2021-01-03 21:31:35 +01:00
int antd_scheduler_next_id(antd_scheduler_t *sched, int input);
2021-01-03 11:24:55 +01:00
/**
* unlock the scheduler
* */
void antd_scheduler_unlock(antd_scheduler_t *);
/**
* weak functions that should be overridden by the application
* that user the scheduler as library
*/
void __attribute__((weak)) antd_scheduler_ext_statistic(int fd, void *data);
int __attribute__((weak)) antd_scheduler_validate_data(antd_task_t *task);
void __attribute__((weak)) antd_scheduler_destroy_data(void *data);
int __attribute__((weak)) antd_task_data_id(void *data);
2018-09-25 17:51:56 +02:00
#endif