C-Thread-Pool/thpool.h

107 lines
2.7 KiB
C
Raw Normal View History

2011-11-05 16:35:31 +04:00
/**********************************
* @author Johan Hanssen Seferidis
* @date 12/08/2011
2014-12-29 14:51:52 +03:00
* License: MIT
2011-11-05 16:35:31 +04:00
*
**********************************/
#ifndef _THPOOL_
#define _THPOOL_
#include <pthread.h>
#include <semaphore.h>
2015-01-16 21:10:25 +03:00
/* thpool is a pointer to a thpool data structure */
typedef struct thpool_* thpool;
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-16 21:10:25 +03:00
* @param num_threads number of threads to be created in the threadpool
* @return thpool pointer to created threadpool on success,
* pointer to NULL on error
2011-11-05 16:35:31 +04:00
*/
2015-01-16 21:10:25 +03:00
extern thpool 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-16 21:10:25 +03:00
* @param thpool threadpool to which the work will be added
* @param function function to add as work
* @param argument single argument to passed function
2015-01-16 22:44:52 +03:00
* @return nothing
2011-11-05 16:35:31 +04:00
*/
2015-01-15 12:12:26 +03:00
extern int thpool_add_work(thpool, void *(*function)(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.
*
2014-12-30 15:52:14 +03:00
* @param threadpool to wait for
* @return nothing
2014-12-29 19:42:27 +03:00
*/
2015-01-15 12:12:26 +03:00
extern void thpool_wait(thpool);
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.
*
* @param threadpool where the threads should be paused
* @return nothing
*/
2015-01-15 12:12:26 +03:00
extern void thpool_pause(thpool);
2015-01-02 22:23:22 +03:00
/**
* @brief Unpauses all threads if they are paused
*
2015-01-16 22:44:52 +03:00
* @param thpool the threadpool where the threads should be unpaused
2015-01-02 22:23:22 +03:00
* @return nothing
*/
2015-01-15 12:12:26 +03:00
extern void thpool_resume(thpool);
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-16 22:44:52 +03:00
* @param thpool the threadpool to destroy
2014-12-30 15:52:14 +03:00
* @return nothing
2011-11-05 16:35:31 +04:00
*/
2015-01-15 12:12:26 +03:00
extern void thpool_destroy(thpool);
2015-01-02 22:23:22 +03:00
2011-11-05 16:35:31 +04:00
#endif