From df328e5d28cd1997742e7ccbd1fbd70576b740ba Mon Sep 17 00:00:00 2001 From: Millian Poquet Date: Sat, 17 Oct 2020 01:01:28 +0200 Subject: [PATCH] test condition variable wait_until+notify_all --- examples/s4u/CMakeLists.txt | 2 +- ...u-synchro-condition-variable-waituntil.cpp | 64 +++++++++++++++++++ ...-synchro-condition-variable-waituntil.tesh | 63 ++++++++++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 examples/s4u/synchro-condition-variable-waituntil/s4u-synchro-condition-variable-waituntil.cpp create mode 100644 examples/s4u/synchro-condition-variable-waituntil/s4u-synchro-condition-variable-waituntil.tesh diff --git a/examples/s4u/CMakeLists.txt b/examples/s4u/CMakeLists.txt index 0d5c32edae..c449b48efc 100644 --- a/examples/s4u/CMakeLists.txt +++ b/examples/s4u/CMakeLists.txt @@ -77,7 +77,7 @@ foreach (example actor-create actor-daemon actor-exiting actor-join actor-kill plugin-host-load plugin-link-load replay-comm replay-io routing-get-clusters - synchro-barrier synchro-condition-variable synchro-mutex synchro-semaphore) + synchro-barrier synchro-condition-variable synchro-condition-variable-waituntil synchro-mutex synchro-semaphore) # Use default source file unless specified otherwise if(NOT DEFINED _${example}_sources) diff --git a/examples/s4u/synchro-condition-variable-waituntil/s4u-synchro-condition-variable-waituntil.cpp b/examples/s4u/synchro-condition-variable-waituntil/s4u-synchro-condition-variable-waituntil.cpp new file mode 100644 index 0000000000..f41ac16f51 --- /dev/null +++ b/examples/s4u/synchro-condition-variable-waituntil/s4u-synchro-condition-variable-waituntil.cpp @@ -0,0 +1,64 @@ +/* Copyright (c) 2006-2020. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include /* std::mutex and std::lock_guard */ +#include /* All of S4U */ + +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category"); + +simgrid::s4u::MutexPtr mtx = nullptr; +simgrid::s4u::ConditionVariablePtr cv = nullptr; +bool ready = false; + +static void competitor(int id) +{ + XBT_INFO("Entering the race..."); + std::unique_lock lck(*mtx); + while (!ready) { + auto now = simgrid::s4u::Engine::get_clock(); + if (cv->wait_until(lck, now + (id+1)*0.25) == std::cv_status::timeout) { + XBT_INFO("Out of wait_until (timeout)"); + } + else { + XBT_INFO("Out of wait_until (YAY!)"); + } + } + XBT_INFO("Running!"); +} + +static void go() +{ + XBT_INFO("Are you ready? ..."); + simgrid::s4u::this_actor::sleep_for(3); + { + std::unique_lock lck(*mtx); + XBT_INFO("Go go go!"); + ready = true; + cv->notify_all(); + } +} + +static void main_actor() +{ + mtx = simgrid::s4u::Mutex::create(); + cv = simgrid::s4u::ConditionVariable::create(); + + auto host = simgrid::s4u::this_actor::get_host(); + for (int i = 0; i < 10; ++i) + simgrid::s4u::Actor::create("competitor", host, competitor, i); + simgrid::s4u::Actor::create("go", host, go); +} + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine e(&argc, argv); + e.load_platform("../../platforms/small_platform.xml"); + + auto host = simgrid::s4u::Host::by_name("Tremblay"); + simgrid::s4u::Actor::create("main", host, main_actor); + + e.run(); + return 0; +} diff --git a/examples/s4u/synchro-condition-variable-waituntil/s4u-synchro-condition-variable-waituntil.tesh b/examples/s4u/synchro-condition-variable-waituntil/s4u-synchro-condition-variable-waituntil.tesh new file mode 100644 index 0000000000..1053222de6 --- /dev/null +++ b/examples/s4u/synchro-condition-variable-waituntil/s4u-synchro-condition-variable-waituntil.tesh @@ -0,0 +1,63 @@ +#!/usr/bin/env tesh + +$ ${bindir:=.}/s4u-synchro-condition-variable-waituntil "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n" +> [ 0.000000] (2:competitor@Tremblay) Entering the race... +> [ 0.000000] (3:competitor@Tremblay) Entering the race... +> [ 0.000000] (4:competitor@Tremblay) Entering the race... +> [ 0.000000] (5:competitor@Tremblay) Entering the race... +> [ 0.000000] (6:competitor@Tremblay) Entering the race... +> [ 0.000000] (7:competitor@Tremblay) Entering the race... +> [ 0.000000] (8:competitor@Tremblay) Entering the race... +> [ 0.000000] (9:competitor@Tremblay) Entering the race... +> [ 0.000000] (10:competitor@Tremblay) Entering the race... +> [ 0.000000] (11:competitor@Tremblay) Entering the race... +> [ 0.000000] (12:go@Tremblay) Are you ready? ... +> [ 0.250000] (2:competitor@Tremblay) Out of wait_until (timeout) +> [ 0.500000] (3:competitor@Tremblay) Out of wait_until (timeout) +> [ 0.500000] (2:competitor@Tremblay) Out of wait_until (timeout) +> [ 0.750000] (4:competitor@Tremblay) Out of wait_until (timeout) +> [ 0.750000] (2:competitor@Tremblay) Out of wait_until (timeout) +> [ 1.000000] (5:competitor@Tremblay) Out of wait_until (timeout) +> [ 1.000000] (3:competitor@Tremblay) Out of wait_until (timeout) +> [ 1.000000] (2:competitor@Tremblay) Out of wait_until (timeout) +> [ 1.250000] (6:competitor@Tremblay) Out of wait_until (timeout) +> [ 1.250000] (2:competitor@Tremblay) Out of wait_until (timeout) +> [ 1.500000] (7:competitor@Tremblay) Out of wait_until (timeout) +> [ 1.500000] (4:competitor@Tremblay) Out of wait_until (timeout) +> [ 1.500000] (3:competitor@Tremblay) Out of wait_until (timeout) +> [ 1.500000] (2:competitor@Tremblay) Out of wait_until (timeout) +> [ 1.750000] (8:competitor@Tremblay) Out of wait_until (timeout) +> [ 1.750000] (2:competitor@Tremblay) Out of wait_until (timeout) +> [ 2.000000] (9:competitor@Tremblay) Out of wait_until (timeout) +> [ 2.000000] (5:competitor@Tremblay) Out of wait_until (timeout) +> [ 2.000000] (3:competitor@Tremblay) Out of wait_until (timeout) +> [ 2.000000] (2:competitor@Tremblay) Out of wait_until (timeout) +> [ 2.250000] (10:competitor@Tremblay) Out of wait_until (timeout) +> [ 2.250000] (4:competitor@Tremblay) Out of wait_until (timeout) +> [ 2.250000] (2:competitor@Tremblay) Out of wait_until (timeout) +> [ 2.500000] (11:competitor@Tremblay) Out of wait_until (timeout) +> [ 2.500000] (6:competitor@Tremblay) Out of wait_until (timeout) +> [ 2.500000] (3:competitor@Tremblay) Out of wait_until (timeout) +> [ 2.500000] (2:competitor@Tremblay) Out of wait_until (timeout) +> [ 2.750000] (2:competitor@Tremblay) Out of wait_until (timeout) +> [ 3.000000] (12:go@Tremblay) Go go go! +> [ 3.000000] (7:competitor@Tremblay) Out of wait_until (timeout) +> [ 3.000000] (7:competitor@Tremblay) Running! +> [ 3.000000] (5:competitor@Tremblay) Out of wait_until (timeout) +> [ 3.000000] (5:competitor@Tremblay) Running! +> [ 3.000000] (4:competitor@Tremblay) Out of wait_until (timeout) +> [ 3.000000] (4:competitor@Tremblay) Running! +> [ 3.000000] (3:competitor@Tremblay) Out of wait_until (timeout) +> [ 3.000000] (3:competitor@Tremblay) Running! +> [ 3.000000] (2:competitor@Tremblay) Out of wait_until (timeout) +> [ 3.000000] (2:competitor@Tremblay) Running! +> [ 3.000000] (8:competitor@Tremblay) Out of wait_until (YAY!) +> [ 3.000000] (8:competitor@Tremblay) Running! +> [ 3.000000] (9:competitor@Tremblay) Out of wait_until (YAY!) +> [ 3.000000] (9:competitor@Tremblay) Running! +> [ 3.000000] (10:competitor@Tremblay) Out of wait_until (YAY!) +> [ 3.000000] (10:competitor@Tremblay) Running! +> [ 3.000000] (11:competitor@Tremblay) Out of wait_until (YAY!) +> [ 3.000000] (11:competitor@Tremblay) Running! +> [ 3.000000] (6:competitor@Tremblay) Out of wait_until (YAY!) +> [ 3.000000] (6:competitor@Tremblay) Running! -- 2.20.1