-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPThreadCondition.cpp
More file actions
170 lines (126 loc) · 4.32 KB
/
PThreadCondition.cpp
File metadata and controls
170 lines (126 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// OpenThread library, Copyright (C) 2002 - 2003 The Open Thread Group
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//
// PThreadCondition.c++ - C++ Condition class built on top of posix threads.
// ~~~~~~~~~~~~~~~~~~~~
//
#include <sys/time.h>
#include <assert.h>
#include <OpenThreads/Condition>
#include "PThreadConditionPrivateData.h"
#include "PThreadMutexPrivateData.h"
using namespace OpenThreads;
//----------------------------------------------------------------------------
// This cancel cleanup handler is necessary to ensure that the barrier's
// mutex gets unlocked on cancel. Otherwise deadlocks could occur with
// later joins.
//
void condition_cleanup_handler(void *arg) {
pthread_mutex_t *mutex = static_cast<pthread_mutex_t *>(arg);
pthread_mutex_unlock(mutex);
}
//----------------------------------------------------------------------------
//
// Decription: Constructor
//
// Use: public.
//
Condition::Condition() {
PThreadConditionPrivateData *pd =
new PThreadConditionPrivateData();
int status = pthread_cond_init( &pd->condition, NULL );
assert(status == 0);
_prvData = static_cast<void *>(pd);
}
//----------------------------------------------------------------------------
//
// Decription: Destructor
//
// Use: public.
//
Condition::~Condition() {
PThreadConditionPrivateData *pd =
static_cast<PThreadConditionPrivateData *>(_prvData);
int status = pthread_cond_destroy( &pd->condition );
assert(status == 0);
delete pd;
}
//----------------------------------------------------------------------------
//
// Decription: wait on a condition
//
// Use: public.
//
int Condition::wait(Mutex *mutex) {
PThreadConditionPrivateData *pd =
static_cast<PThreadConditionPrivateData *>(_prvData);
PThreadMutexPrivateData *mpd =
static_cast<PThreadMutexPrivateData *>(mutex->_prvData);
int status;
pthread_cleanup_push(condition_cleanup_handler, &mpd->mutex);
status = pthread_cond_wait( &pd->condition, &mpd->mutex );
pthread_cleanup_pop(0);
return status;
}
//----------------------------------------------------------------------------
//
// Decription: wait on a condition, for a specified period of time
//
// Use: public.
//
int Condition::wait(Mutex *mutex, unsigned long int ms) {
PThreadConditionPrivateData *pd =
static_cast<PThreadConditionPrivateData *>(_prvData);
PThreadMutexPrivateData *mpd =
static_cast<PThreadMutexPrivateData *>(mutex->_prvData);
struct ::timeval now;
::gettimeofday( &now, 0 );
// Wait time is now + ms milliseconds
unsigned int sec = ms / 1000;
unsigned int nsec = (ms % 1000) * 1000;
struct timespec abstime;
abstime.tv_sec = now.tv_sec + sec;
abstime.tv_nsec = now.tv_usec*1000 + nsec;
int status;
pthread_cleanup_push(condition_cleanup_handler, &mpd->mutex);
status = pthread_cond_timedwait( &pd->condition, &mpd->mutex, &abstime );
pthread_cleanup_pop(0);
return status;
}
//----------------------------------------------------------------------------
//
// Decription: signal a thread to wake up.
//
// Use: public.
//
int Condition::signal() {
PThreadConditionPrivateData *pd =
static_cast<PThreadConditionPrivateData *>(_prvData);
return pthread_cond_signal( &pd->condition );
}
//----------------------------------------------------------------------------
//
// Decription: signal many threads to wake up.
//
// Use: public.
//
int Condition::broadcast() {
PThreadConditionPrivateData *pd =
static_cast<PThreadConditionPrivateData *>(_prvData);
return pthread_cond_broadcast( &pd->condition );
}