C-Thread-Pool/thpool.h

188 lines
4.5 KiB
C
Raw Normal View History

2017-01-17 22:59:29 +03:00
/**********************************
2011-11-05 16:35:31 +04:00
* @author Johan Hanssen Seferidis
2014-12-29 14:51:52 +03:00
* License: MIT
2017-01-17 22:59:29 +03:00
*
2011-11-05 16:35:31 +04:00
**********************************/
#ifndef _THPOOL_
#define _THPOOL_
#ifdef __cplusplus
extern "C" {
#endif
2014-12-30 15:56:19 +03:00
2015-01-17 23:23:11 +03:00
/* =================================== API ======================================= */
2011-11-05 16:35:31 +04:00
2015-01-18 17:09:09 +03:00
typedef struct thpool_* threadpool;
2011-11-05 16:35:31 +04:00
/**
* @brief Initialize threadpool
2017-01-17 22:59:29 +03:00
*
2019-12-16 22:52:37 +03:00
* Initializes a threadpool. This function will not return until all
2015-01-15 12:12:26 +03:00
* threads have initialized successfully.
2017-01-17 22:59:29 +03:00
*
2015-01-17 23:18:17 +03:00
* @example
2017-01-17 22:59:29 +03:00
*
2015-01-17 23:21:31 +03:00
* ..
2015-01-17 23:23:11 +03:00
* threadpool thpool; //First we declare a threadpool
* thpool = thpool_init(4); //then we initialize it to 4 threads
2015-01-17 23:21:31 +03:00
* ..
2017-01-17 22:59:29 +03:00
*
2015-01-16 21:10:25 +03:00
* @param num_threads number of threads to be created in the threadpool
2015-01-17 14:55:30 +03:00
* @return threadpool created threadpool on success,
* NULL on error
2011-11-05 16:35:31 +04:00
*/
2015-01-17 14:55:30 +03:00
threadpool thpool_init(int num_threads);
2011-11-05 16:35:31 +04:00
/**
* @brief Add work to the job queue
2017-01-17 22:59:29 +03:00
*
2011-11-05 16:35:31 +04:00
* Takes an action and its argument and adds it to the threadpool's job queue.
* If you want to add to work a function with more than one arguments then
* a way to implement this is by passing a pointer to a structure.
2017-01-17 22:59:29 +03:00
*
2015-01-03 15:16:18 +03:00
* NOTICE: You have to cast both the function and argument to not get warnings.
2017-01-17 22:59:29 +03:00
*
2015-01-17 23:18:17 +03:00
* @example
2017-01-17 22:59:29 +03:00
*
2015-01-17 23:21:31 +03:00
* void print_num(int num){
* printf("%d\n", num);
* }
2017-01-17 22:59:29 +03:00
*
2015-01-17 23:21:31 +03:00
* int main() {
* ..
* int a = 10;
* thpool_add_work(thpool, (void*)print_num, (void*)a);
* ..
* }
2017-01-17 22:59:29 +03:00
*
2015-01-17 14:55:30 +03:00
* @param threadpool threadpool to which the work will be added
* @param function_p pointer to function to add as work
* @param arg_p pointer to an argument
2015-12-23 04:46:30 +03:00
* @return 0 on successs, -1 otherwise.
2011-11-05 16:35:31 +04:00
*/
int thpool_add_work(threadpool, void (*function_p)(void*), void* arg_p);
2011-11-05 16:35:31 +04:00
2014-12-29 19:42:27 +03:00
/**
2015-01-16 22:44:52 +03:00
* @brief Wait for all queued jobs to finish
2017-01-17 22:59:29 +03:00
*
2015-01-16 22:44:52 +03:00
* Will wait for all jobs - both queued and currently running to finish.
* Once the queue is empty and all work has completed, the calling thread
* (probably the main program) will continue.
2017-01-17 22:59:29 +03:00
*
2015-01-18 15:42:37 +03:00
* Smart polling is used in wait. The polling is initially 0 - meaning that
* there is virtually no polling at all. If after 1 seconds the threads
2017-01-17 22:59:29 +03:00
* haven't finished, the polling interval starts growing exponentially
2019-12-16 22:52:37 +03:00
* until it reaches max_secs seconds. Then it jumps down to a maximum polling
2015-01-18 15:42:37 +03:00
* interval assuming that heavy processing is being used in the threadpool.
2015-01-16 22:44:52 +03:00
*
2015-01-17 23:18:17 +03:00
* @example
2017-01-17 22:59:29 +03:00
*
2015-01-17 23:21:31 +03:00
* ..
* threadpool thpool = thpool_init(4);
2015-01-17 23:18:17 +03:00
* ..
* // Add a bunch of work
* ..
* thpool_wait(thpool);
* puts("All added work has finished");
2015-01-17 23:21:31 +03:00
* ..
2017-01-17 22:59:29 +03:00
*
2015-01-17 14:55:30 +03:00
* @param threadpool the threadpool to wait for
2014-12-30 15:52:14 +03:00
* @return nothing
2014-12-29 19:42:27 +03:00
*/
2015-01-17 14:55:30 +03:00
void thpool_wait(threadpool);
2014-12-29 19:42:27 +03:00
2015-01-02 22:23:22 +03:00
/**
* @brief Pauses all threads immediately
2017-01-17 22:59:29 +03:00
*
2015-01-02 22:23:22 +03:00
* The threads will be paused no matter if they are idle or working.
2015-01-03 15:16:18 +03:00
* The threads return to their previous states once thpool_resume
2015-01-02 22:23:22 +03:00
* is called.
2017-01-17 22:59:29 +03:00
*
2015-01-02 22:23:22 +03:00
* While the thread is being paused, new work can be added.
2017-01-17 22:59:29 +03:00
*
2015-01-17 23:18:17 +03:00
* @example
2017-01-17 22:59:29 +03:00
*
2015-01-17 23:18:17 +03:00
* threadpool thpool = thpool_init(4);
* thpool_pause(thpool);
* ..
* // Add a bunch of work
* ..
* thpool_resume(thpool); // Let the threads start their magic
2017-01-17 22:59:29 +03:00
*
2015-01-17 14:55:30 +03:00
* @param threadpool the threadpool where the threads should be paused
2015-01-02 22:23:22 +03:00
* @return nothing
*/
2015-01-17 14:55:30 +03:00
void thpool_pause(threadpool);
2015-01-02 22:23:22 +03:00
/**
* @brief Unpauses all threads if they are paused
2017-01-17 22:59:29 +03:00
*
2015-01-17 23:18:17 +03:00
* @example
* ..
* thpool_pause(thpool);
* sleep(10); // Delay execution 10 seconds
* thpool_resume(thpool);
* ..
2017-01-17 22:59:29 +03:00
*
2015-01-17 14:55:30 +03:00
* @param threadpool the threadpool where the threads should be unpaused
2015-01-02 22:23:22 +03:00
* @return nothing
*/
2015-01-17 14:55:30 +03:00
void thpool_resume(threadpool);
2015-01-02 22:23:22 +03:00
2011-11-05 16:35:31 +04:00
/**
* @brief Destroy the threadpool
2017-01-17 22:59:29 +03:00
*
2015-01-15 12:12:26 +03:00
* This will wait for the currently active threads to finish and then 'kill'
* the whole threadpool to free up memory.
2017-01-17 22:59:29 +03:00
*
2015-01-17 23:18:17 +03:00
* @example
* int main() {
* threadpool thpool1 = thpool_init(2);
* threadpool thpool2 = thpool_init(2);
* ..
* thpool_destroy(thpool1);
* ..
* return 0;
* }
2017-01-17 22:59:29 +03:00
*
2015-01-17 14:55:30 +03:00
* @param threadpool the threadpool to destroy
2014-12-30 15:52:14 +03:00
* @return nothing
2011-11-05 16:35:31 +04:00
*/
2015-01-17 14:55:30 +03:00
void thpool_destroy(threadpool);
2015-01-02 22:23:22 +03:00
/**
* @brief Show currently working threads
*
* Working threads are the threads that are performing work (not idle).
*
* @example
* int main() {
* threadpool thpool1 = thpool_init(2);
* threadpool thpool2 = thpool_init(2);
* ..
* printf("Working threads: %d\n", thpool_num_threads_working(thpool1));
* ..
* return 0;
* }
*
* @param threadpool the threadpool of interest
* @return integer number of threads working
*/
int thpool_num_threads_working(threadpool);
#ifdef __cplusplus
}
#endif
2011-11-05 16:35:31 +04:00
#endif