bossan 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE.txt +22 -0
- data/ext/bossan/bossan_ext.c +2012 -0
- data/ext/bossan/extconf.rb +13 -0
- data/ext/bossan/http_parser.c +1578 -0
- data/ext/bossan/http_parser.h +169 -0
- data/ext/bossan/picoev.h +382 -0
- data/ext/bossan/picoev_epoll.c +166 -0
- data/ext/bossan/time_cache.c +124 -0
- data/ext/bossan/time_cache.h +20 -0
- data/lib/bossan.rb +5 -0
- data/lib/bossan/version.rb +3 -0
- metadata +58 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/* Copyright 2009,2010 Ryan Dahl <ry@tinyclouds.org>
|
|
2
|
+
*
|
|
3
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
* of this software and associated documentation files (the "Software"), to
|
|
5
|
+
* deal in the Software without restriction, including without limitation the
|
|
6
|
+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
7
|
+
* sell copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
* furnished to do so, subject to the following conditions:
|
|
9
|
+
*
|
|
10
|
+
* The above copyright notice and this permission notice shall be included in
|
|
11
|
+
* all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
18
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
19
|
+
* IN THE SOFTWARE.
|
|
20
|
+
*/
|
|
21
|
+
#ifndef http_parser_h
|
|
22
|
+
#define http_parser_h
|
|
23
|
+
#ifdef __cplusplus
|
|
24
|
+
extern "C" {
|
|
25
|
+
#endif
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
#include <sys/types.h>
|
|
29
|
+
#include <stdint.h>
|
|
30
|
+
|
|
31
|
+
#ifdef _WIN32
|
|
32
|
+
typedef unsigned int size_t;
|
|
33
|
+
typedef int ssize_t;
|
|
34
|
+
#endif
|
|
35
|
+
|
|
36
|
+
/* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
|
|
37
|
+
* faster
|
|
38
|
+
*/
|
|
39
|
+
#ifndef HTTP_PARSER_STRICT
|
|
40
|
+
# define HTTP_PARSER_STRICT 1
|
|
41
|
+
#else
|
|
42
|
+
# define HTTP_PARSER_STRICT 0
|
|
43
|
+
#endif
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
/* Maximium header size allowed */
|
|
47
|
+
#define HTTP_MAX_HEADER_SIZE (80*1024)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
typedef struct http_parser http_parser;
|
|
51
|
+
typedef struct http_parser_settings http_parser_settings;
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
/* Callbacks should return non-zero to indicate an error. The parser will
|
|
55
|
+
* then halt execution.
|
|
56
|
+
*
|
|
57
|
+
* The one exception is on_headers_complete. In a HTTP_RESPONSE parser
|
|
58
|
+
* returning '1' from on_headers_complete will tell the parser that it
|
|
59
|
+
* should not expect a body. This is used when receiving a response to a
|
|
60
|
+
* HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
|
|
61
|
+
* chunked' headers that indicate the presence of a body.
|
|
62
|
+
*
|
|
63
|
+
* http_data_cb does not return data chunks. It will be call arbitrarally
|
|
64
|
+
* many times for each string. E.G. you might get 10 callbacks for "on_path"
|
|
65
|
+
* each providing just a few characters more data.
|
|
66
|
+
*/
|
|
67
|
+
typedef int (*http_data_cb) (http_parser*, const char *at, size_t length, char partial);
|
|
68
|
+
typedef int (*http_cb) (http_parser*);
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
/* Request Methods */
|
|
72
|
+
enum http_method
|
|
73
|
+
{ HTTP_DELETE = 0
|
|
74
|
+
, HTTP_GET
|
|
75
|
+
, HTTP_HEAD
|
|
76
|
+
, HTTP_POST
|
|
77
|
+
, HTTP_PUT
|
|
78
|
+
/* pathological */
|
|
79
|
+
, HTTP_CONNECT
|
|
80
|
+
, HTTP_OPTIONS
|
|
81
|
+
, HTTP_TRACE
|
|
82
|
+
/* webdav */
|
|
83
|
+
, HTTP_COPY
|
|
84
|
+
, HTTP_LOCK
|
|
85
|
+
, HTTP_MKCOL
|
|
86
|
+
, HTTP_MOVE
|
|
87
|
+
, HTTP_PROPFIND
|
|
88
|
+
, HTTP_PROPPATCH
|
|
89
|
+
, HTTP_UNLOCK
|
|
90
|
+
/* subversion */
|
|
91
|
+
, HTTP_REPORT
|
|
92
|
+
, HTTP_MKACTIVITY
|
|
93
|
+
, HTTP_CHECKOUT
|
|
94
|
+
, HTTP_MERGE
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
struct http_parser {
|
|
102
|
+
/** PRIVATE **/
|
|
103
|
+
unsigned char type;
|
|
104
|
+
unsigned char state;
|
|
105
|
+
unsigned char header_state;
|
|
106
|
+
unsigned char index;
|
|
107
|
+
char maybe_ml;
|
|
108
|
+
|
|
109
|
+
char flags;
|
|
110
|
+
|
|
111
|
+
uint64_t nread;
|
|
112
|
+
int64_t content_length;
|
|
113
|
+
|
|
114
|
+
/** READ-ONLY **/
|
|
115
|
+
unsigned short http_major;
|
|
116
|
+
unsigned short http_minor;
|
|
117
|
+
unsigned short status_code; /* responses only */
|
|
118
|
+
unsigned char method; /* requests only */
|
|
119
|
+
|
|
120
|
+
/* 1 = Upgrade header was present and the parser has exited because of that.
|
|
121
|
+
* 0 = No upgrade header present.
|
|
122
|
+
* Should be checked when http_parser_execute() returns in addition to
|
|
123
|
+
* error checking.
|
|
124
|
+
*/
|
|
125
|
+
char upgrade;
|
|
126
|
+
|
|
127
|
+
/** PUBLIC **/
|
|
128
|
+
void *data; /* A pointer to get hook to the "connection" or "socket" object */
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
struct http_parser_settings {
|
|
133
|
+
http_cb on_message_begin;
|
|
134
|
+
http_data_cb on_path;
|
|
135
|
+
http_data_cb on_query_string;
|
|
136
|
+
http_data_cb on_url;
|
|
137
|
+
http_data_cb on_fragment;
|
|
138
|
+
http_data_cb on_header_field;
|
|
139
|
+
http_data_cb on_header_value;
|
|
140
|
+
http_cb on_headers_complete;
|
|
141
|
+
http_data_cb on_body;
|
|
142
|
+
http_cb on_message_complete;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
void http_parser_init(http_parser *parser, enum http_parser_type type);
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
size_t http_parser_execute(http_parser *parser,
|
|
150
|
+
const http_parser_settings *settings,
|
|
151
|
+
const char *data,
|
|
152
|
+
size_t len);
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
/* If http_should_keep_alive() in the on_headers_complete or
|
|
156
|
+
* on_message_complete callback returns true, then this will be should be
|
|
157
|
+
* the last message on the connection.
|
|
158
|
+
* If you are the server, respond with the "Connection: close" header.
|
|
159
|
+
* If you are the client, close the connection.
|
|
160
|
+
*/
|
|
161
|
+
int http_should_keep_alive(http_parser *parser);
|
|
162
|
+
|
|
163
|
+
/* Returns a string version of the HTTP method. */
|
|
164
|
+
const char *http_method_str(enum http_method);
|
|
165
|
+
|
|
166
|
+
#ifdef __cplusplus
|
|
167
|
+
}
|
|
168
|
+
#endif
|
|
169
|
+
#endif
|
data/ext/bossan/picoev.h
ADDED
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2009, Cybozu Labs, Inc.
|
|
3
|
+
* All rights reserved.
|
|
4
|
+
*
|
|
5
|
+
* Redistribution and use in source and binary forms, with or without
|
|
6
|
+
* modification, are permitted provided that the following conditions are met:
|
|
7
|
+
*
|
|
8
|
+
* * Redistributions of source code must retain the above copyright notice,
|
|
9
|
+
* this list of conditions and the following disclaimer.
|
|
10
|
+
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
11
|
+
* this list of conditions and the following disclaimer in the documentation
|
|
12
|
+
* and/or other materials provided with the distribution.
|
|
13
|
+
* * Neither the name of the <ORGANIZATION> nor the names of its contributors
|
|
14
|
+
* may be used to endorse or promote products derived from this software
|
|
15
|
+
* without specific prior written permission.
|
|
16
|
+
*
|
|
17
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
18
|
+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
19
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
20
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
21
|
+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
22
|
+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
23
|
+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
24
|
+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
25
|
+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
26
|
+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
27
|
+
* POSSIBILITY OF SUCH DAMAGE.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
#ifndef picoev_h
|
|
31
|
+
#define picoev_h
|
|
32
|
+
|
|
33
|
+
#ifdef __cplusplus
|
|
34
|
+
extern "C" {
|
|
35
|
+
# define PICOEV_INLINE inline
|
|
36
|
+
#else
|
|
37
|
+
# define PICOEV_INLINE static __inline__
|
|
38
|
+
#endif
|
|
39
|
+
|
|
40
|
+
#include <assert.h>
|
|
41
|
+
#include <limits.h>
|
|
42
|
+
#include <stdlib.h>
|
|
43
|
+
#include <string.h>
|
|
44
|
+
#include <time.h>
|
|
45
|
+
|
|
46
|
+
#define PICOEV_IS_INITED (picoev.max_fd != 0)
|
|
47
|
+
#define PICOEV_IS_INITED_AND_FD_IN_RANGE(fd) \
|
|
48
|
+
(((unsigned)fd) < (unsigned)picoev.max_fd)
|
|
49
|
+
#define PICOEV_TOO_MANY_LOOPS (picoev.num_loops != 0) /* use after ++ */
|
|
50
|
+
#define PICOEV_FD_BELONGS_TO_LOOP(loop, fd) \
|
|
51
|
+
((loop)->loop_id == picoev.fds[fd].loop_id)
|
|
52
|
+
|
|
53
|
+
#define PICOEV_TIMEOUT_VEC_OF(loop, idx) \
|
|
54
|
+
((loop)->timeout.vec + (idx) * picoev.timeout_vec_size)
|
|
55
|
+
#define PICOEV_TIMEOUT_VEC_OF_VEC_OF(loop, idx) \
|
|
56
|
+
((loop)->timeout.vec_of_vec + (idx) * picoev.timeout_vec_of_vec_size)
|
|
57
|
+
#define PICOEV_RND_UP(v, d) (((v) + (d) - 1) / (d) * (d))
|
|
58
|
+
|
|
59
|
+
#define PICOEV_PAGE_SIZE 4096
|
|
60
|
+
#define PICOEV_CACHE_LINE_SIZE 32 /* in bytes, ok if greater than the actual */
|
|
61
|
+
#define PICOEV_SIMD_BITS 128
|
|
62
|
+
#define PICOEV_TIMEOUT_VEC_SIZE 128
|
|
63
|
+
#define PICOEV_SHORT_BITS (sizeof(short) * 8)
|
|
64
|
+
|
|
65
|
+
#define PICOEV_READ 1
|
|
66
|
+
#define PICOEV_WRITE 2
|
|
67
|
+
#define PICOEV_TIMEOUT 4
|
|
68
|
+
#define PICOEV_ADD 0x40000000
|
|
69
|
+
#define PICOEV_DEL 0x20000000
|
|
70
|
+
#define PICOEV_READWRITE (PICOEV_READ | PICOEV_WRITE)
|
|
71
|
+
|
|
72
|
+
#define PICOEV_TIMEOUT_IDX_UNUSED (UCHAR_MAX)
|
|
73
|
+
|
|
74
|
+
typedef unsigned short picoev_loop_id_t;
|
|
75
|
+
|
|
76
|
+
typedef struct picoev_loop_st picoev_loop;
|
|
77
|
+
|
|
78
|
+
typedef void picoev_handler(picoev_loop* loop, int fd, int revents,
|
|
79
|
+
void* cb_arg);
|
|
80
|
+
|
|
81
|
+
typedef struct picoev_fd_st {
|
|
82
|
+
/* use accessors! */
|
|
83
|
+
/* TODO adjust the size to match that of a cache line */
|
|
84
|
+
picoev_handler* callback;
|
|
85
|
+
void* cb_arg;
|
|
86
|
+
picoev_loop_id_t loop_id;
|
|
87
|
+
char events;
|
|
88
|
+
unsigned char timeout_idx; /* PICOEV_TIMEOUT_IDX_UNUSED if not used */
|
|
89
|
+
int _backend; /* can be used by backends (never modified by core) */
|
|
90
|
+
} picoev_fd;
|
|
91
|
+
|
|
92
|
+
struct picoev_loop_st {
|
|
93
|
+
/* read only */
|
|
94
|
+
picoev_loop_id_t loop_id;
|
|
95
|
+
struct {
|
|
96
|
+
short* vec;
|
|
97
|
+
short* vec_of_vec;
|
|
98
|
+
size_t base_idx;
|
|
99
|
+
time_t base_time;
|
|
100
|
+
int resolution;
|
|
101
|
+
void* _free_addr;
|
|
102
|
+
} timeout;
|
|
103
|
+
time_t now;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
typedef struct picoev_globals_st {
|
|
107
|
+
/* read only */
|
|
108
|
+
picoev_fd* fds;
|
|
109
|
+
void* _fds_free_addr;
|
|
110
|
+
int max_fd;
|
|
111
|
+
int num_loops;
|
|
112
|
+
size_t timeout_vec_size; /* # of elements in picoev_loop.timeout.vec[0] */
|
|
113
|
+
size_t timeout_vec_of_vec_size; /* ... in timeout.vec_of_vec[0] */
|
|
114
|
+
} picoev_globals;
|
|
115
|
+
|
|
116
|
+
extern picoev_globals picoev;
|
|
117
|
+
|
|
118
|
+
/* creates a new event loop (defined by each backend) */
|
|
119
|
+
picoev_loop* picoev_create_loop(int max_timeout);
|
|
120
|
+
|
|
121
|
+
/* destroys a loop (defined by each backend) */
|
|
122
|
+
int picoev_destroy_loop(picoev_loop* loop);
|
|
123
|
+
|
|
124
|
+
/* internal: updates events to be watched (defined by each backend) */
|
|
125
|
+
int picoev_update_events_internal(picoev_loop* loop, int fd, int events);
|
|
126
|
+
|
|
127
|
+
/* internal: poll once and call the handlers (defined by each backend) */
|
|
128
|
+
int picoev_poll_once_internal(picoev_loop* loop, int max_wait);
|
|
129
|
+
|
|
130
|
+
/* internal, aligned allocator with address scrambling to avoid cache
|
|
131
|
+
line contention */
|
|
132
|
+
PICOEV_INLINE
|
|
133
|
+
void* picoev_memalign(size_t sz, void** orig_addr, int clear) {
|
|
134
|
+
sz = sz + PICOEV_PAGE_SIZE + PICOEV_CACHE_LINE_SIZE;
|
|
135
|
+
if ((*orig_addr = malloc(sz)) == NULL) {
|
|
136
|
+
return NULL;
|
|
137
|
+
}
|
|
138
|
+
if (clear != 0) {
|
|
139
|
+
memset(*orig_addr, 0, sz);
|
|
140
|
+
}
|
|
141
|
+
return
|
|
142
|
+
(void*)PICOEV_RND_UP((unsigned long)*orig_addr
|
|
143
|
+
+ (rand() % PICOEV_PAGE_SIZE),
|
|
144
|
+
PICOEV_CACHE_LINE_SIZE);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/* initializes picoev */
|
|
148
|
+
PICOEV_INLINE
|
|
149
|
+
int picoev_init(int max_fd) {
|
|
150
|
+
assert(! PICOEV_IS_INITED);
|
|
151
|
+
assert(max_fd > 0);
|
|
152
|
+
if ((picoev.fds = (picoev_fd*)picoev_memalign(sizeof(picoev_fd) * max_fd,
|
|
153
|
+
&picoev._fds_free_addr, 1))
|
|
154
|
+
== NULL) {
|
|
155
|
+
return -1;
|
|
156
|
+
}
|
|
157
|
+
picoev.max_fd = max_fd;
|
|
158
|
+
picoev.num_loops = 0;
|
|
159
|
+
picoev.timeout_vec_size
|
|
160
|
+
= PICOEV_RND_UP(picoev.max_fd, PICOEV_SIMD_BITS) / PICOEV_SHORT_BITS;
|
|
161
|
+
picoev.timeout_vec_of_vec_size
|
|
162
|
+
= PICOEV_RND_UP(picoev.timeout_vec_size, PICOEV_SIMD_BITS)
|
|
163
|
+
/ PICOEV_SHORT_BITS;
|
|
164
|
+
return 0;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/* deinitializes picoev */
|
|
168
|
+
PICOEV_INLINE
|
|
169
|
+
int picoev_deinit(void) {
|
|
170
|
+
assert(PICOEV_IS_INITED);
|
|
171
|
+
free(picoev._fds_free_addr);
|
|
172
|
+
picoev.fds = NULL;
|
|
173
|
+
picoev._fds_free_addr = NULL;
|
|
174
|
+
picoev.max_fd = 0;
|
|
175
|
+
picoev.num_loops = 0;
|
|
176
|
+
return 0;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/* updates timeout */
|
|
180
|
+
PICOEV_INLINE
|
|
181
|
+
void picoev_set_timeout(picoev_loop* loop, int fd, int secs) {
|
|
182
|
+
picoev_fd* target;
|
|
183
|
+
short* vec, * vec_of_vec;
|
|
184
|
+
size_t vi = fd / PICOEV_SHORT_BITS, delta;
|
|
185
|
+
assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
|
|
186
|
+
assert(PICOEV_FD_BELONGS_TO_LOOP(loop, fd));
|
|
187
|
+
target = picoev.fds + fd;
|
|
188
|
+
/* clear timeout */
|
|
189
|
+
if (target->timeout_idx != PICOEV_TIMEOUT_IDX_UNUSED) {
|
|
190
|
+
vec = PICOEV_TIMEOUT_VEC_OF(loop, target->timeout_idx);
|
|
191
|
+
if ((vec[vi] &= ~((unsigned short)SHRT_MIN >> (fd % PICOEV_SHORT_BITS)))
|
|
192
|
+
== 0) {
|
|
193
|
+
vec_of_vec = PICOEV_TIMEOUT_VEC_OF_VEC_OF(loop, target->timeout_idx);
|
|
194
|
+
vec_of_vec[vi / PICOEV_SHORT_BITS]
|
|
195
|
+
&= ~((unsigned short)SHRT_MIN >> (vi % PICOEV_SHORT_BITS));
|
|
196
|
+
}
|
|
197
|
+
target->timeout_idx = PICOEV_TIMEOUT_IDX_UNUSED;
|
|
198
|
+
}
|
|
199
|
+
if (secs != 0) {
|
|
200
|
+
delta = (loop->now + secs - loop->timeout.base_time)
|
|
201
|
+
/ loop->timeout.resolution;
|
|
202
|
+
if (delta >= PICOEV_TIMEOUT_VEC_SIZE) {
|
|
203
|
+
delta = PICOEV_TIMEOUT_VEC_SIZE - 1;
|
|
204
|
+
}
|
|
205
|
+
target->timeout_idx =
|
|
206
|
+
(loop->timeout.base_idx + delta) % PICOEV_TIMEOUT_VEC_SIZE;
|
|
207
|
+
vec = PICOEV_TIMEOUT_VEC_OF(loop, target->timeout_idx);
|
|
208
|
+
vec[vi] |= (unsigned short)SHRT_MIN >> (fd % PICOEV_SHORT_BITS);
|
|
209
|
+
vec_of_vec = PICOEV_TIMEOUT_VEC_OF_VEC_OF(loop, target->timeout_idx);
|
|
210
|
+
vec_of_vec[vi / PICOEV_SHORT_BITS]
|
|
211
|
+
|= (unsigned short)SHRT_MIN >> (vi % PICOEV_SHORT_BITS);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/* registers a file descriptor and callback argument to a event loop */
|
|
216
|
+
PICOEV_INLINE
|
|
217
|
+
int picoev_add(picoev_loop* loop, int fd, int events, int timeout_in_secs,
|
|
218
|
+
picoev_handler* callback, void* cb_arg) {
|
|
219
|
+
picoev_fd* target;
|
|
220
|
+
assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
|
|
221
|
+
target = picoev.fds + fd;
|
|
222
|
+
assert(target->loop_id == 0);
|
|
223
|
+
target->callback = callback;
|
|
224
|
+
target->cb_arg = cb_arg;
|
|
225
|
+
target->loop_id = loop->loop_id;
|
|
226
|
+
target->events = 0;
|
|
227
|
+
target->timeout_idx = PICOEV_TIMEOUT_IDX_UNUSED;
|
|
228
|
+
if (picoev_update_events_internal(loop, fd, events | PICOEV_ADD) != 0) {
|
|
229
|
+
target->loop_id = 0;
|
|
230
|
+
return -1;
|
|
231
|
+
}
|
|
232
|
+
picoev_set_timeout(loop, fd, timeout_in_secs);
|
|
233
|
+
return 0;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/* unregisters a file descriptor from event loop */
|
|
237
|
+
PICOEV_INLINE
|
|
238
|
+
int picoev_del(picoev_loop* loop, int fd) {
|
|
239
|
+
picoev_fd* target;
|
|
240
|
+
assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
|
|
241
|
+
target = picoev.fds + fd;
|
|
242
|
+
if (picoev_update_events_internal(loop, fd, PICOEV_DEL) != 0) {
|
|
243
|
+
return -1;
|
|
244
|
+
}
|
|
245
|
+
picoev_set_timeout(loop, fd, 0);
|
|
246
|
+
target->loop_id = 0;
|
|
247
|
+
return 0;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/* check if fd is registered (checks all loops if loop == NULL) */
|
|
251
|
+
PICOEV_INLINE
|
|
252
|
+
int picoev_is_active(picoev_loop* loop, int fd) {
|
|
253
|
+
assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
|
|
254
|
+
return loop != NULL
|
|
255
|
+
? picoev.fds[fd].loop_id == loop->loop_id
|
|
256
|
+
: picoev.fds[fd].loop_id != 0;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/* returns events being watched for given descriptor */
|
|
260
|
+
PICOEV_INLINE
|
|
261
|
+
int picoev_get_events(picoev_loop* loop __attribute__((unused)), int fd) {
|
|
262
|
+
assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
|
|
263
|
+
return picoev.fds[fd].events & PICOEV_READWRITE;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/* sets events to be watched for given desriptor */
|
|
267
|
+
PICOEV_INLINE
|
|
268
|
+
int picoev_set_events(picoev_loop* loop, int fd, int events) {
|
|
269
|
+
assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(fd));
|
|
270
|
+
if (picoev.fds[fd].events != events
|
|
271
|
+
&& picoev_update_events_internal(loop, fd, events) != 0) {
|
|
272
|
+
return -1;
|
|
273
|
+
}
|
|
274
|
+
return 0;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/* function to iterate registered information. To start iteration, set curfd
|
|
278
|
+
to -1 and call the function until -1 is returned */
|
|
279
|
+
PICOEV_INLINE
|
|
280
|
+
int picoev_next_fd(picoev_loop* loop, int curfd) {
|
|
281
|
+
if (curfd != -1) {
|
|
282
|
+
assert(PICOEV_IS_INITED_AND_FD_IN_RANGE(curfd));
|
|
283
|
+
}
|
|
284
|
+
while (++curfd < picoev.max_fd) {
|
|
285
|
+
if (loop->loop_id == picoev.fds[curfd].loop_id) {
|
|
286
|
+
return curfd;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return -1;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/* internal function */
|
|
293
|
+
PICOEV_INLINE
|
|
294
|
+
int picoev_init_loop_internal(picoev_loop* loop, int max_timeout) {
|
|
295
|
+
loop->loop_id = ++picoev.num_loops;
|
|
296
|
+
assert(PICOEV_TOO_MANY_LOOPS);
|
|
297
|
+
if ((loop->timeout.vec_of_vec
|
|
298
|
+
= (short*)picoev_memalign((picoev.timeout_vec_of_vec_size
|
|
299
|
+
+ picoev.timeout_vec_size)
|
|
300
|
+
* sizeof(short) * PICOEV_TIMEOUT_VEC_SIZE,
|
|
301
|
+
&loop->timeout._free_addr, 1))
|
|
302
|
+
== NULL) {
|
|
303
|
+
--picoev.num_loops;
|
|
304
|
+
return -1;
|
|
305
|
+
}
|
|
306
|
+
loop->timeout.vec = loop->timeout.vec_of_vec
|
|
307
|
+
+ picoev.timeout_vec_of_vec_size * PICOEV_TIMEOUT_VEC_SIZE;
|
|
308
|
+
loop->timeout.base_idx = 0;
|
|
309
|
+
loop->timeout.base_time = time(NULL);
|
|
310
|
+
loop->timeout.resolution
|
|
311
|
+
= PICOEV_RND_UP(max_timeout, PICOEV_TIMEOUT_VEC_SIZE)
|
|
312
|
+
/ PICOEV_TIMEOUT_VEC_SIZE;
|
|
313
|
+
return 0;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/* internal function */
|
|
317
|
+
PICOEV_INLINE
|
|
318
|
+
void picoev_deinit_loop_internal(picoev_loop* loop) {
|
|
319
|
+
free(loop->timeout._free_addr);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/* internal function */
|
|
323
|
+
PICOEV_INLINE
|
|
324
|
+
void picoev_handle_timeout_internal(picoev_loop* loop) {
|
|
325
|
+
size_t i, j, k;
|
|
326
|
+
for (;
|
|
327
|
+
loop->timeout.base_time <= loop->now - loop->timeout.resolution;
|
|
328
|
+
loop->timeout.base_idx
|
|
329
|
+
= (loop->timeout.base_idx + 1) % PICOEV_TIMEOUT_VEC_SIZE,
|
|
330
|
+
loop->timeout.base_time += loop->timeout.resolution) {
|
|
331
|
+
/* TODO use SIMD instructions */
|
|
332
|
+
short* vec = PICOEV_TIMEOUT_VEC_OF(loop, loop->timeout.base_idx);
|
|
333
|
+
short* vec_of_vec
|
|
334
|
+
= PICOEV_TIMEOUT_VEC_OF_VEC_OF(loop, loop->timeout.base_idx);
|
|
335
|
+
for (i = 0; i < picoev.timeout_vec_of_vec_size; ++i) {
|
|
336
|
+
short vv = vec_of_vec[i];
|
|
337
|
+
if (vv != 0) {
|
|
338
|
+
for (j = i * PICOEV_SHORT_BITS; vv != 0; j++, vv <<= 1) {
|
|
339
|
+
if (vv < 0) {
|
|
340
|
+
short v = vec[j];
|
|
341
|
+
assert(v != 0);
|
|
342
|
+
for (k = j * PICOEV_SHORT_BITS; v != 0; k++, v <<= 1) {
|
|
343
|
+
if (v < 0) {
|
|
344
|
+
picoev_fd* fd = picoev.fds + k;
|
|
345
|
+
assert(fd->loop_id == loop->loop_id);
|
|
346
|
+
fd->timeout_idx = PICOEV_TIMEOUT_IDX_UNUSED;
|
|
347
|
+
(*fd->callback)(loop, k, PICOEV_TIMEOUT, fd->cb_arg);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
vec[j] = 0;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
vec_of_vec[i] = 0;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/* loop once */
|
|
360
|
+
PICOEV_INLINE
|
|
361
|
+
int picoev_loop_once(picoev_loop* loop, int max_wait) {
|
|
362
|
+
loop->now = time(NULL);
|
|
363
|
+
if (max_wait > loop->timeout.resolution) {
|
|
364
|
+
max_wait = loop->timeout.resolution;
|
|
365
|
+
}
|
|
366
|
+
if (picoev_poll_once_internal(loop, max_wait) != 0) {
|
|
367
|
+
return -1;
|
|
368
|
+
}
|
|
369
|
+
if (max_wait != 0) {
|
|
370
|
+
loop->now = time(NULL);
|
|
371
|
+
}
|
|
372
|
+
picoev_handle_timeout_internal(loop);
|
|
373
|
+
return 0;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
#undef PICOEV_INLINE
|
|
377
|
+
|
|
378
|
+
#ifdef __cplusplus
|
|
379
|
+
}
|
|
380
|
+
#endif
|
|
381
|
+
|
|
382
|
+
#endif
|