FRIBParallelanalysis  1.0
FrameworkforMPIParalleldataanalysisatFRIB
CTCLMutex.h
1 #ifndef CTCLMUTEX_H
2 #define CTCLMUTEX_H
3 /*
4  This software is Copyright by the Board of Trustees of Michigan
5  State University (c) Copyright 2005.
6 
7  You may use this software under the terms of the GNU public license
8  (GPL). The terms of this license are described at:
9 
10  http://www.gnu.org/licenses/gpl.txt
11 
12  Author:
13  Ron Fox
14  NSCL
15  Michigan State University
16  East Lansing, MI 48824-1321
17 */
18 
19 // Note: For reasons unknown to me I can't get Tcl_MutexLock to actually
20 // do anything so my tests failed. I do know how to use pthread mutexes
21 // so I imported this working class from NSCLDAQ/main/base/thread
22 // and then some typedefs to make names ok.
23 
24 #include <pthread.h>
25 
33 struct CMutexAttr
34 {
35  pthread_mutexattr_t m_attributes;
36  CMutexAttr();
37  ~CMutexAttr();
38 
39  void setShared();
40  void setUnshared();
41  bool isShareable();
42 
43  void setType(int type);
44  int getType();
45 
46  static void throwifbad (int status, const char* message);
47 };
48 
49 
54 class CMutex
55 {
56 public:
57  pthread_mutex_t m_mutex; // The underlying mutex.
58 
59 public:
60  CMutex();
61  CMutex(pthread_mutexattr_t& attributes);
62  CMutex(CMutexAttr& attributes);
63  virtual ~CMutex();
64 
65 private:
66  CMutex(const CMutex&);
67  CMutex& operator=(const CMutex&);
68  int operator==(const CMutex&) const;
69  int operator!=(const CMutex&) const;
70 
71 
72 
73  // Synchronization operations.
74 public:
75 
76  void lock();
77  bool trylock();
78  void unlock();
79 
80 private:
81  void create(pthread_mutexattr_t* pAttributes);
82 };
83 
84 
85 
94 private:
95  CMutex& m_mutex;
96 public:
97  CriticalSection(CMutex& mutex) : m_mutex(mutex)
98  {
99  m_mutex.lock();
100  }
101  ~CriticalSection() {
102  m_mutex.unlock();
103  }
104 };
105 // Sneaky trick.
106 
107 using CTCLMutex = CMutex;
108 using CTCLMutexAttr = CMutexAttr;
109 
110 
111 
112 #endif
Definition: CTCLMutex.h:33
void setShared()
Definition: CTCLMutex.cpp:52
int getType()
Definition: CTCLMutex.cpp:102
Definition: CTCLMutex.h:54
Definition: CTCLMutex.h:93
void setType(int type)
Definition: CTCLMutex.cpp:92
void lock()
Definition: CTCLMutex.cpp:162
CMutexAttr()
Definition: CTCLMutex.cpp:31
void unlock()
Definition: CTCLMutex.cpp:184
bool isShareable()
Definition: CTCLMutex.cpp:75
void setUnshared()
Definition: CTCLMutex.cpp:63
~CMutexAttr()
Definition: CTCLMutex.cpp:41