Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New functions added, changes in the header files, function create_link(cond, action...
[simgrid.git] / src / simix / smx_action.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2007 Arnaud Legrand, Bruno Donnassolo.
4    All rights reserved.                                          */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "private.h"
10 #include "xbt/log.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_action, simix,
13                                 "Logging specific to SIMIX (action)");
14
15 /************************************* Actions *********************************/
16
17 smx_action_t SIMIX_communicate(smx_host_t sender,smx_host_t receiver,char * name, double size, double rate)
18 {
19
20         /* alloc structures */
21         smx_action_t act = xbt_new0(s_smx_action_t,1);
22         act->simdata = xbt_new0(s_simdata_action_t,1);
23         simdata_action_t simdata = act->simdata;
24         simdata->cond_list = xbt_fifo_new();
25         
26         /* initialize them */
27         act-> name = xbt_strdup(name);
28
29
30
31         simdata->surf_action = surf_workstation_resource->extension_public->
32                 communicate(sender->simdata->host,
33                                 receiver->simdata->host, size, rate);
34         surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
35
36         return act;
37 }
38
39 smx_action_t SIMIX_execute(smx_host_t host, char * name, double amount)
40 {
41         CHECK_HOST();
42         /* alloc structures */
43         smx_action_t act = xbt_new0(s_smx_action_t,1);
44         act->simdata = xbt_new0(s_simdata_action_t,1);
45         simdata_action_t simdata = act->simdata;
46         simdata->cond_list = xbt_fifo_new();
47         
48         /* initialize them */
49         simdata->source = host;
50         act-> name = xbt_strdup(name);
51
52         /* set communication */
53         simdata->surf_action = surf_workstation_resource->extension_public->
54                 execute(host->simdata->host, amount);
55
56         surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
57
58         return act;
59 }
60
61 SIMIX_error_t SIMIX_action_cancel(smx_action_t action)
62 {
63   xbt_assert0((action != NULL), "Invalid parameter");
64
65   if(action->simdata->surf_action) {
66     surf_workstation_resource->common_public->action_cancel(action->simdata->surf_action);
67     return SIMIX_OK;
68   }
69   return SIMIX_FATAL;
70 }
71
72 void SIMIX_action_set_priority(smx_action_t action, double priority)
73 {
74         xbt_assert0( (action != NULL) && (action->simdata != NULL), "Invalid parameter" );
75
76         surf_workstation_resource->common_public->
77                 set_priority(action->simdata->surf_action, priority);
78         return;
79 }
80
81 SIMIX_error_t SIMIX_action_destroy(smx_action_t action)
82 {
83
84         xbt_assert0((action != NULL), "Invalid parameter");
85
86         xbt_assert0((xbt_fifo_size(action->simdata->cond_list)==0), 
87                         "Conditional list not empty. There is a problem. Cannot destroy it now!");
88
89         if(action->name) free(action->name);
90
91         xbt_fifo_free(action->simdata->cond_list);
92
93         if(action->simdata->surf_action) 
94                 action->simdata->surf_action->resource_type->common_public->action_free(action->simdata->surf_action);
95
96         return SIMIX_OK;
97 }
98
99 void SIMIX_register_action_to_condition(smx_action_t action, smx_cond_t cond)
100 {
101         xbt_assert0( (action != NULL) && (cond != NULL), "Invalid parameters");
102
103         xbt_fifo_push(cond->actions,action);
104 }
105
106 SIMIX_error_t __SIMIX_wait_for_action(smx_process_t process, smx_action_t action)
107 {
108         e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
109         simdata_action_t simdata = action->simdata;
110
111         xbt_assert0(((process != NULL) && (action != NULL) && (action->simdata != NULL)), "Invalid parameters");
112         
113         /* change context while the action is running  */
114         do {
115                 xbt_context_yield();
116                 state=surf_workstation_resource->common_public->action_get_state(simdata->surf_action);
117         } while (state==SURF_ACTION_RUNNING);
118         
119         /* action finished, we can continue */
120
121         if(state == SURF_ACTION_DONE) {
122                 if(surf_workstation_resource->common_public->action_free(simdata->surf_action)) 
123                         simdata->surf_action = NULL;
124                 SIMIX_RETURN(SIMIX_OK);
125         } else if(surf_workstation_resource->extension_public->
126                         get_state(SIMIX_process_get_host(process)->simdata->host) 
127                         == SURF_CPU_OFF) {
128                 if(surf_workstation_resource->common_public->action_free(simdata->surf_action)) 
129                         simdata->surf_action = NULL;
130                 SIMIX_RETURN(SIMIX_HOST_FAILURE);
131         } else {
132                 if(surf_workstation_resource->common_public->action_free(simdata->surf_action)) 
133                         simdata->surf_action = NULL;
134                 SIMIX_RETURN(SIMIX_ACTION_CANCELLED);
135         }
136
137 }