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 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | #include "Threads.h" |
| 31 | #include "Timeval.h" |
| 32 | |
| 33 | |
| 34 | using namespace std; |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | Mutex gStreamLock; ///< Global lock to control access to cout and cerr. |
| 40 | |
| 41 | void lockCout() |
| 42 | { |
| 43 | gStreamLock.lock(); |
| 44 | Timeval entryTime; |
| 45 | cout << entryTime << " " << pthread_self() << ": "; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | void unlockCout() |
| 50 | { |
| 51 | cout << dec << endl << flush; |
| 52 | gStreamLock.unlock(); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | void lockCerr() |
| 57 | { |
| 58 | gStreamLock.lock(); |
| 59 | Timeval entryTime; |
| 60 | cerr << entryTime << " " << pthread_self() << ": "; |
| 61 | } |
| 62 | |
| 63 | void unlockCerr() |
| 64 | { |
| 65 | cerr << dec << endl << flush; |
| 66 | gStreamLock.unlock(); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | Mutex::Mutex() |
| 76 | { |
| 77 | bool res; |
| 78 | res = pthread_mutexattr_init(&mAttribs); |
| 79 | assert(!res); |
| 80 | res = pthread_mutexattr_settype(&mAttribs,PTHREAD_MUTEX_RECURSIVE); |
| 81 | assert(!res); |
| 82 | res = pthread_mutex_init(&mMutex,&mAttribs); |
| 83 | assert(!res); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | Mutex::~Mutex() |
| 88 | { |
| 89 | pthread_mutex_destroy(&mMutex); |
| 90 | bool res = pthread_mutexattr_destroy(&mAttribs); |
| 91 | assert(!res); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | /** Block for the signal up to the cancellation timeout. */ |
| 98 | void Signal::wait(Mutex& wMutex, unsigned timeout) const |
| 99 | { |
| 100 | Timeval then(timeout); |
| 101 | struct timespec waitTime = then.timespec(); |
| 102 | pthread_cond_timedwait(&mSignal,&wMutex.mMutex,&waitTime); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | void Thread::start(void *(*task)(void*), void *arg) |
| 107 | { |
| 108 | assert(mThread==((pthread_t)0)); |
| 109 | bool res; |
| 110 | res = pthread_attr_init(&mAttrib); |
| 111 | assert(!res); |
| 112 | res = pthread_attr_setstacksize(&mAttrib, mStackSize); |
| 113 | assert(!res); |
| 114 | res = pthread_create(&mThread, &mAttrib, task, arg); |
| 115 | assert(!res); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
| 120 | // vim: ts=4 sw=4 |