Skip to content

Commit f9b30c5

Browse files
Merge branch '175-174-cond_var-and-mutex-testing' into develop
2 parents 4224306 + 24b81ec commit f9b30c5

File tree

10 files changed

+179
-98
lines changed

10 files changed

+179
-98
lines changed

include/pthread/condition_variable.hpp

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,15 @@ namespace pthread {
6464
* @param mtx ralated mutex, which must be locked by the current thread.
6565
* @see notify_one
6666
* @see notify_all
67+
* @see pthread_cond_wait
6768
*/
6869
void wait(mutex &mtx);
6970

70-
/** @see wai
71+
/** Wait for condition to be signaled within given time frame.
72+
*
73+
* The method uses the lock_guard's mutex to execute.
74+
*
75+
* @see #wait(mutex &)
7176
*/
7277
void wait(lock_guard<pthread::mutex> lck);
7378

@@ -82,10 +87,11 @@ namespace pthread {
8287
* The signature of the predicate function should be equivalent to the following: bool pred();
8388
*
8489
* @param mtx ralated mutex, which must be locked by the current thread.
85-
* @param lambda run to check if condition was met.
90+
* @param lambda code that checks if the condition is met (MUST return a boolean).
8691
* @return true if lmabda returned true.
8792
* @see notify_one
8893
* @see notify_all
94+
* @see pthread_cond_wait
8995
*/
9096
template<class Lambda>
9197
bool wait(mutex &mtx, Lambda lambda);
@@ -101,10 +107,11 @@ namespace pthread {
101107
* The signature of the predicate function should be equivalent to the following: bool pred();
102108
*
103109
* @param lck ralated mutex lock_guard, which must be locked by the current thread.
104-
* @param lambda run to check if condition was met.
110+
* @param lambda code that checks if the condition is met (MUST return a boolean).
105111
* @return true if lmabda returned true.
106112
* @see notify_one
107113
* @see notify_all
114+
* @see pthread_cond_timedwait
108115
*/
109116
template<class Lambda>
110117
bool wait(lock_guard<pthread::mutex> &lck, Lambda lambda);
@@ -125,10 +132,15 @@ namespace pthread {
125132
* @throw condition_variable_exception is thrown either if timeout calculation failed or mutex ownership was wrong.
126133
* @see notify_one
127134
* @see notify_all
135+
* @see pthread_cond_timedwait
128136
*/
129137
cv_status wait_for(mutex &mtx, int millis);
130138

131-
/** @see #wait_for (mutex &, int)
139+
/** Wait for condition to be signaled within given time frame.
140+
*
141+
* The method uses the lock_guard's mutex to execute.
142+
*
143+
* @see #wait_for (mutex &, int) NOSONAR this is not comment code
132144
*/
133145
cv_status wait_for(lock_guard<pthread::mutex> &lck, int millis);
134146

@@ -144,15 +156,16 @@ namespace pthread {
144156
*
145157
* @param mtx ralated mutex, which must be locked by the current thread.
146158
* @param millis milli seconds to wait for condition to be signaled.
147-
* @param lambda run to check if condition was met.
159+
* @param lambda code that checks if the condition is met (MUST return a boolean).
148160
* @return true if lmabda returned true.
149161
* @see notify_one
150162
* @see notify_all
163+
* @see pthread_cond_timedwait
151164
*/
152165
template<class Lambda>
153166
bool wait_for(mutex &mtx, int millis, Lambda lambda);
154167

155-
/** NOSONAR Wait for condition to be signaled within a given time frame.
168+
/** Wait for condition to be signaled within a given time frame.
156169
*
157170
* This method atomically release mutex and cause the calling thread to block; atomically here means "atomically with respect to
158171
* access by another thread to the mutex and then the condition variable". Call notify_one or notify_all to signal the condition.
@@ -168,33 +181,33 @@ namespace pthread {
168181
* @return true if lmabda returned true.
169182
* @see notify_one
170183
* @see notify_all
184+
* @see pthread_cond_timedwait
171185
*/
172186
template<class Lambda>
173187
bool wait_for(lock_guard<pthread::mutex> &lck, int millis, Lambda lambda);
174188

175-
/** Signal one waiting thread.
189+
/** signal a condition.
190+
*
191+
* unblocks at least one of the threads that are blocked on the specified condition variable cond (if any threads are blocked on cond).
192+
* > *WARN* the signature of this methode should be the same as `std::condition_variable::notify_one`, we decided to
193+
* > throw an exception anyway, when the pthread function fails and not be strictly compliant.
176194
*
177-
* The call unblocks at least one of the threads that are blocked on the specified condition variable cond (if any threads are blocked on cond).
195+
* @throw condition_variable_exception if the call to pthread_cond_signal return value is different from 0 (zero)
196+
* @see pthread_cond_signal
178197
*/
179-
#if __cplusplus < 201103L
180-
void notify_one () throw() ;
181-
#else
198+
void notify_one() ;
182199

183-
void notify_one() noexcept;
184-
185-
#endif
186-
187-
/** Signal all waiting threads.
200+
/** broadcast a condition.
201+
*
202+
* unblocks all threads currently blocked on the specified condition variable cond.
203+
*
204+
* > *WARN* the signature of this methode should be the same as `std::condition_variable::notify_one`, we decided to
205+
* > throw an exception anyway, when the pthread function fails and not be strictly compliant.
188206
*
189-
* The call unblocks all threads currently blocked on the specified condition variable cond.
207+
* @throw condition_variable_exception if the call to pthread_cond_broadcast return value is different from 0 (zero)
208+
* @see pthread_cond_broadcast
190209
*/
191-
#if __cplusplus < 201103L
192-
void notify_all () throw() ;
193-
#else
194-
195-
void notify_all() noexcept;
196-
197-
#endif
210+
void notify_all();
198211

199212
/**
200213
* not copy-assignable
@@ -203,7 +216,9 @@ namespace pthread {
203216

204217
// constructor/destructor ------------------------------------------------
205218

206-
/** construct a new condition_variable (pthread_cond_init).
219+
/** construct a new condition_variable.
220+
*
221+
* @see pthread_cond_init
207222
*/
208223
condition_variable();
209224

@@ -212,9 +227,10 @@ namespace pthread {
212227
*/
213228
condition_variable(const condition_variable &) = delete;
214229

215-
/** destroy a condition_variable (pthread_cond_destroy)
230+
/** destroy a condition_variable.
216231
*
217232
* @throw condition_variable_exception if conditional variable failed to be destroyed (pthread_cond_destroy != 0)
233+
* @see pthread_cond_destroy
218234
*/
219235
~condition_variable() ;
220236

include/pthread/mutex.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ namespace pthread {
4646
void lock();
4747

4848
/**
49-
* The function pthread_mutex_trylock is identical to pthread_mutex_lock except that
50-
* if the mutex object referenced by mutex is currently locked (by any thread,
51-
* including the current thread), the call returns immediately.
49+
* Identical to lock method except that if the mutex object is currently locked (by any thread, including the
50+
* current thread), the call returns immediately.
5251
*
53-
* @return
52+
* @return true if the mutex is locked, false is returned if the lock is held by some other thread.
5453
* @throw mutex_exception if error conditions preventing this method to succeed.
5554
* @see lock
55+
* @see unlock
5656
*/
5757
bool try_lock();
5858

include/pthread/thread.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ namespace pthread {
150150
*
151151
* @return the stack size in bytes.
152152
*/
153-
size_t stact_size();
153+
size_t stack_size();
154154

155155
/** copy operator is flagged deleted, copying doesn't make sense
156156
*/
@@ -264,8 +264,7 @@ namespace pthread {
264264
/** @return true if this thread can be joined.
265265
*/
266266
bool joinable() const;
267-
268-
267+
269268
/** not copy-assignable */
270269
void operator=(const abstract_thread &) = delete;
271270

src/condition_variable.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,18 @@ namespace pthread {
4545
return status;
4646
}
4747

48-
#if __cplusplus < 201103L
49-
void condition_variable::notify_one() throw() {
50-
#else
51-
void condition_variable::notify_one() noexcept {
52-
#endif
53-
pthread_cond_signal ( &_condition );
48+
void condition_variable::notify_one(){
49+
int rc = pthread_cond_signal ( &_condition );
50+
if ( rc != 0 ){
51+
throw condition_variable_exception{"notify_all failed.", rc};
52+
}
5453
}
5554

56-
#if __cplusplus < 201103L
57-
void condition_variable::notify_all () throw(){
58-
#else
59-
void condition_variable::notify_all () noexcept{
60-
#endif
61-
pthread_cond_broadcast ( &_condition );
62-
55+
void condition_variable::notify_all () {
56+
int rc = pthread_cond_broadcast ( &_condition );
57+
if ( rc != 0 ){
58+
throw condition_variable_exception{"notify_all failed.", rc};
59+
}
6360
}
6461

6562
void condition_variable::milliseconds(int millis){

src/mutex.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ namespace pthread {
3434

3535
auto rc = pthread_mutex_trylock(&_mutex);
3636
if (rc == 0) {
37-
status = true;
37+
status = true; // mutex is locked now
38+
}else if ( rc == EBUSY){
39+
status = false ; // mutex is held by some other thread
3840
} else {
3941
throw mutex_exception("pthread_mutex_trylock failed, already locked.", rc);
4042
}

src/thread.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ namespace pthread {
148148
_attr_ptr = &_attr; // pthread_attribute is always initialized
149149
}
150150

151-
size_t thread::stact_size() {
151+
size_t thread::stack_size() {
152152
size_t size = -1;
153153
int rc = pthread_attr_getstacksize(_attr_ptr, &size);
154154
if ( rc != 0 ){
@@ -174,11 +174,13 @@ namespace pthread {
174174
}
175175

176176
void abstract_thread::join() {
177-
_thread->join();
177+
if ( _thread != nullptr ){
178+
_thread->join();
179+
}
178180
};
179181

180182
bool abstract_thread::joinable() const {
181-
return _thread != 0 && _thread->joinable();
183+
return _thread != nullptr && _thread->joinable();
182184
};
183185

184186
#if __cplusplus < 201103L

tests/abstract_thread_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ TEST(absttract_thread, self_join) {
7777
TEST(abstract_thread_group, start_auto_join) {
7878
pthread::thread_group threads{true};
7979

80+
EXPECT_TRUE(threads.destructor_joins_first());
81+
8082
for (auto x = 10; x > 0; x--) {
8183
threads.add(new test_thread{});
8284
}

0 commit comments

Comments
 (0)