8 n__thread__pool_8h
gullradriel edited this page 2026-10-30 03:26:30 +01:00

include/nilorea/n_thread_pool.h

Thread pool declaration. More...

Structures

Name
struct THREAD_WAITING_PROC
Structure of a waiting process item.
struct THREAD_POOL_NODE
struct THREAD_POOL
Structure of a thread pool.

Detailed Description

Thread pool declaration.

Author: Castagnier Mickael

Version: 1.0

Date: 30/04/2014

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_THREAD_POOL_LIBRARY
#define NILOREA_THREAD_POOL_LIBRARY

#ifdef __cplusplus
extern "C" {
#endif


#include "n_list.h"
#include <pthread.h>
#include <semaphore.h>

#define NORMAL_PROC 1
#define SYNCED_PROC 2
#define DIRECT_PROC 4
#define NO_QUEUE 8
#define IDLE_PROC 16
#define WAITING_PROC 32
#define RUNNING_PROC 64
#define RUNNING_THREAD 128
#define EXITING_THREAD 256
#define EXITED_THREAD 512
#define NO_LOCK 1024

#define N_THREAD_POOL_WAIT_POLL_US 1000

typedef struct THREAD_POOL_NODE {
    void* (*func)(void* param);

    void* param;

    int type;
    int state;
    int thread_state;
    pthread_t thr;

    sem_t th_start,
        th_end;

    pthread_mutex_t lock;

    struct THREAD_POOL* thread_pool;

} THREAD_POOL_NODE;

typedef struct THREAD_POOL {
    THREAD_POOL_NODE** thread_list;

    size_t max_threads;
    size_t nb_max_waiting;
    size_t nb_actives;

    pthread_mutex_t lock;

    sem_t nb_tasks;

    LIST* waiting_list;

    void (*discard)(void* param);

} THREAD_POOL;

typedef struct THREAD_WAITING_PROC {
    void* (*func)(void* param);
    void* param;

} THREAD_WAITING_PROC;

long int get_nb_cpu_cores();
long int get_nb_cpu_physical_cores(void);
THREAD_POOL* new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting);
int add_threaded_process(THREAD_POOL* thread_pool, void* (*func_ptr)(void* param), void* param, int mode);
int start_threaded_pool(THREAD_POOL* thread_pool);
int wait_for_synced_threaded_pool(THREAD_POOL* thread_pool);
int wait_for_threaded_pool(THREAD_POOL* thread_pool);
int destroy_threaded_pool(THREAD_POOL** thread_pool, unsigned int delay);
int set_thread_pool_discard(THREAD_POOL* thread_pool, void (*discard)(void* param));
int refresh_thread_pool(THREAD_POOL* thread_pool);

typedef void (*N_PARALLEL_FN)(size_t begin, size_t end, void* user);

int n_parallel_for(size_t begin, size_t end, size_t grain, N_PARALLEL_FN fn, void* user);

typedef struct N_PARALLEL_TEAM N_PARALLEL_TEAM;

N_PARALLEL_TEAM* n_parallel_team_new(size_t nb_threads);

#define N_PARALLEL_TEAM_MAX 256

size_t n_parallel_team_size(const N_PARALLEL_TEAM* team);

int n_parallel_team_run(N_PARALLEL_TEAM* team, size_t begin, size_t end, size_t grain, N_PARALLEL_FN fn, void* user);

void n_parallel_team_free(N_PARALLEL_TEAM** team);


#ifdef __cplusplus
}
#endif

#endif