10000 Merge pull request #13 from connectivecpp/develop · connectivecpp/wait-queue@f84fb58 · GitHub
[go: up one dir, main page]

Skip to content

Commit f84fb58

Browse files
Merge pull request #13 from connectivecpp/develop
Merge develop to main
2 parents 4c30c34 + 3fa71fc commit f84fb58

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

.github/workflows/build_run_unit_test_cmake.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
name: CMake build and run unit test matrix
33

44
on:
5+
pull_request:
6+
types:
7+
- opened
8+
- reopened
59
push:
610
branches:
711
- main

include/queue/wait_queue.hpp

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,14 @@ class wait_queue {
309309
* external @c std::stop_token was passed in.
310310
*
311311
*/
312-
bool request_stop() noexcept {
312+
auto request_stop() noexcept
313+
-> bool {
314+
313315
if (m_stop_src) {
314316
return (*m_stop_src).request_stop();
315317
}
316318
return false;
319+
317320
}
318321

319322
/**
@@ -327,14 +330,17 @@ class wait_queue {
327330
* @return @c true if successful, @c false if the @c wait_queue has been
328331
* requested to stop.
329332
*/
330-
bool push(const T& val) /* noexcept(std::is_nothrow_copy_constructible<T>::value) */ {
333+
auto push(const T& val) /* noexcept(std::is_nothrow_copy_constructible<T>::value) */
334+
-> bool {
335+
331336
if (m_stop_tok.stop_requested()) {
332337
return false;
333338
}
334339
lock_guard lk{m_mut};
335340
m_data_queue.push_back(val);
336341
m_data_cond.notify_one();
337342
return true;
343+
338344
}
339345

340346
/**
@@ -343,14 +349,17 @@ class wait_queue {
343349
* This method has the same semantics as the other @c push, except that the value will
344350
* be moved (if possible) instead of copied.
345351
*/
346-
bool push(T&& val) /* noexcept(std::is_nothrow_move_constructible<T>::value) */ {
352+
auto push(T&& val) /* noexcept(std::is_nothrow_move_constructible<T>::value) */
353+
-> bool {
354+
347355
if (m_stop_tok.stop_requested()) {
348356
return false;
349357
}
350358
lock_guard lk{m_mut};
351359
m_data_queue.push_back(std::move(val));
352360
m_data_cond.notify_one();
353361
return true;
362+
354363
}
355364

356365
/**
@@ -367,14 +376,17 @@ class wait_queue {
367376
* to stop.
368377
*/
369378
template <typename ... Args>
370-
bool emplace_push(Args &&... args) /* noexcept(std::is_nothrow_constructible<T, Args...>::value)*/ {
379+
auto emplace_push(Args &&... args) /* noexcept(std::is_nothrow_constructible<T, Args...>::value)*/
380+
-> bool {
381+
371382
if (m_stop_tok.stop_requested()) {
372383
return false;
373384
}
374385
lock_guard lk{m_mut};
375386
m_data_queue.emplace_back(std::forward<Args>(args)...);
376387 m_data_cond.notify_one();
377388
return true;
389+
378390
}
379391

380392
/**
@@ -388,14 +400,17 @@ class wait_queue {
388400
* @return A value from the @c wait_queue (if non-empty). If the @c std::optional is empty,
389401
* the @c wait_queue has been requested to be stopped.
390402
*/
391-
std::optional<T> wait_and_pop() /* noexcept(std::is_nothrow_constructible<T>::value) */ {
403+
auto wait_and_pop() /* noexcept(std::is_nothrow_constructible<T>::value) */
404+
-> std::optional<T> {
405+
392406
std::unique_lock<std::mutex> lk{m_mut};
393407
if (!m_data_cond.wait ( lk, m_stop_tok, [this] { return !m_data_queue.empty(); } )) {
394408
return std::optional<T> {}; // queue was request to stop, no data available
395409
}
396410
std::optional<T> val {std::move_if_noexcept(m_data_queue.front())}; // move construct if possible
397411
m_data_queue.pop_front();
398412
return val;
413+
399414
}
400415

401416
/**
@@ -406,7 +421,9 @@ class wait_queue {
406421
* available in the @c wait_queue or if the @c wait_queue has been requested to be
407422
* stopped .
408423
*/
409-
std::optional<T> try_pop() /* noexcept(std::is_nothrow_constructible<T>::value) */ {
424+
auto try_pop() /* noexcept(std::is_nothrow_constructible<T>::value) */
425+
-> std::optional<T> {
426+
410427
if (m_stop_tok.stop_requested()) {
411428
return std::optional<T> {};
412429
}
@@ -417,6 +434,7 @@ class wait_queue {
417434
std::optional<T> val {std::move_if_noexcept(m_data_queue.front())}; // move construct if possible
418435
m_data_queue.pop_front();
419436
return val;
437+
420438
}
421439

422440
// non-modifying methods
@@ -447,11 +465,14 @@ class wait_queue {
447465
* same @c wait_queue since it results in recursive mutex locks.
448466
*/
449467
template <typename F>
450-
void apply(F&& func) const /* noexcept(std::is_nothrow_invocable<F&&, const T&>::value) */ {
468+
auto apply(F&& func) const /* noexcept(std::is_nothrow_invocable<F&&, const T&>::value) */
469+
-> void {
470+
451471
lock_guard lk{m_mut};
452472
for (const T& elem : m_data_queue) {
453473
func(elem);
454474
}
475+
455476
}
456477

457478
/**
@@ -460,28 +481,37 @@ class wait_queue {
460481
*
461482
* @return @c true if the @c stop_requested has been called.
462483
*/
463-
bool stop_requested() const noexcept {
484+
auto stop_requested() const noexcept
485+
-> bool {
486+
464487
return m_stop_tok.stop_requested();
488+
465489
}
466490

467491
/**
468492
* Query whether the @c wait_queue is empty or not.
469493
*
470494
* @return @c true if the @c wait_queue is empty.
471495
*/
472-
bool empty() const /* noexcept */ {
496+
auto empty() const /* noexcept */
497+
-> bool {
498+
473499
lock_guard lk{m_mut};
474500
return m_data_queue.empty();
501+
475502
}
476503

477504
/**
478505
* Get the number of elements in the @c wait_queue.
479506
*
480507
* @return Number of elements in the @c wait_queue.
481508
*/
482-
size_type size() const /* noexcept */ {
509+
auto size() const /* noexcept */
510+
-> size_type {
511+
483512
lock_guard lk{m_mut};
484513
return m_data_queue.size();
514+
485515
}
486516

487517
};

test/wait_queue_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ bool threaded_test(Q& wq, int num_readers, int num_writers, int slice, const T&
164164
std::this_thread::sleep_for(std::chrono::seconds(1));
165165
std::lock_guard<std::mutex> lk(mut);
166166
if (s.size() == tot) {
167-
wq.request_stop(); // tell the readers it's done
168167
done = true;
169168
}
170169
}
170+
wq.request_stop(); // tell the readers it's done
171171

172172
// join readers; since wait queue is stopped they should all join immediately
173173
for (auto& thr : rd_thrs) {

0 commit comments

Comments
 (0)
0