]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - include/fosa_mutexes_and_condvars.h
Uploading first version of FOSA code !!
[frescor/fosa.git] / include / fosa_mutexes_and_condvars.h
1 // -----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2007 FRESCOR consortium partners:
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 for a link to partners' websites
17 //
18 //           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 //  This file is part of the FRSH implementation
24 //
25 //  FRSH is free software; you can  redistribute it and/or  modify
26 //  it under the terms of  the GNU General Public License as published by
27 //  the Free Software Foundation;  either  version 2, or (at  your option)
28 //  any later version.
29 //
30 //  FRSH  is distributed  in  the hope  that  it  will  be useful,  but
31 //  WITHOUT  ANY  WARRANTY;     without  even the   implied   warranty  of
32 //  MERCHANTABILITY  or  FITNESS FOR  A  PARTICULAR PURPOSE. See  the  GNU
33 //  General Public License for more details.
34 //
35 //  You should have  received a  copy of  the  GNU  General Public License
36 //  distributed  with  FRSH;  see file COPYING.   If not,  write to the
37 //  Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
38 //  02111-1307, USA.
39 //
40 //  As a special exception, if you include this header file into source
41 //  files to be compiled, this header file does not by itself cause
42 //  the resulting executable to be covered by the GNU General Public
43 //  License.  This exception does not however invalidate any other
44 //  reasons why the executable file might be covered by the GNU General
45 //  Public License.
46 // -----------------------------------------------------------------------
47 //fosa_mutexes_and_condvars.h
48 //==============================================
49 //  ********  ******    ********  **********
50 //  **///// /**    **  **//////  /**     /**
51 //  **      /**    ** /**        /**     /**
52 //  ******* /**    ** /********* /**********
53 //  **////  /**    ** ////////** /**//////**
54 //  **      /**    **        /** /**     /**
55 //  **      /**    **  ********  /**     /**
56 //  //       /******/  ////////   //      // 
57 //
58 // FOSA(Frescor Operating System Adaptation layer)
59 //================================================
60
61 #ifndef         FOSA_MUTEX_AND_CONDVARS_H_
62 #define         FOSA_MUTEX_AND_CONDVARS_H_
63
64 /**
65  * @defgroup mutexesandcondvars Mutexes and Condvars
66  *
67  * This module defines the types and functions to abstract mutexes and
68  * conditional variables for the FRSH implementation.
69  *
70  * @{
71  **/
72
73
74 /*******************************************************
75  * Mutexes with priority ceiling
76  ******************************************************/
77
78 /**
79  * fosa_mutex_init()
80  *
81  * Initialize a frsh mutex
82  *
83  * The mutex pointed to by mutex is initialized as a mutex using 
84  * the priority ceiling protocol. A priority ceiling of prioceiling
85  * is assigned to this mutex.
86  * 
87  * Returns 0 if successful; otherwise it returns an error code:
88  *    EINVAL: the value of prioceiling is invalid
89  *    EAGAIN: the system lacked the necessary resources to create the mutex
90  *    ENOMEM: Insufficient memory exists to initialize the mutex
91  *    EBUSY:  The system has detected an attempt to reinitialize the mutex
92  *
93  * Alternatively, in case of error the implementation is allowed to
94  * notify it to the system console and then terminate the FRSH
95  * implementation and dependant applications
96  **/
97 int fosa_mutex_init(frsh_mutex_t *mutex, int prioceiling);
98
99 /**
100  * fosa_mutex_destroy()
101  *
102  * Destroy a frsh mutex
103  * 
104  * The mutex pointed to by mutex is destroyed
105  * 
106  * Returns 0 if successful; otherwise it returns an error code:
107  *    EINVAL: the value of mutex is invalid
108  *    EBUSY:  The mutex is in use (is locked)  
109  *
110  * Alternatively, in case of error the implementation is allowed to
111  * notify it to the system console and then terminate the FRSH
112  * implementation and dependant applications
113  **/
114 int fosa_mutex_destroy(frsh_mutex_t *mutex);
115
116 /**
117  * fosa_mutex_set_prioceiling()
118  *
119  * Dynamically set the priority ceiling of a mutex
120  * 
121  * This function locks the mutex (blocking the calling thread if
122  * necessary) and after it is locked it changes its priority ceiling
123  * to the value specified by new_ceiling, and then it unlocks the
124  * mutex. The previous value of the ceiling is returned in
125  * old_ceiling.
126  * 
127  * Returns 0 if successful; otherwise it returns an error code:
128  *     EINVAL: the value of mutex or prioceiling is invalid
129  *
130  * Alternatively, in case of error the implementation is allowed to
131  * notify it to the system console and then terminate the FRSH
132  * implementation and dependant applications
133  **/
134 int fosa_mutex_set_prioceiling
135    (frsh_mutex_t *mutex, int new_ceiling, int *old_ceiling);
136
137 /**
138  * fosa_mutex_get_prioceiling()
139  *
140  * Dynamically get the priority ceiling of a mutex 
141  *
142  * This function copies into the variable pointed to by ceiling the
143  * current priority ceiling of the mutex referenced by mutex
144  * 
145  * Returns 0 if successful; otherwise it returns an error code:
146  *     EINVAL: the value of mutex is invalid
147  *
148  * Alternatively, in case of error the implementation is allowed to
149  * notify it to the system console and then terminate the FRSH
150  * implementation and dependant applications
151  **/
152 int fosa_mutex_get_prioceiling(const frsh_mutex_t *mutex, int *ceiling);
153
154 /**
155  * fosa_mutex_lock()
156  *
157  * Lock a mutex
158  * 
159  * This function locks the mutex specified by mutex. If it is already
160  * locked, the calling thread blocks until the mutex becomes
161  * available. The operation returns with the mutex in the locked
162  * state, with the calling thread as its owner.
163  * 
164  * Returns 0 if successful; otherwise it returns an error code:
165  *    EINVAL: the value of mutex is invalid, or the priority of the
166  *            calling thread is higher than the priority ceiling of the mutex
167  *    EDEADLK: the current thread already owns this mutex
168  *
169  * Alternatively, in case of error the implementation is allowed to
170  * notify it to the system console and then terminate the FRSH
171  * implementation and dependant applications
172  **/
173 int fosa_mutex_lock(frsh_mutex_t *mutex);
174
175 /**
176  * fosa_mutex_trylock()
177  *
178  * Try locking a mutex
179  *
180  * This function is identical to fosa_mutex_lock() except that if the
181  * mutex is already locked the call returns immediately with an error
182  * indication.
183  *
184  * Returns 0 if successful; otherwise it returns an error code:
185  *    EINVAL: the value of mutex is invalid, or the priority of the
186  *            calling thread is higher than the priority ceiling of the mutex
187  *    EBUSY: the mutex was already locked
188  *
189  * Alternatively, except for EBUSY, in case of error the
190  * implementation is allowed to notify it to the system console and
191  * then terminate the FRSH implementation and dependant applications
192  **/
193 int fosa_mutex_trylock(frsh_mutex_t *mutex);
194
195 /**
196  * fosa_mutex_unlock()
197  *
198  * Unlock a mutex
199  * 
200  * This function must be called by the owner of the mutex referenced
201  * by mutex, to unlock it. If there are threads blocked on the mutex
202  * the mutex becomes available and the highest priority thread is
203  * awakened to acquire the mutex.
204  * 
205  * Returns 0 if successful; otherwise it returns an error code:
206  *     EINVAL: the value of mutex is invalid
207  *     EPERM: the calling thread is not the owner of the mutex
208  *
209  * Alternatively, except for EBUSY, in case of error the
210  * implementation is allowed to notify it to the system console and
211  * then terminate the FRSH implementation and dependant applications 
212  **/
213 int fosa_mutex_unlock(frsh_mutex_t *mutex);
214
215
216 /**********************
217  * Condition variables
218  *********************/
219
220 /**
221  * fosa_cond_init()
222  *
223  * Initiatize a condition variable
224  * 
225  * The condition variable referenced by cond is initialized with
226  * the attributes required by the FOSA implementation.
227  *
228  *  Returns 0 if successful; otherwise it returns an error code:
229  *     EAGAIN: the system lacked the necessary resources to create the
230  *             condition variable
231  *     ENOMEM: Insufficient memory exists to initialize the condition variable
232  *     EBUSY:  The system has detected an attempt to reinitialize the 
233  *             condition variable
234  *
235  * Alternatively, in case of error the implementation is allowed to
236  * notify it to the system console and then terminate the FRSH
237  * implementation and dependant applications
238  **/
239 int fosa_cond_init(fosa_cond_t *cond);
240
241 /**
242  * fosa_cond_destroy()
243  *
244  * Destroy a condition variable
245  *
246  * The condition variable pointed to by cond is destroyed
247  * 
248  * Returns 0 if successful; otherwise it returns an error code:
249  *     EINVAL: the value of cond is invalid
250  *     EBUSY:  The condition variable is in use (a thread is waiting on it)  
251  *
252  * Alternatively, in case of error the implementation is allowed to
253  * notify it to the system console and then terminate the FRSH
254  * implementation and dependant applications
255  **/
256 int fosa_cond_destroy(fosa_cond_t *cond);
257
258 /**
259  * fosa_cond_signal()
260  *
261  * Signal a condition variable
262  *
263  * This call unblocks at least one of the threads that are waiting on
264  * the condition variable referenced by cond. If there are no threads
265  * waiting, the function has no effect
266  *
267  * Returns 0 if successful; otherwise it returns an error code:
268  *     EINVAL: the value of cond is invalid
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_signal(fosa_cond_t *cond);
275
276 /**
277  * fosa_cond_broadcast()
278  *
279  * Broadcast a condition variable
280  *
281  * This call unblocks all of the threads that are waiting on the
282  * 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  *     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_broadcast(fosa_cond_t *cond);
293
294 /**
295  * fosa_cond_wait()
296  *
297  * Wait at a condition variable
298  *
299  * This call is used to block on the condition variable referenced by
300  * cond. It shall be called with the mutex referenced by mutex
301  * locked. The function releases the mutex and blocks the calling
302  * thread until the condition is signalled by some other thread and
303  * the calling thread is awakened. Then it locks the mutex and
304  * returns with the mutex locked by the calling thread.
305  *
306  * Returns 0 if successful; otherwise it returns an error code:
307  *    EINVAL: the value of cond or mutex is invalid, or different
308  *            mutexes were used for concurrent wait operations on cond, or
309  *            the mutex was not owned by the calling thread
310  *
311  * Alternatively, in case of error the implementation is allowed to
312  * notify it to the system console and then terminate the FRSH
313  * implementation and dependant applications
314  **/
315 int fosa_cond_wait(fosa_cond_t *cond, frsh_mutex_t *mutex);
316
317 /**
318  * fosa_cond_timedwait()
319  *
320  * Wait at a condition variable, with a timeout
321  * 
322  * This function is equal to fosa_cond_wait(), except that the maximum
323  * wait time is limited to the absolute time referenced by abstime, as
324  * measured by the FOSA_CLOCK_REALTIME clock.
325  *
326  * Returns 0 if successful; otherwise it returns an error code:
327  *     EINVAL: the value of cond or mutex or abstime is invalid, or different
328  *             mutexes were used for concurrent wait operations on cond, or
329  *             the mutex was not owned by the calling thread
330  *     ETIMEDOUT: the timeout expired
331  *
332  * Alternatively, except for ETIMEDOUT, in case of error the
333  * implementation is allowed to notify it to the system console and
334  * then terminate the FRSH implementation and dependant applications
335  **/
336 int fosa_cond_timedwait(fosa_cond_t *cond, frsh_mutex_t *mutex, 
337       const struct timespec abstime);
338
339 /*@}*/ 
340  
341
342
343
344 #endif      /* !FOSA_MUTEX_AND_CONDVARS_H_ */