/src/php-src/main/poll/php_poll_internal.h
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Authors: Jakub Zelenka <bukka@php.net> | |
12 | | +----------------------------------------------------------------------+ |
13 | | */ |
14 | | |
15 | | #ifndef PHP_POLL_INTERNAL_H |
16 | | #define PHP_POLL_INTERNAL_H |
17 | | |
18 | | #include "php_poll.h" |
19 | | #include "php_network.h" |
20 | | |
21 | | /* Allocation macros */ |
22 | | #define php_poll_calloc(nmemb, size, persistent) \ |
23 | 15 | ((persistent) ? calloc((nmemb), (size)) : ecalloc((nmemb), (size))) |
24 | | #define php_poll_malloc(size, persistent) ((persistent) ? malloc((size)) : emalloc((size))) |
25 | | #define php_poll_realloc(ptr, size, persistent) \ |
26 | 0 | ((persistent) ? realloc((ptr), (size)) : erealloc((ptr), (size))) |
27 | | |
28 | | /* Backend interface */ |
29 | | typedef struct php_poll_backend_ops { |
30 | | php_poll_backend_type type; |
31 | | const char *name; |
32 | | |
33 | | /* Initialize backend */ |
34 | | zend_result (*init)(php_poll_ctx *ctx); |
35 | | |
36 | | /* Cleanup backend */ |
37 | | void (*cleanup)(php_poll_ctx *ctx); |
38 | | |
39 | | /* Add file descriptor */ |
40 | | zend_result (*add)(php_poll_ctx *ctx, int fd, uint32_t events, void *data); |
41 | | |
42 | | /* Modify file descriptor */ |
43 | | zend_result (*modify)(php_poll_ctx *ctx, int fd, uint32_t events, void *data); |
44 | | |
45 | | /* Remove file descriptor */ |
46 | | zend_result (*remove)(php_poll_ctx *ctx, int fd); |
47 | | |
48 | | /* Wait for events */ |
49 | | int (*wait)(php_poll_ctx *ctx, php_poll_event *events, int max_events, |
50 | | const struct timespec *timeout); |
51 | | |
52 | | /* Check if backend is available */ |
53 | | bool (*is_available)(void); |
54 | | |
55 | | /* Get suitable max_events for this backend */ |
56 | | int (*get_suitable_max_events)(php_poll_ctx *ctx); |
57 | | |
58 | | /* Backend supports edge triggering natively */ |
59 | | bool supports_et; |
60 | | } php_poll_backend_ops; |
61 | | |
62 | | /* Main poll context */ |
63 | | struct php_poll_ctx { |
64 | | const php_poll_backend_ops *backend_ops; |
65 | | php_poll_backend_type backend_type; |
66 | | php_poll_error last_error; |
67 | | |
68 | | /* Optional capacity hint for backends */ |
69 | | int max_events_hint; |
70 | | |
71 | | /* Flags */ |
72 | | uint32_t initialized : 1; |
73 | | uint32_t persistent : 1; |
74 | | uint32_t raw_events : 1; |
75 | | |
76 | | /* Backend-specific data */ |
77 | | void *backend_data; |
78 | | }; |
79 | | |
80 | | /* Generic FD entry structure */ |
81 | | typedef struct php_poll_fd_entry { |
82 | | int fd; |
83 | | uint32_t events; |
84 | | void *data; |
85 | | bool active; |
86 | | uint32_t last_revents; |
87 | | } php_poll_fd_entry; |
88 | | |
89 | | /* FD tracking table */ |
90 | | typedef struct php_poll_fd_table { |
91 | | HashTable entries_ht; |
92 | | bool persistent; |
93 | | } php_poll_fd_table; |
94 | | |
95 | | /* Iterator callback function type */ |
96 | | typedef bool (*php_poll_fd_iterator_func_t)(int fd, php_poll_fd_entry *entry, void *user_data); |
97 | | |
98 | | /* Poll FD helpers - clean API with accessor functions */ |
99 | | php_poll_fd_table *php_poll_fd_table_init(int initial_capacity, bool persistent); |
100 | | void php_poll_fd_table_cleanup(php_poll_fd_table *table); |
101 | | php_poll_fd_entry *php_poll_fd_table_find(php_poll_fd_table *table, int fd); |
102 | | php_poll_fd_entry *php_poll_fd_table_get(php_poll_fd_table *table, int fd); |
103 | | php_poll_fd_entry *php_poll_fd_table_get_new(php_poll_fd_table *table, int fd); |
104 | | bool php_poll_fd_table_remove(php_poll_fd_table *table, int fd); |
105 | | |
106 | | /* Accessor functions for table properties */ |
107 | | static inline int php_poll_fd_table_count(php_poll_fd_table *table) |
108 | 0 | { |
109 | 0 | return zend_hash_num_elements(&table->entries_ht); |
110 | 0 | } Unexecuted instantiation: poll_backend_epoll.c:php_poll_fd_table_count Unexecuted instantiation: poll_backend_eventport.c:php_poll_fd_table_count Unexecuted instantiation: poll_backend_kqueue.c:php_poll_fd_table_count Unexecuted instantiation: poll_backend_poll.c:php_poll_fd_table_count Unexecuted instantiation: poll_core.c:php_poll_fd_table_count Unexecuted instantiation: poll_fd_table.c:php_poll_fd_table_count |
111 | | |
112 | | static inline bool php_poll_fd_table_is_empty(php_poll_fd_table *table) |
113 | 0 | { |
114 | 0 | return zend_hash_num_elements(&table->entries_ht) == 0; |
115 | 0 | } Unexecuted instantiation: poll_backend_epoll.c:php_poll_fd_table_is_empty Unexecuted instantiation: poll_backend_eventport.c:php_poll_fd_table_is_empty Unexecuted instantiation: poll_backend_kqueue.c:php_poll_fd_table_is_empty Unexecuted instantiation: poll_backend_poll.c:php_poll_fd_table_is_empty Unexecuted instantiation: poll_core.c:php_poll_fd_table_is_empty Unexecuted instantiation: poll_fd_table.c:php_poll_fd_table_is_empty |
116 | | |
117 | | /* New helper functions for improved backend integration */ |
118 | | void php_poll_fd_table_foreach( |
119 | | php_poll_fd_table *table, php_poll_fd_iterator_func_t callback, void *user_data); |
120 | | php_socket_t php_poll_fd_table_get_max_fd(php_poll_fd_table *table); |
121 | | int php_poll_fd_table_collect_events( |
122 | | php_poll_fd_table *table, php_poll_event *events, int max_events); |
123 | | |
124 | | /* Error helper functions */ |
125 | | php_poll_error php_poll_errno_to_error(int err); |
126 | | |
127 | | static inline void php_poll_set_errno_error(php_poll_ctx *ctx, int err) |
128 | 0 | { |
129 | 0 | ctx->last_error = php_poll_errno_to_error(err); |
130 | 0 | } Unexecuted instantiation: poll_backend_epoll.c:php_poll_set_errno_error Unexecuted instantiation: poll_backend_eventport.c:php_poll_set_errno_error Unexecuted instantiation: poll_backend_kqueue.c:php_poll_set_errno_error Unexecuted instantiation: poll_backend_poll.c:php_poll_set_errno_error Unexecuted instantiation: poll_core.c:php_poll_set_errno_error Unexecuted instantiation: poll_fd_table.c:php_poll_set_errno_error |
131 | | |
132 | | static inline void php_poll_set_current_errno_error(php_poll_ctx *ctx) |
133 | 0 | { |
134 | 0 | php_poll_set_errno_error(ctx, errno); |
135 | 0 | } Unexecuted instantiation: poll_backend_epoll.c:php_poll_set_current_errno_error Unexecuted instantiation: poll_backend_eventport.c:php_poll_set_current_errno_error Unexecuted instantiation: poll_backend_kqueue.c:php_poll_set_current_errno_error Unexecuted instantiation: poll_backend_poll.c:php_poll_set_current_errno_error Unexecuted instantiation: poll_core.c:php_poll_set_current_errno_error Unexecuted instantiation: poll_fd_table.c:php_poll_set_current_errno_error |
136 | | |
137 | | static inline bool php_poll_is_not_found_error(void) |
138 | 0 | { |
139 | 0 | return errno == ENOENT; |
140 | 0 | } Unexecuted instantiation: poll_backend_epoll.c:php_poll_is_not_found_error Unexecuted instantiation: poll_backend_eventport.c:php_poll_is_not_found_error Unexecuted instantiation: poll_backend_kqueue.c:php_poll_is_not_found_error Unexecuted instantiation: poll_backend_poll.c:php_poll_is_not_found_error Unexecuted instantiation: poll_core.c:php_poll_is_not_found_error Unexecuted instantiation: poll_fd_table.c:php_poll_is_not_found_error |
141 | | |
142 | | static inline bool php_poll_is_timeout_error(void) |
143 | 0 | { |
144 | 0 | #if defined(ETIME) && defined(ETIMEDOUT) |
145 | 0 | return errno == ETIME || errno == ETIMEDOUT; |
146 | 0 | #elif defined(ETIME) |
147 | 0 | return errno == ETIME; |
148 | 0 | #elif defined(ETIMEDOUT) |
149 | 0 | return errno == ETIMEDOUT; |
150 | 0 | #else |
151 | 0 | return false; |
152 | 0 | #endif |
153 | 0 | } Unexecuted instantiation: poll_backend_epoll.c:php_poll_is_timeout_error Unexecuted instantiation: poll_backend_eventport.c:php_poll_is_timeout_error Unexecuted instantiation: poll_backend_kqueue.c:php_poll_is_timeout_error Unexecuted instantiation: poll_backend_poll.c:php_poll_is_timeout_error Unexecuted instantiation: poll_core.c:php_poll_is_timeout_error Unexecuted instantiation: poll_fd_table.c:php_poll_is_timeout_error |
154 | | |
155 | | |
156 | | static inline void php_poll_set_error(php_poll_ctx *ctx, php_poll_error error) |
157 | 0 | { |
158 | 0 | ctx->last_error = error; |
159 | 0 | } Unexecuted instantiation: poll_backend_epoll.c:php_poll_set_error Unexecuted instantiation: poll_backend_eventport.c:php_poll_set_error Unexecuted instantiation: poll_backend_kqueue.c:php_poll_set_error Unexecuted instantiation: poll_backend_poll.c:php_poll_set_error Unexecuted instantiation: poll_core.c:php_poll_set_error Unexecuted instantiation: poll_fd_table.c:php_poll_set_error |
160 | | |
161 | | static inline int php_poll_timespec_to_ms(const struct timespec *timeout) |
162 | 0 | { |
163 | 0 | if (timeout == NULL) { |
164 | 0 | return -1; |
165 | 0 | } |
166 | | |
167 | 0 | int ms = (int) (timeout->tv_sec * 1000); |
168 | | /* Round nanoseconds up to the next millisecond to avoid premature return */ |
169 | 0 | if (timeout->tv_nsec > 0) { |
170 | 0 | ms += (int) ((timeout->tv_nsec + 999999) / 1000000); |
171 | 0 | } |
172 | |
|
173 | 0 | return ms; |
174 | 0 | } Unexecuted instantiation: poll_backend_epoll.c:php_poll_timespec_to_ms Unexecuted instantiation: poll_backend_eventport.c:php_poll_timespec_to_ms Unexecuted instantiation: poll_backend_kqueue.c:php_poll_timespec_to_ms Unexecuted instantiation: poll_backend_poll.c:php_poll_timespec_to_ms Unexecuted instantiation: poll_core.c:php_poll_timespec_to_ms Unexecuted instantiation: poll_fd_table.c:php_poll_timespec_to_ms |
175 | | |
176 | | #endif /* PHP_POLL_INTERNAL_H */ |