Wed, 27 Nov 2024 23:00:07 +0100
add TODO to use a future ucx feature
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 | /* |
552
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
547
diff
changeset
|
52 | * eventhandler thread |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
547
diff
changeset
|
53 | */ |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
547
diff
changeset
|
54 | pthread_t thr; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
547
diff
changeset
|
55 | |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
547
diff
changeset
|
56 | /* |
185
b4d7ccf4e06d
implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
57 | * epoll fd |
b4d7ccf4e06d
implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
58 | */ |
b4d7ccf4e06d
implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
59 | int ep; |
545
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
60 | |
185
b4d7ccf4e06d
implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
61 | /* |
545
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
62 | * 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
|
63 | * 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
|
64 | */ |
545
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
65 | int event_fd; |
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 | /* |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
68 | * 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
|
69 | * 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
|
70 | * 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
|
71 | * |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
72 | * first 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_begin; |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
75 | |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
76 | /* |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
77 | * last queue block |
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 *queue_end; |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
80 | |
192
6a145e13d933
replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
185
diff
changeset
|
81 | /* |
545
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
82 | * allocated 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 | 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
|
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 | * 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
|
88 | */ |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
89 | 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
|
90 | |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
91 | /* |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
92 | * mutex for event_queue |
192
6a145e13d933
replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
185
diff
changeset
|
93 | */ |
545
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
94 | 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
|
95 | } EventHandlerLinux; |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
96 | |
545
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
97 | struct EventQueue { |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
98 | /* |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
99 | * array of events |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
100 | */ |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
101 | 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
|
102 | /* |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
103 | * number of events |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
104 | */ |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
105 | size_t numevents; |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
106 | /* |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
107 | * next event block |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
108 | */ |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
109 | EventQueue *next; |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
110 | }; |
720893ec7d48
new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
111 | |
541
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
531
diff
changeset
|
112 | void ev_handle_events(EventHandlerLinux *ev); |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
113 | |
185
b4d7ccf4e06d
implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
114 | int ev_convert2sys_events(int events); |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
115 | |
547
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
545
diff
changeset
|
116 | void ev_queue_free(EventQueue *queue); |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
545
diff
changeset
|
117 | |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
118 | #ifdef __cplusplus |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
119 | } |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
120 | #endif |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
121 | |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
122 | #endif /* EVENT_LINUX_H */ |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
123 |