Skip to content

Commit

Permalink
Apply update_interval as soon as it is called.
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanoLusardi committed Mar 15, 2023
1 parent 6e06e90 commit dfac66f
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
**/results
**/unit_tests
**/profiles
**/memcheck
**/callgrind
**/logs
**/.cache
**/.clangd
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ option(SSTS_BUILD_TESTS "Build library tests" True)
option(SSTS_BUILD_EXAMPLES "Build library examples" True)
option(SSTS_INSTALL_LIBRARY "Install library" False)
option(SSTS_INSTALL_EXAMPLES "Install examples (requires installing library and building examples)" False)
option(SSTS_ENABLE_SANITIZERS "Run unit tests with Thread Sanitizer support" True)
option(SSTS_ENABLE_SANITIZERS "Run unit tests with Thread Sanitizer support" False)

if(${SSTS_ENABLE_SANITIZERS})
message(STATUS "::ssTs:: Sanitizers enabled")
Expand Down
6 changes: 2 additions & 4 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ function(add_example EXAMPLE_TARGET)
add_executable(${EXAMPLE_TARGET} src/${EXAMPLE_TARGET}.cpp)
target_link_libraries(${EXAMPLE_TARGET} PRIVATE ssts::ssts PRIVATE spdlog::spdlog)
target_compile_features(${EXAMPLE_TARGET} PRIVATE cxx_std_17)
# if(UNIX)
# target_link_libraries(${EXAMPLE_TARGET} PRIVATE pthread)
# endif()

