1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners: */
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 */
12 /* Thales Communication S.A. FRANCE */
13 /* Visual Tools S.A. SPAIN */
14 /* Rapita Systems Ltd UK */
17 /* See http://www.frescor.org for a link to partners' websites */
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. */
25 /* This file is part of FRSH (FRescor ScHeduler) */
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. */
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 /**************************************************************************/
48 * @file fra_registry.c
49 * @author Michal Sojka <sojkam1@fel.cvut.cz>
50 * @date Thu Oct 23 15:26:19 2008
52 * @brief Registry of FRES Resource Allocator.
54 * This registry is used by fra_register() and its main purpose is to
55 * allow "late" registration of allocators to FCB. Late registration
56 * means, that the allocator is registered with FCB only when the
57 * application accesses the resource.
59 * This functionality is useful at least during testing and debugging,
60 * when it is not necessary to run all the resource managers. Only the
61 * managers for resources used by the application are required.
67 #include <ul_gavlcust.h>
68 #include "fra_generic.h"
71 UL_LOG_CUST(ulogd_fra_registry);
72 ul_log_domain_t ulogd_fra_registry = {UL_LOGL_MSG, "fra_registry"};
74 #define save_errno(cmd) do { int _e = errno; cmd; errno = _e; } while(0)
76 struct registered_allocator {
78 struct fres_allocator *allocator;
79 bool registered_with_fcb;
82 #define FRA_MAX_ALLOCATORS 32
83 static struct registered_allocator allocators[FRA_MAX_ALLOCATORS];
84 static unsigned free_allocator_idx = 0;
86 static struct registry {
88 fres_contract_broker fcb;
89 forb_executor_t *executor;
91 gavl_cust_root_field_t allocators;
95 static inline int fres_allocator_cmp(struct fres_allocator * const *a,
96 struct fres_allocator * const *b)
98 if ((*a)->res_type < (*b)->res_type) {
100 } else if ((*a)->res_type > (*b)->res_type) {
102 } else if ((*a)->res_id < (*b)->res_id) {
104 } else if ((*a)->res_id > (*b)->res_id) {
111 GAVL_CUST_NODE_INT_DEC(fra_registry /* cust_prefix */,
112 struct registry /* cust_root_t */,
113 struct registered_allocator /* cust_item_t */,
114 struct fres_allocator* /* cust_key_t */,
115 allocators /* cust_root_node */,
116 node /* cust_item_node */,
117 allocator /* cust_item_key */,
118 fres_allocator_cmp /* cust_cmp_fnc */);
120 GAVL_CUST_NODE_INT_IMP(fra_registry /* cust_prefix */,
121 struct registry /* cust_root_t */,
122 struct registered_allocator /* cust_item_t */,
123 struct fres_allocator* /* cust_key_t */,
124 allocators /* cust_root_node */,
125 node /* cust_item_node */,
126 allocator /* cust_item_key */,
127 fres_allocator_cmp /* cust_cmp_fnc */);
131 * Initializes registry of resource allocators.
134 * @param fcb Contract broker object reference
135 * @param executor Executor for all allocator objects
137 void fra_registry_init(forb_orb orb,
138 fres_contract_broker fcb,
139 forb_executor_t *executor)
143 registry.executor = executor;
145 fra_registry_init_root_field(®istry);
149 * Registeres the allocator in registry.
153 * @return Zero on success, -1 on error.
155 int fra_register(struct fres_allocator *allocator)
157 struct registered_allocator *ra;
160 ra = fra_registry_find(®istry, &allocator);
161 if (!ra && free_allocator_idx < FRA_MAX_ALLOCATORS) {
162 ra = &allocators[free_allocator_idx];
163 free_allocator_idx++;
164 ra->allocator = allocator;
165 ra->registered_with_fcb = false;
166 fra_registry_insert(®istry, ra);
175 * Creates FORB object reference for the allocator and registeres it
176 * with contract broker.
180 * @return Zero on success, non-zero error code on error.
182 static int register_fra_to_fcb(struct fres_allocator *allocator)
186 fres_resource_allocator fra;
188 fra = fra_new(registry.orb, registry.executor, allocator);
190 if (allocator->activate_callback) {
191 ret = allocator->activate_callback(registry.orb);
192 if (ret) goto err_release;
194 /* Register resource allocator */
195 ret = fres_contract_broker_register_allocator(registry.fcb,
199 if (forb_exception_occurred(&env)) {
200 ret = FRES_ERR_FORB_EXCEPTION;
207 save_errno(forb_object_release(fra));
212 * Creates the allocator object and registeres it with contract
213 * broker. The allocator must be previously registered by
214 * fra_register(). If the allocator is already registered, this
215 * function does nothing.
220 * @return Zero on success, non-zero error code on error.
223 int fra_activate(frsh_resource_type_t res_type,
224 frsh_resource_id_t res_id)
226 struct fres_allocator key, *pkey=&key;
227 struct registered_allocator *ra;
230 key.res_type = res_type;
232 ra = fra_registry_find(®istry, &pkey);
234 if (ra->registered_with_fcb) {
237 ret = register_fra_to_fcb(ra->allocator);
239 ra->registered_with_fcb = true;
243 ret = FRSH_ERR_RESOURCE_ID_INVALID;
250 * Returns allocator for a given resource, previously registered by
256 * @return Pointer to the resource allocator or NULL if no allocator
257 * is registered for the resource.
259 struct fres_allocator *fra_get(frsh_resource_type_t res_type,
260 frsh_resource_id_t res_id)
262 struct registered_allocator *ra;
263 struct fres_allocator key, *pkey=&key;
265 pkey->res_type = res_type;
266 pkey->res_id = res_id;
268 ra = fra_registry_find(®istry, &pkey);
270 return ra->allocator;