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