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 | |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 31 | #include <chrono> |
| 32 | #include <mutex> |
| 33 | #include <condition_variable> |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 34 | #include <pthread.h> |
| 35 | #include <iostream> |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 36 | #include <cassert> |
kurtis.heimerl | 9123274 | 2012-07-22 05:09:00 +0000 | [diff] [blame] | 37 | #include <unistd.h> |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 38 | |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 39 | #include "config.h" |
| 40 | #include "Timeval.h" |
| 41 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 42 | class Mutex; |
| 43 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 44 | /**@defgroup C++ wrappers for pthread mechanisms. */ |
| 45 | //@{ |
| 46 | |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 47 | /** A class for recursive mutexes. */ |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 48 | class Mutex { |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 49 | std::recursive_mutex m; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 50 | |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 51 | public: |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 52 | |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 53 | void lock() { |
| 54 | m.lock(); |
| 55 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 56 | |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 57 | bool trylock() { |
| 58 | return m.try_lock(); |
| 59 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 60 | |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 61 | void unlock() { |
| 62 | m.unlock(); |
| 63 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 64 | |
| 65 | friend class Signal; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 68 | class ScopedLock { |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 69 | Mutex &mMutex; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 70 | |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 71 | public: |
| 72 | ScopedLock(Mutex &wMutex) : mMutex(wMutex) { |
| 73 | mMutex.lock(); |
| 74 | } |
| 75 | ~ScopedLock() { |
| 76 | mMutex.unlock(); |
| 77 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 80 | /** A C++ interthread signal. */ |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 81 | class Signal { |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 82 | /* any, because for some reason our mutex is recursive... */ |
| 83 | std::condition_variable_any mSignal; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 84 | |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 85 | public: |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 86 | |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 87 | void wait(Mutex &wMutex, unsigned timeout) { |
| 88 | mSignal.wait_for(wMutex.m, std::chrono::milliseconds(timeout)); |
| 89 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 90 | |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 91 | void wait(Mutex &wMutex) { |
| 92 | mSignal.wait(wMutex.m); |
| 93 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 94 | |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 95 | void signal() { |
| 96 | mSignal.notify_one(); |
| 97 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 98 | |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 99 | void broadcast() { |
| 100 | mSignal.notify_all(); |
| 101 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
Pau Espin Pedrol | 5b60c98 | 2018-09-20 18:04:46 +0200 | [diff] [blame] | 104 | void set_selfthread_name(const char *name); |
Pau Espin Pedrol | 75cb0b9 | 2019-04-25 19:33:58 +0200 | [diff] [blame] | 105 | void thread_enable_cancel(bool cancel); |
Pau Espin Pedrol | 5b60c98 | 2018-09-20 18:04:46 +0200 | [diff] [blame] | 106 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 107 | /** A C++ wrapper for pthread threads. */ |
| 108 | class Thread { |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 109 | private: |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 110 | pthread_t mThread; |
| 111 | pthread_attr_t mAttrib; |
| 112 | // FIXME -- Can this be reduced now? |
| 113 | size_t mStackSize; |
Pau Espin Pedrol | 46324d3 | 2019-04-25 19:33:11 +0200 | [diff] [blame] | 114 | |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 115 | public: |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 116 | /** Create a thread in a non-running state. */ |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 117 | Thread(size_t wStackSize = 0) : mThread((pthread_t)0) |
| 118 | { |
| 119 | pthread_attr_init(&mAttrib); // (pat) moved this here. |
| 120 | mStackSize = wStackSize; |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 121 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | Destroy the Thread. |
| 125 | It should be stopped and joined. |
| 126 | */ |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 127 | // (pat) If the Thread is destroyed without being started, then mAttrib is undefined. Oops. |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 128 | ~Thread() |
| 129 | { |
| 130 | pthread_attr_destroy(&mAttrib); |
| 131 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 132 | |
| 133 | /** Start the thread on a task. */ |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 134 | void start(void *(*task)(void *), void *arg); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 135 | |
| 136 | /** Join a thread that will stop on its own. */ |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 137 | void join() |
| 138 | { |
Tom Tsou | b999759 | 2014-11-21 12:25:22 -0800 | [diff] [blame] | 139 | if (mThread) { |
| 140 | int s = pthread_join(mThread, NULL); |
| 141 | assert(!s); |
| 142 | } |
| 143 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 144 | |
Martin Hauke | 066fd04 | 2019-10-13 19:08:00 +0200 | [diff] [blame] | 145 | /** Send cancellation to thread */ |
Eric | 5561f11 | 2022-07-19 21:18:21 +0200 | [diff] [blame] | 146 | void cancel() |
| 147 | { |
| 148 | pthread_cancel(mThread); |
| 149 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
Pau Espin Pedrol | e503c98 | 2019-09-13 18:56:08 +0200 | [diff] [blame] | 152 | #ifdef HAVE_ATOMIC_OPS |
| 153 | #define osmo_trx_sync_fetch_and_and(ptr, value) __sync_fetch_and_and((ptr), (value)) |
| 154 | #define osmo_trx_sync_or_and_fetch(ptr, value) __sync_or_and_fetch((ptr), (value)) |
| 155 | #else |
| 156 | extern pthread_mutex_t atomic_ops_mutex; |
| 157 | static inline int osmo_trx_sync_fetch_and_and(int *ptr, int value) |
| 158 | { |
| 159 | pthread_mutex_lock(&atomic_ops_mutex); |
| 160 | int tmp = *ptr; |
| 161 | *ptr &= value; |
| 162 | pthread_mutex_unlock(&atomic_ops_mutex); |
| 163 | return tmp; |
| 164 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 165 | |
Pau Espin Pedrol | e503c98 | 2019-09-13 18:56:08 +0200 | [diff] [blame] | 166 | static inline int osmo_trx_sync_or_and_fetch(int *ptr, int value) |
| 167 | { |
| 168 | int tmp; |
| 169 | pthread_mutex_lock(&atomic_ops_mutex); |
| 170 | *ptr |= value; |
| 171 | tmp = *ptr; |
| 172 | pthread_mutex_unlock(&atomic_ops_mutex); |
| 173 | return tmp; |
| 174 | } |
| 175 | #endif |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 176 | |
| 177 | #endif |
| 178 | // vim: ts=4 sw=4 |