Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_threads.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#include <stdio.h>
28#include <errno.h>
29#include <string.h>
30
31#include "nilorea/n_log.h"
32#include "nilorea/n_time.h"
34
35void usage(void) {
36 fprintf(stderr,
37 " -v version\n"
38 " -h help\n"
39 " -V LOG_LEVEL (LOG_DEBUG,INFO,NOTICE,ERR)\n");
40}
41
42void process_args(int argc, char** argv) {
43 int getoptret = 0,
44 log_level = LOG_ERR; /* default log level */
45
46 while ((getoptret = getopt(argc, argv, "vhV:")) != EOF) {
47 switch (getoptret) {
48 case 'v':
49 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
50 exit(1);
51 case 'V':
52 if (!strcmp("LOG_NULL", optarg))
54 else if (!strcmp("LOG_NOTICE", optarg))
56 else if (!strcmp("LOG_INFO", optarg))
58 else if (!strcmp("LOG_ERR", optarg))
60 else if (!strcmp("LOG_DEBUG", optarg))
62 else {
63 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
64 exit(-1);
65 }
66 break;
67 default:
68 case '?': {
69 if (optopt == 'V') {
70 fprintf(stderr, "\n Missing log level\n");
71 }
72 usage();
73 exit(1);
74 }
75 case 'h': {
76 usage();
77 exit(1);
78 }
79 } /* switch */
81 }
82} /* void process_args( ... ) */
83
84void* occupy_thread(void* rest) {
85 __n_assert(rest, return NULL);
86
87 intptr_t sleep_value = (intptr_t)(rest);
88
89 n_log(LOG_DEBUG, "Starting to sleep %d usecs on thread %lld", sleep_value, pthread_self());
90
91 if (sleep_value < 1000000) {
92 usleep((unsigned int)sleep_value);
93 } else {
94 usleep((unsigned int)((sleep_value) % 1000000));
95 sleep((unsigned int)(sleep_value / 1000000));
96 }
97
98 n_log(LOG_DEBUG, "End of sleep %d usecs on thread %lld", sleep_value, pthread_self());
99
100 return NULL;
101}
102
103int main(int argc, char** argv) {
104 long int cores = get_nb_cpu_cores();
105 int nb_active_threads = (cores > 0) ? (int)cores : 1;
106 int nb_waiting_threads = 2 * nb_active_threads;
107 int nb_total_threads = (nb_active_threads + nb_waiting_threads);
108
110
111 // processing args and set log_level
112 process_args(argc, argv);
113
114 n_log(LOG_INFO, "Creating a new thread pool of %d active and %d waiting threads", nb_active_threads, nb_waiting_threads);
115 THREAD_POOL* thread_pool = new_thread_pool((size_t)nb_active_threads, (size_t)nb_waiting_threads);
116
117 n_log(LOG_INFO, "Adding new %d new tasks...", nb_total_threads);
118 for (int it = 0; it < nb_total_threads; it++) {
119 n_log(LOG_INFO, "adding task %d", it);
120 // sleep time as a payload to the occupy_thread
121 int sleep_value = 1 + rand() % 1000000;
122 // add task and payload
123 if (add_threaded_process(thread_pool, &occupy_thread, (void*)(intptr_t)sleep_value, NORMAL_PROC) == FALSE) {
124 n_log(LOG_ERR, "Error adding client management to thread pool");
125 }
126 }
127 n_log(LOG_INFO, "Adding tasks done. Waiting for pool thread to complete the tasks...");
129
130 n_log(LOG_INFO, "Task completed. Destroying pool...");
132 n_log(LOG_INFO, "Destroyed.");
133
134 /* test SYNCED_PROC with start_threaded_pool and wait_for_synced_threaded_pool */
135 n_log(LOG_INFO, "--- Synced thread pool test ---");
136 thread_pool = new_thread_pool((size_t)nb_active_threads, (size_t)nb_waiting_threads);
137 for (int it = 0; it < nb_active_threads; it++) {
138 int sleep_value = 1 + rand() % 500000;
139 if (add_threaded_process(thread_pool, &occupy_thread, (void*)(intptr_t)sleep_value, SYNCED_PROC) == FALSE) {
140 n_log(LOG_ERR, "Error adding synced process %d", it);
141 }
142 }
143 n_log(LOG_INFO, "Starting synced pool...");
145
146 n_log(LOG_INFO, "Waiting for synced pool...");
148
149 n_log(LOG_INFO, "Synced pool done. Testing refresh_thread_pool...");
151
153 n_log(LOG_INFO, "All thread pool tests done.");
154
155 exit(0);
156} /* END_OF_MAIN() */
static void usage(void)
void process_args(int argc, char **argv)
Definition ex_common.c:47
int main(void)
THREAD_POOL * thread_pool
Definition ex_fluid.c:77
int getoptret
Definition ex_fluid.c:60
int log_level
Definition ex_fluid.c:61
void * occupy_thread(void *rest)
Definition ex_threads.c:84
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:278
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_ERR
error conditions
Definition n_log.h:75
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
#define LOG_NULL
no log output
Definition n_log.h:45
#define LOG_INFO
informational
Definition n_log.h:81
#define NORMAL_PROC
processing mode for added func, synced start, can be queued
int start_threaded_pool(THREAD_POOL *thread_pool)
Launch the process waiting for execution in the thread pool.
THREAD_POOL * new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting)
Create a new pool of nbmaxthr threads.
#define SYNCED_PROC
processing mode for added func, synced start, not queued
int add_threaded_process(THREAD_POOL *thread_pool, void *(*func_ptr)(void *param), void *param, int mode)
add a function and params to a thread pool
int refresh_thread_pool(THREAD_POOL *thread_pool)
try to add some waiting DIRECT_PROCs on some free thread slots, else do nothing
int wait_for_threaded_pool(THREAD_POOL *thread_pool)
Wait for the thread pool to become idle (no active threads, empty waiting list), blocking without pol...
int destroy_threaded_pool(THREAD_POOL **pool, unsigned int delay)
delete a thread_pool, exit the threads and free the structs
int wait_for_synced_threaded_pool(THREAD_POOL *thread_pool)
wait for all the launched process, blocking but light on the CPU as there is no polling
long int get_nb_cpu_cores()
get number of core of current system
Structure of a thread pool.
Generic log system.
Thread pool declaration.
Timing utilities.