1
+ #include < assert.h>
1
2
#include " ../globals.h"
2
3
3
4
/*
9
10
10
11
#include " lock_and_signal.h"
11
12
13
+ // FIXME: This is not a portable way of specifying an invalid pthread_t
14
+ #define INVALID_THREAD 0
15
+
16
+
12
17
#if defined(__WIN32__)
13
18
lock_and_signal::lock_and_signal ()
19
+ : _holding_thread(INVALID_THREAD)
14
20
{
15
21
// FIXME: In order to match the behavior of pthread_cond_broadcast on
16
22
// Windows, we create manual reset events. This however breaks the
@@ -23,7 +29,7 @@ lock_and_signal::lock_and_signal()
23
29
24
30
#else
25
31
lock_and_signal::lock_and_signal ()
26
- : _locked( false )
32
+ : _holding_thread(INVALID_THREAD )
27
33
{
28
34
CHECKED (pthread_cond_init (&_cond, NULL ));
29
35
CHECKED (pthread_mutex_init (&_mutex, NULL ));
@@ -47,11 +53,10 @@ void lock_and_signal::lock() {
47
53
CHECKED (pthread_mutex_lock (&_mutex));
48
54
_holding_thread = pthread_self ();
49
55
#endif
50
- _locked = true ;
51
56
}
52
57
53
58
void lock_and_signal::unlock () {
54
- _locked = false ;
59
+ _holding_thread = INVALID_THREAD ;
55
60
#if defined(__WIN32__)
56
61
LeaveCriticalSection (&_cs);
57
62
#else
@@ -67,7 +72,8 @@ void lock_and_signal::wait() {
67
72
}
68
73
69
74
bool lock_and_signal::timed_wait (size_t timeout_in_ms) {
70
- _locked = false ;
75
+ assert (lock_held_by_current_thread ());
76
+ _holding_thread = INVALID_THREAD;
71
77
bool rv = true ;
72
78
#if defined(__WIN32__)
73
79
LeaveCriticalSection (&_cs);
@@ -105,7 +111,6 @@ bool lock_and_signal::timed_wait(size_t timeout_in_ms) {
105
111
}
106
112
_holding_thread = pthread_self ();
107
113
#endif
108
- _locked = true ;
109
114
return rv;
110
115
}
111
116
@@ -134,9 +139,9 @@ void lock_and_signal::signal_all() {
134
139
bool lock_and_signal::lock_held_by_current_thread ()
135
140
{
136
141
#if defined(__WIN32__)
137
- return _locked && _holding_thread == GetCurrentThreadId ();
142
+ return _holding_thread == GetCurrentThreadId ();
138
143
#else
139
- return _locked && _holding_thread == pthread_self ();
144
+ return pthread_equal ( _holding_thread, pthread_self () );
140
145
#endif
141
146
}
142
147
0 commit comments