Logo AND Algorithmique Numérique Distribuée

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