From ce82146aa1c4283cfc26cf30cc0cac302a011600 Mon Sep 17 00:00:00 2001 From: Karel Burda Date: Mon, 26 Nov 2018 19:12:02 +0100 Subject: [PATCH 1/4] Fixed bug in periodic timer (protection against multiple threads starting synchronous periodic timer) --- include/timers/periodic.hpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/timers/periodic.hpp b/include/timers/periodic.hpp index 62474f2..f732ad6 100644 --- a/include/timers/periodic.hpp +++ b/include/timers/periodic.hpp @@ -15,8 +15,13 @@ class periodic : public single_shot /// @see "single_shot::start" void start(interval interval, callback callback, policies::start::exception policy = policies::start::get_default()) { + const std::lock_guard lock{ m_periodic_protection }; + while(single_shot::start(interval, callback, policy)); } + +private: + std::mutex m_periodic_protection; }; } } From 6fe216d4fd0430b400554ec8072154732e932099 Mon Sep 17 00:00:00 2001 From: Karel Burda Date: Mon, 26 Nov 2018 19:12:28 +0100 Subject: [PATCH 2/4] Added constness --- include/timers/async.hpp | 2 +- include/timers/blocking.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/timers/async.hpp b/include/timers/async.hpp index cda5202..7d3fecc 100644 --- a/include/timers/async.hpp +++ b/include/timers/async.hpp @@ -41,7 +41,7 @@ static_assert(std::is_same::value || std::is_same lock { m_async_protection }; + const std::lock_guard lock { m_async_protection }; underlying_timer::stop(); diff --git a/include/timers/blocking.hpp b/include/timers/blocking.hpp index 8d3690d..c440534 100644 --- a/include/timers/blocking.hpp +++ b/include/timers/blocking.hpp @@ -25,7 +25,7 @@ class blocking : private burda::cpp_utils::primitives::idisable_copy, { throw_if_time_invalid(time); - std::lock_guard lock{ m_block_protection }; + const std::lock_guard lock{ m_block_protection }; std::unique_lock cv_lock{ m_cv_protection }; const auto terminated_after_interval_elapsed = !m_cv.wait_for(cv_lock, time, [&] From bc3dce81e70b9925b2889670a7d72561104c0e1b Mon Sep 17 00:00:00 2001 From: Karel Burda Date: Mon, 26 Nov 2018 19:13:27 +0100 Subject: [PATCH 3/4] Raised timer in tests --- tests/unit/src/periodic_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/src/periodic_test.cpp b/tests/unit/src/periodic_test.cpp index a2e8a79..1e76e6d 100644 --- a/tests/unit/src/periodic_test.cpp +++ b/tests/unit/src/periodic_test.cpp @@ -112,7 +112,7 @@ TEST_F(periodic_test, start_in_parallel) m_timer.start(1s, [&taskFinished2]() { taskFinished2 = true; }); }); - std::this_thread::sleep_for(2s); + std::this_thread::sleep_for(4s); m_timer.stop(); starter1.wait(); From e0327effb45aa374f0bba80bdb251a934cc364fa Mon Sep 17 00:00:00 2001 From: Karel Burda Date: Mon, 26 Nov 2018 19:16:04 +0100 Subject: [PATCH 4/4] Raised version --- CMakeLists.txt | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 20b1f2b..7030cf4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.8 FATAL_ERROR) -project(timers VERSION 1.3.0 LANGUAGES CXX) +project(timers VERSION 1.3.1 LANGUAGES CXX) add_library(${PROJECT_NAME} INTERFACE) diff --git a/README.md b/README.md index 1290cfd..8d40995 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -![Version](https://img.shields.io/badge/version-1.3.0-green.svg) +![Version](https://img.shields.io/badge/version-1.3.1-green.svg) [![License](https://img.shields.io/badge/license-MIT_License-green.svg?style=flat)](LICENSE) [![Build Status](https://travis-ci.org/karel-burda/timers.svg?branch=develop)](https://travis-ci.org/karel-burda/timers) [![Codecov Status](https://codecov.io/gh/karel-burda/timers/branch/develop/graph/badge.svg)](https://codecov.io/gh/karel-burda/timers/branch/develop)