Shadow Network Framework  0.0.1-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/// //////////////////////////////////////////////////////////
14#ifndef thpool_h
15#define thpool_h
16
17#include <SNF/SNF.h>
18
21typedef struct SNF_ThreadPool_t thpool;
22
23/// @brief Defines the 'work' needed to do by defining a 'func'tion and it's 'arg'ument
25{
26 /// @brief The function to be executed upon
27 void *(*func)();
28 /// @brief the argument to be given upon execution
29 void *arg;
30 /// @brief the source Thread Pool
32 /// @brief The thread pool's handling worker
34 /// @brief Saves the next work ( Linked List )
36};
37/// @brief Defines the 'worker'that would handle a certain 'work'
39{
40 /// @brief The worker's thread id
41 pthread_t *worker;
42 /// @brief Pointer to workers' Linked list
44};
45/// @brief The structure for a Thread Pool
47{
48 /// @brief This LinkedList will store the "works" or "jobs"
50 /// @brief Defines current amount of currently awaiting "works"
51 _Atomic int thpool_n_works;
52 /// @brief This LinkedList will store the currently working "workers"
53 /// or more exactly the worker thread info
54 /// @note Linked List saves \ref SNF_ThreadPool_work_t . See \ref SNF_ThreadPool_worker_t .
56 /// @brief Used to synchronize the acces to thpool_works
57 /// @warning Must not be touched or the Thread Pool may not work as intended
58 pthread_mutex_t thpool_works_MUTEX;
59 /// @brief Used to synchronize the acces to thpool_workers
60 /// @warning Must not be touched or the Thread Pool may not work as intended
61 pthread_mutex_t thpool_workers_MUTEX;
62 /// @brief Used to synchronize if all "works" are handled and all "workers" had finished their assigned "work"
63 /// See \ref snf_thpool_wait(thpool *pool)
64 /// @note Use \ref snf_thpool_wait(thpool *pool) .
65 pthread_mutex_t thpool_noworks_MUTEX;
66 /// @brief Used to synchronize if all "works" are handled and all "workers" had finished their assigned "work"
67 /// See \ref snf_thpool_wait(thpool *pool)
68 /// @note Use \ref snf_thpool_wait(thpool *pool) .
69 pthread_cond_t thpool_noworks_cond;
70 /// @brief Used to synchronize the acces to thpool_works
71 /// @warning Must not be touched or the Thread Pool may not work as intended
72 pthread_cond_t thpool_works_cond;
73 /// @brief Thread that manages "workers" and assign their "work" to them
74 pthread_t *thpool_handler;
75 /// @brief The main function that is runs first when the Thread pool has started
77 /// @brief Semaphore used for synchronization;
78 /// Used primarly to notify the conclusion of "workers" to the Thread pool handler,
79 /// and to Limit the amount of "worker" threads with the max being max_workers_count
80 /// @warning Must not be touched or the Thread Pool may not work as intended
82 /// @brief Limiter for the amount of threads that could be created.
83 /// @warning Must not be touched or the Thread Pool may not work as intended
85 /// @brief If set = 1 the thpool_handler will stop creating "workers"
86 /// and wait till all already exising "workers" finish their "work"
87 /// @note Use \ref snf_thpool_stop(thpool *pool) .
88 _Atomic int stop;
89};
90
91/// @brief Insitanciates a new thread pool, with a Limiter ( Max_Threads )
92/// @param ThreadPool Returns the thread pool by writes the newly created instance in the pointer given in this parameter
93/// @param Max_Threads Maximum Total allowed threads ( Must be 2+ if No Main Worker given, else must me 3+ for proper work)
94/// @param Main_Worker Main Function that would be called upon finishing the intialisation of the
95/// @param arg the argument that would be given to the Main_worker function.
96/// @return 0 On Success || -1 On fail { Shall fail only if 1 - ThreadPool is NULL || 2 - Max_Thread is below recommended/required value }
97extern int snf_thpool_inis(thpool **ThreadPool, int Max_Threads, void *(*Main_Worker)(), void *arg);
98
99/// @brief Creates a "work" that will call the *func* function with argument *arg*
100/// @param pool The Thead Pool to be operated on.
101/// @param func The function that would be called once a "worker" picks up the "work".
102/// @param arg argument to be given to the *func* upon call.
103extern void snf_thpool_addwork(thpool *pool, void *(*func)(), void *arg);
104
105/// @brief Blocks current thread until there is No "Worker" is working and no "work" is waiting in queue
106/// @param pool The Thead Pool to be operated on.
107extern void snf_thpool_wait(thpool *pool);
108
109/// @brief Will Block till the Main_worker finishes
110/// @param pool The Thead Pool to be operated on.
111extern void snf_thpool_join(thpool *pool);
112
113/// @brief will stop creating "workers" and wait till all already exising "workers" finish their "work"
114/// @param pool The Thead Pool to be operated on.
115extern void snf_thpool_stop(thpool *pool);
116#endif
This file includes necessary Checks and includes the the nesessary libraries of the library.
The structure for a Thread Pool.
Definition thpool.h:47
pthread_cond_t thpool_works_cond
Used to synchronize the acces to thpool_works.
Definition thpool.h:72
pthread_mutex_t thpool_workers_MUTEX
Used to synchronize the acces to thpool_workers.
Definition thpool.h:61
_Atomic int stop
If set = 1 the thpool_handler will stop creating "workers" and wait till all already exising "workers...
Definition thpool.h:88
SNF_thpool_worker * thpool_workers
This LinkedList will store the currently working "workers" or more exactly the worker thread info.
Definition thpool.h:55
int max_workers_count
Limiter for the amount of threads that could be created.
Definition thpool.h:84
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:65
pthread_t * thpool_main_worker
The main function that is runs first when the Thread pool has started.
Definition thpool.h:76
pthread_mutex_t thpool_works_MUTEX
Used to synchronize the acces to thpool_works.
Definition thpool.h:58
_Atomic int thpool_n_works
Defines current amount of currently awaiting "works".
Definition thpool.h:51
sem_t thpool_workers_sem
Semaphore used for synchronization; Used primarly to notify the conclusion of "workers" to the Thread...
Definition thpool.h:81
pthread_t * thpool_handler
Thread that manages "workers" and assign their "work" to them.
Definition thpool.h:74
pthread_cond_t thpool_noworks_cond
Used to synchronize if all "works" are handled and all "workers" had finished their assigned "work" S...
Definition thpool.h:69
SNF_thpool_work * thpool_works
This LinkedList will store the "works" or "jobs".
Definition thpool.h:49
Defines the 'work' needed to do by defining a 'func'tion and it's 'arg'ument.
Definition thpool.h:25
SNF_thpool_work * next
Saves the next work ( Linked List )
Definition thpool.h:35
SNF_thpool_worker * worker
The thread pool's handling worker.
Definition thpool.h:33
void * arg
the argument to be given upon execution
Definition thpool.h:29
thpool * pool
the source Thread Pool
Definition thpool.h:31
Defines the 'worker'that would handle a certain 'work'.
Definition thpool.h:39
pthread_t * worker
The worker's thread id.
Definition thpool.h:41
SNF_thpool_worker * next
Pointer to workers' Linked list.
Definition thpool.h:43
void snf_thpool_stop(thpool *pool)
will stop creating "workers" and wait till all already exising "workers" finish their "work"
void snf_thpool_join(thpool *pool)
Will Block till the Main_worker finishes.
void snf_thpool_wait(thpool *pool)
Blocks current thread until there is No "Worker" is working and no "work" is waiting in queue.
int snf_thpool_inis(thpool **ThreadPool, int Max_Threads, void *(*Main_Worker)(), void *arg)
Insitanciates a new thread pool, with a Limiter ( Max_Threads )
void snf_thpool_addwork(thpool *pool, void *(*func)(), void *arg)
Creates a "work" that will call the func function with argument arg