Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add first steps to explain How to add a new model in Simgrid
[simgrid.git] / src / simix / smx_new_api.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. 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 /* ****************************************************************************************** */
8 /* TUTORIAL: New API                                                                        */
9 /* ****************************************************************************************** */
10 #include "smx_private.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/log.h"
13 #include "xbt/dict.h"
14 #include "mc/mc.h"
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_new_api, simix,
17                                 "Logging specific to SIMIX (new_api)");
18
19
20 //SIMIX NEW MODEL INIT
21 void SIMIX_pre_new_api_fct(smx_simcall_t simcall)
22 {
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;
28 }
29
30 void SIMIX_post_new_api(smx_action_t action)
31 {
32   xbt_fifo_item_t i;
33   smx_simcall_t simcall;
34
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;
39       break;
40
41     default:
42       break;
43     }
44   }
45
46   switch (surf_workstation_model->action_state_get(action->new_api.surf_new_api)) {
47
48     case SURF_ACTION_FAILED:
49       action->state = SIMIX_FAILED;
50       break;
51
52     case SURF_ACTION_DONE:
53       action->state = SIMIX_DONE;
54       break;
55
56     default:
57       THROW_IMPOSSIBLE;
58       break;
59   }
60
61   SIMIX_new_api_finish(action);
62 }
63
64 smx_action_t SIMIX_new_api_fct(smx_process_t process, const char* param1, double param2)
65 {
66   smx_action_t action;
67   smx_host_t host = process->smx_host;
68
69   /* check if the host is active */
70   if (surf_workstation_model->extension.
71       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
72     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
73            host->name);
74   }
75
76   action = xbt_mallocator_get(simix_global->action_mallocator);
77   action->type = SIMIX_ACTION_NEW_API;
78   action->name = NULL;
79 #ifdef HAVE_TRACING
80   action->category = NULL;
81 #endif
82
83   // Called the function from the new model
84   action->new_api.surf_new_api = surf_workstation_model->extension.new_model.fct();
85
86   surf_workstation_model->action_data_set(action->new_api.surf_new_api, action);
87   XBT_DEBUG("Create NEW MODEL action %p", action);
88
89   return action;
90 }
91
92 void SIMIX_new_api_destroy(smx_action_t action)
93 {
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);
98 }
99
100 void SIMIX_new_api_finish(smx_action_t action)
101 {
102   xbt_fifo_item_t item;
103   smx_simcall_t simcall;
104
105   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
106
107     switch (action->state) {
108
109       case SIMIX_DONE:
110         /* do nothing, action done */
111         break;
112
113       case SIMIX_FAILED:
114         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
115         break;
116
117       case SIMIX_CANCELED:
118         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
119         break;
120
121       default:
122         xbt_die("Internal error in SIMIX_NEW_MODEL_finish: unexpected action state %d",
123             (int)action->state);
124     }
125
126     if (surf_workstation_model->extension.
127         workstation.get_state(simcall->issuer->smx_host->host) != SURF_RESOURCE_ON) {
128       simcall->issuer->context->iwannadie = 1;
129     }
130
131     simcall->issuer->waiting_action = NULL;
132     SIMIX_simcall_answer(simcall);
133   }
134
135   /* We no longer need it */
136   SIMIX_new_api_destroy(action);
137 }