casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AsynchronousTools.h
Go to the documentation of this file.
1 /*
2  * AsynchronousTools.h
3  *
4  * Created on: Nov 1, 2010
5  * Author: jjacobs
6  */
7 
8 #ifndef ASYNCHRONOUSTOOLS_H_
9 #define ASYNCHRONOUSTOOLS_H_
10 
11 #include <casa/aips.h>
12 #include <casa/aipstype.h>
13 #include <casa/BasicSL/String.h>
14 #include <mutex>
15 #include "UtilJ.h"
16 
17 #include <map>
18 #include <queue>
19 
20 using std::map;
21 using std::mutex;
22 using std::queue;
23 
24 namespace casa {
25 
26 namespace async {
27 
28 class MutexImpl;
29 
30 class Mutex {
31 
32  friend class Condition;
33  friend class UniqueLock;
34 
35 public:
36 
37  Mutex ();
38  virtual ~Mutex ();
39 
41 
42  void lock ();
43  //casacore::Bool lock (casacore::Int milliseconds);
44  void unlock ();
45  bool trylock ();
46 
47  // jagonzal: Useful when mandatory is locking
48  void acquirelock();
49 
50 protected:
51 
52  std::mutex & getMutex ();
53 
54 private:
55 
56  bool isLocked_p;
57  MutexImpl * impl_p;
58 
59  Mutex (const Mutex & other); // illegal operation: do not define
60  Mutex operator= (const Mutex & other); // illegal operation: do not define
61 
62 };
63 
64 class LockGuard {
65 
66  friend class LockGuardInverse;
67 
68 public:
69 
70  LockGuard (Mutex & mutex);
71  LockGuard (Mutex * mutex);
72 
73  virtual ~LockGuard ();
74 
75 private:
76 
78 
79 };
80 
82 
83 public:
84 
85  LockGuardInverse (Mutex & mutex);
86  LockGuardInverse (Mutex * mutex);
88 
89  virtual ~LockGuardInverse ();
90 
91 private:
92 
94 
95 };
96 
97 
98 class MutexLocker {
99 
100 public:
101 
102  MutexLocker (Mutex & mutex);
103  MutexLocker (Mutex * mutex);
104 
105  virtual ~MutexLocker ();
106 
107 private:
108 
110 
111  MutexLocker (const MutexLocker & other); // do not define
112  MutexLocker & operator= (const MutexLocker & other); // do not define
113 
114 };
115 
116 class ConditionImpl;
117 class UniqueLock;
118 
119 class Condition {
120 
121 public:
122 
123  Condition ();
124  virtual ~Condition ();
125 
126  void broadcast () __attribute__ ((deprecated)) /*"Use notify_all */;
127  void signal () __attribute__ ((deprecated)) /* Use notify_one */;
128 
129  void notify_all ();
130  void notify_one ();
131  void wait (UniqueLock & uniqueLock);
132  // casacore::Bool wait (Mutex & mutex, int milliseconds);
133 
134 private:
135 
136  ConditionImpl * impl_p;
137 };
138 
139 class SemaphoreImpl;
140 
141 class Semaphore {
142 
143 public:
144 
145  Semaphore (int initialValue = 0);
146  ~Semaphore ();
147 
149  void post ();
151  void wait ();
152  casacore::Bool wait (int milliseconds);
153 
154 private:
155 
156  SemaphoreImpl * impl_p;
158 
159  Semaphore (const Semaphore & other); // illegal operation: do not define
160  Semaphore operator= (const Semaphore & other); // illegal operation: do not define
161 
162 };
163 
164 class Thread {
165 
166 public:
167 
168  typedef void * (* ThreadFunction) (void *);
169 
170  Thread ();
171  virtual ~Thread ();
172 
173  pthread_t getId () const;
174  pid_t gettid () const; // linux only
175  bool isTerminationRequested () const;
176  void * join ();
177  void startThread ();
178  virtual void terminate ();
179 
180 protected:
181 
182  bool isStarted () const;
183  virtual void * run () = 0;
184 
185  static void * threadFunction (void *);
186 
187 private:
188 
189  pthread_t * id_p;
190  bool started_p;
191  volatile bool terminationRequested_p;
192 
193 };
194 
195 class UniqueLock {
196 
197  friend class Condition;
198 
199 public:
200 
201  UniqueLock (Mutex & mutex);
202 
203  void lock ();
204  void unlock ();
205 
206 private:
207 
208  std::unique_lock<std::mutex> uniqueLock_p;
209 };
210 
211 class Logger {
212 
213 public:
214 
215  // make noncopyable...
216  Logger( const Logger& ) = delete;
217  Logger& operator=( const Logger& ) = delete;
218 
219  void log (const char * format, ...);
220  void registerName (const casacore::String & threadName);
221  void start (const char * logFilename);
222 
223  static Logger * get ();
224 
225 protected:
226 
227  class LoggerThread : public Thread {
228  public:
229 
230  LoggerThread ();
231  ~LoggerThread ();
232 
233  void log (const string & text);
234  void setLogFilename (const casacore::String & filename);
235  void terminate ();
236 
237  protected:
238 
239  void * run ();
240 
241  private:
242 
246  std::ostream * logStream_p;
248  queue<std::string> outputQueue_p;
249  };
250 
251 
252 // void log (char * format, ...);
253 // void setLogFilename (char * logFilename);
254 // void terminate ();
255 
256 private:
257 
258  typedef map <pthread_t, casacore::String> ThreadNames;
259 
264 
265  static Logger * singleton_p;
266 
267  Logger (); // singleton
268  ~Logger ();
269 
270  static void initialize ();
271 };
272 
273 
274 }
275 
276 }
277 
278 
279 #endif /* ASYNCHRONOUSTOOLS_H_ */
int Int
Definition: aipstype.h:50
Logger & operator=(const Logger &)=delete
map< pthread_t, casacore::String > ThreadNames
void log (char * format,...); void setLogFilename (char * logFilename); void terminate (); ...
std::unique_lock< std::mutex > uniqueLock_p
Semaphore(int initialValue=0)
void unlock()
casacore::Bool lock (casacore::Int milliseconds);
pid_t gettid() const
virtual void terminate()
std::mutex & getMutex()
volatile bool terminationRequested_p
void lock()
casacore::Bool isLockedByThisThread () const; // for debug purposes only !!!
void log(const string &text)
casacore::Bool trywait()
void signal() __attribute__((deprecated))
void registerName(const casacore::String &threadName)
void wait(UniqueLock &uniqueLock)
static Logger * singleton_p
casacore::Int getValue()
void start(const char *logFilename)
bool isTerminationRequested() const
UniqueLock(Mutex &mutex)
pthread_t getId() const
Semaphore operator=(const Semaphore &other)
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
MutexLocker & operator=(const MutexLocker &other)
void acquirelock()
jagonzal: Useful when mandatory is locking
void broadcast() __attribute__((deprecated))
static void * threadFunction(void *)
LockGuard(Mutex &mutex)
void log(const char *format,...)
MutexLocker(Mutex &mutex)
Mutex operator=(const Mutex &other)
String: the storage and methods of handling collections of characters.
Definition: String.h:223
static void initialize()
bool isStarted() const
ConditionImpl * impl_p
casacore::Bool wait (Mutex &amp; mutex, int milliseconds);
void setLogFilename(const casacore::String &filename)
LoggerThread * loggerThread_p
virtual void * run()=0