Shadow Network Framework  0.0.2-alpha
C Server Library for Shadow Network Framework
Loading...
Searching...
No Matches
thpool.h
Go to the documentation of this file.
1//////////////////////////////////////////////////////////////
2///
3/// \brief this file Defines everything related to ThreadPool
4/// \file thpool.h
5///
6/// Thread Pools By Abdelhadi Seddar
7/// This implemantation of a Threadpool is by no means perfect
8/// nor optimized, because I made it based of a graph depicting
9/// how a threadpool works, without seeing any documentation about it,
10/// this is just a learning experience for
11/// me and im learning through trial and error.
12///
13/// **UPDATE 0.0.2-alpha**:
14/// After research and trial and error, and feedback from
15/// other people and after trying it in a project, i realized
16/// that i made the implementation wrong, as i used to create
17/// each threads whenever there is a `work` to do, which added
18/// CPU Time and latency, so after Reading this [Wikipedia Page](https://en.wikipedia.org/wiki/Thread_pool)
19/// (Last visited on Augest 2, 2024 at 2:37 PM GMT +1)
20/// I noticed that i should've created the threads **At once**
21/// and **from the beggining**. so i did just that in this update
22/// and it did do wonders after comparing the performance
23/// of the two (and doesnt crash as often as 0.0.1-alpha)
24/// //////////////////////////////////////////////////////////
25#ifndef thpool_h
26#define thpool_h
27
28#include <SNF/SNF.h>
29
34
36{
37 /// @brief Used to Signify that the Thread Pool was stopped
38 /// or been ordred to.
39 /// @warning: Only set this status using snf_thpool_stop
41 /// @brief Used to Signify that the Thread Pool is running or been ordred to.
42 /// @warning: Only set this status using snf_thpool_run
44 /// @brief Used to Signify that the Thread Pool is being ordred to be destroyied
45 /// @warning Only Set this Value by calling snf_thpool_destroy
47};
48
49/// @brief Defines the 'work' needed to do by defining a 'func'tion and it's 'arg'ument
51{
52 /// @brief The function to be executed upon
53 void *(*func)();
54 /// @brief the argument to be given upon execution
55 void *arg;
56 /// @brief the source Thread Pool
58 /// @brief Saves the next work ( Linked List )
60};
61/// @brief Defines the 'worker'that would handle a certain 'work'
63{
64 /// @brief The worker's thread id
65 pthread_t worker;
66};
67/// @brief The structure for a Thread Pool
69{
70 /// @brief This LinkedList will store the "works" or "jobs"
72 /// @brief This is the semaphore for handling
74 /// @brief This LinkedList will store the currently working "workers"
75 /// or more exactly the worker thread info
76 /// @note Linked List saves \ref SNF_ThreadPool_work_t . See \ref SNF_ThreadPool_worker_t .
78 /// @brief Used to synchronize the acces to thpool_works
79 /// @warning Must not be touched or the Thread Pool may not work as intended
80 pthread_mutex_t thpool_works_MUTEX;
81 /// @brief Used to synchronize the acces to thpool_workers
82 /// @warning Must not be touched or the Thread Pool may not work as intended
83 pthread_mutex_t thpool_workers_MUTEX;
84 /// @brief Used to synchronize if all "works" are handled and all "workers" had finished their assigned "work"
85 /// See \ref snf_thpool_wait(SNF_thpool *pool)
86 /// @note Use \ref snf_thpool_wait(SNF_thpool *pool) .
87 pthread_mutex_t thpool_noworks_MUTEX;
88 /// @brief Thread that manages "workers" and assign their "work" to them
89 pthread_t *thpool_handler;
90 /// @brief The main function that is runs first when the Thread pool has started
92 /// @brief Semaphore used for synchronization;
93 /// Used primarly to notify the conclusion of "workers" to the Thread pool handler,
94 /// and to Limit the amount of "worker" threads with the max being thpool_n_workers
95 /// @warning Must not be touched or the Thread Pool may not work as intended
97 /// @brief Limiter for the amount of threads that could be created.
98 /// @warning Must not be touched or the Thread Pool may not work as intended
100 /// @brief If set = 1 the thpool_handler will stop creating "workers"
101 /// and wait till all already exising "workers" finish their "work"
102 /// @note Use \ref snf_thpool_stop(SNF_thpool *pool) .
104 pthread_mutex_t thpool_status_MUTEX;
105 pthread_cond_t thpool_status_COND;
107};
108/// @brief This defines the amount of **seconds** to wait a `worker`
109/// to stop, if the `worker` does not stop during this time period
110/// it will be forcefully be stopped.
111/// @note Default Value is 1 Second
112extern time_t SNF_THPOOL_STOPWAIT;
113
114/// @brief Insitanciates a new thread pool, with a Limiter ( Max_Threads )
115/// @param ThreadPool Returns the thread pool by writes the newly created instance in the pointer given in this parameter
116/// @param Max_Threads Maximum Total allowed threads ( Must be 2+ if No Main Worker given, else must me 3+ for proper work)
117/// @param Main_Worker Main Function that would be called upon finishing the intialisation of the
118/// @param arg the argument that would be given to the Main_worker function.
119/// @return 0 On Success || -1 On fail { Shall fail only if 1 - ThreadPool is NULL || 2 - Max_Thread is below recommended/required value }
120extern int snf_thpool_inis(SNF_thpool **ThreadPool, int Max_Threads, void *(*Main_Worker)(void *), void *arg);
121
122/// @brief Creates a "work" that will call the *func* function with argument *arg*
123/// @param pool The Thead Pool to be operated on.
124/// @param func The function that would be called once a "worker" picks up the "work".
125/// @param arg argument to be given to the *func* upon call.
126extern void snf_thpool_addwork(SNF_thpool *pool, void *(*func)(), void *arg);
127
128/// @brief Blocks current thread until the Thread Pool's last changed status has taken effect
129/// @param pool The Thead Pool to be operated on.
130extern void snf_thpool_wait(SNF_thpool *pool);
131
132/// @brief Will Block till the Main_worker finishes
133/// @param pool The Thead Pool to be operated on.
134extern void snf_thpool_join(SNF_thpool *pool);
135
136/// @brief will order the Thread Pool to stop.
137/// @note you can block till the Thread Pool has stopped
138/// @param pool The Thead Pool to be operated on.
139extern void snf_thpool_stop(SNF_thpool *pool);
140extern void snf_thpool_run(SNF_thpool *pool);
141/// @brief
142/// @param pool
143/// @private
144extern void snf_thpool_destroy(SNF_thpool **pool);
145#endif
Main Header FileThis File Calls for all the header files exisitng in this library,...
The structure for a Thread Pool.
Definition thpool.h:69
sem_t thpool_status_sem
Definition thpool.h:106
pthread_mutex_t thpool_workers_MUTEX
Used to synchronize the acces to thpool_workers.
Definition thpool.h:83
int thpool_n_workers
Limiter for the amount of threads that could be created.
Definition thpool.h:99
SNF_thpool_worker * thpool_workers
This LinkedList will store the currently working "workers" or more exactly the worker thread info.
Definition thpool.h:77
pthread_mutex_t thpool_status_MUTEX
Definition thpool.h:104
pthread_mutex_t thpool_noworks_MUTEX
Used to synchronize if all "works" are handled and all "workers" had finished their assigned "work" S...
Definition thpool.h:87
pthread_t * thpool_main_worker
The main function that is runs first when the Thread pool has started.
Definition thpool.h:91
pthread_mutex_t thpool_works_MUTEX
Used to synchronize the acces to thpool_works.
Definition thpool.h:80
sem_t thpool_workers_sem
Semaphore used for synchronization; Used primarly to notify the conclusion of "workers" to the Thread...
Definition thpool.h:96
SNF_thpool_status thpool_status
If set = 1 the thpool_handler will stop creating "workers" and wait till all already exising "workers...
Definition thpool.h:103
pthread_t * thpool_handler
Thread that manages "workers" and assign their "work" to them.
Definition thpool.h:89
pthread_cond_t thpool_status_COND
Definition thpool.h:105
sem_t thpool_works_sem
This is the semaphore for handling.
Definition thpool.h:73
SNF_thpool_work * thpool_works
This LinkedList will store the "works" or "jobs".
Definition thpool.h:71
Defines the 'work' needed to do by defining a 'func'tion and it's 'arg'ument.
Definition thpool.h:51
SNF_thpool * pool
the source Thread Pool
Definition thpool.h:57
SNF_thpool_work * next
Saves the next work ( Linked List )
Definition thpool.h:59
void * arg
the argument to be given upon execution
Definition thpool.h:55
Defines the 'worker'that would handle a certain 'work'.
Definition thpool.h:63
pthread_t worker
The worker's thread id.
Definition thpool.h:65
enum SNF_ThreadPool_Status_e SNF_thpool_status
Definition thpool.h:33
void snf_thpool_stop(SNF_thpool *pool)
will order the Thread Pool to stop.
void snf_thpool_wait(SNF_thpool *pool)
Blocks current thread until the Thread Pool's last changed status has taken effect.
time_t SNF_THPOOL_STOPWAIT
This defines the amount of seconds to wait a worker to stop, if the worker does not stop during this ...
int snf_thpool_inis(SNF_thpool **ThreadPool, int Max_Threads, void *(*Main_Worker)(void *), void *arg)
Insitanciates a new thread pool, with a Limiter ( Max_Threads )
void snf_thpool_run(SNF_thpool *pool)
void snf_thpool_addwork(SNF_thpool *pool, void *(*func)(), void *arg)
Creates a "work" that will call the func function with argument arg
SNF_ThreadPool_Status_e
Definition thpool.h:36
@ SNF_THPOOL_RUN
Used to Signify that the Thread Pool is running or been ordred to.
Definition thpool.h:43
@ SNF_THPOOL_DESTROY
Used to Signify that the Thread Pool is being ordred to be destroyied.
Definition thpool.h:46
@ SNF_THPOOL_STOP
Used to Signify that the Thread Pool was stopped or been ordred to.
Definition thpool.h:40
void snf_thpool_join(SNF_thpool *pool)
Will Block till the Main_worker finishes.