This is Unofficial EPICS BASE Doxygen Site
epicsThread.h
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * Copyright (c) 2013 ITER Organization.
7 * EPICS BASE is distributed subject to a Software License Agreement found
8 * in file LICENSE that is included with this distribution.
9 \*************************************************************************/
10 
55 #ifndef epicsThreadh
56 #define epicsThreadh
57 
58 #include <stddef.h>
59 
60 #include "libComAPI.h"
61 
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65 
66 typedef void (*EPICSTHREADFUNC)(void *parm);
67 
71 #define epicsThreadPriorityMax 99
72 #define epicsThreadPriorityMin 0
73 
74 /* some generic values */
75 #define epicsThreadPriorityLow 10
76 #define epicsThreadPriorityMedium 50
77 #define epicsThreadPriorityHigh 90
78 
79 /* some iocCore specific values */
80 #define epicsThreadPriorityCAServerLow 20
81 #define epicsThreadPriorityCAServerHigh 40
82 #define epicsThreadPriorityScanLow 60
83 #define epicsThreadPriorityScanHigh 70
84 #define epicsThreadPriorityIocsh 91
85 #define epicsThreadPriorityBaseMax 91
86 
89 typedef enum {
92 
93 typedef enum {
96 
102 LIBCOM_API unsigned int epicsStdCall epicsThreadGetStackSize(
103  epicsThreadStackSizeClass size);
104 
107 
108 typedef epicsThreadId epicsThreadOnceId;
109 #define EPICS_THREAD_ONCE_INIT 0
110 
129 LIBCOM_API void epicsStdCall epicsThreadOnce(
130  epicsThreadOnceId *id, EPICSTHREADFUNC, void *arg);
131 
137 LIBCOM_API void epicsThreadRealtimeLock(void);
138 
145 LIBCOM_API void epicsStdCall epicsThreadExitMain(void);
146 
148 typedef struct epicsThreadOpts {
150  unsigned int priority;
154  unsigned int stackSize;
158  unsigned int joinable;
160 
167 #define EPICS_THREAD_OPTS_INIT { \
168  epicsThreadPriorityLow, epicsThreadStackMedium, 0}
169 
177 LIBCOM_API epicsThreadId epicsThreadCreateOpt (
178  const char * name,
179  EPICSTHREADFUNC funptr, void * parm,
180  const epicsThreadOpts *opts );
182 LIBCOM_API epicsThreadId epicsStdCall epicsThreadCreate (
183  const char * name, unsigned int priority, unsigned int stackSize,
184  EPICSTHREADFUNC funptr,void * parm );
188 LIBCOM_API epicsThreadId epicsStdCall epicsThreadMustCreate (
189  const char * name, unsigned int priority, unsigned int stackSize,
190  EPICSTHREADFUNC funptr,void * parm );
191 
192 /* This gets undefined in osdThread.h on VxWorks < 6.9 */
193 #define EPICS_THREAD_CAN_JOIN
194 
195 LIBCOM_API void epicsThreadMustJoin(epicsThreadId id);
197 LIBCOM_API void epicsStdCall epicsThreadSuspendSelf(void);
199 LIBCOM_API void epicsStdCall epicsThreadResume(epicsThreadId id);
201 LIBCOM_API unsigned int epicsStdCall epicsThreadGetPriority(
202  epicsThreadId id);
204 LIBCOM_API unsigned int epicsStdCall epicsThreadGetPrioritySelf(void);
206 LIBCOM_API void epicsStdCall epicsThreadSetPriority(
207  epicsThreadId id,unsigned int priority);
211 LIBCOM_API epicsThreadBooleanStatus epicsStdCall
213  unsigned int priority, unsigned *pPriorityJustBelow);
217 LIBCOM_API epicsThreadBooleanStatus epicsStdCall
219  unsigned int priority, unsigned *pPriorityJustAbove);
221 LIBCOM_API int epicsStdCall epicsThreadIsEqual(
222  epicsThreadId id1, epicsThreadId id2);
228 LIBCOM_API int epicsStdCall epicsThreadIsSuspended(epicsThreadId id);
232 LIBCOM_API void epicsStdCall epicsThreadSleep(double seconds);
239 LIBCOM_API double epicsStdCall epicsThreadSleepQuantum(void);
243 LIBCOM_API epicsThreadId epicsStdCall epicsThreadGetIdSelf(void);
252 LIBCOM_API epicsThreadId epicsStdCall epicsThreadGetId(const char *name);
257 LIBCOM_API int epicsThreadGetCPUs(void);
258 
266 LIBCOM_API const char * epicsStdCall epicsThreadGetNameSelf(void);
267 
274 LIBCOM_API void epicsStdCall epicsThreadGetName(
275  epicsThreadId id, char *name, size_t size);
276 
283 LIBCOM_API int epicsStdCall epicsThreadIsOkToBlock(void);
289 LIBCOM_API void epicsStdCall epicsThreadSetOkToBlock(int isOkToBlock);
290 
294 LIBCOM_API void epicsStdCall epicsThreadShowAll(unsigned int level);
296 LIBCOM_API void epicsStdCall epicsThreadShow(
297  epicsThreadId id,unsigned int level);
298 
302 typedef void (*EPICS_THREAD_HOOK_ROUTINE)(epicsThreadId id);
303 
310 LIBCOM_API int epicsThreadHookAdd(EPICS_THREAD_HOOK_ROUTINE hook);
311 
316 
320 LIBCOM_API void epicsThreadHooksShow(void);
321 
325 LIBCOM_API void epicsThreadMap(EPICS_THREAD_HOOK_ROUTINE func);
326 
332 LIBCOM_API epicsThreadPrivateId epicsStdCall epicsThreadPrivateCreate(void);
334 LIBCOM_API void epicsStdCall epicsThreadPrivateDelete(epicsThreadPrivateId id);
336 LIBCOM_API void epicsStdCall epicsThreadPrivateSet(epicsThreadPrivateId,void *);
338 LIBCOM_API void * epicsStdCall epicsThreadPrivateGet(epicsThreadPrivateId);
339 
340 #ifdef __cplusplus
341 }
342 #endif
343 
344 #ifdef __cplusplus
345 
346 #include "epicsEvent.h"
347 #include "epicsMutex.h"
348 
350 class LIBCOM_API epicsThreadRunable {
351 public:
352  virtual ~epicsThreadRunable () = 0;
356  virtual void run () = 0;
358  virtual void show ( unsigned int level ) const;
359 };
360 
361 extern "C" void epicsThreadCallEntryPoint ( void * );
362 
369 class LIBCOM_API epicsThread {
370 public:
377  epicsThread ( epicsThreadRunable &,const char *name, unsigned int stackSize,
378  unsigned int priority=epicsThreadPriorityLow );
379  ~epicsThread () throw ();
381  void start () throw ();
383  void exitWait () throw ();
387  bool exitWait ( const double delay ) throw ();
390  static void exit ();
392  void resume () throw ();
394  void getName ( char * name, size_t size ) const throw ();
396  epicsThreadId getId () const throw ();
398  unsigned int getPriority () const throw ();
400  void setPriority ( unsigned int ) throw ();
401  bool priorityIsEqual ( const epicsThread & ) const throw ();
402  bool isSuspended () const throw ();
404  bool isCurrentThread () const throw ();
405  bool operator == ( const epicsThread & ) const throw ();
407  void show ( unsigned level ) const throw ();
408 
409  /* these operate on the current thread */
410  static void suspendSelf () throw ();
411  static void sleep (double seconds) throw ();
412  static const char * getNameSelf () throw ();
413  static bool isOkToBlock () throw ();
414  static void setOkToBlock ( bool isOkToBlock ) throw ();
415 
416  /* exceptions */
417  class unableToCreateThread;
418 private:
419  epicsThreadRunable & runable;
420  epicsThreadId id;
421  epicsMutex mutex;
422  epicsEvent event;
423  epicsEvent exitEvent;
424  bool * pThreadDestroyed;
425  bool begin;
426  bool cancel;
427  bool terminated;
428  bool joined;
429 
430  bool beginWait () throw ();
431  epicsThread ( const epicsThread & );
432  epicsThread & operator = ( const epicsThread & );
433  friend void epicsThreadCallEntryPoint ( void * );
434  void printLastChanceExceptionMessage (
435  const char * pExceptionTypeName,
436  const char * pExceptionContext );
437  /* exceptions */
438  class exitException {};
439 };
440 
441 class LIBCOM_API epicsThreadPrivateBase {
442 public:
443  class unableToCreateThreadPrivate {}; /* exception */
444 protected:
445  static void throwUnableToCreateThreadPrivate ();
446 };
447 
448 template < class T >
449 class epicsThreadPrivate :
450  private epicsThreadPrivateBase {
451 public:
452  epicsThreadPrivate ();
453  ~epicsThreadPrivate () throw ();
454  T * get () const throw ();
455  void set (T *) throw ();
456 private:
458 };
459 
460 #endif /* __cplusplus */
461 
462 #include "osdThread.h"
463 
464 #ifdef __cplusplus
465 
466 template <class T>
467 inline epicsThreadPrivate<T>::epicsThreadPrivate ()
468 {
469  this->id = epicsThreadPrivateCreate ();
470  if ( this->id == 0 ) {
471  epicsThreadPrivateBase::throwUnableToCreateThreadPrivate ();
472  }
473 }
474 
475 template <class T>
476 inline epicsThreadPrivate<T>::~epicsThreadPrivate () throw ()
477 {
478  epicsThreadPrivateDelete ( this->id );
479 }
480 
481 template <class T>
482 inline T *epicsThreadPrivate<T>::get () const throw ()
483 {
484  return static_cast<T *> ( epicsThreadPrivateGet (this->id) );
485 }
486 
487 template <class T>
488 inline void epicsThreadPrivate<T>::set (T *pIn) throw ()
489 {
490  epicsThreadPrivateSet ( this->id, static_cast<void *> (pIn) );
491 }
492 
493 #endif /* ifdef __cplusplus */
494 
495 #endif /* epicsThreadh */
epicsThreadBooleanStatus
Definition: epicsThread.h:93
LIBCOM_API unsigned int epicsStdCall epicsThreadGetPriority(epicsThreadId id)
Definition: osdThread.c:701
LIBCOM_API void epicsThreadRealtimeLock(void)
Definition: osdThread.c:425
LIBCOM_API void *epicsStdCall epicsThreadPrivateGet(epicsThreadPrivateId)
Definition: osdThread.c:973
LIBCOM_API void epicsStdCall epicsThreadPrivateDelete(epicsThreadPrivateId id)
Definition: osdThread.c:950
LIBCOM_API double epicsStdCall epicsThreadSleepQuantum(void)
Query a value approximating the OS timer/scheduler resolution.
Definition: osdThread.c:981
LIBCOM_API const char *epicsStdCall epicsThreadGetNameSelf(void)
Definition: osdThread.c:846
void(* EPICS_THREAD_HOOK_ROUTINE)(epicsThreadId id)
Definition: epicsThread.h:302
epicsThreadId epicsThreadOnceId
Definition: epicsThread.h:108
struct epicsThreadOSD * epicsThreadId
Definition: epicsThread.h:106
LIBCOM_API void epicsStdCall epicsThreadShowAll(unsigned int level)
Definition: osdThread.c:883
LIBCOM_API void epicsStdCall epicsThreadShow(epicsThreadId id, unsigned int level)
Definition: osdThread.c:903
LIBCOM_API int epicsStdCall epicsThreadIsEqual(epicsThreadId id1, epicsThreadId id2)
Definition: osdThread.c:776
unsigned int joinable
Definition: epicsThread.h:158
epicsTime begin
Definition: caConnTest.cpp:22
LIBCOM_API unsigned int epicsStdCall epicsThreadGetStackSize(epicsThreadStackSizeClass size)
Definition: osdThread.c:466
LIBCOM_API int epicsStdCall epicsThreadIsOkToBlock(void)
LIBCOM_API int epicsThreadGetCPUs(void)
Definition: osdThread.c:990
#define static
Definition: reader.c:44
LIBCOM_API epicsThreadId epicsStdCall epicsThreadMustCreate(const char *name, unsigned int priority, unsigned int stackSize, EPICSTHREADFUNC funptr, void *parm)
LIBCOM_API void epicsStdCall epicsThreadSuspendSelf(void)
Definition: osdThread.c:664
epicsThreadStackSizeClass
Definition: epicsThread.h:89
LIBCOM_API epicsThreadBooleanStatus epicsStdCall epicsThreadLowestPriorityLevelAbove(unsigned int priority, unsigned *pPriorityJustAbove)
Definition: osdThread.c:757
struct epicsThreadPrivateOSD * epicsThreadPrivateId
Definition: epicsThread.h:328
APIs for the epicsMutex mutual exclusion semaphore.
LIBCOM_API void epicsStdCall epicsThreadOnce(epicsThreadOnceId *id, EPICSTHREADFUNC, void *arg)
unsigned int stackSize
Definition: epicsThread.h:154
LIBCOM_API void epicsStdCall epicsThreadPrivateSet(epicsThreadPrivateId, void *)
Definition: osdThread.c:961
LIBCOM_API epicsThreadId epicsStdCall epicsThreadCreate(const char *name, unsigned int priority, unsigned int stackSize, EPICSTHREADFUNC funptr, void *parm)
Definition: epicsThread.cpp:33
#define epicsThreadPriorityLow
Definition: epicsThread.h:75
LIBCOM_API void epicsStdCall epicsThreadResume(epicsThreadId id)
Definition: osdThread.c:423
LIBCOM_API void epicsStdCall epicsThreadExitMain(void)
Definition: osdThread.c:683
LIBCOM_API void epicsStdCall epicsThreadGetName(epicsThreadId id, char *name, size_t size)
Definition: osdThread.c:857
APIs for the epicsEvent binary semaphore.
LIBCOM_API epicsThreadBooleanStatus epicsStdCall epicsThreadHighestPriorityLevelBelow(unsigned int priority, unsigned *pPriorityJustBelow)
Definition: osdThread.c:740
LIBCOM_API unsigned int epicsStdCall epicsThreadGetPrioritySelf(void)
Definition: osdThread.c:707
struct epicsThreadOpts epicsThreadOpts
LIBCOM_API int epicsThreadHookAdd(EPICS_THREAD_HOOK_ROUTINE hook)
LIBCOM_API int epicsThreadHookDelete(EPICS_THREAD_HOOK_ROUTINE hook)
unsigned int priority
Definition: epicsThread.h:150
LIBCOM_API void epicsStdCall epicsThreadSleep(double seconds)
Block the calling thread for at least the specified time.
Definition: osdThread.c:790
Definition: caget.c:48
LIBCOM_API void epicsThreadMustJoin(epicsThreadId id)
Definition: osdThread.c:632
LIBCOM_API epicsThreadId epicsThreadCreateOpt(const char *name, EPICSTHREADFUNC funptr, void *parm, const epicsThreadOpts *opts)
Allocate and start a new OS thread.
Definition: osdThread.c:529
LIBCOM_API int epicsStdCall epicsThreadIsSuspended(epicsThreadId id)
Definition: osdThread.c:784
void epicsThreadCallEntryPoint(void *pPvt)
Definition: epicsThread.cpp:86
LIBCOM_API void epicsThreadMap(EPICS_THREAD_HOOK_ROUTINE func)
Definition: osdThread.c:864
LIBCOM_API epicsThreadId epicsStdCall epicsThreadGetId(const char *name)
Definition: osdThread.c:826
LIBCOM_API epicsThreadPrivateId epicsStdCall epicsThreadPrivateCreate(void)
Definition: osdThread.c:934
void(* EPICSTHREADFUNC)(void *parm)
Definition: epicsThread.h:66
LIBCOM_API void epicsThreadHooksShow(void)
LIBCOM_API void epicsStdCall epicsThreadSetPriority(epicsThreadId id, unsigned int priority)
Definition: osdThread.c:713
LIBCOM_API epicsThreadId epicsStdCall epicsThreadGetIdSelf(void)
Definition: osdThread.c:810
LIBCOM_API void epicsStdCall epicsThreadSetOkToBlock(int isOkToBlock)