dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * |
| 5 | * This software is distributed under the terms of the GNU Affero Public License. |
| 6 | * See the COPYING file in the main directory for details. |
| 7 | * |
| 8 | * This use of this software may be subject to additional restrictions. |
| 9 | * See the LEGAL file in the main directory for details. |
| 10 | |
| 11 | This program is free software: you can redistribute it and/or modify |
| 12 | it under the terms of the GNU Affero General Public License as published by |
| 13 | the Free Software Foundation, either version 3 of the License, or |
| 14 | (at your option) any later version. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU Affero General Public License for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU Affero General Public License |
| 22 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | |
| 24 | */ |
| 25 | |
| 26 | |
Pau Espin Pedrol | 5b60c98 | 2018-09-20 18:04:46 +0200 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <sys/types.h> |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 29 | |
| 30 | #include "Threads.h" |
| 31 | #include "Timeval.h" |
Pau Espin Pedrol | 5b60c98 | 2018-09-20 18:04:46 +0200 | [diff] [blame] | 32 | #include "Logger.h" |
| 33 | |
| 34 | #ifndef gettid |
| 35 | #include <sys/syscall.h> |
| 36 | #define gettid() syscall(SYS_gettid) |
| 37 | #endif |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | using namespace std; |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | Mutex gStreamLock; ///< Global lock to control access to cout and cerr. |
| 46 | |
| 47 | void lockCout() |
| 48 | { |
| 49 | gStreamLock.lock(); |
| 50 | Timeval entryTime; |
| 51 | cout << entryTime << " " << pthread_self() << ": "; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | void unlockCout() |
| 56 | { |
| 57 | cout << dec << endl << flush; |
| 58 | gStreamLock.unlock(); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | void lockCerr() |
| 63 | { |
| 64 | gStreamLock.lock(); |
| 65 | Timeval entryTime; |
| 66 | cerr << entryTime << " " << pthread_self() << ": "; |
| 67 | } |
| 68 | |
| 69 | void unlockCerr() |
| 70 | { |
| 71 | cerr << dec << endl << flush; |
| 72 | gStreamLock.unlock(); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | Mutex::Mutex() |
| 82 | { |
| 83 | bool res; |
| 84 | res = pthread_mutexattr_init(&mAttribs); |
| 85 | assert(!res); |
| 86 | res = pthread_mutexattr_settype(&mAttribs,PTHREAD_MUTEX_RECURSIVE); |
| 87 | assert(!res); |
| 88 | res = pthread_mutex_init(&mMutex,&mAttribs); |
| 89 | assert(!res); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | Mutex::~Mutex() |
| 94 | { |
| 95 | pthread_mutex_destroy(&mMutex); |
| 96 | bool res = pthread_mutexattr_destroy(&mAttribs); |
| 97 | assert(!res); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | /** Block for the signal up to the cancellation timeout. */ |
| 104 | void Signal::wait(Mutex& wMutex, unsigned timeout) const |
| 105 | { |
| 106 | Timeval then(timeout); |
| 107 | struct timespec waitTime = then.timespec(); |
| 108 | pthread_cond_timedwait(&mSignal,&wMutex.mMutex,&waitTime); |
| 109 | } |
| 110 | |
Pau Espin Pedrol | 5b60c98 | 2018-09-20 18:04:46 +0200 | [diff] [blame] | 111 | void set_selfthread_name(const char *name) |
| 112 | { |
| 113 | pthread_t selfid = pthread_self(); |
| 114 | pid_t tid = gettid(); |
| 115 | if (pthread_setname_np(selfid, name) == 0) { |
| 116 | LOG(INFO) << "Thread "<< selfid << " (task " << tid << ") set name: " << name; |
| 117 | } else { |
| 118 | char buf[256]; |
| 119 | int err = errno; |
| 120 | char* err_str = strerror_r(err, buf, sizeof(buf)); |
| 121 | LOG(NOTICE) << "Thread "<< selfid << " (task " << tid << ") set name \"" << name << "\" failed: (" << err << ") " << err_str; |
| 122 | } |
| 123 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 124 | |
Pau Espin Pedrol | 75cb0b9 | 2019-04-25 19:33:58 +0200 | [diff] [blame] | 125 | void thread_enable_cancel(bool cancel) |
| 126 | { |
| 127 | cancel ? pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) : |
| 128 | pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); |
| 129 | } |
| 130 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 131 | void Thread::start(void *(*task)(void*), void *arg) |
| 132 | { |
| 133 | assert(mThread==((pthread_t)0)); |
| 134 | bool res; |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 135 | // (pat) Moved initialization to constructor to avoid crash in destructor. |
| 136 | //res = pthread_attr_init(&mAttrib); |
| 137 | //assert(!res); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 138 | res = pthread_attr_setstacksize(&mAttrib, mStackSize); |
| 139 | assert(!res); |
| 140 | res = pthread_create(&mThread, &mAttrib, task, arg); |
| 141 | assert(!res); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | // vim: ts=4 sw=4 |