blob: c61a3d8a690bcb870909eea516a9294ebcdab797 [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
2* Copyright 2008, 2011 Free Software Foundation, Inc.
3*
4* This software is distributed under the terms of the GNU Affero Public License.
5* See the COPYING file in the main directory for details.
6*
7* This use of this software may be subject to additional restrictions.
8* See the LEGAL file in the main directory for details.
9
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU Affero General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Affero General Public License for more details.
19
20 You should have received a copy of the GNU Affero General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23*/
24
25
26#ifndef THREADS_H
27#define THREADS_H
28
29#include <pthread.h>
30#include <iostream>
31#include <assert.h>
kurtis.heimerl91232742012-07-22 05:09:00 +000032#include <unistd.h>
dburgess82c46ff2011-10-07 02:40:51 +000033
34class Mutex;
35
36
37/**@name Multithreaded access for standard streams. */
38//@{
39
40/**@name Functions for gStreamLock. */
41//@{
42extern Mutex gStreamLock; ///< global lock for cout and cerr
43void lockCerr(); ///< call prior to writing cerr
44void unlockCerr(); ///< call after writing cerr
45void lockCout(); ///< call prior to writing cout
46void unlockCout(); ///< call after writing cout
47//@}
48
49/**@name Macros for standard messages. */
50//@{
51#define COUT(text) { lockCout(); std::cout << text; unlockCout(); }
52#define CERR(text) { lockCerr(); std::cerr << __FILE__ << ":" << __LINE__ << ": " << text; unlockCerr(); }
53#ifdef NDEBUG
54#define DCOUT(text) {}
55#define OBJDCOUT(text) {}
56#else
57#define DCOUT(text) { COUT(__FILE__ << ":" << __LINE__ << " " << text); }
58#define OBJDCOUT(text) { DCOUT(this << " " << text); }
59#endif
60//@}
61//@}
62
63
64
65/**@defgroup C++ wrappers for pthread mechanisms. */
66//@{
67
68/** A class for recursive mutexes based on pthread_mutex. */
69class Mutex {
70
71 private:
72
73 pthread_mutex_t mMutex;
74 pthread_mutexattr_t mAttribs;
75
76 public:
77
78 Mutex();
79
80 ~Mutex();
81
82 void lock() { pthread_mutex_lock(&mMutex); }
83
84 void unlock() { pthread_mutex_unlock(&mMutex); }
85
86 friend class Signal;
87
88};
89
90
91class ScopedLock {
92
93 private:
94 Mutex& mMutex;
95
96 public:
97 ScopedLock(Mutex& wMutex) :mMutex(wMutex) { mMutex.lock(); }
98 ~ScopedLock() { mMutex.unlock(); }
99
100};
101
102
103
104
105/** A C++ interthread signal based on pthread condition variables. */
106class Signal {
107
108 private:
109
110 mutable pthread_cond_t mSignal;
111
112 public:
113
114 Signal() { int s = pthread_cond_init(&mSignal,NULL); assert(!s); }
115
116 ~Signal() { pthread_cond_destroy(&mSignal); }
117
118 /**
119 Block for the signal up to the cancellation timeout.
120 Under Linux, spurious returns are possible.
121 */
122 void wait(Mutex& wMutex, unsigned timeout) const;
123
124 /**
125 Block for the signal.
126 Under Linux, spurious returns are possible.
127 */
128 void wait(Mutex& wMutex) const
129 { pthread_cond_wait(&mSignal,&wMutex.mMutex); }
130
131 void signal() { pthread_cond_signal(&mSignal); }
132
133 void broadcast() { pthread_cond_broadcast(&mSignal); }
134
135};
136
137
138
139#define START_THREAD(thread,function,argument) \
140 thread.start((void *(*)(void*))function, (void*)argument);
141
142/** A C++ wrapper for pthread threads. */
143class Thread {
144
145 private:
146
147 pthread_t mThread;
148 pthread_attr_t mAttrib;
149 // FIXME -- Can this be reduced now?
150 size_t mStackSize;
151
152
153 public:
154
155 /** Create a thread in a non-running state. */
156 Thread(size_t wStackSize = (65536*4)):mThread((pthread_t)0) { mStackSize=wStackSize;}
157
158 /**
159 Destroy the Thread.
160 It should be stopped and joined.
161 */
162 ~Thread() { pthread_attr_destroy(&mAttrib); }
163
164
165 /** Start the thread on a task. */
166 void start(void *(*task)(void*), void *arg);
167
168 /** Join a thread that will stop on its own. */
169 void join() { int s = pthread_join(mThread,NULL); assert(!s); }
170
171};
172
173
174
175
176#endif
177// vim: ts=4 sw=4