+ XBT_DEBUG("Cleanup process %s (%p), waiting action %p",
+ process->name, process, process->waiting_action);
+
+ /* cancel non-blocking communications */
+ smx_action_t action;
+ while ((action = xbt_fifo_pop(process->comms))) {
+
+ /* make sure no one will finish the comm after this process is destroyed,
+ * because src_proc or dst_proc would be an invalid pointer */
+ SIMIX_comm_cancel(action);
+
+ if (action->comm.src_proc == process) {
+ XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
+ action, action->comm.detached, action->state, action->comm.src_proc, action->comm.dst_proc);
+ action->comm.src_proc = NULL;
+
+ if (action->comm.detached) {
+ if (action->comm.refcount == 0) {
+ XBT_DEBUG("Increase the refcount before destroying it since it's detached");
+ /* I'm not supposed to destroy a detached comm from the sender side,
+ * unless there is no receiver matching the rdv */
+ action->comm.refcount++;
+ SIMIX_comm_destroy(action);
+ }
+ else {
+ XBT_DEBUG("Don't destroy it since its refcount is %d", action->comm.refcount);
+ }
+ } else {
+ SIMIX_comm_destroy(action);
+ }
+ }
+ else if (action->comm.dst_proc == process){
+ XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
+ action, action->state, action->comm.src_proc, action->comm.dst_proc);
+ action->comm.dst_proc = NULL;
+
+ if (action->comm.detached && action->comm.refcount == 1
+ && action->comm.src_proc != NULL) {
+ /* the comm will be freed right now, remove it from the sender */
+ xbt_fifo_remove(action->comm.src_proc->comms, action);
+ }
+ SIMIX_comm_destroy(action);
+ }
+ else {
+ xbt_die("Communication action %p is in my list but I'm not the sender "
+ "or the receiver", action);
+ }
+ }
+
+ /*xbt_swag_remove(process, simix_global->process_to_run);*/
+ xbt_swag_remove(process, simix_global->process_list);
+ xbt_swag_remove(process, process->smx_host->process_list);
+ xbt_swag_insert(process, simix_global->process_to_destroy);