6 n__network__accept__pool_8h
gullradriel edited this page 2026-10-09 00:55:18 +02:00

include/nilorea/n_network_accept_pool.h

Accept pool for parallel connection acceptance (nginx-style). More...

Structures

Name
struct NETW_ACCEPT_POOL_STATS
Statistics for the accept pool.
struct NETW_ACCEPT_POOL
Structure of a parallel accept pool.

Detailed Description

Accept pool for parallel connection acceptance (nginx-style).

Author: Castagnier Mickael

Version: 1.0

Date: 11/03/2026

Source code

/*
 * Nilorea Library
 * Copyright (C) 2005-2026 Castagnier Mickael
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */


#ifndef __NILOREA_NETWORK_ACCEPT_POOL__
#define __NILOREA_NETWORK_ACCEPT_POOL__

#ifdef __cplusplus
extern "C" {
#endif


#include "n_common.h"
#include "n_network.h"
#include "n_thread_pool.h"

#include <time.h>

#define NETW_ACCEPT_POOL_IDLE 0
#define NETW_ACCEPT_POOL_RUNNING 1
#define NETW_ACCEPT_POOL_STOPPING 2
#define NETW_ACCEPT_POOL_STOPPED 3

typedef void (*netw_accept_callback_t)(NETWORK* accepted, void* user_data);

typedef struct NETW_ACCEPT_POOL_STATS {
    size_t total_accepted;
    size_t total_errors;
    size_t total_timeouts;
    size_t active_threads;
    struct timespec start_time;
} NETW_ACCEPT_POOL_STATS;

typedef struct NETW_ACCEPT_POOL {
    NETWORK* server;
    size_t nb_accept_threads;
    pthread_t* accept_threads;
    netw_accept_callback_t callback;
    void* user_data;
    int accept_timeout;
    uint32_t state;
    NETW_ACCEPT_POOL_STATS stats;
    pthread_mutex_t stats_lock;
} NETW_ACCEPT_POOL;

#define netw_accept_pool_atomic_read_state(pool) __atomic_load_n(&(pool)->state, __ATOMIC_ACQUIRE)

#define netw_accept_pool_atomic_write_state(pool, val) __atomic_store_n(&(pool)->state, (val), __ATOMIC_RELEASE)

NETW_ACCEPT_POOL* netw_accept_pool_create(NETWORK* server, size_t nb_threads, int accept_timeout, netw_accept_callback_t callback, void* user_data);

int netw_accept_pool_start(NETW_ACCEPT_POOL* pool);

int netw_accept_pool_stop(NETW_ACCEPT_POOL* pool);

int netw_accept_pool_wait(NETW_ACCEPT_POOL* pool, int timeout_sec);

int netw_accept_pool_get_stats(NETW_ACCEPT_POOL* pool, NETW_ACCEPT_POOL_STATS* stats);

int netw_accept_pool_destroy(NETW_ACCEPT_POOL** pool);


#ifdef __cplusplus
}
#endif

#endif /* __NILOREA_NETWORK_ACCEPT_POOL__ */