Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix initialization order.
[simgrid.git] / src / simix / smx_new_api.c
1 /* Copyright (c) 2007-2010, 2012-2013. 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_action_get_state(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_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
71     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
72            sg_host_name(host));
73   }
74
75   action = xbt_mallocator_get(simix_global->action_mallocator);
76   action->type = SIMIX_ACTION_NEW_API;
77   action->name = NULL;
78 #ifdef HAVE_TRACING
79   action->category = NULL;
80 #endif
81
82   // Called the function from the new model
83   //FIXME:CHECK WHAT TO DO action->new_api.surf_new_api = surf_workstation_model->extension.new_model.fct();
84
85   surf_action_set_data(action->new_api.surf_new_api, action);
86   XBT_DEBUG("Create NEW MODEL action %p", action);
87
88   return action;
89 }
90
91 void SIMIX_new_api_destroy(smx_action_t action)
92 {
93   XBT_DEBUG("Destroy action %p", action);
94   if (action->new_api.surf_new_api)
95     surf_action_unref(action->new_api.surf_new_api);
96   xbt_mallocator_release(simix_global->action_mallocator, action);
97 }
98
99 void SIMIX_new_api_finish(smx_action_t action)
100 {
101   xbt_fifo_item_t item;
102   smx_simcall_t simcall;
103
104   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
105
106     switch (action->state) {
107
108       case SIMIX_DONE:
109         /* do nothing, action done */
110         break;
111
112       case SIMIX_FAILED:
113         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
114         break;
115
116       case SIMIX_CANCELED:
117         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
118         break;
119
120       default:
121         xbt_die("Internal error in SIMIX_NEW_MODEL_finish: unexpected action state %d",
122             (int)action->state);
123     }
124
125     if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
126       simcall->issuer->context->iwannadie = 1;
127     }
128
129     simcall->issuer->waiting_action = NULL;
130     SIMIX_simcall_answer(simcall);
131   }
132
133   /* We no longer need it */
134   SIMIX_new_api_destroy(action);
135 }