Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_threads.c
1
7#include <stdio.h>
8#include <errno.h>
9#include <string.h>
10
11#include "nilorea/n_log.h"
12#include "nilorea/n_time.h"
14
15void usage(void) {
16 fprintf(stderr,
17 " -v version\n"
18 " -h help\n"
19 " -V LOG_LEVEL (LOG_DEBUG,INFO,NOTICE,ERR)\n");
20}
21
22void process_args(int argc, char** argv) {
23 int getoptret = 0,
24 log_level = LOG_ERR; /* default log level */
25
26 while ((getoptret = getopt(argc, argv, "vhV:")) != EOF) {
27 switch (getoptret) {
28 case 'v':
29 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
30 exit(1);
31 case 'V':
32 if (!strncmp("LOG_NULL", optarg, 5)) {
33 log_level = LOG_NULL;
34 } else {
35 if (!strncmp("LOG_NOTICE", optarg, 6)) {
36 log_level = LOG_NOTICE;
37 } else {
38 if (!strncmp("LOG_INFO", optarg, 7)) {
39 log_level = LOG_INFO;
40 } else {
41 if (!strncmp("LOG_ERR", optarg, 5)) {
42 log_level = LOG_ERR;
43 } else {
44 if (!strncmp("LOG_DEBUG", optarg, 5)) {
45 log_level = LOG_DEBUG;
46 } else {
47 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
48 exit(-1);
49 }
50 }
51 }
52 }
53 }
54 break;
55 default:
56 case '?': {
57 if (optopt == 'V') {
58 fprintf(stderr, "\n Missing log level\n");
59 }
60 usage();
61 exit(1);
62 }
63 case 'h': {
64 usage();
65 exit(1);
66 }
67 } /* switch */
68 set_log_level(log_level);
69 }
70} /* void process_args( ... ) */
71
72void* occupy_thread(void* rest) {
73 __n_assert(rest, return NULL);
74
75 intptr_t sleep_value = (intptr_t)(rest);
76
77 n_log(LOG_DEBUG, "Starting to sleep %d usecs on thread %lld", sleep_value, pthread_self());
78
79 if (sleep_value < 1000000) {
80 usleep(sleep_value);
81 } else {
82 usleep((sleep_value) % 1000000);
83 sleep((sleep_value / 1000000));
84 }
85
86 n_log(LOG_DEBUG, "End of sleep %d usecs on thread %lld", sleep_value, pthread_self());
87
88 return NULL;
89}
90
91int main(int argc, char** argv) {
92 int nb_active_threads = get_nb_cpu_cores();
93 int nb_waiting_threads = 2 * nb_active_threads;
94 int nb_total_threads = (nb_active_threads + nb_waiting_threads);
95
97
98 // processing args and set log_level
99 process_args(argc, argv);
100
101 n_log(LOG_INFO, "Creating a new thread pool of %d active and %d waiting threads", nb_active_threads, nb_waiting_threads);
102 THREAD_POOL* thread_pool = new_thread_pool(nb_active_threads, nb_waiting_threads);
103
104 n_log(LOG_INFO, "Adding new %d new tasks...", nb_total_threads);
105 for (int it = 0; it < nb_total_threads; it++) {
106 n_log(LOG_INFO, "adding task %d", it);
107 // sleep time as a payload to the occupy_thread
108 int sleep_value = 1 + rand() % 1000000;
109 // add task and payload
110 if (add_threaded_process(thread_pool, &occupy_thread, (void*)(intptr_t)sleep_value, DIRECT_PROC) == FALSE) {
111 n_log(LOG_ERR, "Error adding client management to thread pool");
112 }
113 }
114 n_log(LOG_INFO, "Adding tasks done. Waiting for pool thread to complete the tasks...");
115 wait_for_threaded_pool(thread_pool, 1000);
116
117 n_log(LOG_INFO, "Task completed. Destroying pool...");
118 destroy_threaded_pool(&thread_pool, 1000);
119
120 n_log(LOG_INFO, "Destroyed.");
121
122 exit(0);
123} /* END_OF_MAIN() */
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:258
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:69
#define LOG_DEBUG
debug-level messages
Definition n_log.h:64
#define LOG_ERR
error conditions
Definition n_log.h:56
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:91
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:60
#define LOG_NULL
no log output
Definition n_log.h:26
#define LOG_INFO
informational
Definition n_log.h:62
THREAD_POOL * new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting)
Create a new pool of nbmaxthr threads.
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 destroy_threaded_pool(THREAD_POOL **pool, unsigned int delay)
delete a thread_pool, exit the threads and free the structs
long int get_nb_cpu_cores()
get number of core of current system
#define DIRECT_PROC
processing mode for added func, direct start
int wait_for_threaded_pool(THREAD_POOL *thread_pool, unsigned int delay)
Wait for all the launched process in the thread pool to terminate.
Structure of a trhead pool.
Generic log system.
Thread pool declaration.
Timing utilities.