EPICS Base 7.0.8.0
Loading...
Searching...
No Matches
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"
63
64#ifdef __cplusplus
65extern "C" {
66#endif
67
68typedef 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
91typedef enum {
92 epicsThreadStackSmall, epicsThreadStackMedium, epicsThreadStackBig
94
95typedef enum {
96 epicsThreadBooleanStatusFail, epicsThreadBooleanStatusSuccess
97} epicsThreadBooleanStatus;
98
104LIBCOM_API unsigned int epicsStdCall epicsThreadGetStackSize(
106
108typedef struct epicsThreadOSD *epicsThreadId;
109
111#define EPICS_THREAD_ONCE_INIT 0
112
131LIBCOM_API void epicsStdCall epicsThreadOnce(
132 epicsThreadOnceId *id, EPICSTHREADFUNC, void *arg);
133
139LIBCOM_API void epicsThreadRealtimeLock(void);
140
151LIBCOM_API void epicsStdCall epicsThreadExitMain(void) EPICS_DEPRECATED;
152
154typedef 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
184 const char * name,
185 EPICSTHREADFUNC funptr, void * parm,
186 const epicsThreadOpts *opts );
188LIBCOM_API epicsThreadId epicsStdCall epicsThreadCreate (
189 const char * name, unsigned int priority, unsigned int stackSize,
190 EPICSTHREADFUNC funptr,void * parm );
194LIBCOM_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
203LIBCOM_API void epicsStdCall epicsThreadSuspendSelf(void);
205LIBCOM_API void epicsStdCall epicsThreadResume(epicsThreadId id);
207LIBCOM_API unsigned int epicsStdCall epicsThreadGetPriority(
208 epicsThreadId id);
210LIBCOM_API unsigned int epicsStdCall epicsThreadGetPrioritySelf(void);
212LIBCOM_API void epicsStdCall epicsThreadSetPriority(
213 epicsThreadId id,unsigned int priority);
217LIBCOM_API epicsThreadBooleanStatus epicsStdCall
219 unsigned int priority, unsigned *pPriorityJustBelow);
223LIBCOM_API epicsThreadBooleanStatus epicsStdCall
225 unsigned int priority, unsigned *pPriorityJustAbove);
227LIBCOM_API int epicsStdCall epicsThreadIsEqual(
234LIBCOM_API int epicsStdCall epicsThreadIsSuspended(epicsThreadId id);
238LIBCOM_API void epicsStdCall epicsThreadSleep(double seconds);
245LIBCOM_API double epicsStdCall epicsThreadSleepQuantum(void);
249LIBCOM_API epicsThreadId epicsStdCall epicsThreadGetIdSelf(void);
258LIBCOM_API epicsThreadId epicsStdCall epicsThreadGetId(const char *name);
263LIBCOM_API int epicsThreadGetCPUs(void);
264
272LIBCOM_API const char * epicsStdCall epicsThreadGetNameSelf(void);
273
280LIBCOM_API void epicsStdCall epicsThreadGetName(
281 epicsThreadId id, char *name, size_t size);
282
289LIBCOM_API int epicsStdCall epicsThreadIsOkToBlock(void);
295LIBCOM_API void epicsStdCall epicsThreadSetOkToBlock(int isOkToBlock);
296
300LIBCOM_API void epicsStdCall epicsThreadShowAll(unsigned int level);
302LIBCOM_API void epicsStdCall epicsThreadShow(
303 epicsThreadId id,unsigned int level);
304
309
317
322
326LIBCOM_API void epicsThreadHooksShow(void);
327
332
338LIBCOM_API epicsThreadPrivateId epicsStdCall epicsThreadPrivateCreate(void);
340LIBCOM_API void epicsStdCall epicsThreadPrivateDelete(epicsThreadPrivateId id);
342LIBCOM_API void epicsStdCall epicsThreadPrivateSet(epicsThreadPrivateId,void *);
344LIBCOM_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
356class LIBCOM_API epicsThreadRunable {
357public:
358 virtual ~epicsThreadRunable () = 0;
362 virtual void run () = 0;
364 virtual void show ( unsigned int level ) const;
365};
366
367extern "C" void epicsThreadCallEntryPoint ( void * );
368
375class LIBCOM_API epicsThread {
376public:
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 ();
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 */
424private:
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 ();
439 friend void epicsThreadCallEntryPoint ( void * );
440 void printLastChanceExceptionMessage (
442 const char * pExceptionContext );
443 /* exceptions */
444 class exitException {};
445};
446
447class LIBCOM_API epicsThreadPrivateBase {
448public:
449 class unableToCreateThreadPrivate {}; /* exception */
450protected:
451 static void throwUnableToCreateThreadPrivate ();
452};
453
454template < class T >
456 private epicsThreadPrivateBase {
457public:
459 ~epicsThreadPrivate () throw ();
460 T * get () const throw ();
461 void set (T *) throw ();
462private:
464};
465
466#endif /* __cplusplus */
467
468#include "osdThread.h"
469
470#ifdef __cplusplus
471
472template <class T>
474{
475 this->id = epicsThreadPrivateCreate ();
476 if ( this->id == 0 ) {
477 epicsThreadPrivateBase::throwUnableToCreateThreadPrivate ();
478 }
479}
480
481template <class T>
483{
484 epicsThreadPrivateDelete ( this->id );
485}
486
487template <class T>
488inline T *epicsThreadPrivate<T>::get () const throw ()
489{
490 return static_cast<T *> ( epicsThreadPrivateGet (this->id) );
491}
492
493template <class T>
494inline 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 */
A binary semaphore.
Definition epicsEvent.h:79
Provides an RAII style lock/unlock of a mutex.
Definition epicsGuard.h:53
The C++ API for an epicsMutex.
Definition epicsMutex.h:69
An OS thread.
epicsThread(epicsThreadRunable &, const char *name, unsigned int stackSize, unsigned int priority=epicsThreadPriorityLow)
Interface used with class epicsThread.
virtual void show(unsigned int level) const
Optional. Called via epicsThread::show()
virtual void run()=0
Compiler specific declarations.
APIs for the epicsEvent binary semaphore.
APIs for the epicsMutex mutual exclusion semaphore.
LIBCOM_API epicsThreadId epicsStdCall epicsThreadGetId(const char *name)
LIBCOM_API epicsThreadBooleanStatus epicsStdCall epicsThreadHighestPriorityLevelBelow(unsigned int priority, unsigned *pPriorityJustBelow)
LIBCOM_API epicsThreadId epicsStdCall epicsThreadGetIdSelf(void)
LIBCOM_API void epicsThreadMustJoin(epicsThreadId id)
LIBCOM_API int epicsStdCall epicsThreadIsOkToBlock(void)
LIBCOM_API unsigned int epicsStdCall epicsThreadGetPrioritySelf(void)
LIBCOM_API void epicsStdCall epicsThreadSetOkToBlock(int isOkToBlock)
LIBCOM_API unsigned int epicsStdCall epicsThreadGetPriority(epicsThreadId id)
LIBCOM_API int epicsThreadGetCPUs(void)
LIBCOM_API epicsThreadId epicsStdCall epicsThreadMustCreate(const char *name, unsigned int priority, unsigned int stackSize, EPICSTHREADFUNC funptr, void *parm)
LIBCOM_API epicsThreadPrivateId epicsStdCall epicsThreadPrivateCreate(void)
LIBCOM_API int epicsThreadHookDelete(EPICS_THREAD_HOOK_ROUTINE hook)
LIBCOM_API unsigned int epicsStdCall epicsThreadGetStackSize(epicsThreadStackSizeClass size)
LIBCOM_API void epicsStdCall epicsThreadSleep(double seconds)
Block the calling thread for at least the specified time.
LIBCOM_API void epicsThreadMap(EPICS_THREAD_HOOK_ROUTINE func)
LIBCOM_API int epicsThreadHookAdd(EPICS_THREAD_HOOK_ROUTINE hook)
LIBCOM_API void epicsStdCall epicsThreadExitMain(void) EPICS_DEPRECATED
LIBCOM_API void epicsThreadHooksShow(void)
LIBCOM_API int epicsStdCall epicsThreadIsSuspended(epicsThreadId id)
LIBCOM_API epicsThreadId epicsThreadCreateOpt(const char *name, EPICSTHREADFUNC funptr, void *parm, const epicsThreadOpts *opts)
Allocate and start a new OS thread.
struct epicsThreadPrivateOSD * epicsThreadPrivateId
LIBCOM_API void epicsStdCall epicsThreadPrivateSet(epicsThreadPrivateId, void *)
LIBCOM_API double epicsStdCall epicsThreadSleepQuantum(void)
Query a value approximating the OS timer/scheduler resolution.
LIBCOM_API epicsThreadId epicsStdCall epicsThreadCreate(const char *name, unsigned int priority, unsigned int stackSize, EPICSTHREADFUNC funptr, void *parm)
LIBCOM_API void epicsStdCall epicsThreadShowAll(unsigned int level)
epicsThreadStackSizeClass
Definition epicsThread.h:91
LIBCOM_API void *epicsStdCall epicsThreadPrivateGet(epicsThreadPrivateId)
LIBCOM_API int epicsStdCall epicsThreadIsEqual(epicsThreadId id1, epicsThreadId id2)
LIBCOM_API void epicsStdCall epicsThreadSuspendSelf(void)
void(* EPICS_THREAD_HOOK_ROUTINE)(epicsThreadId id)
LIBCOM_API void epicsThreadRealtimeLock(void)
LIBCOM_API const char *epicsStdCall epicsThreadGetNameSelf(void)
LIBCOM_API void epicsStdCall epicsThreadResume(epicsThreadId id)
LIBCOM_API epicsThreadBooleanStatus epicsStdCall epicsThreadLowestPriorityLevelAbove(unsigned int priority, unsigned *pPriorityJustAbove)
LIBCOM_API void epicsStdCall epicsThreadGetName(epicsThreadId id, char *name, size_t size)
LIBCOM_API void epicsStdCall epicsThreadOnce(epicsThreadOnceId *id, EPICSTHREADFUNC, void *arg)
LIBCOM_API void epicsStdCall epicsThreadShow(epicsThreadId id, unsigned int level)
LIBCOM_API void epicsStdCall epicsThreadSetPriority(epicsThreadId id, unsigned int priority)
LIBCOM_API void epicsStdCall epicsThreadPrivateDelete(epicsThreadPrivateId id)
unsigned int stackSize
unsigned int joinable
unsigned int priority