if(${SSTS_INSTALL_EXAMPLES})
install(TARGETS ${EXAMPLE_TARGET} RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
Expand All @@ -24,4 +21,5 @@ add_example(example_nested)
add_example(example_bind)
add_example(example_update_interval)
add_example(example_every)
add_example(example_unordered)
add_example(example_unordered)
add_example(example_remove)
9 changes: 3 additions & 6 deletions examples/src/example_every.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#include <ssts/task_scheduler.hpp>
#include <thread>

#include <spdlog/spdlog.h>

#include "utils/utils.hpp"

int main()
{
ssts::utils::log(ssts::version());
spdlog::set_pattern("[%H:%M:%S.%e] %v");
spdlog::info(ssts::version());

ssts::task_scheduler s(8);
s.set_duplicate_allowed(false);
Expand Down Expand Up @@ -36,9 +33,9 @@ int main()

std::this_thread::sleep_for(10s);

ssts::utils::log("Task Scheduler shutdown");
spdlog::info("Task Scheduler shutdown");
s.stop();

ssts::utils::log("Task Scheduler finished");
spdlog::info("Task Scheduler finished");
return 0;
}
32 changes: 32 additions & 0 deletions examples/src/example_remove.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <ssts/task_scheduler.hpp>
#include <spdlog/spdlog.h>
#include "utils/utils.hpp"

int main()
{
spdlog::set_pattern("[%H:%M:%S.%e] %v");
spdlog::info(ssts::version());

ssts::task_scheduler s(8);
s.start();

ssts::utils::timer t;

s.in("Foo"s, 2s, []{ spdlog::info("Foo"); });
s.in("Bar"s, 3s, []{ spdlog::info("Bar"); });

spdlog::info("before remove");
std::this_thread::sleep_for(2s);

s.remove_task("foo"s); // Wrong task ID
s.remove_task("Bar"s);

spdlog::info("after remove");
std::this_thread::sleep_for(2s);

spdlog::info("Task Scheduler shutdown");
s.stop();

spdlog::info("Task Scheduler finished");
return 0;
}
36 changes: 21 additions & 15 deletions examples/src/example_update_interval.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
#include <ssts/task_scheduler.hpp>

#include <spdlog/spdlog.h>
#include "utils/utils.hpp"

void t_update_interval()
int main()
{
ssts::utils::log_test("Update Interval");
ssts::task_scheduler s(2);
spdlog::set_pattern("[%H:%M:%S.%e] %v");
spdlog::info(ssts::version());

ssts::task_scheduler s(8);
s.start();

ssts::utils::timer t;

s.every("Foo"s, 2s, []{ spdlog::info("Foo"); });

s.every("Bar"s, 2s, []{ spdlog::info("Bar"); });

s.every("task_id"s, 250ms, []{ std::cout << "Hi!" << std::endl; });
s.in(1ms, []{ spdlog::info("one millis!"); });

ssts::utils::log("before update");
spdlog::info("before update");
std::this_thread::sleep_for(3s);

s.update_interval("task_id"s, 1s);
ssts::utils::log("after update");
std::this_thread::sleep_for(4s);
}
s.update_interval("foo"s, 100ms); // Wrong task ID
s.update_interval("Bar"s, 100ms);

int main()
{
ssts::utils::log(ssts::version());
spdlog::info("after update");
std::this_thread::sleep_for(2s);

t_update_interval();
spdlog::info("Task Scheduler shutdown");
s.stop();

ssts::utils::log("Task Scheduler finished");
spdlog::info("Task Scheduler finished");
return 0;
}
27 changes: 19 additions & 8 deletions include/ssts/task_scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <condition_variable>
#include <future>
#include <map>
#include <type_traits>
#include <unordered_set>
#include <optional>
#include <string>
Expand Down Expand Up @@ -351,15 +352,26 @@ class task_scheduler
*/
bool update_interval(const std::string& task_id, ssts::clock::duration interval)
{
std::scoped_lock lock(_update_tasks_mtx);
std::unique_lock lock(_update_tasks_mtx);

if (auto task = get_task_iterator(task_id); task != _tasks.end()
&& task->second.interval().has_value())
if (auto task_iterator = get_task_iterator(task_id); task_iterator != _tasks.end() && task_iterator->second.interval().has_value())
{
task->second.set_interval(interval);
const auto task_interval = task_iterator->second.interval().value();
auto task_next_start_time = task_iterator->first - task_interval;

const auto now = ssts::clock::now();
while(now > task_next_start_time)
task_next_start_time += interval;

task_iterator->second.set_interval(interval);
_tasks.emplace(std::move(task_next_start_time), std::move(task_iterator->second));
_tasks.erase(task_iterator);
lock.unlock();

_update_tasks_cv.notify_one();
return true;
}

return false;
}

Expand Down Expand Up @@ -493,11 +505,10 @@ class task_scheduler
{
std::scoped_lock lock(_update_tasks_mtx);

// if (!_is_duplicate_allowed && already_exists(st.hash()))
// return;
if (already_exists(st.hash()))
return;

_tasks.emplace(std::move(timepoint), std::move(st));
_next_task_timepoint = _tasks.begin()->first;
}

_update_tasks_cv.notify_one();
Expand Down
20 changes: 20 additions & 0 deletions scripts/valgrind/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cd ~/Downloads

sudo apt remove --purge valgrind -y

wget https://sourceware.org/pub/valgrind/valgrind-3.20.0.tar.bz2
tar xvf valgrind-3.20.0.tar.bz2

export CC=gcc-12

cd valgrind-3.20.0
./configure
make
sudo make install

cd ..
rm -r valgrind-3.20.0
rm -r valgrind-3.20.0.tar.bz2

echo ""
valgrind --version
17 changes: 17 additions & 0 deletions scripts/valgrind/run_callgrind.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
mkdir -p callgrind

valgrind --tool=callgrind \
--callgrind-out-file=callgrind/callgrind.out \
--verbose \
--log-file=callgrind/callgrind.log \
--error-exitcode=1 \
./build/Debug/examples/example_remove

# --exit-on-first-error=yes \

# callgrind_annotate --auto=yes callgrind.out

ret=$?
echo ""
echo "callgrind exit code: " $ret
exit $ret
19 changes: 19 additions & 0 deletions scripts/valgrind/run_memcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
mkdir -p memcheck

valgrind --tool=memcheck \
--leak-check=full \
--show-leak-kinds=all \
--track-origins=yes \
--verbose \
--xml=yes \
--xml-file=memcheck/memcheck.xml \
--log-file=memcheck/memcheck.log \
--error-exitcode=1 \
./build/Debug/examples/example_remove

# --exit-on-first-error=yes \

ret=$?
echo ""
echo "memcheck exit code: " $ret
exit $ret

0 comments on commit dfac66f

Please sign in to comment.