Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
msg_simix alpha. All functions implemented.
[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 #include "xbt/ex.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_action, simix,
14                                 "Logging specific to SIMIX (action)");
15
16 /************************************* Actions *********************************/
17
18 smx_action_t SIMIX_action_communicate(smx_host_t sender,smx_host_t receiver,char * name, double size, double rate)
19 {
20         /* check if the host is active */
21         if ( surf_workstation_resource->extension_public->get_state(sender->simdata->host)!=SURF_CPU_ON) {
22                 THROW1(network_error,0,"Host %s failed, you cannot call this function",sender->name);
23         }
24         if ( surf_workstation_resource->extension_public->get_state(receiver->simdata->host)!=SURF_CPU_ON) {
25                 THROW1(network_error,0,"Host %s failed, you cannot call this function",receiver->name);
26         }
27
28         /* alloc structures */
29         smx_action_t act = xbt_new0(s_smx_action_t,1);
30         act->simdata = xbt_new0(s_smx_simdata_action_t,1);
31         smx_simdata_action_t simdata = act->simdata;
32         act->cond_list = xbt_fifo_new();
33         
34         /* initialize them */
35         act->name = xbt_strdup(name);
36
37
38         simdata->surf_action = surf_workstation_resource->extension_public->
39                 communicate(sender->simdata->host,
40                                 receiver->simdata->host, size, rate);
41         surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
42
43         return act;
44 }
45
46 smx_action_t SIMIX_action_execute(smx_host_t host, char * name, double amount)
47 {
48         /* check if the host is active */
49         if ( surf_workstation_resource->extension_public->get_state(host->simdata->host)!=SURF_CPU_ON) {
50                 THROW1(host_error,0,"Host %s failed, you cannot call this function",host->name);
51         }
52
53         /* alloc structures */
54         smx_action_t act = xbt_new0(s_smx_action_t,1);
55         act->simdata = xbt_new0(s_smx_simdata_action_t,1);
56         smx_simdata_action_t simdata = act->simdata;
57         act->cond_list = xbt_fifo_new();
58         
59         /* initialize them */
60         simdata->source = host;
61         act-> name = xbt_strdup(name);
62
63         /* set communication */
64         simdata->surf_action = surf_workstation_resource->extension_public->
65                 execute(host->simdata->host, amount);
66
67         surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
68
69         return act;
70 }
71
72
73 smx_action_t SIMIX_action_sleep(smx_host_t host,  double duration)
74 {               
75         char name[] = "sleep";
76
77         /* check if the host is active */
78         if ( surf_workstation_resource->extension_public->get_state(host->simdata->host)!=SURF_CPU_ON) {
79                 THROW1(host_error,0,"Host %s failed, you cannot call this function",host->name);
80         }
81
82         /* alloc structures */
83         smx_action_t act = xbt_new0(s_smx_action_t,1);
84         act->simdata = xbt_new0(s_smx_simdata_action_t,1);
85         smx_simdata_action_t simdata = act->simdata;
86         act->cond_list = xbt_fifo_new();
87         
88         /* initialize them */
89         simdata->source = host;
90         act->name = xbt_strdup(name);
91
92         simdata->surf_action = surf_workstation_resource->extension_public->
93                 sleep(host->simdata->host, duration);
94
95         surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
96
97         return act;
98 }
99
100
101 void SIMIX_action_cancel(smx_action_t action)
102 {
103   xbt_assert0((action != NULL), "Invalid parameter");
104
105   if(action->simdata->surf_action) {
106     surf_workstation_resource->common_public->action_cancel(action->simdata->surf_action);
107   }
108   return;
109 }
110
111 void SIMIX_action_set_priority(smx_action_t action, double priority)
112 {
113         xbt_assert0( (action != NULL) && (action->simdata != NULL), "Invalid parameter" );
114
115         surf_workstation_resource->common_public->
116                 set_priority(action->simdata->surf_action, priority);
117         return;
118 }
119
120 void SIMIX_action_destroy(smx_action_t action)
121 {
122
123         xbt_assert0((action != NULL), "Invalid parameter");
124
125         xbt_assert1((xbt_fifo_size(action->cond_list)==0), 
126                         "Conditional list not empty %d. There is a problem. Cannot destroy it now!", xbt_fifo_size(action->cond_list));
127
128         if(action->name) xbt_free(action->name);
129
130         xbt_fifo_free(action->cond_list);
131
132         if(action->simdata->surf_action) 
133                 action->simdata->surf_action->resource_type->common_public->action_free(action->simdata->surf_action);
134
135         xbt_free(action->simdata);
136         xbt_free(action);
137         return;
138 }
139
140 void SIMIX_register_action_to_condition(smx_action_t action, smx_cond_t cond)
141 {
142         xbt_assert0( (action != NULL) && (cond != NULL), "Invalid parameters");
143
144         xbt_fifo_push(cond->actions,action);
145 }
146
147 double SIMIX_action_get_remains(smx_action_t action) 
148 {
149   xbt_assert0((action != NULL), "Invalid parameter");
150         return action->simdata->surf_action->remains;
151 }