| 1 |
#ifndef PROACTOR_EPOLL_INTERNAL_H |
| 2 |
#define PROACTOR_EPOLL_INTERNAL_H |
| 3 |
|
| 4 |
/* |
| 5 |
* |
| 6 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 7 |
* or more contributor license agreements. See the NOTICE file |
| 8 |
* distributed with this work for additional information |
| 9 |
* regarding copyright ownership. The ASF licenses this file |
| 10 |
* to you under the Apache License, Version 2.0 (the |
| 11 |
* "License"); you may not use this file except in compliance |
| 12 |
* with the License. You may obtain a copy of the License at |
| 13 |
* |
| 14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 15 |
* |
| 16 |
* Unless required by applicable law or agreed to in writing, |
| 17 |
* software distributed under the License is distributed on an |
| 18 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 19 |
* KIND, either express or implied. See the License for the |
| 20 |
* specific language governing permissions and limitations |
| 21 |
* under the License. |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
/* Enable POSIX features beyond c99 for modern pthread and standard strerror_r() */ |
| 26 |
#ifndef _POSIX_C_SOURCE |
| 27 |
#define _POSIX_C_SOURCE 200809L |
| 28 |
#endif |
| 29 |
/* Avoid GNU extensions, in particular the incompatible alternative strerror_r() */ |
| 30 |
#undef _GNU_SOURCE |
| 31 |
|
| 32 |
#include <stdbool.h> |
| 33 |
#include <stdint.h> |
| 34 |
#include <stdio.h> |
| 35 |
#include <stdlib.h> |
| 36 |
|
| 37 |
#include <pthread.h> |
| 38 |
|
| 39 |
#include <netdb.h> |
| 40 |
#include <netinet/in.h> |
| 41 |
#include <sys/socket.h> |
| 42 |
|
| 43 |
|
| 44 |
#include <proton/connection_driver.h> |
| 45 |
#include <proton/proactor.h> |
| 46 |
|
| 47 |
#include "netaddr-internal.h" |
| 48 |
#include "proactor-internal.h" |
| 49 |
|
| 50 |
#ifdef __cplusplus |
| 51 |
extern "C" { |
| 52 |
#endif |
| 53 |
|
| 54 |
typedef struct acceptor_t acceptor_t; |
| 55 |
typedef struct tslot_t tslot_t; |
| 56 |
typedef pthread_mutex_t pmutex; |
| 57 |
typedef struct pni_timer_t pni_timer_t; |
| 58 |
|
| 59 |
typedef enum { |
| 60 |
EVENT_FD, /* schedule() or pn_proactor_interrupt() */ |
| 61 |
LISTENER_IO, |
| 62 |
PCONNECTION_IO, |
| 63 |
RAW_CONNECTION_IO, |
| 64 |
TIMER |
| 65 |
} epoll_type_t; |
| 66 |
|
| 67 |
// Data to use with epoll. |
| 68 |
typedef struct epoll_extended_t { |
| 69 |
int fd; |
| 70 |
epoll_type_t type; // io/timer/eventfd |
| 71 |
uint32_t wanted; // events to poll for |
| 72 |
bool polling; |
| 73 |
pmutex barrier_mutex; |
| 74 |
} epoll_extended_t; |
| 75 |
|
| 76 |
typedef enum { |
| 77 |
PROACTOR, |
| 78 |
PCONNECTION, |
| 79 |
LISTENER, |
| 80 |
RAW_CONNECTION, |
| 81 |
TIMER_MANAGER |
| 82 |
} task_type_t; |
| 83 |
|
| 84 |
typedef struct task_t { |
| 85 |
pmutex mutex; |
| 86 |
pn_proactor_t *proactor; /* Immutable */ |
| 87 |
task_type_t type; |
| 88 |
bool working; |
| 89 |
bool ready; // ready to run and on ready list. Poller notified by eventfd. |
| 90 |
bool waking; |
| 91 |
unsigned int ready_generation; |
| 92 |
struct task_t *ready_next; // ready list, guarded by proactor eventfd_mutex |
| 93 |
struct task_t *resched_next; // resched list, guarded by sched mutex |
| 94 |
bool closing; |
| 95 |
// Next 4 are protected by the proactor mutex |
| 96 |
struct task_t* next; /* Protected by proactor.mutex */ |
| 97 |
struct task_t* prev; /* Protected by proactor.mutex */ |
| 98 |
int disconnect_ops; /* ops remaining before disconnect complete */ |
| 99 |
bool disconnecting; /* pn_proactor_disconnect */ |
| 100 |
// Protected by schedule mutex |
| 101 |
tslot_t *runner __attribute__((aligned(64))); /* designated or running thread */ |
| 102 |
tslot_t *prev_runner; |
| 103 |
bool sched_ready; |
| 104 |
bool sched_pending; /* If true, one or more unseen epoll or other events to process() */ |
| 105 |
int runnables_idx; /* 0 means unset, idx-1 is array position */ |
| 106 |
} task_t; |
| 107 |
|
| 108 |
typedef enum { |
| 109 |
NEW, |
| 110 |
UNUSED, /* pn_proactor_done() called, may never come back */ |
| 111 |
SUSPENDED, |
| 112 |
PROCESSING, /* Hunting for a task */ |
| 113 |
BATCHING, /* Doing work on behalf of a task */ |
| 114 |
DELETING, |
| 115 |
POLLING |
| 116 |
} tslot_state; |
| 117 |
|
| 118 |
// Epoll proactor's concept of a worker thread provided by the application. |
| 119 |
struct tslot_t { |
| 120 |
pmutex mutex; // suspend and resume |
| 121 |
pthread_cond_t cond; |
| 122 |
unsigned int generation; |
| 123 |
bool suspended; |
| 124 |
volatile bool scheduled; |
| 125 |
tslot_state state; |
| 126 |
task_t *task; |
| 127 |
task_t *prev_task; |
| 128 |
bool earmarked; |
| 129 |
tslot_t *suspend_list_prev; |
| 130 |
tslot_t *suspend_list_next; |
| 131 |
tslot_t *earmark_override; // on earmark_drain, which thread was unassigned |
| 132 |
unsigned int earmark_override_gen; |
| 133 |
}; |
| 134 |
|
| 135 |
typedef struct pni_timer_manager_t { |
| 136 |
task_t task; |
| 137 |
epoll_extended_t epoll_timer; |
| 138 |
pmutex deletion_mutex; |
| 139 |
pni_timer_t *proactor_timer; |
| 140 |
pn_list_t *timers_heap; |
| 141 |
uint64_t timerfd_deadline; |
| 142 |
bool sched_timeout; |
| 143 |
} pni_timer_manager_t; |
| 144 |
|
| 145 |
struct pn_proactor_t { |
| 146 |
task_t task; |
| 147 |
pni_timer_manager_t timer_manager; |
| 148 |
epoll_extended_t epoll_schedule; /* ready list */ |
| 149 |
epoll_extended_t epoll_interrupt; |
| 150 |
pn_event_batch_t batch; |
| 151 |
task_t *tasks; /* track in-use tasks for PN_PROACTOR_INACTIVE and disconnect */ |
| 152 |
pni_timer_t *timer; |
| 153 |
size_t disconnects_pending; /* unfinished proactor disconnects*/ |
| 154 |
// need_xxx flags indicate we should generate PN_PROACTOR_XXX on the next update_batch() |
| 155 |
bool need_interrupt; |
| 156 |
bool need_inactive; |
| 157 |
bool need_timeout; |
| 158 |
bool timeout_set; /* timeout has been set by user and not yet cancelled or generated event */ |
| 159 |
bool timeout_processed; /* timeout event dispatched in the most recent event batch */ |
| 160 |
pmutex timeout_mutex; |
| 161 |
int task_count; |
| 162 |
|
| 163 |
// ready list subsystem |
| 164 |
int eventfd; |
| 165 |
pmutex eventfd_mutex; |
| 166 |
bool ready_list_active; |
| 167 |
task_t *ready_list_first; |
| 168 |
task_t *ready_list_last; |
| 169 |
unsigned int ready_list_count; |
| 170 |
unsigned int ready_list_generation; // protected by both eventfd_mutex and a single p->poller instance |
| 171 |
// Interrupts have a dedicated eventfd because they must be async-signal safe. |
| 172 |
int interruptfd; |
| 173 |
// If the process runs out of file descriptors, disarm listening sockets temporarily and save them here. |
| 174 |
acceptor_t *overflow; |
| 175 |
pmutex overflow_mutex; |
| 176 |
|
| 177 |
// Sched vars specific to proactor task. |
| 178 |
bool sched_interrupt; |
| 179 |
|
| 180 |
// Global scheduling/poller vars. |
| 181 |
// Warm runnables have assigned or earmarked tslots and can run right away. |
| 182 |
// Other runnables are run as tslots come available. |
| 183 |
pmutex sched_mutex; |
| 184 |
int n_runnables; |
| 185 |
int next_runnable; |
| 186 |
int n_warm_runnables; |
| 187 |
tslot_t *suspend_list_head; |
| 188 |
tslot_t *suspend_list_tail; |
| 189 |
int suspend_list_count; |
| 190 |
tslot_t *poller; |
| 191 |
bool poller_suspended; |
| 192 |
tslot_t *last_earmark; |
| 193 |
task_t *sched_ready_first; |
| 194 |
task_t *sched_ready_last; |
| 195 |
bool sched_ready_pending; |
| 196 |
unsigned int sched_ready_count; |
| 197 |
task_t *resched_first; |
| 198 |
task_t *resched_last; |
| 199 |
task_t *resched_cutoff; // last resched task of current poller work snapshot. TODO: superseded by polled_resched_count? |
| 200 |
task_t *resched_next; |
| 201 |
unsigned int resched_count; |
| 202 |
unsigned int polled_resched_count; |
| 203 |
pmutex tslot_mutex; |
| 204 |
int earmark_count; |
| 205 |
bool earmark_drain; |
| 206 |
// For debugging help for core dumps with optimized code. |
| 207 |
pn_event_type_t current_event_type; |
| 208 |
|
| 209 |
// Mostly read only: after init or once thread_count stabilizes |
| 210 |
pn_collector_t *collector __attribute__((aligned(64))); |
| 211 |
task_t **warm_runnables; |
| 212 |
task_t **runnables; |
| 213 |
tslot_t **resume_list; |
| 214 |
pn_hash_t *tslot_map; |
| 215 |
struct epoll_event *kevents; |
| 216 |
int epollfd; |
| 217 |
int thread_count; |
| 218 |
int thread_capacity; |
| 219 |
int runnables_capacity; |
| 220 |
int kevents_capacity; |
| 221 |
bool shutting_down; |
| 222 |
}; |
| 223 |
|
| 224 |
/* common to connection and listener */ |
| 225 |
typedef struct psocket_t { |
| 226 |
// Protected by the pconnection/listener mutex |
| 227 |
epoll_extended_t epoll_io; |
| 228 |
uint32_t sched_io_events; |
| 229 |
uint32_t working_io_events; |
| 230 |
} psocket_t; |
| 231 |
|
| 232 |
typedef struct pconnection_t { |
| 233 |
task_t task; |
| 234 |
psocket_t psocket; |
| 235 |
pni_timer_t *timer; |
| 236 |
const char *host, *port; |
| 237 |
uint32_t new_events; |
| 238 |
bool server; /* accept, not connect */ |
| 239 |
bool tick_pending; |
| 240 |
bool queued_disconnect; /* deferred from pn_proactor_disconnect() */ |
| 241 |
pn_condition_t *disconnect_condition; |
| 242 |
// Following values only changed by (sole) working task: |
| 243 |
uint32_t current_arm; // active epoll io events |
| 244 |
bool connected; |
| 245 |
bool read_blocked; |
| 246 |
bool write_blocked; |
| 247 |
bool disconnected; |
| 248 |
int hog_count; // thread hogging limiter |
| 249 |
pn_event_batch_t batch; |
| 250 |
pn_connection_driver_t driver; |
| 251 |
bool output_drained; |
| 252 |
const char *wbuf_current; |
| 253 |
size_t wbuf_remaining; |
| 254 |
size_t wbuf_completed; |
| 255 |
pn_event_type_t current_event_type;/* Sole use for debugging, i.e. crash analysis of optimized code. */ |
| 256 |
uint32_t process_args; /* Sole use for debugging */ |
| 257 |
uint32_t process_events; /* Sole use for debugging */ |
| 258 |
struct pn_netaddr_t local, remote; /* Actual addresses */ |
| 259 |
struct addrinfo *addrinfo; /* Resolved address list */ |
| 260 |
struct addrinfo *ai; /* Current connect address */ |
| 261 |
pmutex rearm_mutex; /* protects pconnection_rearm from out of order arming*/ |
| 262 |
bool io_doublecheck; /* callbacks made and new IO may have arrived */ |
| 263 |
uint64_t expected_timeout; |
| 264 |
char addr_buf[1]; |
| 265 |
} pconnection_t; |
| 266 |
|
| 267 |
/* |
| 268 |
* A listener can have multiple sockets (as specified in the addrinfo). They |
| 269 |
* are armed separately. The individual psockets can be part of at most one |
| 270 |
* list: the global proactor overflow retry list or the per-listener list of |
| 271 |
* pending accepts (valid inbound socket obtained, but pn_listener_accept not |
| 272 |
* yet called by the application). These lists will be small and quick to |
| 273 |
* traverse. |
| 274 |
*/ |
| 275 |
|
| 276 |
struct acceptor_t{ |
| 277 |
psocket_t psocket; |
| 278 |
struct pn_netaddr_t addr; /* listening address */ |
| 279 |
pn_listener_t *listener; |
| 280 |
acceptor_t *next; /* next listener list member */ |
| 281 |
bool armed; |
| 282 |
bool overflowed; |
| 283 |
}; |
| 284 |
|
| 285 |
typedef struct accepted_t{ |
| 286 |
int accepted_fd; |
| 287 |
} accepted_t; |
| 288 |
|
| 289 |
struct pn_listener_t { |
| 290 |
task_t task; |
| 291 |
acceptor_t *acceptors; /* Array of listening sockets */ |
| 292 |
size_t acceptors_size; |
| 293 |
char addr_buf[PN_MAX_ADDR]; |
| 294 |
const char *host, *port; |
| 295 |
int active_count; /* Number of listener sockets registered with epoll */ |
| 296 |
pn_condition_t *condition; |
| 297 |
pn_collector_t *collector; |
| 298 |
pn_event_batch_t batch; |
| 299 |
pn_record_t *attachments; |
| 300 |
void *listener_context; |
| 301 |
accepted_t *pending_accepteds; /* array of accepted connections */ |
| 302 |
size_t pending_first; /* index of first pending connection */ |
| 303 |
size_t pending_count; /* number of pending accepted connections */ |
| 304 |
size_t backlog; /* size of pending accepted array */ |
| 305 |
bool close_dispatched; |
| 306 |
int overflow_count; |
| 307 |
uint32_t sched_io_events; |
| 308 |
}; |
| 309 |
|
| 310 |
typedef char strerrorbuf[1024]; /* used for pstrerror message buffer */ |
| 311 |
void pstrerror(int err, strerrorbuf msg); |
| 312 |
|
| 313 |
/* Internal error, no recovery */ |
| 314 |
#define EPOLL_FATAL(EXPR, SYSERRNO) \ |
| 315 |
do { \ |
| 316 |
strerrorbuf msg; \ |
| 317 |
pstrerror((SYSERRNO), msg); \ |
| 318 |
fprintf(stderr, "epoll proactor failure in %s:%d: %s: %s\n", \ |
| 319 |
__FILE__, __LINE__ , #EXPR, msg); \ |
| 320 |
abort(); \ |
| 321 |
} while (0) |
| 322 |
|
| 323 |
// In general all locks to be held singly and shortly (possibly as spin locks). |
| 324 |
// See above about lock ordering. |
| 325 |
|
| 326 |
static inline void pmutex_init(pthread_mutex_t *pm){ |
| 327 |
pthread_mutexattr_t attr; |
| 328 |
|
| 329 |
pthread_mutexattr_init(&attr); |
| 330 |
// PROTON-2346: Some stdlibs (e.g., musl) don't implement PTHREAD_MUTEX_ADAPTIVE_NP |
| 331 |
// Since this option is a performance hint, it should be harmless to drop it when it's not available. |
| 332 |
#ifdef PTHREAD_MUTEX_ADAPTIVE_NP |
| 333 |
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP); |
| 334 |
#endif |
| 335 |
if (pthread_mutex_init(pm, &attr)) { |
| 336 |
perror("pthread failure"); |
| 337 |
abort(); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
static inline void pmutex_finalize(pthread_mutex_t *m) { pthread_mutex_destroy(m); } |
| 342 |
static inline void lock(pmutex *m) { pthread_mutex_lock(m); } |
| 343 |
static inline void unlock(pmutex *m) { pthread_mutex_unlock(m); } |
| 344 |
|
| 345 |
static inline bool pconnection_has_event(pconnection_t *pc) { |
| 346 |
return pn_connection_driver_has_event(&pc->driver); |
| 347 |
} |
| 348 |
|
| 349 |
static inline bool listener_has_event(pn_listener_t *l) { |
| 350 |
return pn_collector_peek(l->collector) || (l->pending_count); |
| 351 |
} |
| 352 |
|
| 353 |
static inline bool proactor_has_event(pn_proactor_t *p) { |
| 354 |
return pn_collector_peek(p->collector); |
| 355 |
} |
| 356 |
|
| 357 |
bool schedule_if_inactive(pn_proactor_t *p); |
| 358 |
int pclosefd(pn_proactor_t *p, int fd); |
| 359 |
|
| 360 |
void proactor_add(task_t *tsk); |
| 361 |
bool proactor_remove(task_t *tsk); |
| 362 |
|
| 363 |
bool unassign_thread(pn_proactor_t *p, tslot_t *ts, tslot_state new_state, tslot_t **resume_thread); |
| 364 |
|
| 365 |
void task_init(task_t *tsk, task_type_t t, pn_proactor_t *p); |
| 366 |
static void task_finalize(task_t* tsk) { |
| 367 |
pmutex_finalize(&tsk->mutex); |
| 368 |
} |
| 369 |
|
| 370 |
bool schedule(task_t *tsk); |
| 371 |
void notify_poller(pn_proactor_t *p); |
| 372 |
void schedule_done(task_t *tsk); |
| 373 |
|
| 374 |
void psocket_init(psocket_t* ps, epoll_type_t type); |
| 375 |
bool start_polling(epoll_extended_t *ee, int epollfd); |
| 376 |
void stop_polling(epoll_extended_t *ee, int epollfd); |
| 377 |
void rearm_polling(epoll_extended_t *ee, int epollfd); |
| 378 |
|
| 379 |
int pgetaddrinfo(const char *host, const char *port, int flags, struct addrinfo **res); |
| 380 |
void configure_socket(int sock); |
| 381 |
|
| 382 |
accepted_t *listener_accepted_next(pn_listener_t *listener); |
| 383 |
|
| 384 |
task_t *pni_psocket_raw_task(psocket_t *ps); |
| 385 |
psocket_t *pni_task_raw_psocket(task_t *t); |
| 386 |
pn_event_batch_t *pni_raw_connection_process(task_t *t, uint32_t io_events, bool sched_ready); |
| 387 |
|
| 388 |
typedef struct praw_connection_t praw_connection_t; |
| 389 |
task_t *pni_raw_connection_task(praw_connection_t *rc); |
| 390 |
praw_connection_t *pni_batch_raw_connection(pn_event_batch_t* batch); |
| 391 |
void pni_raw_connection_done(praw_connection_t *rc); |
| 392 |
|
| 393 |
pni_timer_t *pni_timer(pni_timer_manager_t *tm, pconnection_t *c); |
| 394 |
void pni_timer_free(pni_timer_t *timer); |
| 395 |
bool pni_timer_set(pni_timer_t *timer, uint64_t deadline); |
| 396 |
bool pni_timer_manager_init(pni_timer_manager_t *tm); |
| 397 |
void pni_timer_manager_finalize(pni_timer_manager_t *tm); |
| 398 |
pn_event_batch_t *pni_timer_manager_process(pni_timer_manager_t *tm, bool timeout, bool sched_ready); |
| 399 |
void pni_pconnection_timeout(pconnection_t *pc); |
| 400 |
void pni_proactor_timeout(pn_proactor_t *p); |
| 401 |
void pni_resume(pn_proactor_t *p, tslot_t *ts); |
| 402 |
|
| 403 |
// Generic wake primitives for a task. |
| 404 |
|
| 405 |
// Call with task lock held. Must call notify_poller() if returns true. |
| 406 |
static inline bool pni_task_wake(task_t *tsk) { |
| 407 |
if (!tsk->waking) { |
| 408 |
tsk->waking = true; |
| 409 |
return schedule(tsk); |
| 410 |
} |
| 411 |
return false; |
| 412 |
} |
| 413 |
|
| 414 |
// Call with task lock held. |
| 415 |
static inline bool pni_task_wake_pending(task_t *tsk) { |
| 416 |
return tsk->waking; |
| 417 |
} |
| 418 |
|
| 419 |
// Call with task lock held and only from the running task. |
| 420 |
static inline void pni_task_wake_done(task_t *tsk) { |
| 421 |
tsk->waking = false; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
#ifdef __cplusplus |
| 426 |
} |
| 427 |
#endif |
| 428 |
#endif // PROACTOR_EPOLL_INTERNAL_H |