forked from MysteryOfPanda/leactor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_lea.h
177 lines (155 loc) · 4.75 KB
/
event_lea.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//Name: Leactor
//Author: lhanjian
//Start 20130407
#ifndef _LEA_EVENT_H_INCLUDED_
#define _LEA_EVENT_H_INCLUDED_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <time.h>
//system independence
#include <sys/epoll.h>
#include <sys/uio.h>
#define MAX_ACTIVE
#define MAX_READY
#define DEFAULT_BUF_SIZE (16384)
#define INIT_EPEV (512)
#define EVLIST_LEN (DEFAULT_MMAP_THRESHOLD_MAX)
#define EPEV_MAX EVLIST_LEN
#define LV_LAG (0x0000008)
#define LV_CONN (0x0000004)
#define LV_FDRD (0x0000001)
#define LV_FDWR (0x0000002)
#define LV_ONESHOT (0x0000010)
#define INF (LONG_MAX)
#define NO_TIMEOUT (-1L)
#define NULL_ARG (NULL)
#define DEFAULT_MMAP_THRESHOLD_MAX (4*1024*1024)
#define UNDELETED (0)
#define LEZERO (-2)
#define LEAGAIN (-3)
#define LEINTR (-4)
typedef struct lt_buffer {
char *start;
char *pos;
char *last;
char *end;
int head;
int written;
char buf[DEFAULT_BUF_SIZE];
struct lt_buffer *next;
} lt_buffer_t;
//#define
//static funtion didn't dispatch return value;
//reduce passing parameter;
typedef struct timespec lt_time_t;
typedef int numlist_t;
typedef int epfd_t;
typedef numlist_t numactlist_t;
typedef numlist_t numeeadylist_t;
typedef int (*func_t)(int fd, void *arg);
typedef int res_t;
/*typedef union {
int error;
int correct;
} res_t;*/
typedef long to_t;
typedef struct lt_memory_piece {
struct lt_memory_piece *next;
} lt_memory_piece_t;
typedef struct lt_memory_pool {
size_t one_item_size;
lt_memory_piece_t *head;
lt_memory_piece_t *pos;
lt_memory_piece_t *tail;
void *all_item;
struct lt_memory_pool *next;
struct lt_memory_pool *manager;
} lt_memory_pool_t, lt_memory_manager_t;
lt_memory_manager_t *
lt_new_memory_pool_manager(lt_memory_manager_t *);
lt_memory_pool_t* lt_new_memory_pool(size_t one_item_size, lt_memory_manager_t *manager, ...);
void* lt_alloc(lt_memory_pool_t *pool, lt_memory_manager_t *manager);
void lt_free(lt_memory_pool_t *pool, void *object_contents);
void lt_destroy_memory_pool(lt_memory_pool_t *pool, lt_memory_pool_t *manager);
lt_buffer_t *lt_new_buffer_chain(lt_memory_pool_t *, lt_memory_pool_t *, size_t);
lt_buffer_t *lt_new_buffer(lt_memory_pool_t *, lt_memory_pool_t *);
typedef int flag_t;
typedef struct event {
func_t callback;
void *arg;
flag_t flag;
int fd;
int min_heap_idx;
lt_time_t endtime;
int deleted;
int pos_in_ready;
struct event *next_active_ev;
// int epfd;
} event_t;
typedef struct min_heap {
event_t **p;
unsigned n;
unsigned a;
} min_heap_t;
typedef struct {
int event_len;
lt_memory_pool_t *event_pool;
lt_memory_pool_t *event_pool_manager;
// int hole_len;
// event_t ***hole_list;//deleted position
} ready_evlist_t;//, activelist_t, evlist_t;
typedef struct {
int event_len;
event_t **eventarray;
event_t *head;
} active_evlist_t;
typedef struct {
int event_len;
event_t **eventarray;
} deleted_evlist_t;
typedef struct base {
//active event list and its number
active_evlist_t activelist;//ref? //TODO: certain binary tree?
//eventlist list and its number
ready_evlist_t readylist;
deleted_evlist_t deletedlist;
//epoll functions need it.
int epfd;
struct epoll_event *epevent;
int eptimeout;
lt_time_t now;
min_heap_t timeheap;
// int readylist_pos;
} base_t;
lt_time_t lt_gettime(void);
base_t* lt_base_init(void);
event_t* lt_io_add(base_t *base, int fd, flag_t flag_set, func_t callback, void *arg, to_t timeout);
void lt_io_remove(base_t *base, event_t *ev);
res_t lt_base_loop(base_t *base, long timeout);
lt_time_t lt_timeout_add(base_t *base, event_t *ev, to_t to);
//void lt_free_evlist(evlist_t *list);
res_t lt_ev_check_timeout(event_t *ev, lt_time_t timeout);
//res_t lt_remove_from_readylist(event_t *ev, ready_evlist_t *evlist);
//res_t lt_remove_from_readylist(event_t *ev, active_evlist_t *evlist);
#define time_a_gt_b(X,Y,Z) ((long long)X Y (unsigned long long)Z)
//#define lt_time_add(X, Y) ((lt_time_t)(X))
lt_time_t lt_time_addition(lt_time_t , to_t);
long long lt_time_a_sub_b(lt_time_t a, lt_time_t b);
/*
//initialize a base
res_t base_init(base_t *base_null);
//push a io event to base
res_t io_add(base_t *base_rlve, int fd, flag_t flag_set,
func_t callback, (void *) arg);
//core dispatch
res_t base_dispatch(base_t *base_dispatch
/);
*/
/*ToDo001 timeout to limit dispatch time?*/
//Free a base*/
/*
res_t base_free(base_t *base_rlve);
*/
#endif