Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
char *name transformed in const char *name to avoid compilation complain
[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, e_surf_action_state_t state)
84 {
85   surf_action_state_t action_state = &(action->model_type->states);
86   XBT_IN2("(%p,%s)", action, surf_action_state_names[state]);
87   xbt_swag_remove(action, action->state_set);
88
89   if (state == SURF_ACTION_READY)
90     action->state_set = action_state->ready_action_set;
91   else if (state == SURF_ACTION_RUNNING)
92     action->state_set = action_state->running_action_set;
93   else if (state == SURF_ACTION_FAILED)
94     action->state_set = action_state->failed_action_set;
95   else if (state == SURF_ACTION_DONE)
96     action->state_set = action_state->done_action_set;
97   else
98     action->state_set = NULL;
99
100   if (action->state_set)
101     xbt_swag_insert(action, action->state_set);
102   XBT_OUT;
103 }
104
105 void surf_action_data_set(surf_action_t action, void *data)
106 {
107   action->data = data;
108 }
109
110 XBT_INLINE void surf_action_ref(surf_action_t action)
111 {
112   action->refcount++;
113 }
114
115 /*
116 void surf_action_suspend(surf_action_t action)
117 {
118   action->suspended = 1;
119 }*/
120
121 /*
122  * Maxmin action
123  */