EPICS Base  7.0.6.1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 #include "compilerDependencies.h"
63 
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 typedef void (*EPICSTHREADFUNC)(void *parm);
69 
73 #define epicsThreadPriorityMax 99
74 #define epicsThreadPriorityMin 0
75 
76 /* some generic values */
77 #define epicsThreadPriorityLow 10
78 #define epicsThreadPriorityMedium 50
79 #define epicsThreadPriorityHigh 90
80 
81 /* some iocCore specific values */
82 #define epicsThreadPriorityCAServerLow 20
83 #define epicsThreadPriorityCAServerHigh 40
84 #define epicsThreadPriorityScanLow 60
85 #define epicsThreadPriorityScanHigh 70
86 #define epicsThreadPriorityIocsh 91
87 #define epicsThreadPriorityBaseMax 91
88 
91 typedef enum {
92  epicsThreadStackSmall, epicsThreadStackMedium, epicsThreadStackBig
94 
95 typedef enum {
96  epicsThreadBooleanStatusFail, epicsThreadBooleanStatusSuccess
97 } epicsThreadBooleanStatus;
98 
104 LIBCOM_API unsigned int epicsStdCall epicsThreadGetStackSize(
106 
108 typedef struct epicsThreadOSD *epicsThreadId;
109 
110 typedef epicsThreadId epicsThreadOnceId;
111 #define EPICS_THREAD_ONCE_INIT 0
112 
131 LIBCOM_API void epicsStdCall epicsThreadOnce(
132  epicsThreadOnceId *id, EPICSTHREADFUNC, void *arg);
133 
139 LIBCOM_API void epicsThreadRealtimeLock(void);
140 
151 LIBCOM_API void epicsStdCall epicsThreadExitMain(void) EPICS_DEPRECATED;
152 
154 typedef struct epicsThreadOpts {
156  unsigned int priority;
160  unsigned int stackSize;
164  unsigned int joinable;
166 
173 #define EPICS_THREAD_OPTS_INIT { \
174  epicsThreadPriorityLow, epicsThreadStackMedium, 0}
175 
183 LIBCOM_API epicsThreadId epicsThreadCreateOpt (
184  const char * name,
185  EPICSTHREADFUNC funptr, void * parm,
186  const epicsThreadOpts *opts );
188 LIBCOM_API epicsThreadId epicsStdCall epicsThreadCreate (
189  const char * name, unsigned int priority, unsigned int stackSize,
190  EPICSTHREADFUNC funptr,void * parm );
194 LIBCOM_API epicsThreadId epicsStdCall epicsThreadMustCreate (
195  const char * name, unsigned int priority, unsigned int stackSize,
196  EPICSTHREADFUNC funptr,void * parm );
197 
198 /* This gets undefined in osdThread.h on VxWorks < 6.9 */
199 #define EPICS_THREAD_CAN_JOIN
200 
201 LIBCOM_API void epicsThreadMustJoin(epicsThreadId id);
203 LIBCOM_API void epicsStdCall epicsThreadSuspendSelf(void);
205 LIBCOM_API void epicsStdCall epicsThreadResume(epicsThreadId id);
207 LIBCOM_API unsigned int epicsStdCall epicsThreadGetPriority(
208  epicsThreadId id);
210 LIBCOM_API unsigned int epicsStdCall epicsThreadGetPrioritySelf(void);
212 LIBCOM_API void epicsStdCall epicsThreadSetPriority(
213  epicsThreadId id,unsigned int priority);
217 LIBCOM_API epicsThreadBooleanStatus epicsStdCall
219  unsigned int priority, unsigned *pPriorityJustBelow);
223 LIBCOM_API epicsThreadBooleanStatus epicsStdCall
225  unsigned int priority, unsigned *pPriorityJustAbove);
227 LIBCOM_API int epicsStdCall epicsThreadIsEqual(
228  epicsThreadId id1, epicsThreadId id2);
234 LIBCOM_API int epicsStdCall epicsThreadIsSuspended(epicsThreadId id);
238 LIBCOM_API void epicsStdCall epicsThreadSleep(double seconds);
245 LIBCOM_API double epicsStdCall epicsThreadSleepQuantum(void);
249 LIBCOM_API epicsThreadId epicsStdCall epicsThreadGetIdSelf(void);
258 LIBCOM_API epicsThreadId epicsStdCall epicsThreadGetId(const char *name);
263 LIBCOM_API int epicsThreadGetCPUs(void);
264 
272 LIBCOM_API const char * epicsStdCall epicsThreadGetNameSelf(void);
273 
280 LIBCOM_API void epicsStdCall epicsThreadGetName(
281  epicsThreadId id, char *name, size_t size);
282 
289 LIBCOM_API int epicsStdCall epicsThreadIsOkToBlock(void);
295 LIBCOM_API void epicsStdCall epicsThreadSetOkToBlock(int isOkToBlock);
296 
300 LIBCOM_API void epicsStdCall epicsThreadShowAll(unsigned int level);
302 LIBCOM_API void epicsStdCall epicsThreadShow(
303  epicsThreadId id,unsigned int level);
304 
308 typedef void (*EPICS_THREAD_HOOK_ROUTINE)(epicsThreadId id);
309 
316 LIBCOM_API int epicsThreadHookAdd(EPICS_THREAD_HOOK_ROUTINE hook);
317 
322 
326 LIBCOM_API void epicsThreadHooksShow(void);
327 
331 LIBCOM_API void epicsThreadMap(EPICS_THREAD_HOOK_ROUTINE func);
332 
334 typedef struct epicsThreadPrivateOSD * epicsThreadPrivateId;
338 LIBCOM_API epicsThreadPrivateId epicsStdCall epicsThreadPrivateCreate(void);
340 LIBCOM_API void epicsStdCall epicsThreadPrivateDelete(epicsThreadPrivateId id);
342 LIBCOM_API void epicsStdCall epicsThreadPrivateSet(epicsThreadPrivateId,void *);
344 LIBCOM_API void * epicsStdCall epicsThreadPrivateGet(epicsThreadPrivateId);
345 
346 #ifdef __cplusplus
347 }
348 #endif
349 
350 #ifdef __cplusplus
351 
352 #include "epicsEvent.h"
353 #include "epicsMutex.h"
354 
356 class LIBCOM_API epicsThreadRunable {
357 public:
358  virtual ~epicsThreadRunable () = 0;
362  virtual void run () = 0;
364  virtual void show ( unsigned int level ) const;
365 };
366 
367 extern "C" void epicsThreadCallEntryPoint ( void * );
368 
375 class LIBCOM_API epicsThread {
376 public:
383  epicsThread ( epicsThreadRunable &,const char *name, unsigned int stackSize,
384  unsigned int priority=epicsThreadPriorityLow );
385  ~epicsThread () throw ();
387  void start () throw ();
389  void exitWait () throw ();
393  bool exitWait ( const double delay ) throw ();
396  static void exit ();
398  void resume () throw ();
400  void getName ( char * name, size_t size ) const throw ();
402  epicsThreadId getId () const throw ();
404  unsigned int getPriority () const throw ();
406  void setPriority ( unsigned int ) throw ();
407  bool priorityIsEqual ( const epicsThread & ) const throw ();
408  bool isSuspended () const throw ();
410  bool isCurrentThread () const throw ();
411  bool operator == ( const epicsThread & ) const throw ();
413  void show ( unsigned level ) const throw ();
414 
415  /* these operate on the current thread */
416  static void suspendSelf () throw ();
417  static void sleep (double seconds) throw ();
418  static const char * getNameSelf () throw ();
419  static bool isOkToBlock () throw ();
420  static void setOkToBlock ( bool isOkToBlock ) throw ();
421 
422  /* exceptions */
423  class unableToCreateThread;
424 private:
425  epicsThreadRunable & runable;
426  epicsThreadId id;
427  epicsMutex mutex;
428  epicsEvent event;
429  epicsEvent exitEvent;
430  bool * pThreadDestroyed;
431  bool begin;
432  bool cancel;
433  bool terminated;
434  bool joined;
435 
436  bool beginWait () throw ();
437  epicsThread ( const epicsThread & );
438  epicsThread & operator = ( const epicsThread & );
439  friend void epicsThreadCallEntryPoint ( void * );
440  void printLastChanceExceptionMessage (
441  const char * pExceptionTypeName,
442  const char * pExceptionContext );
443  /* exceptions */
444  class exitException {};
445 };
446 
447 class LIBCOM_API epicsThreadPrivateBase {
448 public:
449  class unableToCreateThreadPrivate {}; /* exception */
450 protected:
451  static void throwUnableToCreateThreadPrivate ();
452 };
453 
454 template < class T >
456  private epicsThreadPrivateBase {
457 public:
459  ~epicsThreadPrivate () throw ();
460  T * get () const throw ();
461  void set (T *) throw ();
462 private:
464 };
465 
466 #endif /* __cplusplus */
467 
468 #include "osdThread.h"
469 
470 #ifdef __cplusplus
471 
472 template <class T>
474 {
475  this->id = epicsThreadPrivateCreate ();
476  if ( this->id == 0 ) {
477  epicsThreadPrivateBase::throwUnableToCreateThreadPrivate ();
478  }
479 }
480 
481 template <class T>
483 {
484  epicsThreadPrivateDelete ( this->id );
485 }
486 
487 template <class T>
488 inline T *epicsThreadPrivate<T>::get () const throw ()
489 {
490  return static_cast<T *> ( epicsThreadPrivateGet (this->id) );
491 }
492 
493 template <class T>
494 inline void epicsThreadPrivate<T>::set (T *pIn) throw ()
495 {
496  epicsThreadPrivateSet ( this->id, static_cast<void *> (pIn) );
497 }
498 
499 #endif /* ifdef __cplusplus */
500 
501 #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 void epicsStdCall epicsThreadExitMain(void) EPICS_DEPRECATED
LIBCOM_API epicsThreadBooleanStatus epicsStdCall epicsThreadLowestPriorityLevelAbove(unsigned int priority, unsigned *pPriorityJustAbove)
LIBCOM_API int epicsStdCall epicsThreadIsOkToBlock(void)
unsigned int stackSize
Definition: epicsThread.h:160
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:156
epicsThreadStackSizeClass
Definition: epicsThread.h:91
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 epicsThreadSetOkToBlock(int isOkToBlock)
LIBCOM_API void epicsThreadHooksShow(void)
Compiler specific declarations.
LIBCOM_API void epicsStdCall epicsThreadOnce(epicsThreadOnceId *id, EPICSTHREADFUNC, void *arg)
void(* EPICS_THREAD_HOOK_ROUTINE)(epicsThreadId id)
Definition: epicsThread.h:308
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:334
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:356
LIBCOM_API epicsThreadId epicsStdCall epicsThreadGetId(const char *name)
LIBCOM_API const char *epicsStdCall epicsThreadGetNameSelf(void)
LIBCOM_API void epicsStdCall epicsThreadPrivateDelete(epicsThreadPrivateId id)
An OS thread.
Definition: epicsThread.h:375
unsigned int joinable
Definition: epicsThread.h:164
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.