- if (comm_test_n_destroy(ctrl_comm)) {
- msg = (message* )MSG_task_get_data(ctrl_task);
- from = MSG_task_get_source(ctrl_task);
- MSG_task_destroy(ctrl_task);
- ctrl_task = NULL;
- ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
-
- } else if (comm_test_n_destroy(data_comm)) {
- msg = (message* )MSG_task_get_data(data_task);
- from = MSG_task_get_source(data_task);
- MSG_task_destroy(data_task);
- data_task = NULL;
- data_comm = MSG_task_irecv(&data_task, get_data_mbox());
- }
+ do {
+ if (ctrl_comm && comm_test_n_destroy(ctrl_comm)) {
+ msg = (message* )MSG_task_get_data(ctrl_task);
+ from = MSG_task_get_source(ctrl_task);
+ MSG_task_destroy(ctrl_task);
+ ctrl_task = NULL;
+ ctrl_comm =
+ (!ctrl_close_is_last || msg->get_type() != message::CTRL_CLOSE)
+ ? ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox())
+ : ctrl_comm = NULL;
+
+ } else if (data_comm && comm_test_n_destroy(data_comm)) {
+ msg = (message* )MSG_task_get_data(data_task);
+ from = MSG_task_get_source(data_task);
+ MSG_task_destroy(data_task);
+ data_task = NULL;
+ data_comm =
+ (!data_close_is_last || msg->get_type() != message::DATA_CLOSE)
+ ? data_comm = MSG_task_irecv(&data_task, get_data_mbox())
+ : data_comm = NULL;
+ }
+
+ restart = wait && !msg && (ctrl_comm || data_comm);
+ if (restart) {
+ xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
+ if (ctrl_comm)
+ xbt_dynar_push(comms, &ctrl_comm);
+ if (data_comm)
+ xbt_dynar_push(comms, &data_comm);
+ MSG_comm_waitany(comms);
+ xbt_dynar_free(&comms);
+ }
+ } while (restart);