nidas v1.2.3
ThreadSupport.h
Go to the documentation of this file.
1// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2// vim: set shiftwidth=4 softtabstop=4 expandtab:
3/*
4 ********************************************************************
5 ** NIDAS: NCAR In-situ Data Acquistion Software
6 **
7 ** 2006, Copyright University Corporation for Atmospheric Research
8 **
9 ** This program is free software; you can redistribute it and/or modify
10 ** it under the terms of the GNU General Public License as published by
11 ** the Free Software Foundation; either version 2 of the License, or
12 ** (at your option) any later version.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ** GNU General Public License for more details.
18 **
19 ** The LICENSE.txt file accompanying this software contains
20 ** a copy of the GNU General Public License. If it is not found,
21 ** write to the Free Software Foundation, Inc.,
22 ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 **
24 ********************************************************************
25*/
26
27#ifndef NIDAS_UTIL_THREADSUPPORT_H
28#define NIDAS_UTIL_THREADSUPPORT_H
29
30#include <pthread.h>
31#include <semaphore.h>
32#include <string>
33#include <iostream>
34
35#include "Exception.h"
36
37namespace nidas { namespace util {
38
52{
53public:
54
61
66
68
76 void setType(int val);
77 int getType() const;
78
79#ifdef PTHREAD_PRIO_INHERIT
88 void setPriorityCeiling(int val) throw(Exception);
89 int getPriorityCeiling() const;
90#endif
91
92#ifdef PTHREAD_PRIO_INHERIT
98 void setProtocol(int val) throw(Exception);
99 int getProtocol() const;
100#endif
101
109 void setPShared(int val);
110 int getPShared() const;
111
112
113 pthread_mutexattr_t* ptr()
114 {
115 return &_attrs;
116 }
117private:
118 pthread_mutexattr_t _attrs;
119};
120
127{
128public:
130
135
137
145 void setPShared(int val);
146 int getPShared() const;
147
148
149 pthread_rwlockattr_t* ptr()
150 {
151 return &_attrs;
152 }
153private:
154 pthread_rwlockattr_t _attrs;
155};
156
160class Mutex
161{
162
163public:
177 explicit Mutex(int type=PTHREAD_MUTEX_DEFAULT) throw();
178
189 Mutex(const MutexAttributes& attr);
190
196 Mutex(const Mutex& x) throw();
197
202 ~Mutex();
203
216 void lock()
217 {
218 int res;
219 if((res = ::pthread_mutex_lock(&_p_mutex)))
220 throw Exception("Mutex::lock",res);
221 }
222
230 void unlock()
231 {
232 int res;
233 if ((res = ::pthread_mutex_unlock(&_p_mutex)))
234 throw Exception("Mutex::unlock",res);
235 }
236
240 pthread_mutex_t* ptr();
241
242private:
247
248 pthread_mutex_t _p_mutex;
249
251
252};
253
257class Cond
258{
259
260public:
261
266 Cond() throw();
267
271 Cond (const Cond &x) throw();
272
278 ~Cond();
279
304 void lock()
305 {
306 _mutex.lock();
307 }
308
315 void unlock()
316 {
317 _mutex.unlock();
318 }
319
328 void signal()
329 {
330 // only fails with EINVAL if _p_cond is not initialized.
331 int res;
332 if ((res = ::pthread_cond_signal (&_p_cond)))
333 throw Exception("Cond::signal",res);
334 }
335
341 {
342 int res;
343 // only fails with EINVAL if _p_cond is not initialized.
344 if ((res = ::pthread_cond_broadcast (&_p_cond)))
345 throw Exception("Cond::broadcast",res);
346 }
347
361 void wait();
362
363private:
368
369 pthread_cond_t _p_cond;
370
372
373};
374
378class RWLock
379{
380
381public:
382
387 RWLock() throw();
388
392 RWLock(const RWLockAttributes& attr);
393
397 RWLock(const RWLock& x) throw();
398
403 ~RWLock();
404
411 void rdlock()
412 {
413 int res;
414 if ((res = ::pthread_rwlock_rdlock(&_p_rwlock)))
415 throw Exception("RWLock::rdlock",res);
416 }
417
424 void wrlock()
425 {
426 int res;
427 if ((res = ::pthread_rwlock_wrlock(&_p_rwlock)))
428 throw Exception("RWLock::wrlock",res);
429 }
430
437 void unlock()
438 {
439 int res;
440 if ((res = ::pthread_rwlock_unlock(&_p_rwlock)))
441 throw Exception("RWLock::unlock",res);
442 }
443
447 pthread_rwlock_t* ptr();
448
449private:
454
455 pthread_rwlock_t _p_rwlock;
456
458
459};
460
465{
466public:
467
472 {
473 sem_init(&_sem,0,0);
474 }
475
476
481 {
482 sem_destroy(&_sem);
483 }
484
489 void wait() throw()
490 {
491 sem_wait(&_sem);
492 }
493
499 bool check() throw()
500 {
501 return sem_trywait(&_sem) == 0;
502 }
503
507 void post() throw()
508 {
509 // man page for sem_post:
510 // return value of sem_post can be < 0, in which case errno is ERANGE:
511 // the semaphore value would exceed SEM_VALUE_MAX
512 // (the semaphore count is left unchanged in this case)
513 // Hmmm: do we throw an exception for this pathelogical situation,
514 // which compels users to have to catch it? Naa.
515 sem_post(&_sem);
516 }
517
521 int getValue() throw()
522 {
523 int val;
524 sem_getvalue(&_sem,&val);
525 return val;
526 }
527
531 sem_t* ptr() { return &_sem; }
532
533private:
534
539
544
545 sem_t _sem;
546};
547
549{
550
551public:
552 Multisync(int n);
553 void sync();
554 void sync(std::string& msg);
555 void init(); // to reinitialize
556
557private:
559 int _n;
561 int debug;
562
563};
564
565
575{
576public:
577
586 Synchronized (Cond &cond_): mutexp(0),condp(&cond_)
587 {
588 condp->lock();
589 }
590
594 Synchronized (Mutex &mutex_): mutexp(&mutex_),condp(0)
595 {
596 mutexp->lock();
597 }
598
603 {
604 if (condp) condp->unlock();
605 else if (mutexp) mutexp->unlock();
606 }
607
608private:
611
612private:
615
616};
617
627{
628public:
629
635 Autolock (Cond &cond): _mutexp(0),_condp(&cond)
636 {
637 cond.lock();
638 }
644 Autolock (Mutex &mutex): _mutexp(&mutex),_condp(0)
645 {
646 mutex.lock();
647 }
648
653 {
654 if (_condp) _condp->unlock();
655 else if (_mutexp) _mutexp->unlock();
656 }
657
658 private:
661
662 private:
665
666};
667
672{
673public:
674
680 AutoRdLock (RWLock &rwlock): _rwlock(rwlock)
681 {
682 _rwlock.rdlock();
683 }
684
689 {
690 _rwlock.unlock();
691 }
692
693private:
695
696private:
699};
700
705{
706public:
707
713 AutoWrLock (RWLock &rwlock): _rwlock(rwlock)
714 {
715 _rwlock.wrlock();
716 }
717
722 {
723 _rwlock.unlock();
724 }
725
726private:
728
729private:
732};
733
734
735}} // namespace nidas namespace util
736
737#endif
738
Autolock for acquiring/releasing a read lock on a RWLock.
Definition ThreadSupport.h:672
AutoRdLock(const Autolock &)
AutoRdLock(RWLock &rwlock)
Construct the guard object and lock() the lock.
Definition ThreadSupport.h:680
~AutoRdLock()
On destruction, unlock the lock.
Definition ThreadSupport.h:688
AutoRdLock & operator=(const Autolock &)
RWLock & _rwlock
Definition ThreadSupport.h:694
Autolock for acquiring/releasing a write lock on a RWLock.
Definition ThreadSupport.h:705
AutoWrLock & operator=(const Autolock &)
AutoWrLock(RWLock &rwlock)
Construct the guard object and lock() the lock.
Definition ThreadSupport.h:713
RWLock & _rwlock
Definition ThreadSupport.h:727
~AutoWrLock()
On destruction, unlock the lock.
Definition ThreadSupport.h:721
AutoWrLock(const Autolock &)
Autolock is used a simple guard object for critical sections.
Definition ThreadSupport.h:627
Cond * _condp
Definition ThreadSupport.h:660
Autolock(Cond &cond)
Construct the guard object and lock() the lock.
Definition ThreadSupport.h:635
~Autolock()
On destruction, unlock the lock.
Definition ThreadSupport.h:652
Mutex * _mutexp
Definition ThreadSupport.h:659
Autolock(Mutex &mutex)
Construct the guard object and lock() the lock.
Definition ThreadSupport.h:644
Autolock & operator=(const Autolock &)
Autolock(const Autolock &)
A wrapper class for a Posix condition variable.
Definition ThreadSupport.h:258
void lock()
Lock the mutex associated with the condition variable.
Definition ThreadSupport.h:304
pthread_cond_t _p_cond
Definition ThreadSupport.h:369
void broadcast()
Restart all threads waiting on the condition variable.
Definition ThreadSupport.h:340
void wait()
Wait on the condition variable.
Definition ThreadSupport.cc:306
Mutex _mutex
Definition ThreadSupport.h:371
Cond & operator=(const Cond &)
No assignment allowed.
Cond()
Construct a POSIX condition variable, with default attributes.
Definition ThreadSupport.cc:263
~Cond()
Destruct a Cond.
Definition ThreadSupport.cc:281
void signal()
Unblock at least one thread waiting on the condition variable.
Definition ThreadSupport.h:328
void unlock()
Unlock the mutex associated with the condition variable.
Definition ThreadSupport.h:315
Definition Exception.h:35
Definition ThreadSupport.h:549
void sync()
Definition ThreadSupport.cc:381
int debug
Definition ThreadSupport.h:561
Cond _co
Definition ThreadSupport.h:558
int _n
Definition ThreadSupport.h:559
int _count
Definition ThreadSupport.h:560
Multisync(int n)
Definition ThreadSupport.cc:370
void init()
Definition ThreadSupport.cc:399
A C++ wrapper for a POSIX mutex attributes.
Definition ThreadSupport.h:52
void setPShared(int val)
Set the mutex pshared attribute, one of PTHREAD_PROCESS_PRIVATE or PTHREAD_PROCESS_SHARED.
Definition ThreadSupport.cc:144
int getType() const
Definition ThreadSupport.cc:87
int getPShared() const
Definition ThreadSupport.cc:160
pthread_mutexattr_t _attrs
Definition ThreadSupport.h:118
void setType(int val)
Set the mutex type attribute, one of PTHREAD_MUTEX_NORMAL,PTHREAD_MUTEX_ERRORCHECK,...
Definition ThreadSupport.cc:71
pthread_mutexattr_t * ptr()
Definition ThreadSupport.h:113
MutexAttributes()
Create instance with default values, type = PTHREAD_MUTEX_DEFAULT, priority protocol=PTHREAD_PRIO_NON...
Definition ThreadSupport.cc:42
~MutexAttributes()
Definition ThreadSupport.cc:66
A C++ wrapper for a POSIX mutex.
Definition ThreadSupport.h:161
void lock()
Lock the Mutex.
Definition ThreadSupport.h:216
pthread_mutex_t _p_mutex
Definition ThreadSupport.h:248
Mutex(int type=PTHREAD_MUTEX_DEFAULT)
Construct a POSIX mutex of the given type.
Definition ThreadSupport.cc:212
MutexAttributes _attrs
Definition ThreadSupport.h:250
pthread_mutex_t * ptr()
Get the pointer to the pthread_mutex_t.
Definition ThreadSupport.cc:258
void unlock()
Unlock the Mutex.
Definition ThreadSupport.h:230
~Mutex()
Destruct a Mutex.
Definition ThreadSupport.cc:240
Mutex & operator=(const Mutex &)
No assignment allowed.
A C++ wrapper for a POSIX rwlock attributes.
Definition ThreadSupport.h:127
pthread_rwlockattr_t * ptr()
Definition ThreadSupport.h:149
void setPShared(int val)
Set the mutex pshared attribute, one of PTHREAD_PROCESS_PRIVATE or PTHREAD_PROCESS_SHARED.
Definition ThreadSupport.cc:189
~RWLockAttributes()
Definition ThreadSupport.cc:184
int getPShared() const
Definition ThreadSupport.cc:205
RWLockAttributes()
Definition ThreadSupport.cc:167
pthread_rwlockattr_t _attrs
Definition ThreadSupport.h:154
A C++ wrapper for a POSIX rwlock.
Definition ThreadSupport.h:379
void unlock()
Unlock the RWLock.
Definition ThreadSupport.h:437
RWLockAttributes _attrs
Definition ThreadSupport.h:457
~RWLock()
Destruct a RWLock.
Definition ThreadSupport.cc:349
RWLock & operator=(const RWLock &)
No assignment allowed.
void wrlock()
Acquire a write lock.
Definition ThreadSupport.h:424
void rdlock()
Acquire a read lock.
Definition ThreadSupport.h:411
RWLock()
Construct a POSIX rwlock.
Definition ThreadSupport.cc:320
pthread_rwlock_t _p_rwlock
Definition ThreadSupport.h:455
pthread_rwlock_t * ptr()
Get the pointer to the pthread_rwlock_t.
Definition ThreadSupport.cc:366
A POSIX semaphore.
Definition ThreadSupport.h:465
Semaphore & operator=(const Semaphore &)
No assignment allowed.
void wait()
Suspend calling thread until the semaphore has a non-zero count.
Definition ThreadSupport.h:489
int getValue()
Get the current value of the Semaphore.
Definition ThreadSupport.h:521
void post()
Atomically increment the Semaphore.
Definition ThreadSupport.h:507
sem_t _sem
Definition ThreadSupport.h:545
bool check()
Do a non-blocking wait on the Semaphore.
Definition ThreadSupport.h:499
sem_t * ptr()
Get the pointer to the semaphore (for legacy C code).
Definition ThreadSupport.h:531
Semaphore()
Constructor.
Definition ThreadSupport.h:471
Semaphore(const Semaphore &)
No copying.
~Semaphore()
Destructor.
Definition ThreadSupport.h:480
Synchronized is used a simple guard object for critical sections.
Definition ThreadSupport.h:575
Cond * condp
Definition ThreadSupport.h:610
Mutex * mutexp
Definition ThreadSupport.h:609
Synchronized(Mutex &mutex_)
Definition ThreadSupport.h:594
Synchronized & operator=(const Synchronized &)
~Synchronized()
On destruction, unlock the lock.
Definition ThreadSupport.h:602
Synchronized(Cond &cond_)
Construct the guard object and lock() the lock.
Definition ThreadSupport.h:586
Synchronized(const Synchronized &)
Root namespace for the NCAR In-Situ Data Acquisition Software.
Definition A2DConverter.h:31