-
Notifications
You must be signed in to change notification settings - Fork 30
/
condition.cpp
145 lines (123 loc) · 3.29 KB
/
condition.cpp
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
/* Copyright (c) 2010-2012, Stefan Eilemann <eile@eyescale.ch>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 2.1 as published
* by the Free Software Foundation.
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "condition.h"
#include "debug.h"
#include "time.h"
#include <cstring>
#include <errno.h>
#ifdef _WIN32
# include "condition_w32.ipp"
#else
# include <pthread.h>
# include <sys/time.h>
#endif
namespace lunchbox
{
namespace detail
{
class Condition
{
public:
pthread_mutex_t mutex;
pthread_cond_t cond;
};
}
Condition::Condition()
: _impl( new detail::Condition )
{
// mutex init
int error = pthread_mutex_init( &_impl->mutex, 0 );
if( error )
{
LBERROR << "Error creating pthread mutex: " << strerror( error )
<< std::endl;
return;
}
// condvar init
error = pthread_cond_init( &_impl->cond, 0 );
if( error )
{
LBERROR << "Error creating pthread condition: " << strerror( error )
<< std::endl;
return;
}
}
Condition::~Condition()
{
int error = pthread_mutex_destroy( &_impl->mutex );
if( error )
LBERROR << "Error destroying pthread mutex: " << strerror( error )
<< std::endl;
error = pthread_cond_destroy( &_impl->cond );
if( error )
LBERROR << "Error destroying pthread condition: " << strerror( error )
<< std::endl;
delete _impl;
}
void Condition::lock()
{
pthread_mutex_lock( &_impl->mutex );
}
void Condition::signal()
{
pthread_cond_signal( &_impl->cond );
}
void Condition::broadcast()
{
pthread_cond_broadcast( &_impl->cond );
}
void Condition::unlock()
{
pthread_mutex_unlock( &_impl->mutex );
}
void Condition::wait()
{
pthread_cond_wait( &_impl->cond, &_impl->mutex );
}
bool Condition::timedWait( const uint32_t timeout )
{
if( timeout == LB_TIMEOUT_INDEFINITE )
{
wait();
return true;
}
const uint32_t time = timeout == LB_TIMEOUT_DEFAULT ?
300000 /* 5 min */ : timeout;
#ifdef _WIN32
int error = pthread_cond_timedwait_w32_np( &_impl->cond, &_impl->mutex,
time );
#else
const timespec delta = convertToTimespec( time );
timeval now;
gettimeofday( &now, 0 );
timespec then;
then.tv_sec = delta.tv_sec + now.tv_sec;
then.tv_nsec = delta.tv_nsec + now.tv_usec * 1000;
while( then.tv_nsec > 1000000000 )
{
++then.tv_sec;
then.tv_nsec -= 1000000000;
}
int error = pthread_cond_timedwait( &_impl->cond, &_impl->mutex, &then );
#endif
if( error == ETIMEDOUT )
return false;
if( error )
LBERROR << "pthread_cond_timedwait failed: " << strerror( error )
<< std::endl;
return true;
}
}