]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - include/fosa_mutexes_and_condvars.h
Migrating FOSA trunk to d-ac2v2. Phase 1 moving FRSH-FOSA to FOSA
[frescor/fosa.git] / include / fosa_mutexes_and_condvars.h
1 //----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2007 by the FRESCOR consortium:
3 //
4 //    Universidad de Cantabria,              SPAIN
5 //    University of York,                    UK
6 //    Scuola Superiore Sant'Anna,            ITALY
7 //    Kaiserslautern University,             GERMANY
8 //    Univ. Politecnica  Valencia,           SPAIN
9 //    Czech Technical University in Prague,  CZECH REPUBLIC
10 //    ENEA                                   SWEDEN
11 //    Thales Communication S.A.              FRANCE
12 //    Visual Tools S.A.                      SPAIN
13 //    Rapita Systems Ltd                     UK
14 //    Evidence                               ITALY
15 //
16 //    See http://www.frescor.org
17 //
18 //        The FRESCOR project (FP6/2005/IST/5-034026) is funded
19 //        in part by the European Union Sixth Framework Programme
20 //        The European Union is not liable of any use that may be
21 //        made of this code.
22 //
23 //
24 //  based on previous work (FSF) done in the FIRST project
25 //
26 //   Copyright (C) 2005  Mälardalen University, SWEDEN
27 //                       Scuola Superiore S.Anna, ITALY
28 //                       Universidad de Cantabria, SPAIN
29 //                       University of York, UK
30 //
31 // This file is part of FOSA (Frsh Operating System Abstraction)
32 //
33 // FOSA is free software; you can redistribute it and/or modify it
34 // under terms of the GNU General Public License as published by the
35 // Free Software Foundation; either version 2, or (at your option) any
36 // later version.  FOSA is distributed in the hope that it will be
37 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
38 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 // General Public License for more details. You should have received a
40 // copy of the GNU General Public License along with FOSA; see file
41 // COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
42 // Cambridge, MA 02139, USA.
43 //
44 // As a special exception, including FOSA header files in a file,
45 // instantiating FOSA generics or templates, or linking other files
46 // with FOSA objects to produce an executable application, does not
47 // by itself cause the resulting executable application to be covered
48 // by the GNU General Public License. This exception does not
49 // however invalidate any other reasons why the executable file might be
50 // covered by the GNU Public License.
51 // -----------------------------------------------------------------------
52 //fosa_mutexes_and_condvars.h
53 //==============================================
54 //  ********  ******    ********  **********
55 //  **///// /**    **  **//////  /**     /**
56 //  **      /**    ** /**        /**     /**
57 //  ******* /**    ** /********* /**********
58 //  **////  /**    ** ////////** /**//////**
59 //  **      /**    **        /** /**     /**
60 //  **      /**    **  ********  /**     /**
61 //  //       /******/  ////////   //      //
62 //
63 // FOSA(Frescor Operating System Adaptation layer)
64 //================================================
65
66 #ifndef         FOSA_MUTEX_AND_CONDVARS_H_
67 #define         FOSA_MUTEX_AND_CONDVARS_H_
68
69
70 #include "fosa_types.h"
71
72 /**
73  * @defgroup mutexesandcondvars Mutexes and Condvars
74  * @ingroup fosa
75  *
76  * This module defines the types and functions to abstract mutexes and
77  * conditional variables for the FRSH implementation.
78  *
79  * @{
80  **/
81
82
83 /*******************************************************
84  * Mutexes with priority ceiling
85  ******************************************************/
86
87 /**
88  * fosa_mutex_init()
89  *
90  * Initialize a frsh mutex
91  *
92  * The mutex pointed to by mutex is initialized as a mutex using
93  * the priority ceiling protocol. A priority ceiling of prioceiling
94  * is assigned to this mutex.
95  *
96  * Returns 0 if successful; otherwise it returns an error code:
97  *
98  * FOSA_EINVAL: the value of prioceiling is invalid \n
99  * FOSA_EAGAIN: the system lacked the necessary resources to create
100  *                the mutex \n
101  * FOSA_ENOMEM: Insufficient memory exists to initialize the mutex \n
102  * FOSA_EBUSY:  The system has detected an attempt to reinitialize the mutex
103  *
104  * Alternatively, in case of error the implementation is allowed to
105  * notify it to the system console and then terminate the FRSH
106  * implementation and dependant applications
107  **/
108 int fosa_mutex_init(fosa_mutex_t *mutex, int prioceiling);
109
110 /**
111  * fosa_mutex_destroy()
112  *
113  * Destroy a frsh mutex
114  *
115  * The mutex pointed to by mutex is destroyed
116  *
117  * Returns 0 if successful; otherwise it returns an error code:
118  *
119  *    FOSA_EINVAL: the value of mutex is invalid \n
120  *    FOSA_EBUSY:  The mutex is in use (is locked)\n
121  *
122  * Alternatively, in case of error the implementation is allowed to
123  * notify it to the system console and then terminate the FRSH
124  * implementation and dependant applications
125  **/
126 int fosa_mutex_destroy(fosa_mutex_t *mutex);
127
128 /**
129  * fosa_mutex_set_prioceiling()
130  *
131  * Dynamically set the priority ceiling of a mutex
132  *
133  * This function locks the mutex (blocking the calling thread if
134  * necessary) and after it is locked it changes its priority ceiling
135  * to the value specified by new_ceiling, and then it unlocks the
136  * mutex. The previous value of the ceiling is returned in
137  * old_ceiling.
138  *
139  * Returns 0 if successful; otherwise it returns an error code:
140  *
141  *     FOSA_EINVAL: the value of mutex or prioceiling is invalid \n
142  *
143  * Alternatively, in case of error the implementation is allowed to
144  * notify it to the system console and then terminate the FRSH
145  * implementation and dependant applications
146  **/
147 int fosa_mutex_set_prioceiling
148    (fosa_mutex_t *mutex, int new_ceiling, int *old_ceiling);
149
150 /**
151  * fosa_mutex_get_prioceiling()
152  *
153  * Dynamically get the priority ceiling of a mutex
154  *
155  * This function copies into the variable pointed to by ceiling the
156  * current priority ceiling of the mutex referenced by mutex
157  *
158  * Returns 0 if successful; otherwise it returns an error code:
159  *
160  *     FOSA_EINVAL: the value of mutex is invalid \n
161  *
162  * Alternatively, in case of error the implementation is allowed to
163  * notify it to the system console and then terminate the FRSH
164  * implementation and dependant applications
165  **/
166 int fosa_mutex_get_prioceiling(const fosa_mutex_t *mutex, int *ceiling);
167
168 /**
169  * fosa_mutex_lock()
170  *
171  * Lock a mutex
172  *
173  * This function locks the mutex specified by mutex. If it is already
174  * locked, the calling thread blocks until the mutex becomes
175  * available. The operation returns with the mutex in the locked
176  * state, with the calling thread as its owner.
177  *
178  * Returns 0 if successful; otherwise it returns an error code: \n
179  *
180  *    FOSA_EINVAL: the value of mutex is invalid, or the priority of the
181  *            calling thread is higher than the priority ceiling of
182  *            the mutex \n
183  *
184  *    FOSA_EDEADLK: the current thread already owns this mutex \n
185  *
186  * Alternatively, in case of error the implementation is allowed to
187  * notify it to the system console and then terminate the FRSH
188  * implementation and dependant applications
189  **/
190 int fosa_mutex_lock(fosa_mutex_t *mutex);
191
192 /**
193  * fosa_mutex_trylock()
194  *
195  * Try locking a mutex
196  *
197  * This function is identical to fosa_mutex_lock() except that if the
198  * mutex is already locked the call returns immediately with an error
199  * indication.
200  *
201  * Returns 0 if successful; otherwise it returns an error code:
202  *    FOSA_EINVAL: the value of mutex is invalid, or the priority of the
203  *       calling thread is higher than the priority ceiling of the
204  *       mutex \n
205  *    FOSA_EBUSY: the mutex was already locked \n
206  *
207  * Alternatively, except for FOSA_EBUSY, in case of error the
208  * implementation is allowed to notify it to the system console and
209  * then terminate the FRSH implementation and dependant applications
210  **/
211 int fosa_mutex_trylock(fosa_mutex_t *mutex);
212
213 /**
214  * fosa_mutex_unlock()
215  *
216  * Unlock a mutex
217  *
218  * This function must be called by the owner of the mutex referenced
219  * by mutex, to unlock it. If there are threads blocked on the mutex
220  * the mutex becomes available and the highest priority thread is
221  * awakened to acquire the mutex.
222  *
223  * Returns 0 if successful; otherwise it returns an error code:
224  *     FOSA_EINVAL: the value of mutex is invalid
225  *     FOSA_EPERM: the calling thread is not the owner of the mutex
226  *
227  * Alternatively, except for FOSA_EBUSY, in case of error the
228  * implementation is allowed to notify it to the system console and
229  * then terminate the FRSH implementation and dependant applications
230  **/
231 int fosa_mutex_unlock(fosa_mutex_t *mutex);
232
233
234 /**********************
235  * Condition variables
236  *********************/
237
238 /**
239  * fosa_cond_init()
240  *
241  * Initiatize a condition variable
242  *
243  * The condition variable referenced by cond is initialized with
244  * the attributes required by the FOSA implementation.
245  *
246  *  Returns 0 if successful; otherwise it returns an error code:
247  *     FOSA_EAGAIN: the system lacked the necessary resources to create the
248  *             condition variable
249  *     FOSA_ENOMEM: Insufficient memory exists to initialize the condition variable
250  *     FOSA_EBUSY:  The system has detected an attempt to reinitialize the
251  *             condition variable
252  *
253  * Alternatively, in case of error the implementation is allowed to
254  * notify it to the system console and then terminate the FRSH
255  * implementation and dependant applications
256  **/
257 int fosa_cond_init(fosa_cond_t *cond);
258
259 /**
260  * fosa_cond_destroy()
261  *
262  * Destroy a condition variable
263  *
264  * The condition variable pointed to by cond is destroyed
265  *
266  * Returns 0 if successful; otherwise it returns an error code:
267  *     FOSA_EINVAL: the value of cond is invalid
268  *     FOSA_EBUSY:  The condition variable is in use (a thread is waiting on it)
269  *
270  * Alternatively, in case of error the implementation is allowed to
271  * notify it to the system console and then terminate the FRSH
272  * implementation and dependant applications
273  **/
274 int fosa_cond_destroy(fosa_cond_t *cond);
275
276 /**
277  * fosa_cond_signal()
278  *
279  * Signal a condition variable
280  *
281  * This call unblocks at least one of the threads that are waiting on
282  * the condition variable referenced by cond. If there are no threads
283  * waiting, the function has no effect
284  *
285  * Returns 0 if successful; otherwise it returns an error code:
286  *     FOSA_EINVAL: the value of cond is invalid
287  *
288  * Alternatively, in case of error the implementation is allowed to
289  * notify it to the system console and then terminate the FRSH
290  * implementation and dependant applications
291  **/
292 int fosa_cond_signal(fosa_cond_t *cond);
293
294 /**
295  * fosa_cond_broadcast()
296  *
297  * Broadcast a condition variable
298  *
299  * This call unblocks all of the threads that are waiting on the
300  * condition variable referenced by cond. If there are no threads
301  * waiting, the function has no effect.
302  *
303  * Returns 0 if successful; otherwise it returns an error code:
304  *     FOSA_EINVAL: the value of cond is invalid
305  *
306  * Alternatively, in case of error the implementation is allowed to
307  * notify it to the system console and then terminate the FRSH
308  * implementation and dependant applications
309  **/
310 int fosa_cond_broadcast(fosa_cond_t *cond);
311
312 /**
313  * fosa_cond_wait()
314  *
315  * Wait at a condition variable
316  *
317  * This call is used to block on the condition variable referenced by
318  * cond. It shall be called with the mutex referenced by mutex
319  * locked. The function releases the mutex and blocks the calling
320  * thread until the condition is signalled by some other thread and
321  * the calling thread is awakened. Then it locks the mutex and
322  * returns with the mutex locked by the calling thread.
323  *
324  * Returns 0 if successful; otherwise it returns an error code:
325  *    FOSA_EINVAL: the value of cond or mutex is invalid, or different
326  *            mutexes were used for concurrent wait operations on cond, or
327  *            the mutex was not owned by the calling thread
328  *
329  * Alternatively, in case of error the implementation is allowed to
330  * notify it to the system console and then terminate the FRSH
331  * implementation and dependant applications
332  **/
333 int fosa_cond_wait(fosa_cond_t *cond, fosa_mutex_t *mutex);
334
335 /**
336  * fosa_cond_timedwait()
337  *
338  * Wait at a condition variable, with a timeout
339  *
340  * This function is equal to fosa_cond_wait(), except that the maximum
341  * wait time is limited to the absolute time referenced by abstime, as
342  * measured by the FOSA_CLOCK_ABSOLUTE clock.
343  *
344  * Returns 0 if successful; otherwise it returns an error code:
345  *     FOSA_EINVAL: the value of cond or mutex or abstime is invalid, or different
346  *             mutexes were used for concurrent wait operations on cond, or
347  *             the mutex was not owned by the calling thread
348  *     FOSA_ETIMEDOUT: the timeout expired
349  *
350  * Alternatively, except for FOSA_ETIMEDOUT, in case of error the
351  * implementation is allowed to notify it to the system console and
352  * then terminate the FRSH implementation and dependant applications
353  **/
354 int fosa_cond_timedwait(fosa_cond_t *cond, fosa_mutex_t *mutex,
355       const struct timespec *abstime);
356
357 /*@}*/
358
359
360
361
362 #endif      /* !FOSA_MUTEX_AND_CONDVARS_H_ */