Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / simix / ActorImpl.hpp
1 /* Copyright (c) 2007-2016. 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 #ifndef SIMIX_ACTORIMPL_H
7 #define SIMIX_ACTORIMPL_H
8
9 #include "simgrid/s4u/Actor.hpp"
10 #include "src/simix/popping_private.h"
11 #include "xbt/swag.h"
12 #include <list>
13
14 typedef struct s_smx_process_exit_fun {
15   int_f_pvoid_pvoid_t fun;
16   void *arg;
17 } s_smx_process_exit_fun_t;
18 typedef s_smx_process_exit_fun_t* smx_process_exit_fun_t;
19
20 namespace simgrid {
21 namespace simix {
22
23 class ProcessArg {
24 public:
25   std::string name;
26   std::function<void()> code;
27   void *data            = nullptr;
28   sg_host_t host        = nullptr;
29   double kill_time      = 0.0;
30   xbt_dict_t properties = nullptr;
31   bool auto_restart     = false;
32 };
33
34 class ActorImpl {
35 public:
36   ActorImpl() : piface_(this) {}
37   ~ActorImpl();
38
39   // TODO, replace with boost intrusive container hooks
40   s_xbt_swag_hookup_t process_hookup   = { nullptr, nullptr }; /* simix_global->process_list */
41   s_xbt_swag_hookup_t synchro_hookup   = { nullptr, nullptr }; /* {mutex,cond,sem}->sleeping */
42   s_xbt_swag_hookup_t host_proc_hookup = { nullptr, nullptr }; /* smx_host->process_lis */
43   s_xbt_swag_hookup_t destroy_hookup   = { nullptr, nullptr }; /* simix_global->process_to_destroy */
44
45   aid_t pid  = 0;
46   aid_t ppid = -1;
47   simgrid::xbt::string name;
48   const char* cname() { return name.c_str(); }
49   s4u::Host* host       = nullptr; /* the host on which the process is running */
50   smx_context_t context = nullptr; /* the context (uctx/raw/thread) that executes the user function */
51
52   // TODO, pack them
53   std::exception_ptr exception;
54   bool finished     = false;
55   bool blocked      = false;
56   bool suspended    = false;
57   bool auto_restart = false;
58
59   sg_host_t new_host             = nullptr; /* if not null, the host on which the process must migrate to */
60   smx_activity_t waiting_synchro = nullptr; /* the current blocking synchro if any */
61   std::list<smx_activity_t> comms;          /* the current non-blocking communication synchros */
62   xbt_dict_t properties         = nullptr;
63   s_smx_simcall_t simcall;
64   void* userdata = nullptr;                      /* kept for compatibility, it should be replaced with moddata */
65   std::vector<s_smx_process_exit_fun_t> on_exit; /* list of functions executed when the process dies */
66
67   std::function<void()> code;
68   smx_timer_t kill_timer = nullptr;
69   int segment_index = -1; /* Reference to an SMPI process' data segment. Default value is -1 if not in SMPI context*/
70
71   /* Refcounting */
72 private:
73   std::atomic_int_fast32_t refcount_{0};
74
75 public:
76   friend void intrusive_ptr_add_ref(ActorImpl* process)
77   {
78     // std::memory_order_relaxed ought to be enough here instead of std::memory_order_seq_cst
79     // But then, we have a threading issue when an actor commits a suicide:
80     //  it seems that in this case, the worker thread kills the last occurrence of the actor
81     //  while usually, the maestro does so. FIXME: we should change how actors suicide
82     process->refcount_.fetch_add(1, std::memory_order_seq_cst);
83   }
84   friend void intrusive_ptr_release(ActorImpl* process)
85   {
86     // inspired from http://www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html
87     if (process->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
88       // Make sure that any changes done on other threads before their acquire are committed before our delete
89       // http://stackoverflow.com/questions/27751025/why-is-an-acquire-barrier-needed-before-deleting-the-data-in-an-atomically-refer
90       std::atomic_thread_fence(std::memory_order_acquire);
91       delete process;
92     }
93   }
94
95   /* S4U/implem interfaces */
96 private:
97   simgrid::s4u::Actor piface_; // Our interface is part of ourselves
98 public:
99   simgrid::s4u::ActorPtr iface() { return s4u::ActorPtr(&piface_); }
100   simgrid::s4u::Actor* ciface() { return &piface_; }
101
102   /* Daemon actors are automatically killed when the last non-daemon leaves */
103 private:
104   bool daemon = false;
105 public:
106   void daemonize();
107   bool isDaemon() { return daemon; } /** Whether this actor has been daemonized */
108   bool isSuspended() { return suspended; }
109   simgrid::s4u::Actor* restart();
110   smx_activity_t suspend(ActorImpl* issuer);
111   void resume();
112   smx_activity_t sleep(double duration);
113   void setUserData(void* data) { userdata = data; }
114   void* getUserData() { return userdata; }
115 };
116
117 }
118 }
119
120 typedef simgrid::simix::ProcessArg *smx_process_arg_t;
121
122 typedef simgrid::simix::ActorImpl* smx_actor_t;
123
124 SG_BEGIN_DECL()
125
126 XBT_PRIVATE smx_actor_t SIMIX_process_create(
127                           const char *name,
128                           std::function<void()> code,
129                           void *data,
130                           sg_host_t host,
131                           xbt_dict_t properties,
132                           smx_actor_t parent_process);
133
134 XBT_PRIVATE void SIMIX_process_runall();
135 XBT_PRIVATE void SIMIX_process_kill(smx_actor_t process, smx_actor_t issuer);
136 XBT_PRIVATE void SIMIX_process_killall(smx_actor_t issuer, int reset_pid);
137 XBT_PRIVATE void SIMIX_process_cleanup(smx_actor_t arg);
138 XBT_PRIVATE void SIMIX_process_empty_trash();
139 XBT_PRIVATE void SIMIX_process_yield(smx_actor_t self);
140 XBT_PRIVATE void SIMIX_process_exception_terminate(xbt_ex_t * e);
141 XBT_PRIVATE void SIMIX_process_change_host(smx_actor_t process, sg_host_t dest);
142 XBT_PRIVATE void SIMIX_process_set_data(smx_actor_t process, void *data);
143 XBT_PRIVATE smx_actor_t SIMIX_process_get_by_name(const char* name);
144
145 XBT_PRIVATE void SIMIX_process_auto_restart_set(smx_actor_t process, int auto_restart);
146
147 extern void (*SMPI_switch_data_segment)(int dest);
148
149 SG_END_DECL()
150
151 XBT_PRIVATE void SIMIX_process_sleep_destroy(smx_activity_t synchro);
152 XBT_PRIVATE smx_activity_t SIMIX_process_join(smx_actor_t issuer, smx_actor_t process, double timeout);
153
154 #endif