Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2c34a4e77e542ba005e26a24b339d595c4d3daaf
[simgrid.git] / src / surf / surf_action.c
1 /* Copyright (c) 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 #include "surf_private.h"
8
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_kernel);
10
11 /*
12  * Generic action
13  */
14
15 const char *surf_action_state_names[6] = {
16   "SURF_ACTION_READY",
17   "SURF_ACTION_RUNNING",
18   "SURF_ACTION_FAILED",
19   "SURF_ACTION_DONE",
20   "SURF_ACTION_TO_FREE",
21   "SURF_ACTION_NOT_IN_THE_SYSTEM"
22 };
23
24 void *surf_action_new(size_t size, double cost, surf_model_t model,
25                       int failed)
26 {
27   surf_action_t action = xbt_malloc0(size);
28   action->refcount = 1;
29   action->cost = cost;
30   action->remains = cost;
31   action->priority = 1.0;
32   action->max_duration = NO_MAX_DURATION;
33   action->start = surf_get_clock();
34   action->finish = -1.0;
35   action->model_type = model;
36 #ifdef HAVE_TRACING
37   action->category = NULL;
38 #endif
39
40   if (failed)
41     action->state_set = model->states.failed_action_set;
42   else
43     action->state_set = model->states.running_action_set;
44
45   xbt_swag_insert(action, action->state_set);
46
47   return action;
48 }
49
50 e_surf_action_state_t surf_action_state_get(surf_action_t action)
51 {
52   surf_action_state_t action_state = &(action->model_type->states);
53
54   if (action->state_set == action_state->ready_action_set)
55     return SURF_ACTION_READY;
56   if (action->state_set == action_state->running_action_set)
57     return SURF_ACTION_RUNNING;
58   if (action->state_set == action_state->failed_action_set)
59     return SURF_ACTION_FAILED;
60   if (action->state_set == action_state->done_action_set)
61     return SURF_ACTION_DONE;
62   return SURF_ACTION_NOT_IN_THE_SYSTEM;
63 }
64
65 double surf_action_get_start_time(surf_action_t action)
66 {
67   return action->start;
68 }
69
70 double surf_action_get_finish_time(surf_action_t action)
71 {
72   /* keep the function behavior, some models (cpu_ti) change the finish time before the action end */
73   return action->remains == 0 ? action->finish : -1;
74 }
75
76 XBT_INLINE void surf_action_free(surf_action_t * action)
77 {
78   (*action)->model_type->action_cancel(*action);
79   free(*action);
80   *action = NULL;
81 }
82
83 void surf_action_state_set(surf_action_t action,
84                            e_surf_action_state_t state)
85 {
86   surf_action_state_t action_state = &(action->model_type->states);
87   XBT_IN2("(%p,%s)", action, surf_action_state_names[state]);
88   xbt_swag_remove(action, action->state_set);
89
90   if (state == SURF_ACTION_READY)
91     action->state_set = action_state->ready_action_set;
92   else if (state == SURF_ACTION_RUNNING)
93     action->state_set = action_state->running_action_set;
94   else if (state == SURF_ACTION_FAILED)
95     action->state_set = action_state->failed_action_set;
96   else if (state == SURF_ACTION_DONE)
97     action->state_set = action_state->done_action_set;
98   else
99     action->state_set = NULL;
100
101   if (action->state_set)
102     xbt_swag_insert(action, action->state_set);
103   XBT_OUT;
104 }
105
106 void surf_action_data_set(surf_action_t action, void *data)
107 {
108   action->data = data;
109 }
110
111 XBT_INLINE void surf_action_ref(surf_action_t action)
112 {
113   action->refcount++;
114 }
115
116 /*
117 void surf_action_suspend(surf_action_t action)
118 {
119   action->suspended = 1;
120 }*/
121
122 /*
123  * Maxmin action
124  */