2011-11-05 16:35:31 +04:00
|
|
|
/**********************************
|
|
|
|
* @author Johan Hanssen Seferidis
|
2014-12-29 14:51:52 +03:00
|
|
|
* License: MIT
|
2011-11-05 16:35:31 +04:00
|
|
|
*
|
|
|
|
**********************************/
|
|
|
|
|
|
|
|
#ifndef _THPOOL_
|
|
|
|
#define _THPOOL_
|
|
|
|
|
|
|
|
|
2015-01-16 21:10:25 +03:00
|
|
|
/* thpool is a pointer to a thpool data structure */
|
2015-01-17 14:55:30 +03:00
|
|
|
typedef struct thpool_* threadpool;
|
2014-12-30 15:56:19 +03:00
|
|
|
|
|
|
|
|
2014-12-29 14:51:52 +03:00
|
|
|
/* =========================== FUNCTIONS ============================ */
|
2011-11-05 16:35:31 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initialize threadpool
|
|
|
|
*
|
2015-01-03 15:16:18 +03:00
|
|
|
* Initializes a threadpool. This function will not return untill all
|
2015-01-15 12:12:26 +03:00
|
|
|
* threads have initialized successfully.
|
2011-11-05 16:35:31 +04:00
|
|
|
*
|
2015-01-17 23:18:17 +03:00
|
|
|
* @example
|
|
|
|
* threadpool thpool; //First we declare a threadpool
|
|
|
|
* thpool = thpool_init(4); //then we initialize it to 4 threads
|
|
|
|
*
|
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
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2015-01-03 15:16:18 +03:00
|
|
|
* NOTICE: You have to cast both the function and argument to not get warnings.
|
2011-11-05 16:35:31 +04:00
|
|
|
*
|
2015-01-17 23:18:17 +03:00
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* void print_num(int num){
|
|
|
|
* printf("%d\n", num);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* int main() {
|
|
|
|
* threadpool thpool = thpool_init(4);
|
|
|
|
* int a = 10;
|
|
|
|
* thpool_add_work(thpool, (void*)print_num, (void*)a);
|
|
|
|
* thpool_wait(thpool);
|
|
|
|
* return 0;
|
|
|
|
* }
|
|
|
|
*
|
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-01-16 22:44:52 +03:00
|
|
|
* @return nothing
|
2011-11-05 16:35:31 +04:00
|
|
|
*/
|
2015-01-17 14:55:30 +03: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
|
2014-12-29 19:42:27 +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.
|
2014-12-29 19:42:27 +03:00
|
|
|
*
|
2015-01-16 22:44:52 +03:00
|
|
|
* Polling is used in wait. By default the polling interval is one second.
|
|
|
|
*
|
2015-01-17 23:18:17 +03:00
|
|
|
* @example
|
|
|
|
* int main() {
|
|
|
|
* threadpool thpool1 = thpool_init(4);
|
|
|
|
* ..
|
|
|
|
* // Add a bunch of work
|
|
|
|
* ..
|
|
|
|
* thpool_wait(thpool);
|
|
|
|
* puts("All added work has finished");
|
|
|
|
* return 0;
|
|
|
|
* }
|
|
|
|
*
|
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
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* While the thread is being paused, new work can be added.
|
|
|
|
*
|
2015-01-17 23:18:17 +03:00
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* threadpool thpool = thpool_init(4);
|
|
|
|
* thpool_pause(thpool);
|
|
|
|
* ..
|
|
|
|
* // Add a bunch of work
|
|
|
|
* ..
|
|
|
|
* thpool_resume(thpool); // Let the threads start their magic
|
|
|
|
*
|
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
|
|
|
|
*
|
2015-01-17 23:18:17 +03:00
|
|
|
* @example
|
|
|
|
* ..
|
|
|
|
* thpool_pause(thpool);
|
|
|
|
* sleep(10); // Delay execution 10 seconds
|
|
|
|
* thpool_resume(thpool);
|
|
|
|
* ..
|
|
|
|
*
|
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
|
|
|
|
*
|
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.
|
2011-11-05 16:35:31 +04: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;
|
|
|
|
* }
|
|
|
|
*
|
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
|
|
|
|
|
|
|
|
2014-12-29 19:21:01 +03:00
|
|
|
|
|
|
|
|
2011-11-05 16:35:31 +04:00
|
|
|
#endif
|