set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/${example}/s4u_${example}.cpp)
endforeach()
+add_executable (s4u_basic_function basic/s4u_basic_function.cpp)
+target_link_libraries(s4u_basic_function simgrid)
+set_target_properties(s4u_basic_function PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/basic)
+
set(examples_src ${examples_src} PARENT_SCOPE)
set(tesh_files ${tesh_files} PARENT_SCOPE)
set(xml_files ${xml_files} ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/s4u_actions-comm_split_d.xml
#include <xbt/sysdep.h>
-#include "simgrid/s4u.h"
-
-XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
-
-class Worker {
-public:
- void operator()() {
- XBT_INFO("Hello s4u, I'm ready to serve");
- char *msg = static_cast<char*>(simgrid::s4u::this_actor::recv(
- *simgrid::s4u::Mailbox::byName("worker")));
- XBT_INFO("I received '%s'",msg);
- XBT_INFO("I'm done. See you.");
- }
-};
-
-class Master {
-public:
- void operator()() {
- const char *msg = "GaBuZoMeu";
- XBT_INFO("Hello s4u, I have something to send");
- simgrid::s4u::this_actor::send(*simgrid::s4u::Mailbox::byName("worker"), xbt_strdup(msg), strlen(msg));
- XBT_INFO("I'm done. See you.");
- }
-};
+#include <simgrid/s4u.h>
+#include "s4u_basic.h"
int main(int argc, char **argv) {
simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
--- /dev/null
+/* Copyright (c) 2006-2016. 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 <xbt/sysdep.h>
+
+#include "simgrid/s4u.h"
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
+
+class Worker {
+public:
+ Worker() {};
+ Worker(simgrid::xbt::args args) {}
+ void operator()() {
+ XBT_INFO("Hello s4u, I'm ready to serve");
+ char *msg = static_cast<char*>(simgrid::s4u::this_actor::recv(
+ *simgrid::s4u::Mailbox::byName("worker")));
+ XBT_INFO("I received '%s'",msg);
+ XBT_INFO("I'm done. See you.");
+ }
+};
+
+class Master {
+public:
+ Master() {};
+ Master(simgrid::xbt::args args) {}
+ void operator()() {
+ const char *msg = "GaBuZoMeu";
+ XBT_INFO("Hello s4u, I have something to send");
+ simgrid::s4u::this_actor::send(*simgrid::s4u::Mailbox::byName("worker"), xbt_strdup(msg), strlen(msg));
+ XBT_INFO("I'm done. See you.");
+ }
+};
> [Tremblay:worker:(0) 0.001301] [s4u_test/INFO] I received 'GaBuZoMeu'
> [Tremblay:worker:(0) 0.001301] [s4u_test/INFO] I'm done. See you.
> [Jupiter:master:(0) 0.001301] [s4u_test/INFO] I'm done. See you.
+
+$ $SG_TEST_EXENV ${bindir:=.}/s4u_basic_function
+> [Tremblay:worker:(0) 0.000000] [s4u_test/INFO] Hello s4u, I'm ready to serve
+> [Jupiter:master:(0) 0.000000] [s4u_test/INFO] Hello s4u, I have something to send
+> [Tremblay:worker:(0) 0.001301] [s4u_test/INFO] I received 'GaBuZoMeu'
+> [Tremblay:worker:(0) 0.001301] [s4u_test/INFO] I'm done. See you.
+> [Jupiter:master:(0) 0.001301] [s4u_test/INFO] I'm done. See you.
--- /dev/null
+/* Copyright (c) 2006-2016. 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 <xbt/sysdep.h>
+
+#include <simgrid/s4u.h>
+
+#include "s4u_basic.h"
+
+int main(int argc, char **argv) {
+ simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
+ e->loadPlatform("../../platforms/two_hosts.xml");
+ e->registerFunction<Worker>("worker");
+ e->registerFunction<Master>("master");
+ simgrid::xbt::args args;
+ simgrid::s4u::Actor("worker", simgrid::s4u::Host::by_name("Tremblay"), "worker", args);
+ simgrid::s4u::Actor("master", simgrid::s4u::Host::by_name("Jupiter"), "master", args);
+ e->run();
+ return 0;
+}
Actor(name, host, wrap_task(std::move(code), std::move(args)...))
{}
+ // Create actor from function name:
+
+ Actor(const char* name, s4u::Host *host, double killTime, const char* function, simgrid::xbt::args args);
+
+ Actor(const char* name, s4u::Host *host, const char* function, simgrid::xbt::args args) :
+ Actor(name, host, -1.0, function, std::move(args)) {}
+
/** Retrieves the actor that have the given PID (or NULL if not existing) */
//static Actor *byPid(int pid); not implemented
#define SIMGRID_S4U_ENGINE_HPP
#include <xbt/base.h>
+#include <xbt/functional.hpp>
+
+#include <simgrid/simix.hpp>
#include <simgrid/s4u/forward.hpp>
/** @brief Retrieve the AS of the given name (or nullptr if not found) */
simgrid::s4u::As *asByNameOrNull(const char *name);
+ template<class F>
+ void registerFunction(const char* name)
+ {
+ simgrid::simix::registerFunction(name, [](simgrid::xbt::args args){
+ return simgrid::simix::ActorCode([args] {
+ F code(std::move(args));
+ code();
+ });
+ });
+ }
+
+ template<class F>
+ void registerFunction(const char* name, F code)
+ {
+ simgrid::simix::registerFunction(name, [code](simgrid::xbt::args args){
+ return simgrid::simix::ActorCode([code,args] {
+ code(std::move(args));
+ });
+ });
+ }
+
private:
static s4u::Engine *instance_;
};
#include <xbt/function_types.h>
#include <xbt/future.hpp>
+#include <xbt/functional.hpp>
#include <simgrid/simix.h>
XBT_PUBLIC(void) set_maestro(std::function<void()> code);
XBT_PUBLIC(void) create_maestro(std::function<void()> code);
+// What's executed as SIMIX actor code:
+typedef std::function<void()> ActorCode;
+
+// Create ActorCode based on argv:
+typedef std::function<ActorCode(simgrid::xbt::args args)> ActorCodeFactory;
+
+XBT_PUBLIC(void) registerFunction(const char* name, ActorCodeFactory factory);
+
}
}
#include "simgrid/s4u/host.hpp"
#include "simgrid/s4u/mailbox.hpp"
+#include "src/simix/smx_private.h"
+
XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor,"S4U actors");
using namespace simgrid;
killTime, nullptr, 0));
}
+s4u::Actor::Actor(const char* name, s4u::Host *host, double killTime, const char* function, simgrid::xbt::args args)
+{
+ simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
+ simgrid::simix::ActorCode code = factory(std::move(args));
+ this->pimpl_ = SIMIX_process_ref(simcall_process_create(
+ name, std::move(code), nullptr, host->name().c_str(),
+ killTime, nullptr, 0));
+}
+
void s4u::Actor::join() {
simcall_process_join(pimpl_, -1);
}
process.on_failure = SURF_PROCESS_ON_FAILURE_DIE;
sg_platf_new_process(&process);
}
+
+namespace simgrid {
+namespace simix {
+
+void registerFunction(const char* name, ActorCodeFactory factory)
+{
+ simix_global->registered_functions[name] = std::move(factory);
+}
+
+}
+}
\ No newline at end of file
#include <xbt/functional.hpp>
+
#include "src/internal_config.h"
#include "simgrid/simix.h"
#include "surf/surf.h"
namespace simgrid {
namespace simix {
-// What's executed as SIMIX actor code:
-typedef std::function<void()> ActorCode;
-
-// Create ActorCode based on argv:
-typedef std::function<ActorCode(simgrid::xbt::args args)> ActorCodeFactory;
-
class Global {
public:
smx_context_factory_t context_factory = nullptr;