EPICS Base  7.0.6.1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
epicsMutex.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 * SPDX-License-Identifier: EPICS
7 * EPICS BASE is distributed subject to a Software License Agreement found
8 * in file LICENSE that is included with this distribution.
9 \*************************************************************************/
10 
41 #ifndef epicsMutexh
42 #define epicsMutexh
43 
44 #include "epicsAssert.h"
45 
46 #include "libComAPI.h"
47 
49 typedef struct epicsMutexParm *epicsMutexId;
50 
52 typedef enum {
53  epicsMutexLockOK = 0,
54  epicsMutexLockTimeout,
55  epicsMutexLockError
57 
58 #ifdef __cplusplus
59 
60 #include "compilerDependencies.h"
61 #include "epicsGuard.h"
62 
65 #define newEpicsMutex new epicsMutex(__FILE__,__LINE__)
66 
69 class LIBCOM_API epicsMutex {
70 public:
73  class mutexCreateFailed; /* exception payload */
74  class invalidMutex; /* exception payload */
75 
76 #if !defined(__GNUC__) || __GNUC__<4 || (__GNUC__==4 && __GNUC_MINOR__<8)
77 
79  epicsMutex ();
80 
87  epicsMutex ( const char *pFileName, int lineno );
88 #else
89 
93  epicsMutex ( const char *pFileName = __builtin_FILE(), int lineno = __builtin_LINE() );
94 #endif
95 
98  ~epicsMutex ();
99 
106  void show ( unsigned level ) const;
107 
116  void lock ();
117 
123  void unlock ();
124 
131  bool tryLock ();
132 private:
133  epicsMutexId id;
134  epicsMutex ( const epicsMutex & );
135  epicsMutex & operator = ( const epicsMutex & );
136 };
137 
139 class LIBCOM_API epicsDeadlockDetectMutex {
140 public:
143  typedef unsigned hierarchyLevel_t;
144  epicsDeadlockDetectMutex ( unsigned hierarchyLevel_t );
146  void show ( unsigned level ) const;
147  void lock (); /* blocks until success */
148  void unlock ();
149  bool tryLock (); /* true if successful */
150 private:
151  epicsMutex mutex;
152  const hierarchyLevel_t hierarchyLevel;
153  class epicsDeadlockDetectMutex * pPreviousLevel;
155  epicsDeadlockDetectMutex & operator = ( const epicsDeadlockDetectMutex & );
156 };
157 
158 #endif /*__cplusplus*/
159 
160 #ifdef __cplusplus
161 extern "C" {
162 #endif /*__cplusplus*/
163 
169 #define epicsMutexCreate() epicsMutexOsiCreate(__FILE__,__LINE__)
170 
171 LIBCOM_API epicsMutexId epicsStdCall epicsMutexOsiCreate(
172  const char *pFileName,int lineno);
173 
180 #define epicsMutexMustCreate() epicsMutexOsiMustCreate(__FILE__,__LINE__)
181 
182 LIBCOM_API epicsMutexId epicsStdCall epicsMutexOsiMustCreate(
183  const char *pFileName,int lineno);
184 
188 LIBCOM_API void epicsStdCall epicsMutexDestroy(epicsMutexId id);
189 
195 LIBCOM_API void epicsStdCall epicsMutexUnlock(epicsMutexId id);
196 
207 LIBCOM_API epicsMutexLockStatus epicsStdCall epicsMutexLock(
208  epicsMutexId id);
209 
215 #define epicsMutexMustLock(ID) { \
216  epicsMutexLockStatus status = epicsMutexLock(ID); \
217  assert(status == epicsMutexLockOK); \
218 }
219 
227 LIBCOM_API epicsMutexLockStatus epicsStdCall epicsMutexTryLock(
228  epicsMutexId id);
229 
237 LIBCOM_API void epicsStdCall epicsMutexShow(
238  epicsMutexId id,unsigned int level);
239 
247 LIBCOM_API void epicsStdCall epicsMutexShowAll(
248  int onlyLocked,unsigned int level);
249 
255 struct epicsMutexOSD * epicsMutexOsdCreate(void);
256 void epicsMutexOsdDestroy(struct epicsMutexOSD *);
257 void epicsMutexOsdUnlock(struct epicsMutexOSD *);
258 epicsMutexLockStatus epicsMutexOsdLock(struct epicsMutexOSD *);
259 epicsMutexLockStatus epicsMutexOsdTryLock(struct epicsMutexOSD *);
260 void epicsMutexOsdShow(struct epicsMutexOSD *,unsigned int level);
261 #ifdef EPICS_PRIVATE_API
262 void epicsMutexOsdShowAll(void);
263 #endif
264 
265 #ifdef __cplusplus
266 }
267 #endif
268 
269 #include "osdMutex.h"
270 
271 #endif /* epicsMutexh */
LIBCOM_API epicsMutexLockStatus epicsStdCall epicsMutexLock(epicsMutexId id)
Claim the semaphore, waiting until it&#39;s free if currently owned owned by a different thread...
LIBCOM_API epicsMutexId epicsStdCall epicsMutexOsiCreate(const char *pFileName, int lineno)
Internal API, used by epicsMutexCreate().
LIBCOM_API void epicsStdCall epicsMutexUnlock(epicsMutexId id)
Release the semaphore.
LIBCOM_API void epicsStdCall epicsMutexDestroy(epicsMutexId id)
Destroy an epicsMutex semaphore.
epicsMutexLockStatus
Definition: epicsMutex.h:52
An EPICS-specific replacement for ANSI C&#39;s assert.
Compiler specific declarations.
LIBCOM_API epicsMutexLockStatus epicsStdCall epicsMutexTryLock(epicsMutexId id)
Similar to epicsMutexLock() except that the call returns immediately, with the return status indicati...
LIBCOM_API epicsMutexId epicsStdCall epicsMutexOsiMustCreate(const char *pFileName, int lineno)
Internal API, used by epicsMutexMustCreate().
The C++ API for an epicsMutex.
Definition: epicsMutex.h:69
A semaphore for locating deadlocks in C++ code.
Definition: epicsMutex.h:139
LIBCOM_API void epicsStdCall epicsMutexShowAll(int onlyLocked, unsigned int level)
Display information about all epicsMutex semaphores.
LIBCOM_API void epicsStdCall epicsMutexShow(epicsMutexId id, unsigned int level)
Display information about the semaphore.
struct epicsMutexParm * epicsMutexId
An identifier for an epicsMutex for use with the C API.
Definition: epicsMutex.h:49