Logo AND Algorithmique Numérique Distribuée

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