blob: de6520b216c5f1620902a998ff0b0b1eb57da6fa [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
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
34using namespace std;
35
36
37
38
39Mutex gStreamLock; ///< Global lock to control access to cout and cerr.
40
41void lockCout()
42{
43 gStreamLock.lock();
44 Timeval entryTime;
45 cout << entryTime << " " << pthread_self() << ": ";
46}
47
48
49void unlockCout()
50{
51 cout << dec << endl << flush;
52 gStreamLock.unlock();
53}
54
55
56void lockCerr()
57{
58 gStreamLock.lock();
59 Timeval entryTime;
60 cerr << entryTime << " " << pthread_self() << ": ";
61}
62
63void unlockCerr()
64{
65 cerr << dec << endl << flush;
66 gStreamLock.unlock();
67}
68
69
70
71
72
73
74
75Mutex::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
87Mutex::~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. */
98void 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
106void Thread::start(void *(*task)(void*), void *arg)
107{
108 assert(mThread==((pthread_t)0));
109 bool res;
kurtis.heimerl5a872472013-05-31 21:47:25 +0000110 // (pat) Moved initialization to constructor to avoid crash in destructor.
111 //res = pthread_attr_init(&mAttrib);
112 //assert(!res);
dburgess82c46ff2011-10-07 02:40:51 +0000113 res = pthread_attr_setstacksize(&mAttrib, mStackSize);
114 assert(!res);
115 res = pthread_create(&mThread, &mAttrib, task, arg);
116 assert(!res);
117}
118
119
120
121// vim: ts=4 sw=4