#include <simgrid/s4u/Activity.hpp>
#include <simgrid/s4u/Actor.hpp>
+#include <simgrid/s4u/Barrier.hpp>
#include <simgrid/s4u/Comm.hpp>
#include <simgrid/s4u/ConditionVariable.hpp>
#include <simgrid/s4u/Engine.hpp>
--- /dev/null
+/* Copyright (c) 2018. 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. */
+
+#ifndef SIMGRID_S4U_BARRIERHPP
+#define SIMGRID_S4U_BARRIER_HPP
+
+#include "simgrid/s4u/ConditionVariable.hpp"
+#include <simgrid/chrono.hpp>
+#include <simgrid/s4u/Mutex.hpp>
+
+#include <future>
+
+namespace simgrid {
+namespace s4u {
+
+class XBT_PUBLIC Barrier {
+private:
+ MutexPtr mutex_;
+ ConditionVariablePtr cond_;
+ unsigned int expected_processes_;
+ unsigned int arrived_processes_ = 0;
+
+public:
+ explicit Barrier(unsigned int count);
+ ~Barrier() = default;
+ Barrier(Barrier const&) = delete;
+ Barrier& operator=(Barrier const&) = delete;
+
+ int wait();
+};
+}
+} // namespace simgrid::s4u
+
+#endif
--- /dev/null
+/* Copyright (c) 2018. 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 <exception>
+#include <mutex>
+
+#include <xbt/ex.hpp>
+#include <xbt/log.hpp>
+
+#include "simgrid/s4u/Barrier.hpp"
+#include "simgrid/simix.h"
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_barrier, "S4U barrier");
+
+namespace simgrid {
+namespace s4u {
+
+Barrier::Barrier(unsigned int count) : expected_processes_(count)
+{
+ mutex_ = Mutex::create();
+ cond_ = ConditionVariable::create();
+}
+
+/**
+ * Wait functions
+ */
+int Barrier::wait()
+{
+ mutex_->lock();
+ arrived_processes_++;
+ XBT_DEBUG("waiting %p %u/%u", this, arrived_processes_, expected_processes_);
+ if (arrived_processes_ == expected_processes_) {
+ cond_->notify_all();
+ mutex_->unlock();
+ arrived_processes_ = 0;
+ return -1;
+ }
+
+ cond_->wait(mutex_);
+ mutex_->unlock();
+ return 0;
+}
+} // namespace s4u
+} // namespace simgrid
-foreach(x actor actor-autorestart
+foreach(x actor actor-autorestart actor-migration
comm-pt2pt
cloud-interrupt-migration
concurrent_rw storage_client_server host_on_off_wait listen_async pid )
## Add the tests.
## Some need to be run with all factories, some need not tesh to run
-foreach(x actor actor-autorestart cloud-interrupt-migration concurrent_rw)
+foreach(x actor actor-autorestart actor-migration cloud-interrupt-migration concurrent_rw)
set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.tesh)
ADD_TESH_FACTORIES(tesh-s4u-${x} "thread;ucontext;raw;boost" --setenv srcdir=${CMAKE_HOME_DIRECTORY}/teshsuite/s4u/${x} --setenv platfdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --cd ${CMAKE_BINARY_DIR}/teshsuite/s4u/${x} ${CMAKE_HOME_DIRECTORY}/teshsuite/s4u/${x}/${x}.tesh)
endforeach()
--- /dev/null
+/* Copyright (c) 2018. 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 "simgrid/s4u.hpp"
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_migration, "Messages specific for this s4u example");
+
+simgrid::s4u::Barrier* barrier;
+static simgrid::s4u::ActorPtr controlled_process;
+
+/* The Emigrant process will be moved from host to host. */
+static void emigrant()
+{
+ XBT_INFO("I'll look for a new job on another machine ('Boivin') where the grass is greener.");
+ simgrid::s4u::this_actor::migrate(
+ simgrid::s4u::Host::by_name("Boivin")); /* - First, move to another host by myself */
+
+ XBT_INFO("Yeah, found something to do");
+ simgrid::s4u::this_actor::execute(98095000); /* - Execute some work there */
+ simgrid::s4u::this_actor::sleep_for(2);
+ XBT_INFO("Moving back home after work");
+ simgrid::s4u::this_actor::migrate(simgrid::s4u::Host::by_name("Jacquelin")); /* - Move back to original location */
+ simgrid::s4u::this_actor::migrate(simgrid::s4u::Host::by_name("Boivin")); /* - Go back to the other host to sleep*/
+ simgrid::s4u::this_actor::sleep_for(4);
+ controlled_process = simgrid::s4u::Actor::self(); /* - Get controlled at checkpoint */
+ barrier->wait();
+ simgrid::s4u::this_actor::suspend();
+ simgrid::s4u::Host* h = simgrid::s4u::this_actor::get_host();
+ XBT_INFO("I've been moved on this new host: %s", h->get_cname());
+ XBT_INFO("Uh, nothing to do here. Stopping now");
+}
+
+/* The policeman check for emigrants and move them back to 'Jacquelin' */
+static void policeman()
+{
+ XBT_INFO("Wait at the checkpoint."); /* - block on the mutex+condition */
+ barrier->wait();
+ controlled_process->migrate(simgrid::s4u::Host::by_name("Jacquelin")); /* - Move an emigrant to Jacquelin */
+ XBT_INFO("I moved the emigrant");
+ controlled_process->resume();
+}
+
+int main(int argc, char* argv[])
+{
+ simgrid::s4u::Engine e(&argc, argv);
+ e.load_platform(argv[1]);
+
+ simgrid::s4u::Actor::create("emigrant", simgrid::s4u::Host::by_name("Jacquelin"), emigrant);
+ simgrid::s4u::Actor::create("policeman", simgrid::s4u::Host::by_name("Boivin"), policeman);
+
+ barrier = new simgrid::s4u::Barrier(2);
+ e.run();
+ XBT_INFO("Simulation time %g", e.get_clock());
+ delete barrier;
+
+ return 0;
+}
--- /dev/null
+#!/usr/bin/env tesh
+
+p Testing the migration feature of S4U
+
+! output sort 19
+$ $SG_TEST_EXENV ${bindir:=.}/actor-migration ${platfdir:=.}/small_platform.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n"
+> [ 0.000000] (1:emigrant@Jacquelin) I'll look for a new job on another machine ('Boivin') where the grass is greener.
+> [ 0.000000] (1:emigrant@Boivin) Yeah, found something to do
+> [ 0.000000] (2:policeman@Boivin) Wait at the checkpoint.
+> [ 3.000000] (1:emigrant@Boivin) Moving back home after work
+> [ 7.000000] (0:maestro@) Simulation time 7
+> [ 7.000000] (1:emigrant@Jacquelin) I've been moved on this new host: Jacquelin
+> [ 7.000000] (1:emigrant@Jacquelin) Uh, nothing to do here. Stopping now
+> [ 7.000000] (2:policeman@Boivin) I moved the emigrant
set(S4U_SRC
src/s4u/s4u_Actor.cpp
src/s4u/s4u_Activity.cpp
+ src/s4u/s4u_Barrier.cpp
src/s4u/s4u_ConditionVariable.cpp
src/s4u/s4u_Comm.cpp
src/s4u/s4u_Engine.cpp
include/simgrid/zone.h
include/simgrid/s4u/Activity.hpp
include/simgrid/s4u/Actor.hpp
+ include/simgrid/s4u/Barrier.hpp
include/simgrid/s4u/Comm.hpp
include/simgrid/s4u/ConditionVariable.hpp
include/simgrid/s4u/Engine.hpp