Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add sg_actor_exit and sg_actor_on_exit
[simgrid.git] / src / msg / msg_process.cpp
1 /* Copyright (c) 2004-2020. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "msg_private.hpp"
7 #include "simgrid/Exception.hpp"
8 #include "simgrid/s4u/Host.hpp"
9 #include "src/simix/smx_private.hpp"
10
11 /******************************** Process ************************************/
12 /** @brief Creates and runs a new #msg_process_t.
13  *
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).
16  */
17 msg_process_t MSG_process_create(const char* name, int (*code)(int, char**), void* data, msg_host_t host)
18 {
19   return MSG_process_create_with_environment(name == nullptr ? "" : name, code, data, host, 0, nullptr, nullptr);
20 }
21
22 /** @brief Creates and runs a new process.
23
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
33  */
34
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)
37 {
38   return MSG_process_create_with_environment(name, code, data, host, argc, argv, nullptr);
39 }
40
41 /**
42  * @brief Creates and runs a new #msg_process_t.
43
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
55  * @see msg_process_t
56  * @return The new corresponding object.
57  */
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)
60 {
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);
63
64   try {
65     if (data != nullptr) {
66       sg_actor_data_set(actor, data);
67       xbt_dict_cursor_t cursor = nullptr;
68       char* key;
69       char* value;
70       xbt_dict_foreach (properties, cursor, key, value)
71         actor->set_property(key, value);
72     }
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());
76   }
77
78   xbt_dict_free(&properties);
79   for (int i = 0; i != argc; ++i)
80     xbt_free(argv[i]);
81   xbt_free(argv);
82
83   simgrid::s4u::this_actor::yield();
84   return actor;
85 }
86
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
89  */
90 XBT_PUBLIC void MSG_process_set_data_cleanup(void_f_pvoid_t data_cleanup)
91 {
92   msg_global->process_data_cleanup = data_cleanup;
93 }
94
95 /** @brief returns a list of all currently existing processes */
96 xbt_dynar_t MSG_processes_as_dynar() {
97   xbt_dynar_t res = xbt_dynar_new(sizeof(smx_actor_t), nullptr);
98   for (auto const& kv : simix_global->process_list) {
99     smx_actor_t actor = kv.second;
100     xbt_dynar_push(res, &actor);
101   }
102   return res;
103 }