blob: 47c72751757c1d00bb06543cb91b6aeee1bd745a [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
kurtis.heimerlc9f268d2012-11-22 05:30:27 +000084 bool trylock() { return pthread_mutex_trylock(&mMutex)==0; }
85
dburgess82c46ff2011-10-07 02:40:51 +000086 void unlock() { pthread_mutex_unlock(&mMutex); }
87
88 friend class Signal;
89
90};
91
92
93class ScopedLock {
94
95 private:
96 Mutex& mMutex;
97
98 public:
99 ScopedLock(Mutex& wMutex) :mMutex(wMutex) { mMutex.lock(); }
100 ~ScopedLock() { mMutex.unlock(); }
101
102};
103
104
105
106
107/** A C++ interthread signal based on pthread condition variables. */
108class Signal {
109
110 private:
111
112 mutable pthread_cond_t mSignal;
113
114 public:
115
116 Signal() { int s = pthread_cond_init(&mSignal,NULL); assert(!s); }
117
118 ~Signal() { pthread_cond_destroy(&mSignal); }
119
120 /**
121 Block for the signal up to the cancellation timeout.
122 Under Linux, spurious returns are possible.
123 */
124 void wait(Mutex& wMutex, unsigned timeout) const;
125
126 /**
127 Block for the signal.
128 Under Linux, spurious returns are possible.
129 */
130 void wait(Mutex& wMutex) const
131 { pthread_cond_wait(&mSignal,&wMutex.mMutex); }
132
133 void signal() { pthread_cond_signal(&mSignal); }
134
135 void broadcast() { pthread_cond_broadcast(&mSignal); }
136
137};
138
139
140
141#define START_THREAD(thread,function,argument) \
142 thread.start((void *(*)(void*))function, (void*)argument);
143
144/** A C++ wrapper for pthread threads. */
145class Thread {
146
147 private:
148
149 pthread_t mThread;
150 pthread_attr_t mAttrib;
151 // FIXME -- Can this be reduced now?
152 size_t mStackSize;
153
154
155 public:
156
157 /** Create a thread in a non-running state. */
kurtis.heimerl5a872472013-05-31 21:47:25 +0000158 Thread(size_t wStackSize = (65536*4)):mThread((pthread_t)0) {
159 pthread_attr_init(&mAttrib); // (pat) moved this here.
160 mStackSize=wStackSize;
161 }
dburgess82c46ff2011-10-07 02:40:51 +0000162
163 /**
164 Destroy the Thread.
165 It should be stopped and joined.
166 */
kurtis.heimerl5a872472013-05-31 21:47:25 +0000167 // (pat) If the Thread is destroyed without being started, then mAttrib is undefined. Oops.
dburgess82c46ff2011-10-07 02:40:51 +0000168 ~Thread() { pthread_attr_destroy(&mAttrib); }
169
170
171 /** Start the thread on a task. */
172 void start(void *(*task)(void*), void *arg);
173
174 /** Join a thread that will stop on its own. */
Tom Tsoub9997592014-11-21 12:25:22 -0800175 void join() {
176 if (mThread) {
177 int s = pthread_join(mThread, NULL);
178 assert(!s);
179 }
180 }
dburgess82c46ff2011-10-07 02:40:51 +0000181
Tom Tsoub9997592014-11-21 12:25:22 -0800182 /** Send cancelation to thread */
183 void cancel() { pthread_cancel(mThread); }
dburgess82c46ff2011-10-07 02:40:51 +0000184};
185
186
187
188
189#endif
190// vim: ts=4 sw=4