Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move some content to the SynchroRaw dtor
[simgrid.git] / src / simix / smx_process.cpp
1 /* Copyright (c) 2007-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 #include "src/surf/surf_interface.hpp"
8 #include "smx_private.h"
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "xbt/dict.h"
12 #include "mc/mc.h"
13 #include "src/mc/mc_replay.h"
14 #include "src/mc/Client.hpp"
15 #include "src/msg/msg_private.h"
16
17 #include "src/simix/SynchroSleep.hpp"
18 #include "src/simix/SynchroRaw.hpp"
19 #include "src/simix/SynchroIo.hpp"
20
21 #ifdef HAVE_SMPI
22 #include "src/smpi/private.h"
23 #endif
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
26
27 unsigned long simix_process_maxpid = 0;
28
29 /**
30  * \brief Returns the current agent.
31  *
32  * This functions returns the currently running SIMIX process.
33  *
34  * \return The SIMIX process
35  */
36 smx_process_t SIMIX_process_self(void)
37 {
38   smx_context_t self_context = SIMIX_context_self();
39
40   return self_context ? self_context->process() : NULL;
41 }
42
43 /**
44  * \brief Returns whether a process has pending asynchronous communications.
45  * \return true if there are asynchronous communications in this process
46  */
47 int SIMIX_process_has_pending_comms(smx_process_t process) {
48
49   return xbt_fifo_size(process->comms) > 0;
50 }
51
52 /**
53  * \brief Moves a process to the list of processes to destroy.
54  */
55 void SIMIX_process_cleanup(smx_process_t process)
56 {
57   XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p",
58       process->name, process, process->waiting_synchro);
59
60   SIMIX_process_on_exit_runall(process);
61
62   /* Unregister from the kill timer if any */
63   if (process->kill_timer != NULL)
64       SIMIX_timer_remove(process->kill_timer);
65
66   xbt_os_mutex_acquire(simix_global->mutex);
67
68   /* cancel non-blocking communications */
69   smx_synchro_t synchro;
70   while ((synchro = (smx_synchro_t) xbt_fifo_pop(process->comms))) {
71     simgrid::simix::Comm *comm = static_cast<simgrid::simix::Comm*>(synchro);
72
73     /* make sure no one will finish the comm after this process is destroyed,
74      * because src_proc or dst_proc would be an invalid pointer */
75     comm->cancel();
76
77     if (comm->src_proc == process) {
78       XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
79           comm, comm->detached, (int)comm->state, comm->src_proc, comm->dst_proc);
80       comm->src_proc = NULL;
81
82       /* I'm not supposed to destroy a detached comm from the sender side, */
83       if (comm->detached)
84         XBT_DEBUG("Don't destroy it since it's a detached comm and I'm the sender");
85       else
86         comm->unref();
87
88     }
89     else if (comm->dst_proc == process){
90       XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
91           comm, (int)comm->state, comm->src_proc, comm->dst_proc);
92       comm->dst_proc = NULL;
93
94       if (comm->detached && /* FIXME: This code should be moved within comm->unref() anyway. comm->refcount == 1 &&*/ comm->src_proc != NULL) {
95         /* the comm will be freed right now, remove it from the sender */
96         xbt_fifo_remove(comm->src_proc->comms, comm);
97       }
98       comm->unref();
99     } else {
100       xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro);
101     }
102   }
103
104   XBT_DEBUG("%p should not be run anymore",process);
105   xbt_swag_remove(process, simix_global->process_list);
106   if (process->host)
107     xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
108   xbt_swag_insert(process, simix_global->process_to_destroy);
109   process->context->iwannadie = 0;
110
111   xbt_os_mutex_release(simix_global->mutex);
112 }
113
114 /**
115  * Garbage collection
116  *
117  * Should be called some time to time to free the memory allocated for processes
118  * that have finished (or killed).
119  */
120 void SIMIX_process_empty_trash(void)
121 {
122   smx_process_t process = NULL;
123
124   while ((process = (smx_process_t) xbt_swag_extract(simix_global->process_to_destroy))) {
125     XBT_DEBUG("Getting rid of %p",process);
126
127     delete process->context;
128
129     /* Free the exception allocated at creation time */
130     free(process->running_ctx);
131     xbt_dict_free(&process->properties);
132
133     xbt_fifo_free(process->comms);
134
135     xbt_dynar_free(&process->on_exit);
136
137     xbt_free(process->name);
138     xbt_free(process);
139   }
140 }
141
142 namespace simgrid {
143 namespace simix {
144
145 void create_maestro(std::function<void()> code)
146 {
147   smx_process_t maestro = NULL;
148   /* Create maestro process and intilialize it */
149   maestro = xbt_new0(s_smx_process_t, 1);
150   maestro->pid = simix_process_maxpid++;
151   maestro->ppid = -1;
152   maestro->name = (char*) "";
153   maestro->data = nullptr;
154   maestro->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
155   XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
156
157   if (!code) {
158     maestro->context = SIMIX_context_new(NULL, 0, nullptr, NULL, maestro);
159   } else {
160     if (!simix_global)
161       xbt_die("simix is not initialized, please call MSG_init first");
162     maestro->context =
163       simix_global->context_factory->create_maestro(code, maestro);
164   }
165
166   maestro->simcall.issuer = maestro;
167   simix_global->maestro_process = maestro;
168 }
169
170 }
171 }
172
173 /**
174  * \brief Creates and runs the maestro process
175  */
176 void SIMIX_maestro_create(void (*code)(void*), void* data)
177 {
178   simgrid::simix::create_maestro(std::bind(code, data));
179 }
180
181 /**
182  * \brief Stops a process.
183  *
184  * Stops the process, execute all the registered on_exit functions,
185  * register it to the list of the process to restart if needed
186  * and stops its context.
187  */
188 void SIMIX_process_stop(smx_process_t arg) {
189   /* execute the on_exit functions */
190   SIMIX_process_on_exit_runall(arg);
191   /* Add the process to the list of process to restart, only if the host is down */
192   if (arg->auto_restart && arg->host->isOff()) {
193     SIMIX_host_add_auto_restart_process(arg->host,arg->name,arg->code, arg->data,
194                                         sg_host_get_name(arg->host),
195                                         SIMIX_timer_get_date(arg->kill_timer),
196                                         arg->argc,arg->argv,arg->properties,
197                                         arg->auto_restart);
198   }
199   XBT_DEBUG("Process %s (%s) is dead",arg->name,sg_host_get_name(arg->host));
200   arg->context->stop();
201 }
202
203 /**
204  * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
205  * This function frees the argument.
206  * \return the process created
207  */
208 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
209
210   smx_process_t process = simix_global->create_process_function(
211                                         args->name,
212                                         args->code,
213                                         args->data,
214                                         args->hostname,
215                                         args->kill_time,
216                                         args->argc,
217                                         args->argv,
218                                         args->properties,
219                                         args->auto_restart,
220                                         NULL);
221   xbt_free(args);
222   return process;
223 }
224
225
226 void* simcall_HANDLER_process_create(smx_simcall_t simcall,
227                           const char *name,
228                           xbt_main_func_t code,
229                           void *data,
230                           const char *hostname,
231                           double kill_time,
232                           int argc, char **argv,
233                           xbt_dict_t properties,
234                           int auto_restart){
235   return (void*)SIMIX_process_create(name, code, data, hostname,
236                        kill_time, argc, argv, properties, auto_restart,
237                        simcall->issuer);
238 }
239
240 static void kill_process(void* process)
241 {
242   simix_global->kill_process_function((smx_process_t) process);
243 }
244
245 /**
246  * \brief Internal function to create a process.
247  *
248  * This function actually creates the process.
249  * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
250  * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
251  *
252  * \return the process created
253  */
254 smx_process_t SIMIX_process_create(
255                           const char *name,
256                           xbt_main_func_t code,
257                           void *data,
258                           const char *hostname,
259                           double kill_time,
260                           int argc, char **argv,
261                           xbt_dict_t properties,
262                           int auto_restart,
263                           smx_process_t parent_process)
264 {
265   smx_process_t process = NULL;
266   sg_host_t host = sg_host_by_name(hostname);
267
268   XBT_DEBUG("Start process %s on host '%s'", name, hostname);
269
270   if (host->isOff()) {
271     int i;
272     XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
273           hostname);
274     for (i = 0; i < argc; i++)
275       xbt_free(argv[i]);
276     xbt_free(argv);
277   }
278   else {
279     process = xbt_new0(s_smx_process_t, 1);
280
281     xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
282     /* Process data */
283     process->pid = simix_process_maxpid++;
284     process->name = xbt_strdup(name);
285     process->host = host;
286     process->data = data;
287     process->comms = xbt_fifo_new();
288     process->simcall.issuer = process;
289     /* Initiliaze data segment to default value */
290     SIMIX_segment_index_set(process, -1);
291
292      if (parent_process != NULL) {
293        process->ppid = SIMIX_process_get_PID(parent_process);
294        /* SMPI process have their own data segment and
295           each other inherit from their father */
296 #if HAVE_SMPI
297        if(smpi_privatize_global_variables){
298          if( parent_process->pid != 0){
299            SIMIX_segment_index_set(process, parent_process->segment_index);
300          } else {
301            SIMIX_segment_index_set(process, process->pid - 1);
302          }
303        }
304 #endif
305      } else {
306        process->ppid = -1;
307      }
308
309     /* Process data for auto-restart */
310     process->auto_restart = auto_restart;
311     process->code = code;
312     process->argc = argc;
313     process->argv = argv;
314
315
316     XBT_VERB("Create context %s", process->name);
317     process->context = SIMIX_context_new(code, argc, argv, simix_global->cleanup_process_function, process);
318
319     process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
320     XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
321
322     if(MC_is_active()){
323       MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
324     }
325
326     /* Add properties */
327     process->properties = properties;
328
329     /* Add the process to it's host process list */
330     xbt_swag_insert(process, sg_host_simix(host)->process_list);
331
332     XBT_DEBUG("Start context '%s'", process->name);
333
334     /* Now insert it in the global process list and in the process to run list */
335     xbt_swag_insert(process, simix_global->process_list);
336     XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_get_name(host));
337     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
338
339     if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
340       XBT_DEBUG("Process %s(%s) will be kill at time %f", process->name,
341           sg_host_get_name(process->host), kill_time);
342       process->kill_timer = SIMIX_timer_set(kill_time, kill_process, process);
343     }
344
345     /* Tracing the process creation */
346     TRACE_msg_process_create(process->name, process->pid, process->host);
347   }
348   return process;
349 }
350
351 smx_process_t SIMIX_process_attach(
352   const char* name,
353   void *data,
354   const char* hostname,
355   xbt_dict_t properties,
356   smx_process_t parent_process)
357 {
358   // This is mostly a copy/paste from SIMIX_process_new(),
359   // it'd be nice to share some code between those two functions.
360
361   sg_host_t host = sg_host_by_name(hostname);
362   XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
363
364   if (host->isOff()) {
365     XBT_WARN("Cannot launch process '%s' on failed host '%s'",
366       name, hostname);
367     return nullptr;
368   }
369
370   smx_process_t process = xbt_new0(s_smx_process_t, 1);
371   /* Process data */
372   process->pid = simix_process_maxpid++;
373   process->name = xbt_strdup(name);
374   process->host = host;
375   process->data = data;
376   process->comms = xbt_fifo_new();
377   process->simcall.issuer = process;
378   process->ppid = -1;
379   /* Initiliaze data segment to default value */
380   SIMIX_segment_index_set(process, -1);
381   if (parent_process != NULL) {
382     process->ppid = SIMIX_process_get_PID(parent_process);
383    /* SMPI process have their own data segment and
384       each other inherit from their father */
385   #if HAVE_SMPI
386     if(smpi_privatize_global_variables){
387       if(parent_process->pid != 0){
388         SIMIX_segment_index_set(process, parent_process->segment_index);
389       } else {
390         SIMIX_segment_index_set(process, process->pid - 1);
391       }
392     }
393   #endif
394   }
395
396   /* Process data for auto-restart */
397   process->auto_restart = false;
398   process->code = nullptr;
399   process->argc = 0;
400   process->argv = nullptr;
401
402   XBT_VERB("Create context %s", process->name);
403   if (!simix_global)
404     xbt_die("simix is not initialized, please call MSG_init first");
405   process->context = simix_global->context_factory->attach(
406     simix_global->cleanup_process_function, process);
407
408   process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
409   XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
410
411   if(MC_is_active()){
412     MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
413   }
414
415   /* Add properties */
416   process->properties = properties;
417
418   /* Add the process to it's host process list */
419   xbt_swag_insert(process, sg_host_simix(host)->process_list);
420
421   /* Now insert it in the global process list and in the process to run list */
422   xbt_swag_insert(process, simix_global->process_list);
423   XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_get_name(host));
424   xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
425
426   /* Tracing the process creation */
427   TRACE_msg_process_create(process->name, process->pid, process->host);
428
429   auto context = dynamic_cast<simgrid::simix::AttachContext*>(process->context);
430   if (!context)
431     xbt_die("Not a suitable context");
432
433   context->attach_start();
434   return process;
435 }
436
437 void SIMIX_process_detach(void)
438 {
439   auto context = dynamic_cast<simgrid::simix::AttachContext*>(SIMIX_context_self());
440   if (!context)
441     xbt_die("Not a suitable context");
442
443   simix_global->cleanup_process_function(context->process());
444
445   // Let maestro ignore we are still alive:
446   // xbt_swag_remove(context->process(), simix_global->process_list);
447
448   // TODDO, Remove from proces list:
449   //   xbt_swag_remove(process, sg_host_simix(host)->process_list);
450
451   context->attach_stop();
452   // delete context;
453 }
454
455 /**
456  * \brief Executes the processes from simix_global->process_to_run.
457  *
458  * The processes of simix_global->process_to_run are run (in parallel if
459  * possible).  On exit, simix_global->process_to_run is empty, and
460  * simix_global->process_that_ran contains the list of processes that just ran.
461  * The two lists are swapped so, be careful when using them before and after a
462  * call to this function.
463  */
464 void SIMIX_process_runall(void)
465 {
466   SIMIX_context_runall();
467
468   xbt_dynar_t tmp = simix_global->process_that_ran;
469   simix_global->process_that_ran = simix_global->process_to_run;
470   simix_global->process_to_run = tmp;
471   xbt_dynar_reset(simix_global->process_to_run);
472 }
473
474 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
475   SIMIX_process_kill(process, simcall->issuer);
476 }
477 /**
478  * \brief Internal function to kill a SIMIX process.
479  *
480  * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
481  * or directly for SIMIX internal purposes.
482  *
483  * \param process poor victim
484  * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
485  */
486 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
487
488   XBT_DEBUG("Killing process %s on %s", process->name, sg_host_get_name(process->host));
489
490   process->context->iwannadie = 1;
491   process->blocked = 0;
492   process->suspended = 0;
493   process->doexception = 0;
494
495   /* destroy the blocking synchro if any */
496   if (process->waiting_synchro) {
497
498     simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro);
499     simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro);
500     simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro);
501     simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro);
502     simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro);
503
504     if (exec != nullptr) {
505       exec->unref();
506
507     } else if (comm != nullptr) {
508       xbt_fifo_remove(process->comms, process->waiting_synchro);
509       comm->cancel();
510       xbt_fifo_remove(process->waiting_synchro->simcalls, &process->simcall);
511       comm->unref();
512
513     } else if (sleep != nullptr) {
514       SIMIX_process_sleep_destroy(process->waiting_synchro);
515
516     } else if (raw != nullptr) {
517       SIMIX_synchro_stop_waiting(process, &process->simcall);
518       SIMIX_synchro_destroy(process->waiting_synchro);
519
520     } else if (io != nullptr) {
521       SIMIX_io_destroy(process->waiting_synchro);
522     }
523
524     /*
525     switch (process->waiting_synchro->type) {
526     case SIMIX_SYNC_JOIN:
527       SIMIX_process_sleep_destroy(process->waiting_synchro);
528       break;
529     } */
530
531     process->waiting_synchro = NULL;
532   }
533   if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
534     XBT_DEBUG("Inserting %s in the to_run list", process->name);
535     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
536   }
537
538 }
539
540 /** @brief Ask another process to raise the given exception
541  *
542  * @param cat category of exception
543  * @param value value associated to the exception
544  * @param msg string information associated to the exception
545  */
546 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
547   SMX_EXCEPTION(process, cat, value, msg);
548
549   if (process->suspended)
550     SIMIX_process_resume(process,SIMIX_process_self());
551
552   /* cancel the blocking synchro if any */
553   if (process->waiting_synchro) {
554
555     simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro);
556     if (exec != nullptr) {
557       SIMIX_execution_cancel(process->waiting_synchro);
558     }
559
560     simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro);
561     if (comm != nullptr) {
562       xbt_fifo_remove(process->comms, comm);
563       comm->cancel();
564     }
565
566     simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro);
567     if (sleep != nullptr) {
568       SIMIX_process_sleep_destroy(process->waiting_synchro);
569       if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
570         XBT_DEBUG("Inserting %s in the to_run list", process->name);
571         xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
572       }
573     }
574
575     simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro);
576     if (raw != nullptr) {
577       SIMIX_synchro_stop_waiting(process, &process->simcall);
578     }
579
580     simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro);
581     if (io != nullptr) {
582       SIMIX_io_destroy(process->waiting_synchro);
583     }
584   }
585   process->waiting_synchro = NULL;
586
587 }
588
589 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
590   SIMIX_process_killall(simcall->issuer, reset_pid);
591 }
592 /**
593  * \brief Kills all running processes.
594  * \param issuer this one will not be killed
595  */
596 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
597 {
598   smx_process_t p = NULL;
599
600   while ((p = (smx_process_t) xbt_swag_extract(simix_global->process_list))) {
601     if (p != issuer) {
602       SIMIX_process_kill(p,issuer);
603     }
604   }
605
606   if (reset_pid > 0)
607     simix_process_maxpid = reset_pid;
608
609   SIMIX_context_runall();
610
611   SIMIX_process_empty_trash();
612 }
613
614 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
615 {
616   process->new_host = dest;
617 }
618 void SIMIX_process_change_host(smx_process_t process,
619              sg_host_t dest)
620 {
621   xbt_assert((process != NULL), "Invalid parameters");
622   xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
623   process->host = dest;
624   xbt_swag_insert(process, sg_host_simix(dest)->process_list);
625 }
626
627
628 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
629 {
630   smx_synchro_t sync_suspend = SIMIX_process_suspend(process, simcall->issuer);
631
632   if (process != simcall->issuer) {
633     SIMIX_simcall_answer(simcall);
634   } else {
635     xbt_fifo_push(sync_suspend->simcalls, simcall);
636     process->waiting_synchro = sync_suspend;
637     process->waiting_synchro->suspend();
638   }
639   /* If we are suspending ourselves, then just do not finish the simcall now */
640 }
641
642 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
643 {
644   if (process->suspended) {
645     XBT_DEBUG("Process '%s' is already suspended", process->name);
646     return NULL;
647   }
648
649   process->suspended = 1;
650
651   /* If we are suspending another process that is waiting on a sync, suspend its synchronization. */
652   if (process != issuer) {
653
654     if (process->waiting_synchro)
655       process->waiting_synchro->suspend();
656     /* If the other process is not waiting, its suspension is delayed to when the process is rescheduled. */
657
658     return NULL;
659   } else {
660     /* FIXME: computation size is zero. Is it okay that bound is zero ? */
661     return SIMIX_execution_start(process, "suspend", 0.0, 1.0, 0.0, 0);
662   }
663 }
664
665 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
666   SIMIX_process_resume(process, simcall->issuer);
667 }
668
669 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
670 {
671   XBT_IN("process = %p, issuer = %p", process, issuer);
672
673   if(process->context->iwannadie) {
674     XBT_VERB("Ignoring request to suspend a process that is currently dying.");
675     return;
676   }
677
678   if(!process->suspended) return;
679   process->suspended = 0;
680
681   /* If we are resuming another process, resume the synchronization it was waiting for
682      if any. Otherwise add it to the list of process to run in the next round. */
683   if (process != issuer) {
684
685     if (process->waiting_synchro) {
686       process->waiting_synchro->resume();
687     }
688   } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
689
690   XBT_OUT();
691 }
692
693 int SIMIX_process_get_maxpid(void) {
694   return simix_process_maxpid;
695 }
696
697 int SIMIX_process_count(void)
698 {
699   return xbt_swag_size(simix_global->process_list);
700 }
701
702 int SIMIX_process_get_PID(smx_process_t self){
703   if (self == NULL)
704     return 0;
705   else
706     return self->pid;
707 }
708
709 int SIMIX_process_get_PPID(smx_process_t self){
710   if (self == NULL)
711     return 0;
712   else
713     return self->ppid;
714 }
715
716 void* SIMIX_process_self_get_data()
717 {
718   smx_process_t self = SIMIX_process_self();
719
720   if (!self) {
721     return NULL;
722   }
723   return SIMIX_process_get_data(self);
724 }
725
726 void SIMIX_process_self_set_data(void *data)
727 {
728   smx_process_t self = SIMIX_process_self();
729
730   SIMIX_process_set_data(self, data);
731 }
732
733 void* SIMIX_process_get_data(smx_process_t process)
734 {
735   return process->data;
736 }
737
738 void SIMIX_process_set_data(smx_process_t process, void *data)
739 {
740   process->data = data;
741 }
742
743 sg_host_t SIMIX_process_get_host(smx_process_t process)
744 {
745   return process->host;
746 }
747
748 xbt_main_func_t SIMIX_process_get_code(void){
749   return SIMIX_process_self()->code;
750 }
751
752 /* needs to be public and without simcall because it is called
753    by exceptions and logging events */
754 const char* SIMIX_process_self_get_name(void) {
755
756   smx_process_t process = SIMIX_process_self();
757   if (process == NULL || process == simix_global->maestro_process)
758     return "maestro";
759
760   return SIMIX_process_get_name(process);
761 }
762
763 const char* SIMIX_process_get_name(smx_process_t process)
764 {
765   return process->name;
766 }
767
768 smx_process_t SIMIX_process_get_by_name(const char* name)
769 {
770   smx_process_t proc;
771
772   xbt_swag_foreach(proc, simix_global->process_list)
773   {
774     if(!strcmp(name, proc->name))
775       return proc;
776   }
777   return NULL;
778 }
779
780 int SIMIX_process_is_suspended(smx_process_t process)
781 {
782   return process->suspended;
783 }
784
785 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
786 {
787   return process->properties;
788 }
789
790 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
791 {
792   smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
793   xbt_fifo_push(sync->simcalls, simcall);
794   simcall->issuer->waiting_synchro = sync;
795 }
796
797 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t synchro){
798   simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
799
800   if (sleep->surf_sleep) {
801     sleep->surf_sleep->cancel();
802
803     smx_simcall_t simcall;
804     while ((simcall = (smx_simcall_t) xbt_fifo_shift(sleep->simcalls))) {
805       simcall_process_sleep__set__result(simcall, SIMIX_DONE);
806       simcall->issuer->waiting_synchro = NULL;
807       if (simcall->issuer->suspended) {
808         XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
809         simcall->issuer->suspended = 0;
810         simcall_HANDLER_process_suspend(simcall, simcall->issuer);
811       } else {
812         SIMIX_simcall_answer(simcall);
813       }
814     }
815     sleep->surf_sleep->unref();
816     sleep->surf_sleep = NULL;
817   }
818   delete sleep;
819   return 0;
820 }
821
822 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
823 {
824   smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
825   SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
826   return res;
827 }
828
829 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
830 {
831   if (MC_is_active() || MC_record_replay_is_active()) {
832     MC_process_clock_add(simcall->issuer, duration);
833     simcall_process_sleep__set__result(simcall, SIMIX_DONE);
834     SIMIX_simcall_answer(simcall);
835     return;
836   }
837   smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
838   xbt_fifo_push(sync->simcalls, simcall);
839   simcall->issuer->waiting_synchro = sync;
840 }
841
842 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
843 {
844   sg_host_t host = process->host;
845
846   /* check if the host is active */
847   if (host->isOff())
848     THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
849
850   simgrid::simix::Sleep *synchro = new simgrid::simix::Sleep();
851   synchro->name = NULL;
852
853   synchro->host = host;
854   synchro->surf_sleep = surf_host_sleep(host, duration);
855   synchro->surf_sleep->setData(synchro);
856   XBT_DEBUG("Create sleep synchronization %p", synchro);
857
858   return synchro;
859 }
860
861 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
862 {
863   XBT_DEBUG("Destroy synchro %p", synchro);
864   simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
865
866   if (sleep->surf_sleep) {
867     sleep->surf_sleep->unref();
868     sleep->surf_sleep = NULL;
869   }
870 }
871
872 /**
873  * \brief Calling this function makes the process to yield.
874  *
875  * Only the current process can call this function, giving back the control to
876  * maestro.
877  *
878  * \param self the current process
879  */
880 void SIMIX_process_yield(smx_process_t self)
881 {
882   XBT_DEBUG("Yield process '%s'", self->name);
883
884   /* Go into sleep and return control to maestro */
885   self->context->suspend();
886
887   /* Ok, maestro returned control to us */
888   XBT_DEBUG("Control returned to me: '%s'", self->name);
889
890   if (self->new_host) {
891     SIMIX_process_change_host(self, self->new_host);
892     self->new_host = NULL;
893   }
894
895   if (self->context->iwannadie){
896     XBT_DEBUG("I wanna die!");
897     SIMIX_process_stop(self);
898   }
899
900   if (self->suspended) {
901     XBT_DEBUG("Hey! I'm suspended.");
902     xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
903     self->suspended = 0;
904     SIMIX_process_suspend(self, self);
905   }
906
907   if (self->doexception) {
908     XBT_DEBUG("Wait, maestro left me an exception");
909     self->doexception = 0;
910     RETHROW;
911   }
912
913   if(SMPI_switch_data_segment && self->segment_index != -1){
914     SMPI_switch_data_segment(self->segment_index);
915   }
916 }
917
918 /* callback: context fetching */
919 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
920 {
921   smx_process_t process = SIMIX_process_self();
922   if (process)
923     return process->running_ctx;
924   else
925     return nullptr;
926 }
927
928 /* callback: termination */
929 void SIMIX_process_exception_terminate(xbt_ex_t * e)
930 {
931   xbt_ex_display(e);
932   xbt_abort();
933 }
934
935 smx_context_t SIMIX_process_get_context(smx_process_t p) {
936   return p->context;
937 }
938
939 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
940   p->context = c;
941 }
942
943 /**
944  * \brief Returns the list of processes to run.
945  */
946 xbt_dynar_t SIMIX_process_get_runnable(void)
947 {
948   return simix_global->process_to_run;
949 }
950
951 /**
952  * \brief Returns the process from PID.
953  */
954 smx_process_t SIMIX_process_from_PID(int PID)
955 {
956   smx_process_t proc;
957   xbt_swag_foreach(proc, simix_global->process_list) {
958    if (proc->pid == (unsigned long) PID)
959     return proc;
960   }
961   return NULL;
962 }
963
964 /** @brief returns a dynar containg all currently existing processes */
965 xbt_dynar_t SIMIX_processes_as_dynar(void) {
966   smx_process_t proc;
967   xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
968   xbt_swag_foreach(proc, simix_global->process_list) {
969     xbt_dynar_push(res,&proc);
970   }
971   return res;
972 }
973
974
975 void SIMIX_process_on_exit_runall(smx_process_t process) {
976   s_smx_process_exit_fun_t exit_fun;
977   smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
978                                          SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
979   while (!xbt_dynar_is_empty(process->on_exit)) {
980     exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
981     (exit_fun.fun)((void*)exit_status, exit_fun.arg);
982   }
983 }
984
985 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
986   xbt_assert(process, "current process not found: are you in maestro context ?");
987
988   if (!process->on_exit) {
989     process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
990   }
991
992   s_smx_process_exit_fun_t exit_fun = {fun, data};
993
994   xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
995 }
996
997 /**
998  * \brief Sets the auto-restart status of the process.
999  * If set to 1, the process will be automatically restarted when its host
1000  * comes back.
1001  */
1002 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
1003   process->auto_restart = auto_restart;
1004 }
1005
1006 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
1007   return SIMIX_process_restart(process, simcall->issuer);
1008 }
1009 /** @brief Restart a process, starting it again from the beginning. */
1010 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
1011   XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_get_name(process->host));
1012   //retrieve the arguments of the old process
1013   //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
1014   s_smx_process_arg_t arg;
1015   arg.code = process->code;
1016   arg.hostname = sg_host_get_name(process->host);
1017   arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
1018   arg.argc = process->argc;
1019   arg.data = process->data;
1020   int i;
1021   arg.argv = xbt_new(char*,process->argc + 1);
1022   for (i = 0; i < arg.argc; i++) {
1023     arg.argv[i] = xbt_strdup(process->argv[i]);
1024   }
1025   arg.argv[process->argc] = NULL;
1026   arg.properties = NULL;
1027   arg.auto_restart = process->auto_restart;
1028   //kill the old process
1029   SIMIX_process_kill(process,issuer);
1030   //start the new process
1031   smx_process_t new_process;
1032   if (simix_global->create_process_function) {
1033     new_process = simix_global->create_process_function(
1034                                           arg.argv[0],
1035                                           arg.code,
1036                                           arg.data,
1037                                           arg.hostname,
1038                                           arg.kill_time,
1039                                           arg.argc,
1040                                           arg.argv,
1041                                           arg.properties,
1042                                           arg.auto_restart,
1043                                           NULL);
1044   } else {
1045     new_process = simcall_process_create(
1046                            arg.argv[0],
1047                            arg.code,
1048                            arg.data,
1049                            arg.hostname,
1050                            arg.kill_time,
1051                            arg.argc,
1052                            arg.argv,
1053                            arg.properties,
1054                            arg.auto_restart);
1055
1056   }
1057   return new_process;
1058 }
1059
1060 void SIMIX_segment_index_set(smx_process_t proc, int index){
1061   proc->segment_index = index;
1062 }