]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - src_frescan/frescan_bwres_robjs.c
cdf29151d4abbd28445123d71f64fcfb4dcdab6c
[frescor/fna.git] / src_frescan / frescan_bwres_robjs.c
1 /*!
2  * @file frescan_bwres_robjs.c
3  *
4  * @brief FRESCAN bandwith reservation layer: reply objects
5  *
6  * This module contains the definition of the data object and operations to
7  * create a pool of objects, obtain the id of an unused object, wait upon it,
8  * signal it, and finish using it.
9  *
10  * @version 0.01
11  *
12  * @date 1-Apr-2008
13  *
14  * @author Daniel Sangorrin <daniel.sangorrin@unican.es>
15  *
16  * @license
17  *
18  * -----------------------------------------------------------------------
19  *  Copyright (C) 2006 - 2008 FRESCOR consortium partners:
20  *
21  *    Universidad de Cantabria,              SPAIN
22  *    University of York,                    UK
23  *    Scuola Superiore Sant'Anna,            ITALY
24  *    Kaiserslautern University,             GERMANY
25  *    Univ. Politécnica  Valencia,           SPAIN
26  *    Czech Technical University in Prague,  CZECH REPUBLIC
27  *    ENEA                                   SWEDEN
28  *    Thales Communication S.A.              FRANCE
29  *    Visual Tools S.A.                      SPAIN
30  *    Rapita Systems Ltd                     UK
31  *    Evidence                               ITALY
32  *
33  *    See http://www.frescor.org for a link to partners' websites
34  *
35  *           FRESCOR project (FP6/2005/IST/5-034026) is funded
36  *        in part by the European Union Sixth Framework Programme
37  *        The European Union is not liable of any use that may be
38  *        made of this code.
39  *
40  *  This file is part of FRESCAN
41  *
42  *  FRESCAN is free software; you can  redistribute it and/or  modify
43  *  it under the terms of  the GNU General Public License as published by
44  *  the Free Software Foundation;  either  version 2, or (at  your option)
45  *  any later version.
46  *
47  *  FRESCAN  is distributed  in  the hope  that  it  will  be useful,  but
48  *  WITHOUT  ANY  WARRANTY;     without  even the   implied   warranty  of
49  *  MERCHANTABILITY  or  FITNESS FOR  A  PARTICULAR PURPOSE. See  the  GNU
50  *  General Public License for more details.
51  *
52  *  You should have  received a  copy of  the  GNU  General Public License
53  *  distributed  with  FRESCAN;  see file COPYING.   If not,  write to the
54  *  Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
55  *  02111-1307, USA.
56  *
57  * As a special exception, including FRESCAN header files in a file,
58  * instantiating FRESCAN generics or templates, or linking other files
59  * with FRESCAN objects to produce an executable application, does not
60  * by itself cause the resulting executable application to be covered
61  * by the GNU General Public License. This exception does not
62  * however invalidate any other reasons why the executable file might be
63  * covered by the GNU Public License.
64  * -----------------------------------------------------------------------
65  *
66  */
67
68 #include <misc/freelist.h>
69 #include "frescan_bwres_robjs.h"
70 #include "fosa_mutexes_and_condvars.h"
71 #include "frescan_config.h"
72 #include "frescan_debug.h"
73
74 static bool is_initialized = false;
75
76 /**
77  * the_reply_objects
78  *
79  * We will use a freelist to allocate and free the reply object. Reply objects
80  * are FOSA conditional variables (we can't use synchobjects because they are
81  * only for bound-workload) stored in an array together with their condition
82  * and a mutex variable to control the access and to be able to wait and
83  * signal the conditional variables. We could have used semaphores instead, but
84  * it would introduce a dependency on the OS because they are not in FOSA.
85  *
86  **/
87
88 static fosa_mutex_t freelist_mutex;
89
90 struct replyobj_t {
91         bool is_work_done;
92         fosa_cond_t cond;
93         fosa_mutex_t mutex;
94 };
95
96 static struct replyobj_t the_reply_objects[FRESCAN_BWRES_MX_REPLY_OBJECTS];
97 static freelist_t freelist;
98
99 /**
100  * frescan_bwres_robjs_init()
101  *
102  * Init the freelist and its mutex. The conditional variables are not
103  * initialized here but when allocating a reply object. This can be more
104  * efficient for memory usage in some OS (not in MaRTE OS though).
105  *
106  **/
107
108 int frescan_bwres_robjs_init(int max_ceiling)
109 {
110         int err;
111
112         err = fosa_mutex_init(&freelist_mutex, max_ceiling);
113         if (err != 0) return err;
114
115         err = freelist_init(&freelist, FRESCAN_BWRES_MX_REPLY_OBJECTS);
116         if (err != 0) return err;
117
118         is_initialized = true;
119         return 0;
120 }
121
122 /**
123  * frescan_bwres_robjs_alloc()
124  *
125  * Allocate an reply object with the freelist_mutex locked and then initialize
126  * its cond variable, condition (predicate) and mutex. The ID of the allocated
127  * reply object represents the position in the array the_reply_objects.
128  *
129  **/
130
131 int frescan_bwres_robjs_alloc(frescan_bwres_robj_id_t *id, int ceiling)
132 {
133         int err, pos;
134
135         DEBUG(FRESCAN_BWRES_ROBJS_ENABLE_DEBUG,
136               "allocating reply object, is_initialized=%d\n", is_initialized);
137
138         if (is_initialized == false) return -1;
139
140         err = fosa_mutex_lock(&freelist_mutex);
141         if (err != 0) return err;
142
143                 pos = freelist_alloc(&freelist);
144                 if (pos < 0) goto locked_error;
145
146         err = fosa_mutex_unlock(&freelist_mutex);
147         if (err != 0) return err;
148
149         err = fosa_cond_init(&the_reply_objects[pos].cond);
150         if (err != 0) return err;
151
152         the_reply_objects[pos].is_work_done = false;
153         *id = (unsigned int)pos;
154
155         err = fosa_mutex_init(&the_reply_objects[pos].mutex, ceiling);
156         if (err != 0) return err;
157
158         DEBUG(FRESCAN_BWRES_ROBJS_ENABLE_DEBUG,
159               "reply object allocated, id=%d\n", *id);
160
161         return 0;
162
163 locked_error:
164         fosa_mutex_unlock(&freelist_mutex);
165         return pos;
166 }
167
168 /**
169  * frescan_bwres_robjs_free()
170  *
171  * Destroy the cond variable and then free the replyobject
172  *
173  **/
174
175 int frescan_bwres_robjs_free(frescan_bwres_robj_id_t id)
176 {
177         int err;
178
179         DEBUG(FRESCAN_BWRES_ROBJS_ENABLE_DEBUG,
180               "free reply id=%d, is_initialized=%d\n", id, is_initialized);
181
182         if (is_initialized == false) return -1;
183
184         err = fosa_mutex_lock(&freelist_mutex);
185         if (err != 0) return err;
186
187                 err = freelist_free(&freelist, id);
188                 if (err != 0) goto locked_error;
189
190                 err = fosa_cond_destroy(&the_reply_objects[id].cond);
191                 if (err != 0) return err;
192
193                 err = fosa_mutex_destroy(&the_reply_objects[id].mutex);
194                 if (err != 0) return err;
195
196                 the_reply_objects[id].is_work_done = false;
197
198         err = fosa_mutex_unlock(&freelist_mutex);
199         if (err != 0) return err;
200
201         return 0;
202
203 locked_error:
204         fosa_mutex_unlock(&freelist_mutex);
205         return err;
206 }
207
208 /**
209  * frescan_bwres_robjs_signal()
210  *
211  * Signal the cond variable
212  *
213  **/
214
215 int frescan_bwres_robjs_signal(frescan_bwres_robj_id_t id)
216 {
217         int err;
218
219         DEBUG(FRESCAN_BWRES_ROBJS_ENABLE_DEBUG,
220               "is_initialized=%d\n", is_initialized);
221         if (is_initialized == false) return -1;
222
223         DEBUG(FRESCAN_BWRES_ROBJS_ENABLE_DEBUG,
224               "taking mutex of the reply id=%d\n", id);
225         err = fosa_mutex_lock(&the_reply_objects[id].mutex);
226         if (err != 0) return err;
227
228                 the_reply_objects[id].is_work_done = true;
229
230                 DEBUG(FRESCAN_BWRES_ROBJS_ENABLE_DEBUG,
231                       "signal the cond variable\n");
232                 err = fosa_cond_signal(&the_reply_objects[id].cond);
233                 if (err != 0) goto locked_error;
234
235         err = fosa_mutex_unlock(&the_reply_objects[id].mutex);
236         if (err != 0) return err;
237
238         return 0;
239
240 locked_error:
241         FRESCAN_ERROR("locked error %d\n", err);
242         fosa_mutex_unlock(&the_reply_objects[id].mutex);
243         return err;
244 }
245
246 /**
247  * frescan_bwres_robjs_wait()
248  *
249  * Wait on the cond variable.
250  *
251  **/
252
253 int frescan_bwres_robjs_wait(frescan_bwres_robj_id_t id)
254 {
255         int err;
256
257         if (is_initialized == false) return -1;
258
259         err = fosa_mutex_lock(&the_reply_objects[id].mutex);
260         if (err != 0) return err;
261
262                 while (the_reply_objects[id].is_work_done == false) {
263                         err = fosa_cond_wait(&the_reply_objects[id].cond,
264                                              &the_reply_objects[id].mutex);
265                         if (err != 0) goto locked_error;
266                 }
267
268                 the_reply_objects[id].is_work_done = false;
269
270         err = fosa_mutex_unlock(&the_reply_objects[id].mutex);
271         if (err != 0) return err;
272
273         return 0;
274
275 locked_error:
276         fosa_mutex_unlock(&the_reply_objects[id].mutex);
277         return err;
278 }
279
280 /**
281  * frescan_bwres_robjs_timedwait()
282  *
283  * Wait on the cond variable with a timeout.
284  *
285  **/
286
287 int frescan_bwres_robjs_timedwait(frescan_bwres_robj_id_t id,
288                                   const struct timespec *abstime)
289 {
290         int err;
291
292         if (is_initialized == false) return -1;
293
294         err = fosa_mutex_lock(&the_reply_objects[id].mutex);
295         if (err != 0) return err;
296
297         while (the_reply_objects[id].is_work_done == false) {
298                 err = fosa_cond_timedwait(&the_reply_objects[id].cond,
299                                           &the_reply_objects[id].mutex,
300                                           abstime);
301                 if (err != 0) goto locked_error;
302         }
303
304         the_reply_objects[id].is_work_done = false;
305
306         err = fosa_mutex_unlock(&the_reply_objects[id].mutex);
307         if (err != 0) return err;
308
309         return 0;
310
311 locked_error:
312         fosa_mutex_unlock(&the_reply_objects[id].mutex);
313         return err;
314 }