1 /* Copyright (c) 2004-2021. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "msg_private.hpp"
7 #include "simgrid/Exception.hpp"
8 #include "simgrid/s4u/Host.hpp"
9 #include "src/simix/smx_private.hpp"
11 /******************************** Process ************************************/
12 /** @brief Creates and runs a new #msg_process_t.
14 * Does exactly the same as #MSG_process_create_with_arguments but without providing standard arguments
15 * (@a argc, @a argv, @a start_time, @a kill_time).
17 msg_process_t MSG_process_create(const char* name, int (*code)(int, char**), void* data, msg_host_t host)
19 return MSG_process_create_with_environment(name == nullptr ? "" : name, code, data, host, 0, nullptr, nullptr);
22 /** @brief Creates and runs a new process.
24 * A constructor for #msg_process_t taking four arguments and returning the corresponding object. The structure (and
25 * the corresponding thread) is created, and put in the list of ready process.
26 * @param name a name for the object. It is for user-level information and can be nullptr.
27 * @param code is a function describing the behavior of the process.
28 * @param data a pointer to any data one may want to attach to the new object. It is for user-level information and
29 * can be nullptr. It can be retrieved with the function MSG_process_get_data().
30 * @param host the location where the new process is executed.
31 * @param argc first argument passed to @a code
32 * @param argv second argument passed to @a code
35 msg_process_t MSG_process_create_with_arguments(const char* name, int (*code)(int, char**), void* data, msg_host_t host,
36 int argc, char** argv)
38 return MSG_process_create_with_environment(name, code, data, host, argc, argv, nullptr);
42 * @brief Creates and runs a new #msg_process_t.
44 * A constructor for #msg_process_t taking four arguments and returning the corresponding object. The structure (and
45 * the corresponding thread) is created, and put in the list of ready process.
46 * @param name a name for the object. It is for user-level information and can be nullptr.
47 * @param code is a function describing the behavior of the process.
48 * @param data a pointer to any data one may want to attach to the new object. It is for user-level information and
49 * can be nullptr. It can be retrieved with the function MSG_process_get_data().
50 * @param host the location where the new process is executed.
51 * @param argc first argument passed to @a code
52 * @param argv second argument passed to @a code. WARNING, these strings are freed by the SimGrid kernel when the
53 * process exits, so they cannot be static nor shared between several processes.
54 * @param properties list a properties defined for this process
56 * @return The new corresponding object.
58 msg_process_t MSG_process_create_with_environment(const char* name, int (*code)(int, char**), void* data,
59 msg_host_t host, int argc, char** argv, xbt_dict_t properties)
61 xbt_assert(host != nullptr, "Invalid parameters: host param must not be nullptr");
62 sg_actor_t actor = sg_actor_init(std::move(name), host);
65 if (data != nullptr) {
66 sg_actor_set_data(actor, data);
67 xbt_dict_cursor_t cursor = nullptr;
70 xbt_dict_foreach (properties, cursor, key, value)
71 actor->set_property(key, value);
73 actor->start(std::move(simgrid::xbt::wrap_main(code, argc, argv)));
74 } catch (simgrid::HostFailureException const&) {
75 xbt_die("Could not launch a new process on failed host %s.", host->get_cname());
78 xbt_dict_free(&properties);
79 for (int i = 0; i != argc; ++i)
83 simgrid::s4u::this_actor::yield();
87 /** @brief Sets a cleanup function to be called to free the userdata of a process when a process is destroyed.
88 * @param data_cleanup a cleanup function for the userdata of a process, or nullptr to call no function
90 XBT_PUBLIC void MSG_process_set_data_cleanup(void_f_pvoid_t data_cleanup)
92 msg_global->process_data_cleanup = data_cleanup;
95 /** @brief returns a list of all currently existing processes */
96 xbt_dynar_t MSG_processes_as_dynar() // XBT_ATTRIB_DEPRECATED_v330
98 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_actor_t), nullptr);
99 for (auto const& kv : simix_global->process_list) {
100 smx_actor_t actor = kv.second;
101 xbt_dynar_push(res, &actor);