This is Unofficial EPICS BASE Doxygen Site
epicsThreadPool.h
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2014 Brookhaven Science Associates, as Operator of
3 * Brookhaven National Laboratory.
4 * EPICS BASE is distributed subject to a Software License Agreement found
5 * in file LICENSE that is included with this distribution.
6 \*************************************************************************/
7 /* General purpose worker thread pool manager
8  * mdavidsaver@bnl.gov
9  */
10 #ifndef EPICSTHREADPOOL_H
11 #define EPICSTHREADPOOL_H
12 
13 #include <stdlib.h>
14 #include <stdio.h>
15 
16 #include "libComAPI.h"
17 #include "errMdef.h"
18 
19 #define S_pool_jobBusy (M_pool| 1) /*Job already queued or running*/
20 #define S_pool_jobIdle (M_pool| 2) /*Job was not queued or running*/
21 #define S_pool_noPool (M_pool| 3) /*Job not associated with a pool*/
22 #define S_pool_paused (M_pool| 4) /*Pool not currently accepting jobs*/
23 #define S_pool_noThreads (M_pool| 5) /*Can't create worker thread*/
24 #define S_pool_timeout (M_pool| 6) /*Pool still busy after timeout*/
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 typedef struct {
31  unsigned int initialThreads;
32  unsigned int maxThreads;
33  unsigned int workerStack;
34  unsigned int workerPriority;
36 
38 
39 /* Job function call modes */
40 typedef enum {
41  /* Normal run of job */
43 
44  /* Thread pool is being destroyed.
45  * A chance to cleanup the job immediately with epicsJobDestroy().
46  * If ignored, the job is orphaned (dissociated from the thread pool)
47  * and epicsJobDestroy() must be called later.
48  */
50 } epicsJobMode;
51 
52 typedef void (*epicsJobFunction)(void* arg, epicsJobMode mode);
53 
54 typedef struct epicsJob epicsJob;
55 
56 /* Pool operations */
57 
58 /* Initialize a pool config with default values.
59  * This much be done to preserve future compatibility
60  * when new options are added.
61  */
63 
64 /* fetch or create a thread pool which can be shared with other users.
65  * may return NULL for allocation failures
66  */
69 
70 /* If opts is NULL then defaults are used.
71  * The opts pointer is not stored by this call, and may exist on the stack.
72  */
74 
75 /* Blocks until all worker threads have stopped.
76  * Any jobs still attached to this pool receive a callback with EPICSJOB_CLEANUP
77  * and are then orphaned.
78  */
79 LIBCOM_API void epicsThreadPoolDestroy(epicsThreadPool *);
80 
81 /* pool control options */
82 typedef enum {
83  epicsThreadPoolQueueAdd, /* val==0 causes epicsJobQueue to fail, 1 is default */
84  epicsThreadPoolQueueRun /* val==0 prevents workers from running jobs, 1 is default */
86 
88  epicsThreadPoolOption opt,
89  unsigned int val);
90 
91 /* Block until job queue is emptied and no jobs are running.
92  * Useful after calling epicsThreadPoolControl() with option epicsThreadPoolQueueAdd=0
93  *
94  * timeout<0 waits forever, timeout==0 polls, timeout>0 waits at most one timeout period
95  * Returns 0 for success or non-zero on error (timeout is ETIMEOUT)
96  */
97 LIBCOM_API int epicsThreadPoolWait(epicsThreadPool* pool, double timeout);
98 
99 
100 /* Per job operations */
101 
102 /* Special flag for epicsJobCreate().
103  * When passed as the third argument "user"
104  * the argument passed to the job callback
105  * will be the epicsJob*
106  */
107 #define EPICSJOB_SELF epicsJobArgSelfMagic
108 LIBCOM_API extern void* epicsJobArgSelfMagic;
109 
110 /* Creates, but does not add, a new job.
111  * If pool is NULL then the job is not associated with any pool and
112  * epicsJobMove() must be called before epicsJobQueue().
113  * Safe to call from a running job function.
114  * Returns a new job pointer, or NULL on error.
115  */
117  epicsJobFunction cb,
118  void* user);
119 
120 /* Cancel and free a job structure. Does not block.
121  * Job may not be immediately free'd.
122  * Safe to call from a running job function.
123  */
124 LIBCOM_API void epicsJobDestroy(epicsJob*);
125 
126 /* Move the job to a different pool.
127  * If pool is NULL then the job will no longer be associated
128  * with any pool.
129  * Not thread safe. Job must not be running or queued.
130  * returns 0 on success, non-zero on error.
131  */
132 LIBCOM_API int epicsJobMove(epicsJob* job, epicsThreadPool* pool);
133 
134 /* Adds the job to the run queue
135  * Safe to call from a running job function.
136  * returns 0 for success, non-zero on error.
137  */
138 LIBCOM_API int epicsJobQueue(epicsJob*);
139 
140 /* Remove a job from the run queue if it is queued.
141  * Safe to call from a running job function.
142  * returns 0 if job was queued and now is not.
143  * 1 if job already ran, is running, or was not queued before,
144  * Other non-zero on error
145  */
146 LIBCOM_API int epicsJobUnqueue(epicsJob*);
147 
148 
149 /* Mostly useful for debugging */
150 
151 LIBCOM_API void epicsThreadPoolReport(epicsThreadPool *pool, FILE *fd);
152 
153 /* Current number of active workers. May be less than the maximum */
154 LIBCOM_API unsigned int epicsThreadPoolNThreads(epicsThreadPool *);
155 
156 #ifdef __cplusplus
157 }
158 #endif
159 
160 #endif // EPICSTHREADPOOL_H
double timeout
Definition: pvutils.cpp:25
LIBCOM_API void epicsJobDestroy(epicsJob *)
Definition: poolJob.c:155
epicsThreadPool * pool
Definition: poolPriv.h:89
LIBCOM_API epicsJob * epicsJobCreate(epicsThreadPool *pool, epicsJobFunction cb, void *user)
Definition: poolJob.c:134
LIBCOM_API epicsThreadPool * epicsThreadPoolCreate(epicsThreadPoolConfig *opts)
Definition: threadPool.c:37
LIBCOM_API void epicsThreadPoolDestroy(epicsThreadPool *)
Definition: threadPool.c:199
LIBCOM_API void epicsThreadPoolReport(epicsThreadPool *pool, FILE *fd)
Definition: threadPool.c:266
LIBCOM_API unsigned int epicsThreadPoolNThreads(epicsThreadPool *)
Definition: threadPool.c:302
unsigned int initialThreads
LIBCOM_API void * epicsJobArgSelfMagic
Definition: poolJob.c:24
LIBCOM_API int epicsJobUnqueue(epicsJob *)
Definition: poolJob.c:301
epicsJobMode
LIBCOM_API int epicsThreadPoolWait(epicsThreadPool *pool, double timeout)
Definition: threadPool.c:159
unsigned int workerStack
LIBCOM_API void epicsThreadPoolReleaseShared(epicsThreadPool *pool)
Definition: threadPool.c:385
unsigned int workerPriority
LIBCOM_API int epicsJobMove(epicsJob *job, epicsThreadPool *pool)
Definition: poolJob.c:182
epicsThreadPoolOption
LIBCOM_API int epicsJobQueue(epicsJob *)
Definition: poolJob.c:214
LIBCOM_API void epicsThreadPoolControl(epicsThreadPool *pool, epicsThreadPoolOption opt, unsigned int val)
Definition: threadPool.c:152
void(* epicsJobFunction)(void *arg, epicsJobMode mode)
LIBCOM_API void epicsThreadPoolConfigDefaults(epicsThreadPoolConfig *)
Definition: threadPool.c:26
LIBCOM_API epicsThreadPool * epicsThreadPoolGetShared(epicsThreadPoolConfig *opts)
Definition: threadPool.c:328