Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_reactor.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
44#ifndef NILOREA_N_REACTOR_H
45#define NILOREA_N_REACTOR_H
46
47/* Compile-time availability gate. Linux + Android both ship epoll
48 * + eventfd. Solaris has /dev/poll and event ports (different
49 * primitive); Windows has IOCP (different architecture). For now
50 * the reactor is Linux/Android-only and falls back to the existing
51 * thread engine on every other target.
52 *
53 * Wrapped in `#ifndef` so the build system can force the gate off
54 * via `-DN_REACTOR_AVAILABLE=0` (e.g. on Linux when the user
55 * deliberately opts out of reactor mode to drop the n_reactor.o
56 * link dependency). The Makefile sets this when HAVE_REACTOR=0. */
57#ifndef N_REACTOR_AVAILABLE
58#if defined(__linux__) || defined(__ANDROID__)
59#define N_REACTOR_AVAILABLE 1
60#else
61#define N_REACTOR_AVAILABLE 0
62#endif
63#endif
64
65#ifdef __cplusplus
66extern "C" {
67#endif
68
69#include "nilorea/n_common.h"
70#include "nilorea/n_network.h"
71
74typedef struct n_reactor n_reactor;
75
78typedef struct {
79 long long events_processed;
80 long long writes_partial;
81 long long reads_partial;
82 long long fds_registered;
83 long long fds_unregistered;
84 long long wake_signals;
85 /* Registered-list walk diagnostics. wake_walks counts how many
86 * times the wake-time list walk fired; wake_walk_visits sums
87 * every NETWORK visited; wake_walk_drains sums every visit that
88 * found data to send. Ratio (drains/visits) measures how
89 * productive the walk is. */
90 long long wake_walks;
94
108n_reactor* n_reactor_new(int max_fds_hint);
109
121void n_reactor_destroy(n_reactor** reactor);
122
130void n_reactor_run(n_reactor* reactor);
131
139void* n_reactor_run_thread_entry(void* arg);
140
148void n_reactor_stop(n_reactor* reactor);
149
157void n_reactor_get_stats(const n_reactor* reactor, n_reactor_stats* out);
158
174
183
196
225
250 size_t send_list_limit,
251 size_t recv_list_limit,
252 int blocking,
253 n_reactor* reactor,
254 int* retval);
255
256#ifdef __cplusplus
257} /* extern "C" */
258#endif
259
260#endif /* NILOREA_N_REACTOR_H */
NETWORK * netw
Network for server mode, accepting incomming.
Definition ex_network.c:39
Structure of a NETWORK.
Definition n_network.h:309
Common headers and low-level functions & define.
Network Engine.
long long fds_registered
lifetime register call count
Definition n_reactor.h:82
long long writes_partial
EAGAIN on send -> re-armed EPOLLOUT.
Definition n_reactor.h:80
long long reads_partial
EAGAIN on recv -> kept accumulator.
Definition n_reactor.h:81
long long events_processed
total epoll events dispatched
Definition n_reactor.h:79
void * n_reactor_run_thread_entry(void *arg)
pthread_create-compatible entry point that calls n_reactor_run on the reactor passed via arg.
Definition n_reactor.c:1227
long long wake_signals
eventfd wake events processed
Definition n_reactor.h:84
void n_reactor_run(n_reactor *reactor)
Run the epoll loop on the calling thread.
Definition n_reactor.c:1219
long long wake_walk_visits
Definition n_reactor.h:91
long long wake_walk_drains
Definition n_reactor.h:92
void n_reactor_get_stats(const n_reactor *reactor, n_reactor_stats *out)
Read current stats counters into *out.
Definition n_reactor.c:1232
struct n_reactor n_reactor
Opaque reactor handle.
Definition n_reactor.h:74
n_reactor * n_reactor_new(int max_fds_hint)
Create a new reactor.
Definition n_reactor.c:1207
long long fds_unregistered
lifetime unregister call count
Definition n_reactor.h:83
NETWORK * netw_accept_into_reactor(NETWORK *listener, size_t send_list_limit, size_t recv_list_limit, int blocking, n_reactor *reactor, int *retval)
Accept a connection on listener and register it with reactor instead of starting per-connection threa...
Definition n_reactor.c:1256
void n_reactor_unregister(n_reactor *reactor, NETWORK *netw)
Unregister a NETWORK from the reactor.
Definition n_reactor.c:1243
int n_reactor_register(n_reactor *reactor, NETWORK *netw)
Register a NETWORK with the reactor.
Definition n_reactor.c:1237
long long wake_walks
Definition n_reactor.h:90
void n_reactor_notify_send(NETWORK *netw)
Producer-side wake-up after a netw_add_msg.
Definition n_reactor.c:1248
void n_reactor_close_netw_sync(NETWORK *netw)
Synchronously close a reactor-registered NETWORK from the game thread.
Definition n_reactor.c:1252
void n_reactor_stop(n_reactor *reactor)
Signal the run loop to exit at the next iteration.
Definition n_reactor.c:1223
void n_reactor_destroy(n_reactor **reactor)
Tear down a reactor.
Definition n_reactor.c:1215
Counters for the dashboard / profile_server.sh.
Definition n_reactor.h:78