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 <atomic>
10 #include <functional>
11 #include <string>
12
13 #include <xbt/base.h>
14 #include <xbt/string.hpp>
15
16 #include <simgrid/simix.hpp>
17 #include <simgrid/s4u/Actor.hpp>
18
19 #include "simgrid/simix.h"
20 #include "popping_private.h"
21
22 typedef struct s_smx_process_exit_fun {
23   int_f_pvoid_pvoid_t fun;
24   void *arg;
25 } s_smx_process_exit_fun_t, *smx_process_exit_fun_t;
26
27 namespace simgrid {
28 namespace simix {
29
30 class ProcessArg {
31 public:
32   std::string name;
33   std::function<void()> code;
34   void *data            = nullptr;
35   const char *hostname  = nullptr;
36   double kill_time      = 0.0;
37   xbt_dict_t properties = nullptr;
38   bool auto_restart     = false;
39 };
40
41 class ActorImpl {
42 public:
43   ActorImpl() : piface_(this) {}
44
45   // TODO, replace with boost intrusive container hooks
46   s_xbt_swag_hookup_t process_hookup   = { nullptr, nullptr }; /* simix_global->process_list */
47   s_xbt_swag_hookup_t synchro_hookup   = { nullptr, nullptr }; /* {mutex,cond,sem}->sleeping */
48   s_xbt_swag_hookup_t host_proc_hookup = { nullptr, nullptr }; /* smx_host->process_lis */
49   s_xbt_swag_hookup_t destroy_hookup   = { nullptr, nullptr }; /* simix_global->process_to_destroy */
50
51   unsigned long pid  = 0;
52   unsigned long ppid = 0;
53   simgrid::xbt::string name;
54   sg_host_t host        = nullptr; /* the host on which the process is running */
55   smx_context_t context = nullptr; /* the context (uctx/raw/thread) that executes the user function */
56
57   // TODO, pack them
58   std::exception_ptr exception;
59   bool finished     = false;
60   bool blocked      = false;
61   bool suspended    = false;
62   bool auto_restart = false;
63
64   sg_host_t new_host            = nullptr; /* if not null, the host on which the process must migrate to */
65   smx_synchro_t waiting_synchro = nullptr; /* the current blocking synchro if any */
66   xbt_fifo_t comms              = nullptr; /* the current non-blocking communication synchros */
67   xbt_dict_t properties         = nullptr;
68   s_smx_simcall_t simcall;
69   void *data          = nullptr; /* kept for compatibility, it should be replaced with moddata */
70   xbt_dynar_t on_exit = nullptr; /* list of functions executed when the process dies */
71
72   std::function<void()> code;
73   smx_timer_t kill_timer = nullptr;
74   int segment_index      = 0; /* Reference to an SMPI process' data segment. Default value is -1 if not in SMPI context*/
75
76   friend void intrusive_ptr_add_ref(ActorImpl* process)
77   {
78     // Atomic operation! Do not split in two instructions!
79     auto previous = (process->refcount_)++;
80     xbt_assert(previous != 0);
81     (void) previous;
82   }
83   friend void intrusive_ptr_release(ActorImpl* process)
84   {
85     // Atomic operation! Do not split in two instructions!
86     auto count = --(process->refcount_);
87     if (count == 0)
88       delete process;
89   }
90
91   ~ActorImpl();
92
93   simgrid::s4u::Actor& getIface() { return piface_; }
94
95 private:
96   std::atomic_int_fast32_t refcount_ { 1 };
97   simgrid::s4u::Actor piface_;
98 };
99
100 }
101 }
102
103 typedef simgrid::simix::ProcessArg *smx_process_arg_t;
104
105 typedef simgrid::simix::ActorImpl* smx_process_t;
106
107 SG_BEGIN_DECL()
108
109 XBT_PRIVATE smx_process_t SIMIX_process_create(
110                           const char *name,
111                           std::function<void()> code,
112                           void *data,
113                           const char *hostname,
114                           double kill_time,
115                           xbt_dict_t properties,
116                           int auto_restart,
117                           smx_process_t parent_process);
118
119 XBT_PRIVATE void SIMIX_process_runall();
120 XBT_PRIVATE void SIMIX_process_kill(smx_process_t process, smx_process_t issuer);
121 XBT_PRIVATE void SIMIX_process_killall(smx_process_t issuer, int reset_pid);
122 XBT_PRIVATE void SIMIX_process_stop(smx_process_t arg);
123 XBT_PRIVATE void SIMIX_process_cleanup(smx_process_t arg);
124 XBT_PRIVATE void SIMIX_process_empty_trash();
125 XBT_PRIVATE void SIMIX_process_yield(smx_process_t self);
126 XBT_PRIVATE void SIMIX_process_exception_terminate(xbt_ex_t * e);
127 XBT_PRIVATE void SIMIX_process_change_host(smx_process_t process, sg_host_t dest);
128 XBT_PRIVATE smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer);
129 XBT_PRIVATE void SIMIX_process_resume(smx_process_t process, smx_process_t issuer);
130 XBT_PRIVATE int SIMIX_process_get_PID(smx_process_t self);
131 XBT_PRIVATE void* SIMIX_process_get_data(smx_process_t process);
132 XBT_PRIVATE void SIMIX_process_set_data(smx_process_t process, void *data);
133 XBT_PRIVATE smx_process_t SIMIX_process_get_by_name(const char* name);
134 XBT_PRIVATE int SIMIX_process_is_suspended(smx_process_t process);
135 XBT_PRIVATE xbt_dict_t SIMIX_process_get_properties(smx_process_t process);
136 XBT_PRIVATE smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout);
137 XBT_PRIVATE smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration);
138
139 XBT_PRIVATE void SIMIX_process_sleep_destroy(smx_synchro_t synchro);
140 XBT_PRIVATE void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart);
141 XBT_PRIVATE smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer);
142
143 void SIMIX_segment_index_set(smx_process_t process, int segment_index);
144 extern void (*SMPI_switch_data_segment)(int dest);
145
146 SG_END_DECL()
147
148 #endif