Tue, 13 Aug 2024 20:08:13 +0200
increase keep-alive timeout
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
41
diff
changeset
|
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | #ifndef EVENT_LINUX_H |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
30 | #define EVENT_LINUX_H |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31 | |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32 | #include "event.h" |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33 | #include <inttypes.h> |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | |
545
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
35 | #include <cx/list.h> |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
36 | |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | #ifdef __cplusplus |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | extern "C" { |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | #endif |
531
9b15b1f72bef
change nsapi_function_return behavior: move saf return to the end of the event cycle
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
192
diff
changeset
|
40 | |
545
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
41 | #define EV_QUEUE_RESERVE 8 |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
42 | |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
43 | typedef struct EventQueue EventQueue; |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
44 | |
541
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
531
diff
changeset
|
45 | typedef struct EventHandlerLinux { |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
531
diff
changeset
|
46 | /* |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
531
diff
changeset
|
47 | * base eventhandler elements (fnreturn, watchlist) |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
531
diff
changeset
|
48 | */ |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
531
diff
changeset
|
49 | EventHandler base; |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
531
diff
changeset
|
50 | |
185
b4d7ccf4e06d
implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
51 | /* |
b4d7ccf4e06d
implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
52 | * epoll fd |
b4d7ccf4e06d
implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
53 | */ |
b4d7ccf4e06d
implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
54 | int ep; |
545
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
55 | |
185
b4d7ccf4e06d
implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
56 | /* |
545
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
57 | * eventfd for notifying that new elements |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
58 | * in the event_queue are available |
185
b4d7ccf4e06d
implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
59 | */ |
545
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
60 | int event_fd; |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
61 | |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
62 | /* |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
63 | * queue for custom events (event_send) |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
64 | * each block can contain up to EV_MAX_EVENTS events |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
65 | * the event loop should not retrieve more than one block |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
66 | * |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
67 | * first queue block |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
68 | */ |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
69 | EventQueue *queue_begin; |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
70 | |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
71 | /* |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
72 | * last queue block |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
73 | */ |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
74 | EventQueue *queue_end; |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
75 | |
192
6a145e13d933
replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
185
diff
changeset
|
76 | /* |
545
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
77 | * allocated unused blocks |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
78 | */ |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
79 | EventQueue *reserve_block[EV_QUEUE_RESERVE]; |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
80 | |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
81 | /* |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
82 | * number of unused blocks |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
83 | */ |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
84 | size_t num_reserve; |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
85 | |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
86 | /* |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
87 | * mutex for event_queue |
192
6a145e13d933
replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
185
diff
changeset
|
88 | */ |
545
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
89 | pthread_mutex_t queue_lock; |
541
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
531
diff
changeset
|
90 | } EventHandlerLinux; |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91 | |
545
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
92 | struct EventQueue { |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
93 | /* |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
94 | * array of events |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
95 | */ |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
96 | Event *events[EV_MAX_EVENTS]; |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
97 | /* |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
98 | * number of events |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
99 | */ |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
100 | size_t numevents; |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
101 | /* |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
102 | * next event block |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
103 | */ |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
104 | EventQueue *next; |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
105 | }; |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
106 | |
541
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
531
diff
changeset
|
107 | void ev_handle_events(EventHandlerLinux *ev); |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
108 | |
185
b4d7ccf4e06d
implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
109 | int ev_convert2sys_events(int events); |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
110 | |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
111 | #ifdef __cplusplus |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
112 | } |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
113 | #endif |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
114 | |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
115 | #endif /* EVENT_LINUX_H */ |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
116 |