EPICS Base  7.0.5.1
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 * SPDX-License-Identifier: EPICS
8 * EPICS BASE is distributed subject to a Software License Agreement found
9 * in file LICENSE that is included with this distribution.
10 \*************************************************************************/
11 
56 #ifndef epicsThreadh
57 #define epicsThreadh
58 
59 #include <stddef.h>
60 
61 #include "libComAPI.h"
62 
63 #ifdef __cplusplus
64 extern "C" {
65 #endif
66 
67 typedef void (*EPICSTHREADFUNC)(void *parm);
68 
72 #define epicsThreadPriorityMax 99
73 #define epicsThreadPriorityMin 0
74 
75 /* some generic values */
76 #define epicsThreadPriorityLow 10
77 #define epicsThreadPriorityMedium 50
78 #define epicsThreadPriorityHigh 90
79 
80 /* some iocCore specific values */
81 #define epicsThreadPriorityCAServerLow 20
82 #define epicsThreadPriorityCAServerHigh 40
83 #define epicsThreadPriorityScanLow 60
84 #define epicsThreadPriorityScanHigh 70
85 #define epicsThreadPriorityIocsh 91
86 #define epicsThreadPriorityBaseMax 91
87 
90 typedef enum {
91  epicsThreadStackSmall, epicsThreadStackMedium, epicsThreadStackBig
93 
94 typedef enum {
95  epicsThreadBooleanStatusFail, epicsThreadBooleanStatusSuccess
96 } epicsThreadBooleanStatus;
97 
103 LIBCOM_API unsigned int epicsStdCall epicsThreadGetStackSize(
104  epicsThreadStackSizeClass size);
105 
107 typedef struct epicsThreadOSD *epicsThreadId;
108 
109 typedef epicsThreadId epicsThreadOnceId;
110 #define EPICS_THREAD_ONCE_INIT 0
111 
130 LIBCOM_API void epicsStdCall epicsThreadOnce(
131  epicsThreadOnceId *id, EPICSTHREADFUNC, void *arg);
132 
138 LIBCOM_API void epicsThreadRealtimeLock(void);
139 
146 LIBCOM_API void epicsStdCall epicsThreadExitMain(void);
147 
149 typedef struct epicsThreadOpts {
151  unsigned int priority;
155  unsigned int stackSize;
159  unsigned int joinable;
161 
168 #define EPICS_THREAD_OPTS_INIT { \
169  epicsThreadPriorityLow, epicsThreadStackMedium, 0}
170 
178 LIBCOM_API epicsThreadId epicsThreadCreateOpt (
179  const char * name,
180  EPICSTHREADFUNC funptr, void * parm,
181  const epicsThreadOpts *opts );
183 LIBCOM_API epicsThreadId epicsStdCall epicsThreadCreate (
184  const char * name, unsigned int priority, unsigned int stackSize,
185  EPICSTHREADFUNC funptr,void * parm );
189 LIBCOM_API epicsThreadId epicsStdCall epicsThreadMustCreate (
190  const char * name, unsigned int priority, unsigned int stackSize,
191  EPICSTHREADFUNC funptr,void * parm );
192 
193 /* This gets undefined in osdThread.h on VxWorks < 6.9 */
194 #define EPICS_THREAD_CAN_JOIN
195 
196 LIBCOM_API void epicsThreadMustJoin(epicsThreadId id);
198 LIBCOM_API void epicsStdCall epicsThreadSuspendSelf(void);
200 LIBCOM_API void epicsStdCall epicsThreadResume(epicsThreadId id);
202 LIBCOM_API unsigned int epicsStdCall epicsThreadGetPriority(
203  epicsThreadId id);
205 LIBCOM_API unsigned int epicsStdCall epicsThreadGetPrioritySelf(void);
207 LIBCOM_API void epicsStdCall epicsThreadSetPriority(
208  epicsThreadId id,unsigned int priority);
212 LIBCOM_API epicsThreadBooleanStatus epicsStdCall
214  unsigned int priority, unsigned *pPriorityJustBelow);
218 LIBCOM_API epicsThreadBooleanStatus epicsStdCall
220  unsigned int priority, unsigned *pPriorityJustAbove);
222 LIBCOM_API int epicsStdCall epicsThreadIsEqual(
223  epicsThreadId id1, epicsThreadId id2);
229 LIBCOM_API int epicsStdCall epicsThreadIsSuspended(epicsThreadId id);
233 LIBCOM_API void epicsStdCall epicsThreadSleep(double seconds);
240 LIBCOM_API double epicsStdCall epicsThreadSleepQuantum(void);
244 LIBCOM_API epicsThreadId epicsStdCall epicsThreadGetIdSelf(void);
253 LIBCOM_API epicsThreadId epicsStdCall epicsThreadGetId(const char *name);
258 LIBCOM_API int epicsThreadGetCPUs(void);
259 
267 LIBCOM_API const char * epicsStdCall epicsThreadGetNameSelf(void);
268 
275 LIBCOM_API void epicsStdCall epicsThreadGetName(
276  epicsThreadId id, char *name, size_t size);
277 
284 LIBCOM_API int epicsStdCall epicsThreadIsOkToBlock(void);
290 LIBCOM_API void epicsStdCall epicsThreadSetOkToBlock(int isOkToBlock);
291 
295 LIBCOM_API void epicsStdCall epicsThreadShowAll(unsigned int level);
297 LIBCOM_API void epicsStdCall epicsThreadShow(
298  epicsThreadId id,unsigned int level);
299 
303 typedef void (*EPICS_THREAD_HOOK_ROUTINE)(epicsThreadId id);
304 
311 LIBCOM_API int epicsThreadHookAdd(EPICS_THREAD_HOOK_ROUTINE hook);
312 
317 
321 LIBCOM_API void epicsThreadHooksShow(void);
322 
326 LIBCOM_API void epicsThreadMap(EPICS_THREAD_HOOK_ROUTINE func);
327 
329 typedef struct epicsThreadPrivateOSD * epicsThreadPrivateId;
333 LIBCOM_API epicsThreadPrivateId epicsStdCall epicsThreadPrivateCreate(void);
335 LIBCOM_API void epicsStdCall epicsThreadPrivateDelete(epicsThreadPrivateId id);
337 LIBCOM_API void epicsStdCall epicsThreadPrivateSet(epicsThreadPrivateId,void *);
339 LIBCOM_API void * epicsStdCall epicsThreadPrivateGet(epicsThreadPrivateId);
340 
341 #ifdef __cplusplus
342 }
343 #endif
344 
345 #ifdef __cplusplus
346 
347 #include "epicsEvent.h"
348 #include "epicsMutex.h"
349 
351 class LIBCOM_API epicsThreadRunable {
352 public:
353  virtual ~epicsThreadRunable () = 0;
357  virtual void run () = 0;
359  virtual void show ( unsigned int level ) const;
360 };
361 
362 extern "C" void epicsThreadCallEntryPoint ( void * );
363 
370 class LIBCOM_API epicsThread {
371 public:
378  epicsThread ( epicsThreadRunable &,const char *name, unsigned int stackSize,
379  unsigned int priority=epicsThreadPriorityLow );
380  ~epicsThread () throw ();
382  void start () throw ();
384  void exitWait () throw ();
388  bool exitWait ( const double delay ) throw ();
391  static void exit ();
393  void resume () throw ();
395  void getName ( char * name, size_t size ) const throw ();
397  epicsThreadId getId () const throw ();
399  unsigned int getPriority () const throw ();
401  void setPriority ( unsigned int ) throw ();
402  bool priorityIsEqual ( const epicsThread & ) const throw ();
403  bool isSuspended () const throw ();
405  bool isCurrentThread () const throw ();
406  bool operator == ( const epicsThread & ) const throw ();
408  void show ( unsigned level ) const throw ();
409 
410  /* these operate on the current thread */
411  static void suspendSelf () throw ();
412  static void sleep (double seconds) throw ();
413  static const char * getNameSelf () throw ();
414  static bool isOkToBlock () throw ();
415  static void setOkToBlock ( bool isOkToBlock ) throw ();
416 
417  /* exceptions */
418  class unableToCreateThread;
419 private:
420  epicsThreadRunable & runable;
421  epicsThreadId id;
422  epicsMutex mutex;
423  epicsEvent event;
424  epicsEvent exitEvent;
425  bool * pThreadDestroyed;
426  bool begin;
427  bool cancel;
428  bool terminated;
429  bool joined;
430 
431  bool beginWait () throw ();
432  epicsThread ( const epicsThread & );
433  epicsThread & operator = ( const epicsThread & );
434  friend void epicsThreadCallEntryPoint ( void * );
435  void printLastChanceExceptionMessage (
436  const char * pExceptionTypeName,
437  const char * pExceptionContext );
438  /* exceptions */
439  class exitException {};
440 };
441 
442 class LIBCOM_API epicsThreadPrivateBase {
443 public:
444  class unableToCreateThreadPrivate {}; /* exception */
445 protected:
446  static void throwUnableToCreateThreadPrivate ();
447 };
448 
449 template < class T >
451  private epicsThreadPrivateBase {
452 public:
454  ~epicsThreadPrivate () throw ();
455  T * get () const throw ();
456  void set (T *) throw ();
457 private:
459 };
460 
461 #endif /* __cplusplus */
462 
463 #include "osdThread.h"
464 
465 #ifdef __cplusplus
466 
467 template <class T>
469 {
470  this->id = epicsThreadPrivateCreate ();
471  if ( this->id == 0 ) {
472  epicsThreadPrivateBase::throwUnableToCreateThreadPrivate ();
473  }
474 }
475 
476 template <class T>
478 {
479  epicsThreadPrivateDelete ( this->id );
480 }
481 
482 template <class T>
483 inline T *epicsThreadPrivate<T>::get () const throw ()
484 {
485  return static_cast<T *> ( epicsThreadPrivateGet (this->id) );
486 }
487 
488 template <class T>
489 inline void epicsThreadPrivate<T>::set (T *pIn) throw ()
490 {
491  epicsThreadPrivateSet ( this->id, static_cast<void *> (pIn) );
492 }
493 
494 #endif /* ifdef __cplusplus */
495 
496 #endif /* epicsThreadh */
LIBCOM_API void epicsStdCall epicsThreadSetPriority(epicsThreadId id, unsigned int priority)
LIBCOM_API unsigned int epicsStdCall epicsThreadGetPriority(epicsThreadId id)
LIBCOM_API void epicsThreadMap(EPICS_THREAD_HOOK_ROUTINE func)
LIBCOM_API epicsThreadId epicsStdCall epicsThreadGetIdSelf(void)
LIBCOM_API epicsThreadBooleanStatus epicsStdCall epicsThreadLowestPriorityLevelAbove(unsigned int priority, unsigned *pPriorityJustAbove)
LIBCOM_API int epicsStdCall epicsThreadIsOkToBlock(void)
unsigned int stackSize
Definition: epicsThread.h:155
APIs for the epicsEvent binary semaphore.
LIBCOM_API epicsThreadBooleanStatus epicsStdCall epicsThreadHighestPriorityLevelBelow(unsigned int priority, unsigned *pPriorityJustBelow)
LIBCOM_API int epicsStdCall epicsThreadIsSuspended(epicsThreadId id)
LIBCOM_API void epicsThreadRealtimeLock(void)
LIBCOM_API unsigned int epicsStdCall epicsThreadGetPrioritySelf(void)
unsigned int priority
Definition: epicsThread.h:151
epicsThreadStackSizeClass
Definition: epicsThread.h:90
LIBCOM_API int epicsStdCall epicsThreadIsEqual(epicsThreadId id1, epicsThreadId id2)
LIBCOM_API void epicsStdCall epicsThreadSleep(double seconds)
Block the calling thread for at least the specified time.
A binary semaphore.
Definition: epicsEvent.h:79
LIBCOM_API epicsThreadPrivateId epicsStdCall epicsThreadPrivateCreate(void)
LIBCOM_API void epicsStdCall epicsThreadExitMain(void)
LIBCOM_API void epicsStdCall epicsThreadSetOkToBlock(int isOkToBlock)
LIBCOM_API void epicsThreadHooksShow(void)
LIBCOM_API void epicsStdCall epicsThreadOnce(epicsThreadOnceId *id, EPICSTHREADFUNC, void *arg)
LIBCOM_API epicsThreadId epicsStdCall epicsThreadMustCreate(const char *name, unsigned int priority, unsigned int stackSize, EPICSTHREADFUNC funptr, void *parm)
LIBCOM_API int epicsThreadHookDelete(EPICS_THREAD_HOOK_ROUTINE hook)
LIBCOM_API int epicsThreadGetCPUs(void)
The C++ API for an epicsMutex.
Definition: epicsMutex.h:69
LIBCOM_API void epicsStdCall epicsThreadPrivateSet(epicsThreadPrivateId, void *)
LIBCOM_API int epicsThreadHookAdd(EPICS_THREAD_HOOK_ROUTINE hook)
LIBCOM_API void epicsThreadMustJoin(epicsThreadId id)
LIBCOM_API void epicsStdCall epicsThreadResume(epicsThreadId id)
LIBCOM_API void epicsStdCall epicsThreadShow(epicsThreadId id, unsigned int level)
LIBCOM_API epicsThreadId epicsThreadCreateOpt(const char *name, EPICSTHREADFUNC funptr, void *parm, const epicsThreadOpts *opts)
Allocate and start a new OS thread.
struct epicsThreadPrivateOSD * epicsThreadPrivateId
Definition: epicsThread.h:329
LIBCOM_API epicsThreadId epicsStdCall epicsThreadCreate(const char *name, unsigned int priority, unsigned int stackSize, EPICSTHREADFUNC funptr, void *parm)
LIBCOM_API void *epicsStdCall epicsThreadPrivateGet(epicsThreadPrivateId)
APIs for the epicsMutex mutual exclusion semaphore.
Interface used with class epicsThread.
Definition: epicsThread.h:351
LIBCOM_API epicsThreadId epicsStdCall epicsThreadGetId(const char *name)
LIBCOM_API const char *epicsStdCall epicsThreadGetNameSelf(void)
void(* EPICS_THREAD_HOOK_ROUTINE)(epicsThreadId id)
Definition: epicsThread.h:303
LIBCOM_API void epicsStdCall epicsThreadPrivateDelete(epicsThreadPrivateId id)
An OS thread.
Definition: epicsThread.h:370
unsigned int joinable
Definition: epicsThread.h:159
LIBCOM_API void epicsStdCall epicsThreadShowAll(unsigned int level)
LIBCOM_API void epicsStdCall epicsThreadSuspendSelf(void)
LIBCOM_API unsigned int epicsStdCall epicsThreadGetStackSize(epicsThreadStackSizeClass size)
LIBCOM_API void epicsStdCall epicsThreadGetName(epicsThreadId id, char *name, size_t size)
LIBCOM_API double epicsStdCall epicsThreadSleepQuantum(void)
Query a value approximating the OS timer/scheduler resolution.