blob: 24fec00249014a9e10a99f05c844572a1b21d7e2 [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
Eric5561f112022-07-19 21:18:21 +020031#include <chrono>
32#include <mutex>
33#include <condition_variable>
dburgess82c46ff2011-10-07 02:40:51 +000034#include <pthread.h>
35#include <iostream>
Eric5561f112022-07-19 21:18:21 +020036#include <cassert>
kurtis.heimerl91232742012-07-22 05:09:00 +000037#include <unistd.h>
dburgess82c46ff2011-10-07 02:40:51 +000038
Eric5561f112022-07-19 21:18:21 +020039#include "config.h"
40#include "Timeval.h"
41
dburgess82c46ff2011-10-07 02:40:51 +000042class Mutex;
43
dburgess82c46ff2011-10-07 02:40:51 +000044/**@defgroup C++ wrappers for pthread mechanisms. */
45//@{
46
Eric5561f112022-07-19 21:18:21 +020047/** A class for recursive mutexes. */
dburgess82c46ff2011-10-07 02:40:51 +000048class Mutex {
Eric5561f112022-07-19 21:18:21 +020049 std::recursive_mutex m;
dburgess82c46ff2011-10-07 02:40:51 +000050
Eric5561f112022-07-19 21:18:21 +020051 public:
dburgess82c46ff2011-10-07 02:40:51 +000052
Eric5561f112022-07-19 21:18:21 +020053 void lock() {
54 m.lock();
55 }
dburgess82c46ff2011-10-07 02:40:51 +000056
Eric5561f112022-07-19 21:18:21 +020057 bool trylock() {
58 return m.try_lock();
59 }
dburgess82c46ff2011-10-07 02:40:51 +000060
Eric5561f112022-07-19 21:18:21 +020061 void unlock() {
62 m.unlock();
63 }
dburgess82c46ff2011-10-07 02:40:51 +000064
65 friend class Signal;
dburgess82c46ff2011-10-07 02:40:51 +000066};
67
dburgess82c46ff2011-10-07 02:40:51 +000068class ScopedLock {
Eric5561f112022-07-19 21:18:21 +020069 Mutex &mMutex;
dburgess82c46ff2011-10-07 02:40:51 +000070
Eric5561f112022-07-19 21:18:21 +020071 public:
72 ScopedLock(Mutex &wMutex) : mMutex(wMutex) {
73 mMutex.lock();
74 }
75 ~ScopedLock() {
76 mMutex.unlock();
77 }
dburgess82c46ff2011-10-07 02:40:51 +000078};
79
Eric5561f112022-07-19 21:18:21 +020080/** A C++ interthread signal. */
dburgess82c46ff2011-10-07 02:40:51 +000081class Signal {
Eric5561f112022-07-19 21:18:21 +020082 /* any, because for some reason our mutex is recursive... */
83 std::condition_variable_any mSignal;
dburgess82c46ff2011-10-07 02:40:51 +000084
Eric5561f112022-07-19 21:18:21 +020085 public:
dburgess82c46ff2011-10-07 02:40:51 +000086
Eric5561f112022-07-19 21:18:21 +020087 void wait(Mutex &wMutex, unsigned timeout) {
88 mSignal.wait_for(wMutex.m, std::chrono::milliseconds(timeout));
89 }
dburgess82c46ff2011-10-07 02:40:51 +000090
Eric5561f112022-07-19 21:18:21 +020091 void wait(Mutex &wMutex) {
92 mSignal.wait(wMutex.m);
93 }
dburgess82c46ff2011-10-07 02:40:51 +000094
Eric5561f112022-07-19 21:18:21 +020095 void signal() {
96 mSignal.notify_one();
97 }
dburgess82c46ff2011-10-07 02:40:51 +000098
Eric5561f112022-07-19 21:18:21 +020099 void broadcast() {
100 mSignal.notify_all();
101 }
dburgess82c46ff2011-10-07 02:40:51 +0000102};
103
Pau Espin Pedrol5b60c982018-09-20 18:04:46 +0200104void set_selfthread_name(const char *name);
Pau Espin Pedrol75cb0b92019-04-25 19:33:58 +0200105void thread_enable_cancel(bool cancel);
Pau Espin Pedrol5b60c982018-09-20 18:04:46 +0200106
dburgess82c46ff2011-10-07 02:40:51 +0000107/** A C++ wrapper for pthread threads. */
108class Thread {
Eric5561f112022-07-19 21:18:21 +0200109 private:
dburgess82c46ff2011-10-07 02:40:51 +0000110 pthread_t mThread;
111 pthread_attr_t mAttrib;
112 // FIXME -- Can this be reduced now?
113 size_t mStackSize;
Pau Espin Pedrol46324d32019-04-25 19:33:11 +0200114
Eric5561f112022-07-19 21:18:21 +0200115 public:
dburgess82c46ff2011-10-07 02:40:51 +0000116 /** Create a thread in a non-running state. */
Eric5561f112022-07-19 21:18:21 +0200117 Thread(size_t wStackSize = 0) : mThread((pthread_t)0)
118 {
119 pthread_attr_init(&mAttrib); // (pat) moved this here.
120 mStackSize = wStackSize;
kurtis.heimerl5a872472013-05-31 21:47:25 +0000121 }
dburgess82c46ff2011-10-07 02:40:51 +0000122
123 /**
124 Destroy the Thread.
125 It should be stopped and joined.
126 */
kurtis.heimerl5a872472013-05-31 21:47:25 +0000127 // (pat) If the Thread is destroyed without being started, then mAttrib is undefined. Oops.
Eric5561f112022-07-19 21:18:21 +0200128 ~Thread()
129 {
130 pthread_attr_destroy(&mAttrib);
131 }
dburgess82c46ff2011-10-07 02:40:51 +0000132
133 /** Start the thread on a task. */
Eric5561f112022-07-19 21:18:21 +0200134 void start(void *(*task)(void *), void *arg);
dburgess82c46ff2011-10-07 02:40:51 +0000135
136 /** Join a thread that will stop on its own. */
Eric5561f112022-07-19 21:18:21 +0200137 void join()
138 {
Tom Tsoub9997592014-11-21 12:25:22 -0800139 if (mThread) {
140 int s = pthread_join(mThread, NULL);
141 assert(!s);
142 }
143 }
dburgess82c46ff2011-10-07 02:40:51 +0000144
Martin Hauke066fd042019-10-13 19:08:00 +0200145 /** Send cancellation to thread */
Eric5561f112022-07-19 21:18:21 +0200146 void cancel()
147 {
148 pthread_cancel(mThread);
149 }
dburgess82c46ff2011-10-07 02:40:51 +0000150};
151
Pau Espin Pedrole503c982019-09-13 18:56:08 +0200152#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
156extern pthread_mutex_t atomic_ops_mutex;
157static 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}
dburgess82c46ff2011-10-07 02:40:51 +0000165
Pau Espin Pedrole503c982019-09-13 18:56:08 +0200166static 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
dburgess82c46ff2011-10-07 02:40:51 +0000176
177#endif
178// vim: ts=4 sw=4