dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 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.heimerl | 9123274 | 2012-07-22 05:09:00 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 33 | |
| 34 | class Mutex; |
| 35 | |
| 36 | |
| 37 | /**@name Multithreaded access for standard streams. */ |
| 38 | //@{ |
| 39 | |
| 40 | /**@name Functions for gStreamLock. */ |
| 41 | //@{ |
| 42 | extern Mutex gStreamLock; ///< global lock for cout and cerr |
| 43 | void lockCerr(); ///< call prior to writing cerr |
| 44 | void unlockCerr(); ///< call after writing cerr |
| 45 | void lockCout(); ///< call prior to writing cout |
| 46 | void 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. */ |
| 69 | class 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.heimerl | c9f268d | 2012-11-22 05:30:27 +0000 | [diff] [blame] | 84 | bool trylock() { return pthread_mutex_trylock(&mMutex)==0; } |
| 85 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 86 | void unlock() { pthread_mutex_unlock(&mMutex); } |
| 87 | |
| 88 | friend class Signal; |
| 89 | |
| 90 | }; |
| 91 | |
| 92 | |
| 93 | class 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. */ |
| 108 | class 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. */ |
| 145 | class 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.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 158 | Thread(size_t wStackSize = (65536*4)):mThread((pthread_t)0) { |
| 159 | pthread_attr_init(&mAttrib); // (pat) moved this here. |
| 160 | mStackSize=wStackSize; |
| 161 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 162 | |
| 163 | /** |
| 164 | Destroy the Thread. |
| 165 | It should be stopped and joined. |
| 166 | */ |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 167 | // (pat) If the Thread is destroyed without being started, then mAttrib is undefined. Oops. |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 168 | ~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 Tsou | b999759 | 2014-11-21 12:25:22 -0800 | [diff] [blame] | 175 | void join() { |
| 176 | if (mThread) { |
| 177 | int s = pthread_join(mThread, NULL); |
| 178 | assert(!s); |
| 179 | } |
| 180 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 181 | |
Tom Tsou | b999759 | 2014-11-21 12:25:22 -0800 | [diff] [blame] | 182 | /** Send cancelation to thread */ |
| 183 | void cancel() { pthread_cancel(mThread); } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | #endif |
| 190 | // vim: ts=4 sw=4 |