]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - fres/resalloc/fra_generic.h
Implemented change_vres() callback in dummy allocator
[frescor/frsh.git] / fres / resalloc / fra_generic.h
1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners:                 */
4 /*                                                                        */
5 /*   Universidad de Cantabria,              SPAIN                         */
6 /*   University of York,                    UK                            */
7 /*   Scuola Superiore Sant'Anna,            ITALY                         */
8 /*   Kaiserslautern University,             GERMANY                       */
9 /*   Univ. Politécnica  Valencia,           SPAIN                        */
10 /*   Czech Technical University in Prague,  CZECH REPUBLIC                */
11 /*   ENEA                                   SWEDEN                        */
12 /*   Thales Communication S.A.              FRANCE                        */
13 /*   Visual Tools S.A.                      SPAIN                         */
14 /*   Rapita Systems Ltd                     UK                            */
15 /*   Evidence                               ITALY                         */
16 /*                                                                        */
17 /*   See http://www.frescor.org for a link to partners' websites          */
18 /*                                                                        */
19 /*          FRESCOR project (FP6/2005/IST/5-034026) is funded             */
20 /*       in part by the European Union Sixth Framework Programme          */
21 /*       The European Union is not liable of any use that may be          */
22 /*       made of this code.                                               */
23 /*                                                                        */
24 /*                                                                        */
25 /*  This file is part of FRSH (FRescor ScHeduler)                         */
26 /*                                                                        */
27 /* FRSH is free software; you can redistribute it and/or modify it        */
28 /* under terms of the GNU General Public License as published by the      */
29 /* Free Software Foundation; either version 2, or (at your option) any    */
30 /* later version.  FRSH is distributed in the hope that it will be        */
31 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
32 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
33 /* General Public License for more details. You should have received a    */
34 /* copy of the GNU General Public License along with FRSH; see file       */
35 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
36 /* Cambridge, MA 02139, USA.                                              */
37 /*                                                                        */
38 /* As a special exception, including FRSH header files in a file,         */
39 /* instantiating FRSH generics or templates, or linking other files       */
40 /* with FRSH objects to produce an executable application, does not       */
41 /* by itself cause the resulting executable application to be covered     */
42 /* by the GNU General Public License. This exception does not             */
43 /* however invalidate any other reasons why the executable file might be  */
44 /* covered by the GNU Public License.                                     */
45 /**************************************************************************/
46
47 /**
48  * @file   fra_generic.h
49  * @author Michal Sojka <sojkam1@fel.cvut.cz>
50  * @date   Mon Nov  3 15:32:54 2008
51  * 
52  * @brief  FORB interface of FRES Resource Allocator
53  * 
54  * 
55  */
56 #ifndef FRA_GENERIC_H
57 #define FRA_GENERIC_H
58 #include <fcb.h>
59 #include <fra.h>
60 #include <fres_contract.h>
61 #include <ul_gavlcust.h>
62 #include <fosa.h>
63 #include <forb/executor.h>
64 #include <fres_contract_idl.h>
65 #include <fres_vres.h>
66
67 /** Registry of all virtual resources in an application. */
68 struct fres_vreses {
69         fosa_mutex_t mutex;     /**< Mutex for manipulation with vreses tree. If all allocators are executed from one executor, locking can be avoided. */
70         gavl_cust_root_field_t vreses; /**< Container of all virtual resources */
71 };
72
73 GAVL_CUST_NODE_INT_DEC(fres_vreses_nolock /* cust_prefix */,
74                        struct fres_vreses /* cust_root_t */,
75                        struct fres_vres   /* cust_item_t */,
76                        fres_contract_id_t /* cust_key_t */,
77                        vreses             /* cust_root_node */,
78                        node               /* cust_item_node */,
79                        id                 /* cust_item_key */,
80                        fres_contract_id_cmp /* cust_cmp_fnc */)
81
82 extern struct fres_vreses fres_vreses;
83
84
85 /** Registry of all threads in an application that are bound to a VRES. */
86 struct fres_threads_vres {
87         fosa_mutex_t mutex;     /**< Mutex for manipulation with vreses tree. If all allocators are executed from one executor, locking can be avoided. */
88         gavl_cust_root_field_t threads; /**< Container of all virtual resources */
89 };
90
91 GAVL_CUST_NODE_INT_DEC(fres_threads_vres_nolock /* cust_prefix */,
92                        struct fres_threads_vres /* cust_root_t */,
93                        struct fres_thread_vres  /* cust_item_t */,
94                        fosa_thread_id_t         /* cust_key_t */,
95                        threads                  /* cust_root_node */,
96                        node                     /* cust_item_node */,
97                        id                       /* cust_item_key */,
98                        fres_thread_vres_cmp     /* cust_cmp_fnc */)
99
100 extern struct fres_threads_vres fres_threads_vres;
101
102
103 /**
104  * Structure describing FRES allocator. It is used as a parameter to
105  * fra_register(), which registeres this allocator with contract
106  * broker and provides FORB interface to the real allocator.
107  * 
108  */
109 struct fres_allocator {
110         frsh_resource_type_t res_type; /**< Resource type */
111         frsh_resource_id_t res_id;     /**< Resource ID */
112         /** @name Simple interface
113          * The allocator cannot influence the order of applying changes. */
114         /*@{*/
115         /** 
116          * Should create the VRES according to the parameters in
117          * fres_vres::new.
118          * 
119          * @param vres VRES to create.
120          * @param priv User supplied pointer registered by fra_register()
121          * 
122          * @return Zero in case of success, nonzero error code on error.
123          */
124         int (*create_vres)(fres_vres_t *vres, void *priv);
125         /** 
126          * Should change the already crated VRES according to the
127          * parameters in fres_vres::new. The currenlty allocated
128          * parameters are in fres_vres::allocated. This function
129          * should change the fres_vres::perceived to @a fres_vres::new
130          * at some point in time (depending on the kind of the change).
131          * The fres_vres::allocated parameters will be freed after
132          * the return of this function and then fres_vres::allocated
133          * will be set to fres_vres::new.
134          * 
135          * @param vres VRES to change.
136          * @param priv User supplied pointer registered by fra_register()
137          * 
138          * @return Zero in case of success, nonzero error code on error.
139          */
140         int (*change_vres)(fres_vres_t *vres, void *priv);
141         /** 
142          * Cancel the CRES
143          * 
144          * @param cancel_vres VRES to cancel
145          * 
146          * @return Zero on succaess, non-zero error code on error.
147          */
148         int (*cancel_vres)(fres_vres_t *vres, void *priv);
149         /*@}*/
150
151         /** @name Full interface
152          * 
153          * Full interface provides a way for the allocator to
154          * influence the order of applying changes.
155          */
156         /*@{*/
157         /**
158          * A more general (and more compilcated) allocator
159          * interface. If this field is non-NULL, the simple interface
160          * is not used.
161          *
162          * The advantage of this method is that it can apply the
163          * changes in whatever order and even in parallel. The new
164          * version of VRES parameters is stored in fres_vres::new,
165          * whereas the previous version (if any) is stored in
166          * fres_vres::allocated. This function should change the
167          * fres_vres::perceived to @a fres_vres::new at some point in
168          * time (depending on the kind of the change) as the
169          * fres_vres::allocated parameters will be freed after the
170          * return of this function. When the fres_vres::new contains
171          * no blocks, the VRES should be canceled.
172          * 
173          * @param vreses Array of pointers to VRESes.
174          * @param length The number of elements in @a vreses.
175          * @param priv Value of @a priv field in this structure.
176          * 
177          * @return Zero in case of success, non-zero error code otherwise.
178          */
179         int (*apply_vres_changes)(fres_vres_t *vreses[], unsigned length, void *priv);
180         /*@}*/
181
182         void *priv;             /**< Pointer to allocator's private data */
183
184         /**
185          * A callback called whenever the resource allocator is to be
186          * activated by fra_activate().
187          */
188         int (*activate_callback)(forb_orb orb);
189 };
190
191 fres_resource_allocator fra_new(forb_orb orb,
192                                 forb_executor_t *executor,
193                                 struct fres_allocator *allocator);
194
195 void fra_registry_init(forb_orb            orb,
196                        fres_contract_broker fcb,
197                        forb_executor_t     *executor);
198 int fra_register(struct fres_allocator *allocator);
199 int fra_activate(frsh_resource_type_t res_type,
200                  frsh_resource_id_t   res_id);
201
202 fres_vres_t *fra_get_vres(fres_contract_id_t *id);
203
204 int fra_insert_thread_vres(const fosa_thread_id_t *id, fres_vres_t *vres);
205 int fra_delete_thread_vres(const fosa_thread_id_t *id);
206 fres_thread_vres_t *fra_get_thread_vres(const fosa_thread_id_t *id);
207 fres_vres_t *fra_get_vres_thread_vres(const fosa_thread_id_t *id);
208
209 #endif