blob: c1cfc750e1a3b32f30ea96535f7d4aa308912faa [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>
32
33class Mutex;
34
35
36/**@name Multithreaded access for standard streams. */
37//@{
38
39/**@name Functions for gStreamLock. */
40//@{
41extern Mutex gStreamLock; ///< global lock for cout and cerr
42void lockCerr(); ///< call prior to writing cerr
43void unlockCerr(); ///< call after writing cerr
44void lockCout(); ///< call prior to writing cout
45void unlockCout(); ///< call after writing cout
46//@}
47
48/**@name Macros for standard messages. */
49//@{
50#define COUT(text) { lockCout(); std::cout << text; unlockCout(); }
51#define CERR(text) { lockCerr(); std::cerr << __FILE__ << ":" << __LINE__ << ": " << text; unlockCerr(); }
52#ifdef NDEBUG
53#define DCOUT(text) {}
54#define OBJDCOUT(text) {}
55#else
56#define DCOUT(text) { COUT(__FILE__ << ":" << __LINE__ << " " << text); }
57#define OBJDCOUT(text) { DCOUT(this << " " << text); }
58#endif
59//@}
60//@}
61
62
63
64/**@defgroup C++ wrappers for pthread mechanisms. */
65//@{
66
67/** A class for recursive mutexes based on pthread_mutex. */
68class Mutex {
69
70 private:
71
72 pthread_mutex_t mMutex;
73 pthread_mutexattr_t mAttribs;
74
75 public:
76
77 Mutex();
78
79 ~Mutex();
80
81 void lock() { pthread_mutex_lock(&mMutex); }
82
83 void unlock() { pthread_mutex_unlock(&mMutex); }
84
85 friend class Signal;
86
87};
88
89
90class ScopedLock {
91
92 private:
93 Mutex& mMutex;
94
95 public:
96 ScopedLock(Mutex& wMutex) :mMutex(wMutex) { mMutex.lock(); }
97 ~ScopedLock() { mMutex.unlock(); }
98
99};
100
101
102
103
104/** A C++ interthread signal based on pthread condition variables. */
105class Signal {
106
107 private:
108
109 mutable pthread_cond_t mSignal;
110
111 public:
112
113 Signal() { int s = pthread_cond_init(&mSignal,NULL); assert(!s); }
114
115 ~Signal() { pthread_cond_destroy(&mSignal); }
116
117 /**
118 Block for the signal up to the cancellation timeout.
119 Under Linux, spurious returns are possible.
120 */
121 void wait(Mutex& wMutex, unsigned timeout) const;
122
123 /**
124 Block for the signal.
125 Under Linux, spurious returns are possible.
126 */
127 void wait(Mutex& wMutex) const
128 { pthread_cond_wait(&mSignal,&wMutex.mMutex); }
129
130 void signal() { pthread_cond_signal(&mSignal); }
131
132 void broadcast() { pthread_cond_broadcast(&mSignal); }
133
134};
135
136
137
138#define START_THREAD(thread,function,argument) \
139 thread.start((void *(*)(void*))function, (void*)argument);
140
141/** A C++ wrapper for pthread threads. */
142class Thread {
143
144 private:
145
146 pthread_t mThread;
147 pthread_attr_t mAttrib;
148 // FIXME -- Can this be reduced now?
149 size_t mStackSize;
150
151
152 public:
153
154 /** Create a thread in a non-running state. */
155 Thread(size_t wStackSize = (65536*4)):mThread((pthread_t)0) { mStackSize=wStackSize;}
156
157 /**
158 Destroy the Thread.
159 It should be stopped and joined.
160 */
161 ~Thread() { pthread_attr_destroy(&mAttrib); }
162
163
164 /** Start the thread on a task. */
165 void start(void *(*task)(void*), void *arg);
166
167 /** Join a thread that will stop on its own. */
168 void join() { int s = pthread_join(mThread,NULL); assert(!s); }
169
170};
171
172
173
174
175#endif
176// vim: ts=4 sw=4