dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008, 2011 Free Software Foundation, Inc. |
| 3 | * |
Pau Espin Pedrol | 21d03d3 | 2019-07-22 12:05:52 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: AGPL-3.0+ |
| 5 | * |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 6 | * This software is distributed under the terms of the GNU Affero Public License. |
| 7 | * See the COPYING file in the main directory for details. |
| 8 | * |
| 9 | * This use of this software may be subject to additional restrictions. |
| 10 | * See the LEGAL file in the main directory for details. |
| 11 | |
| 12 | This program is free software: you can redistribute it and/or modify |
| 13 | it under the terms of the GNU Affero General Public License as published by |
| 14 | the Free Software Foundation, either version 3 of the License, or |
| 15 | (at your option) any later version. |
| 16 | |
| 17 | This program is distributed in the hope that it will be useful, |
| 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | GNU Affero General Public License for more details. |
| 21 | |
| 22 | You should have received a copy of the GNU Affero General Public License |
| 23 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | #ifndef THREADS_H |
| 29 | #define THREADS_H |
| 30 | |
Pau Espin Pedrol | e503c98 | 2019-09-13 18:56:08 +0200 | [diff] [blame] | 31 | #include "config.h" |
| 32 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 33 | #include <pthread.h> |
| 34 | #include <iostream> |
| 35 | #include <assert.h> |
kurtis.heimerl | 9123274 | 2012-07-22 05:09:00 +0000 | [diff] [blame] | 36 | #include <unistd.h> |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 37 | |
| 38 | class Mutex; |
| 39 | |
| 40 | |
| 41 | /**@name Multithreaded access for standard streams. */ |
| 42 | //@{ |
| 43 | |
| 44 | /**@name Functions for gStreamLock. */ |
| 45 | //@{ |
| 46 | extern Mutex gStreamLock; ///< global lock for cout and cerr |
| 47 | void lockCerr(); ///< call prior to writing cerr |
| 48 | void unlockCerr(); ///< call after writing cerr |
| 49 | void lockCout(); ///< call prior to writing cout |
| 50 | void unlockCout(); ///< call after writing cout |
| 51 | //@} |
| 52 | |
| 53 | /**@name Macros for standard messages. */ |
| 54 | //@{ |
| 55 | #define COUT(text) { lockCout(); std::cout << text; unlockCout(); } |
| 56 | #define CERR(text) { lockCerr(); std::cerr << __FILE__ << ":" << __LINE__ << ": " << text; unlockCerr(); } |
| 57 | #ifdef NDEBUG |
| 58 | #define DCOUT(text) {} |
| 59 | #define OBJDCOUT(text) {} |
| 60 | #else |
| 61 | #define DCOUT(text) { COUT(__FILE__ << ":" << __LINE__ << " " << text); } |
Pau Espin Pedrol | 46324d3 | 2019-04-25 19:33:11 +0200 | [diff] [blame] | 62 | #define OBJDCOUT(text) { DCOUT(this << " " << text); } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 63 | #endif |
| 64 | //@} |
| 65 | //@} |
| 66 | |
| 67 | |
| 68 | |
| 69 | /**@defgroup C++ wrappers for pthread mechanisms. */ |
| 70 | //@{ |
| 71 | |
| 72 | /** A class for recursive mutexes based on pthread_mutex. */ |
| 73 | class Mutex { |
| 74 | |
| 75 | private: |
| 76 | |
| 77 | pthread_mutex_t mMutex; |
| 78 | pthread_mutexattr_t mAttribs; |
| 79 | |
| 80 | public: |
| 81 | |
| 82 | Mutex(); |
| 83 | |
| 84 | ~Mutex(); |
| 85 | |
| 86 | void lock() { pthread_mutex_lock(&mMutex); } |
| 87 | |
kurtis.heimerl | c9f268d | 2012-11-22 05:30:27 +0000 | [diff] [blame] | 88 | bool trylock() { return pthread_mutex_trylock(&mMutex)==0; } |
| 89 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 90 | void unlock() { pthread_mutex_unlock(&mMutex); } |
| 91 | |
| 92 | friend class Signal; |
| 93 | |
| 94 | }; |
| 95 | |
| 96 | |
| 97 | class ScopedLock { |
| 98 | |
| 99 | private: |
| 100 | Mutex& mMutex; |
| 101 | |
| 102 | public: |
| 103 | ScopedLock(Mutex& wMutex) :mMutex(wMutex) { mMutex.lock(); } |
| 104 | ~ScopedLock() { mMutex.unlock(); } |
| 105 | |
| 106 | }; |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | /** A C++ interthread signal based on pthread condition variables. */ |
| 112 | class Signal { |
| 113 | |
| 114 | private: |
| 115 | |
| 116 | mutable pthread_cond_t mSignal; |
| 117 | |
| 118 | public: |
| 119 | |
| 120 | Signal() { int s = pthread_cond_init(&mSignal,NULL); assert(!s); } |
| 121 | |
| 122 | ~Signal() { pthread_cond_destroy(&mSignal); } |
| 123 | |
| 124 | /** |
| 125 | Block for the signal up to the cancellation timeout. |
| 126 | Under Linux, spurious returns are possible. |
| 127 | */ |
| 128 | void wait(Mutex& wMutex, unsigned timeout) const; |
| 129 | |
| 130 | /** |
| 131 | Block for the signal. |
| 132 | Under Linux, spurious returns are possible. |
| 133 | */ |
| 134 | void wait(Mutex& wMutex) const |
| 135 | { pthread_cond_wait(&mSignal,&wMutex.mMutex); } |
| 136 | |
| 137 | void signal() { pthread_cond_signal(&mSignal); } |
| 138 | |
| 139 | void broadcast() { pthread_cond_broadcast(&mSignal); } |
| 140 | |
| 141 | }; |
| 142 | |
| 143 | |
| 144 | |
| 145 | #define START_THREAD(thread,function,argument) \ |
| 146 | thread.start((void *(*)(void*))function, (void*)argument); |
| 147 | |
Pau Espin Pedrol | 5b60c98 | 2018-09-20 18:04:46 +0200 | [diff] [blame] | 148 | void set_selfthread_name(const char *name); |
Pau Espin Pedrol | 75cb0b9 | 2019-04-25 19:33:58 +0200 | [diff] [blame] | 149 | void thread_enable_cancel(bool cancel); |
Pau Espin Pedrol | 5b60c98 | 2018-09-20 18:04:46 +0200 | [diff] [blame] | 150 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 151 | /** A C++ wrapper for pthread threads. */ |
| 152 | class Thread { |
| 153 | |
| 154 | private: |
| 155 | |
| 156 | pthread_t mThread; |
| 157 | pthread_attr_t mAttrib; |
| 158 | // FIXME -- Can this be reduced now? |
| 159 | size_t mStackSize; |
Pau Espin Pedrol | 46324d3 | 2019-04-25 19:33:11 +0200 | [diff] [blame] | 160 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 161 | |
| 162 | public: |
| 163 | |
| 164 | /** Create a thread in a non-running state. */ |
Eric Wild | ac0487e | 2019-06-17 13:02:44 +0200 | [diff] [blame] | 165 | Thread(size_t wStackSize = 0):mThread((pthread_t)0) { |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 166 | pthread_attr_init(&mAttrib); // (pat) moved this here. |
| 167 | mStackSize=wStackSize; |
| 168 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | Destroy the Thread. |
| 172 | It should be stopped and joined. |
| 173 | */ |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 174 | // (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] | 175 | ~Thread() { pthread_attr_destroy(&mAttrib); } |
| 176 | |
| 177 | |
| 178 | /** Start the thread on a task. */ |
| 179 | void start(void *(*task)(void*), void *arg); |
| 180 | |
| 181 | /** Join a thread that will stop on its own. */ |
Tom Tsou | b999759 | 2014-11-21 12:25:22 -0800 | [diff] [blame] | 182 | void join() { |
| 183 | if (mThread) { |
| 184 | int s = pthread_join(mThread, NULL); |
| 185 | assert(!s); |
| 186 | } |
| 187 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 188 | |
Martin Hauke | 066fd04 | 2019-10-13 19:08:00 +0200 | [diff] [blame] | 189 | /** Send cancellation to thread */ |
Tom Tsou | b999759 | 2014-11-21 12:25:22 -0800 | [diff] [blame] | 190 | void cancel() { pthread_cancel(mThread); } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
Pau Espin Pedrol | e503c98 | 2019-09-13 18:56:08 +0200 | [diff] [blame] | 193 | #ifdef HAVE_ATOMIC_OPS |
| 194 | #define osmo_trx_sync_fetch_and_and(ptr, value) __sync_fetch_and_and((ptr), (value)) |
| 195 | #define osmo_trx_sync_or_and_fetch(ptr, value) __sync_or_and_fetch((ptr), (value)) |
| 196 | #else |
| 197 | extern pthread_mutex_t atomic_ops_mutex; |
| 198 | static inline int osmo_trx_sync_fetch_and_and(int *ptr, int value) |
| 199 | { |
| 200 | pthread_mutex_lock(&atomic_ops_mutex); |
| 201 | int tmp = *ptr; |
| 202 | *ptr &= value; |
| 203 | pthread_mutex_unlock(&atomic_ops_mutex); |
| 204 | return tmp; |
| 205 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 206 | |
Pau Espin Pedrol | e503c98 | 2019-09-13 18:56:08 +0200 | [diff] [blame] | 207 | static inline int osmo_trx_sync_or_and_fetch(int *ptr, int value) |
| 208 | { |
| 209 | int tmp; |
| 210 | pthread_mutex_lock(&atomic_ops_mutex); |
| 211 | *ptr |= value; |
| 212 | tmp = *ptr; |
| 213 | pthread_mutex_unlock(&atomic_ops_mutex); |
| 214 | return tmp; |
| 215 | } |
| 216 | #endif |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 217 | |
| 218 | #endif |
| 219 | // vim: ts=4 sw=4 |