1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
7 /* ****************************************************************************************** */
8 /* TUTORIAL: New API */
9 /* ****************************************************************************************** */
10 #include "smx_private.h"
11 #include "xbt/sysdep.h"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_new_api, simix,
17 "Logging specific to SIMIX (new_api)");
20 //SIMIX NEW MODEL INIT
21 void SIMIX_pre_new_api_fct(smx_simcall_t simcall)
23 smx_action_t action = SIMIX_new_api_fct(simcall->issuer,
24 simcall->new_api.param1,
25 simcall->new_api.param2);
26 xbt_fifo_push(action->simcalls, simcall);
27 simcall->issuer->waiting_action = action;
30 void SIMIX_post_new_api(smx_action_t action)
33 smx_simcall_t simcall;
35 xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
36 switch (simcall->call) {
37 case SIMCALL_NEW_API_INIT:
38 simcall->new_api.result = 0;
46 switch (surf_workstation_model->action_state_get(action->new_api.surf_new_api)) {
48 case SURF_ACTION_FAILED:
49 action->state = SIMIX_FAILED;
52 case SURF_ACTION_DONE:
53 action->state = SIMIX_DONE;
61 SIMIX_new_api_finish(action);
64 smx_action_t SIMIX_new_api_fct(smx_process_t process, const char* param1, double param2)
67 smx_host_t host = process->smx_host;
69 /* check if the host is active */
70 if (surf_workstation_model->extension.
71 workstation.get_state(host) != SURF_RESOURCE_ON) {
72 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
76 action = xbt_mallocator_get(simix_global->action_mallocator);
77 action->type = SIMIX_ACTION_NEW_API;
80 action->category = NULL;
83 // Called the function from the new model
84 action->new_api.surf_new_api = surf_workstation_model->extension.new_model.fct();
86 surf_workstation_model->action_data_set(action->new_api.surf_new_api, action);
87 XBT_DEBUG("Create NEW MODEL action %p", action);
92 void SIMIX_new_api_destroy(smx_action_t action)
94 XBT_DEBUG("Destroy action %p", action);
95 if (action->new_api.surf_new_api)
96 action->new_api.surf_new_api->model_type->action_unref(action->new_api.surf_new_api);
97 xbt_mallocator_release(simix_global->action_mallocator, action);
100 void SIMIX_new_api_finish(smx_action_t action)
102 xbt_fifo_item_t item;
103 smx_simcall_t simcall;
105 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
107 switch (action->state) {
110 /* do nothing, action done */
114 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
118 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
122 xbt_die("Internal error in SIMIX_NEW_MODEL_finish: unexpected action state %d",
126 if (surf_workstation_model->extension.
127 workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
128 simcall->issuer->context->iwannadie = 1;
131 simcall->issuer->waiting_action = NULL;
132 SIMIX_simcall_answer(simcall);
135 /* We no longer need it */
136 SIMIX_new_api_destroy(action);