blob: 5ff137bc01be4ebc64e9795bcb66413e29da20d0 [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
2* Copyright 2008, 2011 Free Software Foundation, Inc.
3*
Pau Espin Pedrol21d03d32019-07-22 12:05:52 +02004* SPDX-License-Identifier: AGPL-3.0+
5*
dburgess82c46ff2011-10-07 02:40:51 +00006* 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 Pedrole503c982019-09-13 18:56:08 +020031#include "config.h"
32
dburgess82c46ff2011-10-07 02:40:51 +000033#include <pthread.h>
34#include <iostream>
35#include <assert.h>
kurtis.heimerl91232742012-07-22 05:09:00 +000036#include <unistd.h>
dburgess82c46ff2011-10-07 02:40:51 +000037
38class Mutex;
39
40
41/**@name Multithreaded access for standard streams. */
42//@{
43
44/**@name Functions for gStreamLock. */
45//@{
46extern Mutex gStreamLock; ///< global lock for cout and cerr
47void lockCerr(); ///< call prior to writing cerr
48void unlockCerr(); ///< call after writing cerr
49void lockCout(); ///< call prior to writing cout
50void 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 Pedrol46324d32019-04-25 19:33:11 +020062#define OBJDCOUT(text) { DCOUT(this << " " << text); }
dburgess82c46ff2011-10-07 02:40:51 +000063#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. */
73class 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.heimerlc9f268d2012-11-22 05:30:27 +000088 bool trylock() { return pthread_mutex_trylock(&mMutex)==0; }
89
dburgess82c46ff2011-10-07 02:40:51 +000090 void unlock() { pthread_mutex_unlock(&mMutex); }
91
92 friend class Signal;
93
94};
95
96
97class 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. */
112class 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 Pedrol5b60c982018-09-20 18:04:46 +0200148void set_selfthread_name(const char *name);
Pau Espin Pedrol75cb0b92019-04-25 19:33:58 +0200149void thread_enable_cancel(bool cancel);
Pau Espin Pedrol5b60c982018-09-20 18:04:46 +0200150
dburgess82c46ff2011-10-07 02:40:51 +0000151/** A C++ wrapper for pthread threads. */
152class 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 Pedrol46324d32019-04-25 19:33:11 +0200160
dburgess82c46ff2011-10-07 02:40:51 +0000161
162 public:
163
164 /** Create a thread in a non-running state. */
Eric Wildac0487e2019-06-17 13:02:44 +0200165 Thread(size_t wStackSize = 0):mThread((pthread_t)0) {
kurtis.heimerl5a872472013-05-31 21:47:25 +0000166 pthread_attr_init(&mAttrib); // (pat) moved this here.
167 mStackSize=wStackSize;
168 }
dburgess82c46ff2011-10-07 02:40:51 +0000169
170 /**
171 Destroy the Thread.
172 It should be stopped and joined.
173 */
kurtis.heimerl5a872472013-05-31 21:47:25 +0000174 // (pat) If the Thread is destroyed without being started, then mAttrib is undefined. Oops.
dburgess82c46ff2011-10-07 02:40:51 +0000175 ~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 Tsoub9997592014-11-21 12:25:22 -0800182 void join() {
183 if (mThread) {
184 int s = pthread_join(mThread, NULL);
185 assert(!s);
186 }
187 }
dburgess82c46ff2011-10-07 02:40:51 +0000188
Martin Hauke066fd042019-10-13 19:08:00 +0200189 /** Send cancellation to thread */
Tom Tsoub9997592014-11-21 12:25:22 -0800190 void cancel() { pthread_cancel(mThread); }
dburgess82c46ff2011-10-07 02:40:51 +0000191};
192
Pau Espin Pedrole503c982019-09-13 18:56:08 +0200193#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
197extern pthread_mutex_t atomic_ops_mutex;
198static 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}
dburgess82c46ff2011-10-07 02:40:51 +0000206
Pau Espin Pedrole503c982019-09-13 18:56:08 +0200207static 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
dburgess82c46ff2011-10-07 02:40:51 +0000217
218#endif
219// vim: ts=4 sw=4