]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - include/fosa_mutexes_and_condvars.h
Renaming FOSA ADS function to use _ to separate words.
[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  * @ingroup fosa
67  *
68  * This module defines the types and functions to abstract mutexes and
69  * conditional variables for the FRSH implementation.
70  *
71  * @{
72  **/
73
74
75 /*******************************************************
76  * Mutexes with priority ceiling
77  ******************************************************/
78
79 /**
80  * fosa_mutex_init()
81  *
82  * Initialize a frsh mutex
83  *
84  * The mutex pointed to by mutex is initialized as a mutex using 
85  * the priority ceiling protocol. A priority ceiling of prioceiling
86  * is assigned to this mutex.
87  * 
88  * Returns 0 if successful; otherwise it returns an error code:
89  *    EINVAL: the value of prioceiling is invalid
90  *    EAGAIN: the system lacked the necessary resources to create the mutex
91  *    ENOMEM: Insufficient memory exists to initialize the mutex
92  *    EBUSY:  The system has detected an attempt to reinitialize the mutex
93  *
94  * Alternatively, in case of error the implementation is allowed to
95  * notify it to the system console and then terminate the FRSH
96  * implementation and dependant applications
97  **/
98 int fosa_mutex_init(frsh_mutex_t *mutex, int prioceiling);
99
100 /**
101  * fosa_mutex_destroy()
102  *
103  * Destroy a frsh mutex
104  * 
105  * The mutex pointed to by mutex is destroyed
106  * 
107  * Returns 0 if successful; otherwise it returns an error code:
108  *    EINVAL: the value of mutex is invalid
109  *    EBUSY:  The mutex is in use (is locked)  
110  *
111  * Alternatively, in case of error the implementation is allowed to
112  * notify it to the system console and then terminate the FRSH
113  * implementation and dependant applications
114  **/
115 int fosa_mutex_destroy(frsh_mutex_t *mutex);
116
117 /**
118  * fosa_mutex_set_prioceiling()
119  *
120  * Dynamically set the priority ceiling of a mutex
121  * 
122  * This function locks the mutex (blocking the calling thread if
123  * necessary) and after it is locked it changes its priority ceiling
124  * to the value specified by new_ceiling, and then it unlocks the
125  * mutex. The previous value of the ceiling is returned in
126  * old_ceiling.
127  * 
128  * Returns 0 if successful; otherwise it returns an error code:
129  *     EINVAL: the value of mutex or prioceiling is invalid
130  *
131  * Alternatively, in case of error the implementation is allowed to
132  * notify it to the system console and then terminate the FRSH
133  * implementation and dependant applications
134  **/
135 int fosa_mutex_set_prioceiling
136    (frsh_mutex_t *mutex, int new_ceiling, int *old_ceiling);
137
138 /**
139  * fosa_mutex_get_prioceiling()
140  *
141  * Dynamically get the priority ceiling of a mutex 
142  *
143  * This function copies into the variable pointed to by ceiling the
144  * current priority ceiling of the mutex referenced by mutex
145  * 
146  * Returns 0 if successful; otherwise it returns an error code:
147  *     EINVAL: the value of mutex is invalid
148  *
149  * Alternatively, in case of error the implementation is allowed to
150  * notify it to the system console and then terminate the FRSH
151  * implementation and dependant applications
152  **/
153 int fosa_mutex_get_prioceiling(const frsh_mutex_t *mutex, int *ceiling);
154
155 /**
156  * fosa_mutex_lock()
157  *
158  * Lock a mutex
159  * 
160  * This function locks the mutex specified by mutex. If it is already
161  * locked, the calling thread blocks until the mutex becomes
162  * available. The operation returns with the mutex in the locked
163  * state, with the calling thread as its owner.
164  * 
165  * Returns 0 if successful; otherwise it returns an error code:
166  *    EINVAL: the value of mutex is invalid, or the priority of the
167  *            calling thread is higher than the priority ceiling of the mutex
168  *    EDEADLK: the current thread already owns this mutex
169  *
170  * Alternatively, in case of error the implementation is allowed to
171  * notify it to the system console and then terminate the FRSH
172  * implementation and dependant applications
173  **/
174 int fosa_mutex_lock(frsh_mutex_t *mutex);
175
176 /**
177  * fosa_mutex_trylock()
178  *
179  * Try locking a mutex
180  *
181  * This function is identical to fosa_mutex_lock() except that if the
182  * mutex is already locked the call returns immediately with an error
183  * indication.
184  *
185  * Returns 0 if successful; otherwise it returns an error code:
186  *    EINVAL: the value of mutex is invalid, or the priority of the
187  *            calling thread is higher than the priority ceiling of the mutex
188  *    EBUSY: the mutex was already locked
189  *
190  * Alternatively, except for EBUSY, in case of error the
191  * implementation is allowed to notify it to the system console and
192  * then terminate the FRSH implementation and dependant applications
193  **/
194 int fosa_mutex_trylock(frsh_mutex_t *mutex);
195
196 /**
197  * fosa_mutex_unlock()
198  *
199  * Unlock a mutex
200  * 
201  * This function must be called by the owner of the mutex referenced
202  * by mutex, to unlock it. If there are threads blocked on the mutex
203  * the mutex becomes available and the highest priority thread is
204  * awakened to acquire the mutex.
205  * 
206  * Returns 0 if successful; otherwise it returns an error code:
207  *     EINVAL: the value of mutex is invalid
208  *     EPERM: the calling thread is not the owner of the mutex
209  *
210  * Alternatively, except for EBUSY, in case of error the
211  * implementation is allowed to notify it to the system console and
212  * then terminate the FRSH implementation and dependant applications 
213  **/
214 int fosa_mutex_unlock(frsh_mutex_t *mutex);
215
216
217 /**********************
218  * Condition variables
219  *********************/
220
221 /**
222  * fosa_cond_init()
223  *
224  * Initiatize a condition variable
225  * 
226  * The condition variable referenced by cond is initialized with
227  * the attributes required by the FOSA implementation.
228  *
229  *  Returns 0 if successful; otherwise it returns an error code:
230  *     EAGAIN: the system lacked the necessary resources to create the
231  *             condition variable
232  *     ENOMEM: Insufficient memory exists to initialize the condition variable
233  *     EBUSY:  The system has detected an attempt to reinitialize the 
234  *             condition variable
235  *
236  * Alternatively, in case of error the implementation is allowed to
237  * notify it to the system console and then terminate the FRSH
238  * implementation and dependant applications
239  **/
240 int fosa_cond_init(fosa_cond_t *cond);
241
242 /**
243  * fosa_cond_destroy()
244  *
245  * Destroy a condition variable
246  *
247  * The condition variable pointed to by cond is destroyed
248  * 
249  * Returns 0 if successful; otherwise it returns an error code:
250  *     EINVAL: the value of cond is invalid
251  *     EBUSY:  The condition variable is in use (a thread is waiting on it)  
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_destroy(fosa_cond_t *cond);
258
259 /**
260  * fosa_cond_signal()
261  *
262  * Signal a condition variable
263  *
264  * This call unblocks at least one of the threads that are waiting on
265  * the condition variable referenced by cond. If there are no threads
266  * waiting, the function has no effect
267  *
268  * Returns 0 if successful; otherwise it returns an error code:
269  *     EINVAL: the value of cond is invalid
270  *
271  * Alternatively, in case of error the implementation is allowed to
272  * notify it to the system console and then terminate the FRSH
273  * implementation and dependant applications
274  **/
275 int fosa_cond_signal(fosa_cond_t *cond);
276
277 /**
278  * fosa_cond_broadcast()
279  *
280  * Broadcast a condition variable
281  *
282  * This call unblocks all of the threads that are waiting on the
283  * condition variable referenced by cond. If there are no threads
284  * waiting, the function has no effect.
285  *
286  * Returns 0 if successful; otherwise it returns an error code:
287  *     EINVAL: the value of cond is invalid
288  *
289  * Alternatively, in case of error the implementation is allowed to
290  * notify it to the system console and then terminate the FRSH
291  * implementation and dependant applications
292  **/
293 int fosa_cond_broadcast(fosa_cond_t *cond);
294
295 /**
296  * fosa_cond_wait()
297  *
298  * Wait at a condition variable
299  *
300  * This call is used to block on the condition variable referenced by
301  * cond. It shall be called with the mutex referenced by mutex
302  * locked. The function releases the mutex and blocks the calling
303  * thread until the condition is signalled by some other thread and
304  * the calling thread is awakened. Then it locks the mutex and
305  * returns with the mutex locked by the calling thread.
306  *
307  * Returns 0 if successful; otherwise it returns an error code:
308  *    EINVAL: the value of cond or mutex is invalid, or different
309  *            mutexes were used for concurrent wait operations on cond, or
310  *            the mutex was not owned by the calling thread
311  *
312  * Alternatively, in case of error the implementation is allowed to
313  * notify it to the system console and then terminate the FRSH
314  * implementation and dependant applications
315  **/
316 int fosa_cond_wait(fosa_cond_t *cond, frsh_mutex_t *mutex);
317
318 /**
319  * fosa_cond_timedwait()
320  *
321  * Wait at a condition variable, with a timeout
322  * 
323  * This function is equal to fosa_cond_wait(), except that the maximum
324  * wait time is limited to the absolute time referenced by abstime, as
325  * measured by the FOSA_CLOCK_REALTIME clock.
326  *
327  * Returns 0 if successful; otherwise it returns an error code:
328  *     EINVAL: the value of cond or mutex or abstime is invalid, or different
329  *             mutexes were used for concurrent wait operations on cond, or
330  *             the mutex was not owned by the calling thread
331  *     ETIMEDOUT: the timeout expired
332  *
333  * Alternatively, except for ETIMEDOUT, in case of error the
334  * implementation is allowed to notify it to the system console and
335  * then terminate the FRSH implementation and dependant applications
336  **/
337 int fosa_cond_timedwait(fosa_cond_t *cond, frsh_mutex_t *mutex, 
338       const struct timespec abstime);
339
340 /*@}*/ 
341  
342
343
344
345 #endif      /* !FOSA_MUTEX_AND_CONDVARS_H_ */