dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 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+ |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 5 | * |
| 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 | |
Pau Espin Pedrol | 5b60c98 | 2018-09-20 18:04:46 +0200 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <sys/types.h> |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 30 | |
| 31 | #include "Threads.h" |
| 32 | #include "Timeval.h" |
Pau Espin Pedrol | 5b60c98 | 2018-09-20 18:04:46 +0200 | [diff] [blame] | 33 | #include "Logger.h" |
| 34 | |
Pau Espin Pedrol | 6c646c3 | 2021-03-01 16:35:35 +0100 | [diff] [blame] | 35 | extern "C" { |
Pau Espin Pedrol | 5738940 | 2021-02-17 18:25:45 +0100 | [diff] [blame] | 36 | #include <osmocom/core/thread.h> |
Pau Espin Pedrol | 6c646c3 | 2021-03-01 16:35:35 +0100 | [diff] [blame] | 37 | } |
Pau Espin Pedrol | 5738940 | 2021-02-17 18:25:45 +0100 | [diff] [blame] | 38 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 39 | using namespace std; |
| 40 | |
Pau Espin Pedrol | e503c98 | 2019-09-13 18:56:08 +0200 | [diff] [blame] | 41 | #ifndef HAVE_ATOMIC_OPS |
| 42 | pthread_mutex_t atomic_ops_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 43 | #endif |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 44 | |
| 45 | |
| 46 | Mutex gStreamLock; ///< Global lock to control access to cout and cerr. |
| 47 | |
| 48 | void lockCout() |
| 49 | { |
| 50 | gStreamLock.lock(); |
| 51 | Timeval entryTime; |
Pau Espin Pedrol | 17ce774 | 2021-03-01 13:25:28 +0100 | [diff] [blame] | 52 | cout << entryTime << " " << osmo_gettid() << ": "; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | |
| 56 | void unlockCout() |
| 57 | { |
| 58 | cout << dec << endl << flush; |
| 59 | gStreamLock.unlock(); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | void lockCerr() |
| 64 | { |
| 65 | gStreamLock.lock(); |
| 66 | Timeval entryTime; |
Pau Espin Pedrol | 17ce774 | 2021-03-01 13:25:28 +0100 | [diff] [blame] | 67 | cerr << entryTime << " " << osmo_gettid() << ": "; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void unlockCerr() |
| 71 | { |
| 72 | cerr << dec << endl << flush; |
| 73 | gStreamLock.unlock(); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | Mutex::Mutex() |
| 83 | { |
| 84 | bool res; |
| 85 | res = pthread_mutexattr_init(&mAttribs); |
| 86 | assert(!res); |
| 87 | res = pthread_mutexattr_settype(&mAttribs,PTHREAD_MUTEX_RECURSIVE); |
| 88 | assert(!res); |
| 89 | res = pthread_mutex_init(&mMutex,&mAttribs); |
| 90 | assert(!res); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | Mutex::~Mutex() |
| 95 | { |
| 96 | pthread_mutex_destroy(&mMutex); |
| 97 | bool res = pthread_mutexattr_destroy(&mAttribs); |
| 98 | assert(!res); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | /** Block for the signal up to the cancellation timeout. */ |
| 105 | void Signal::wait(Mutex& wMutex, unsigned timeout) const |
| 106 | { |
| 107 | Timeval then(timeout); |
| 108 | struct timespec waitTime = then.timespec(); |
| 109 | pthread_cond_timedwait(&mSignal,&wMutex.mMutex,&waitTime); |
| 110 | } |
| 111 | |
Pau Espin Pedrol | 5b60c98 | 2018-09-20 18:04:46 +0200 | [diff] [blame] | 112 | void set_selfthread_name(const char *name) |
| 113 | { |
| 114 | pthread_t selfid = pthread_self(); |
Pau Espin Pedrol | 5738940 | 2021-02-17 18:25:45 +0100 | [diff] [blame] | 115 | pid_t tid = osmo_gettid(); |
Pau Espin Pedrol | 5b60c98 | 2018-09-20 18:04:46 +0200 | [diff] [blame] | 116 | if (pthread_setname_np(selfid, name) == 0) { |
| 117 | LOG(INFO) << "Thread "<< selfid << " (task " << tid << ") set name: " << name; |
| 118 | } else { |
| 119 | char buf[256]; |
| 120 | int err = errno; |
| 121 | char* err_str = strerror_r(err, buf, sizeof(buf)); |
| 122 | LOG(NOTICE) << "Thread "<< selfid << " (task " << tid << ") set name \"" << name << "\" failed: (" << err << ") " << err_str; |
| 123 | } |
| 124 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 125 | |
Pau Espin Pedrol | 75cb0b9 | 2019-04-25 19:33:58 +0200 | [diff] [blame] | 126 | void thread_enable_cancel(bool cancel) |
| 127 | { |
| 128 | cancel ? pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) : |
| 129 | pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); |
| 130 | } |
| 131 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 132 | void Thread::start(void *(*task)(void*), void *arg) |
| 133 | { |
| 134 | assert(mThread==((pthread_t)0)); |
| 135 | bool res; |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 136 | // (pat) Moved initialization to constructor to avoid crash in destructor. |
| 137 | //res = pthread_attr_init(&mAttrib); |
| 138 | //assert(!res); |
Eric Wild | ac0487e | 2019-06-17 13:02:44 +0200 | [diff] [blame] | 139 | if (mStackSize != 0) { |
| 140 | res = pthread_attr_setstacksize(&mAttrib, mStackSize); |
| 141 | assert(!res); |
| 142 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 143 | res = pthread_create(&mThread, &mAttrib, task, arg); |
| 144 | assert(!res); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | |
| 149 | // vim: ts=4 sw=4 |