Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
clean the hierarchical routing code
[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
37   if (failed)
38     action->state_set = model->states.failed_action_set;
39   else
40     action->state_set = model->states.running_action_set;
41
42   xbt_swag_insert(action, action->state_set);
43
44   return action;
45 }
46
47 e_surf_action_state_t surf_action_state_get(surf_action_t action)
48 {
49   surf_action_state_t action_state = &(action->model_type->states);
50
51   if (action->state_set == action_state->ready_action_set)
52     return SURF_ACTION_READY;
53   if (action->state_set == action_state->running_action_set)
54     return SURF_ACTION_RUNNING;
55   if (action->state_set == action_state->failed_action_set)
56     return SURF_ACTION_FAILED;
57   if (action->state_set == action_state->done_action_set)
58     return SURF_ACTION_DONE;
59   return SURF_ACTION_NOT_IN_THE_SYSTEM;
60 }
61
62 double surf_action_get_start_time(surf_action_t action)
63 {
64   return action->start;
65 }
66
67 double surf_action_get_finish_time(surf_action_t action)
68 {
69   /* keep the function behavior, some models (cpu_ti) change the finish time before the action end */
70   return action->remains == 0 ? action->finish : -1;
71 }
72
73 XBT_INLINE void surf_action_free(surf_action_t * action)
74 {
75   (*action)->model_type->action_cancel(*action);
76   free(*action);
77   *action = NULL;
78 }
79
80 void surf_action_state_set(surf_action_t action, e_surf_action_state_t state)
81 {
82   surf_action_state_t action_state = &(action->model_type->states);
83   XBT_IN2("(%p,%s)", action, surf_action_state_names[state]);
84   xbt_swag_remove(action, action->state_set);
85
86   if (state == SURF_ACTION_READY)
87     action->state_set = action_state->ready_action_set;
88   else if (state == SURF_ACTION_RUNNING)
89     action->state_set = action_state->running_action_set;
90   else if (state == SURF_ACTION_FAILED)
91     action->state_set = action_state->failed_action_set;
92   else if (state == SURF_ACTION_DONE)
93     action->state_set = action_state->done_action_set;
94   else
95     action->state_set = NULL;
96
97   if (action->state_set)
98     xbt_swag_insert(action, action->state_set);
99   XBT_OUT;
100 }
101
102 void surf_action_data_set(surf_action_t action, void *data)
103 {
104   action->data = data;
105 }
106
107 XBT_INLINE void surf_action_ref(surf_action_t action)
108 {
109   action->refcount++;
110 }
111
112 /*
113 void surf_action_suspend(surf_action_t action)
114 {
115   action->suspended = 1;
116 }*/
117
118 /*
119  * Maxmin action
120  */