1 //----------------------------------------------------------------------
2 // Copyright (C) 2006 - 2007 by the FRESCOR consortium:
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
11 // Thales Communication S.A. FRANCE
12 // Visual Tools S.A. SPAIN
13 // Rapita Systems Ltd UK
16 // See http://www.frescor.org
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
24 // based on previous work (FSF) done in the FIRST project
26 // Copyright (C) 2005 Mälardalen University, SWEDEN
27 // Scuola Superiore S.Anna, ITALY
28 // Universidad de Cantabria, SPAIN
29 // University of York, UK
31 // This file is part of FOSA (Frsh Operating System Abstraction)
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.
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 // // /******/ //////// // //
63 // FOSA(Frescor Operating System Adaptation layer)
64 //================================================
66 #ifndef FOSA_MUTEX_AND_CONDVARS_H_
67 #define FOSA_MUTEX_AND_CONDVARS_H_
70 #include "fosa_types.h"
75 * @defgroup mutexesandcondvars Mutexes and Condvars
78 * This module defines the types and functions to abstract mutexes and
79 * conditional variables for the FRSH implementation.
85 /*******************************************************
86 * Mutexes with priority ceiling
87 ******************************************************/
92 * Initialize a frsh mutex
94 * The mutex pointed to by mutex is initialized as a mutex using
95 * the priority ceiling protocol. A priority ceiling of prioceiling
96 * is assigned to this mutex.
98 * Returns 0 if successful; otherwise it returns an error code:
100 * FOSA_EINVAL: the value of prioceiling is invalid \n
101 * FOSA_EAGAIN: the system lacked the necessary resources to create
103 * FOSA_ENOMEM: Insufficient memory exists to initialize the mutex \n
104 * FOSA_EBUSY: The system has detected an attempt to reinitialize the mutex
106 * Alternatively, in case of error the implementation is allowed to
107 * notify it to the system console and then terminate the FRSH
108 * implementation and dependant applications
110 int fosa_mutex_init(fosa_mutex_t *mutex, int prioceiling);
113 * fosa_mutex_destroy()
115 * Destroy a frsh mutex
117 * The mutex pointed to by mutex is destroyed
119 * Returns 0 if successful; otherwise it returns an error code:
121 * FOSA_EINVAL: the value of mutex is invalid \n
122 * FOSA_EBUSY: The mutex is in use (is locked)\n
124 * Alternatively, in case of error the implementation is allowed to
125 * notify it to the system console and then terminate the FRSH
126 * implementation and dependant applications
128 int fosa_mutex_destroy(fosa_mutex_t *mutex);
131 * fosa_mutex_set_prioceiling()
133 * Dynamically set the priority ceiling of a mutex
135 * This function locks the mutex (blocking the calling thread if
136 * necessary) and after it is locked it changes its priority ceiling
137 * to the value specified by new_ceiling, and then it unlocks the
138 * mutex. The previous value of the ceiling is returned in
141 * Returns 0 if successful; otherwise it returns an error code:
143 * FOSA_EINVAL: the value of mutex or prioceiling is invalid \n
145 * Alternatively, in case of error the implementation is allowed to
146 * notify it to the system console and then terminate the FRSH
147 * implementation and dependant applications
149 int fosa_mutex_set_prioceiling
150 (fosa_mutex_t *mutex, int new_ceiling, int *old_ceiling);
153 * fosa_mutex_get_prioceiling()
155 * Dynamically get the priority ceiling of a mutex
157 * This function copies into the variable pointed to by ceiling the
158 * current priority ceiling of the mutex referenced by mutex
160 * Returns 0 if successful; otherwise it returns an error code:
162 * FOSA_EINVAL: the value of mutex is invalid \n
164 * Alternatively, in case of error the implementation is allowed to
165 * notify it to the system console and then terminate the FRSH
166 * implementation and dependant applications
168 int fosa_mutex_get_prioceiling(const fosa_mutex_t *mutex, int *ceiling);
175 * This function locks the mutex specified by mutex. If it is already
176 * locked, the calling thread blocks until the mutex becomes
177 * available. The operation returns with the mutex in the locked
178 * state, with the calling thread as its owner.
180 * Returns 0 if successful; otherwise it returns an error code: \n
182 * FOSA_EINVAL: the value of mutex is invalid, or the priority of the
183 * calling thread is higher than the priority ceiling of
186 * FOSA_EDEADLK: the current thread already owns this mutex \n
188 * Alternatively, in case of error the implementation is allowed to
189 * notify it to the system console and then terminate the FRSH
190 * implementation and dependant applications
192 int fosa_mutex_lock(fosa_mutex_t *mutex);
195 * fosa_mutex_trylock()
197 * Try locking a mutex
199 * This function is identical to fosa_mutex_lock() except that if the
200 * mutex is already locked the call returns immediately with an error
203 * Returns 0 if successful; otherwise it returns an error code:
204 * FOSA_EINVAL: the value of mutex is invalid, or the priority of the
205 * calling thread is higher than the priority ceiling of the
207 * FOSA_EBUSY: the mutex was already locked \n
209 * Alternatively, except for FOSA_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
213 int fosa_mutex_trylock(fosa_mutex_t *mutex);
216 * fosa_mutex_unlock()
220 * This function must be called by the owner of the mutex referenced
221 * by mutex, to unlock it. If there are threads blocked on the mutex
222 * the mutex becomes available and the highest priority thread is
223 * awakened to acquire the mutex.
225 * Returns 0 if successful; otherwise it returns an error code:
226 * FOSA_EINVAL: the value of mutex is invalid
227 * FOSA_EPERM: the calling thread is not the owner of the mutex
229 * Alternatively, except for FOSA_EBUSY, in case of error the
230 * implementation is allowed to notify it to the system console and
231 * then terminate the FRSH implementation and dependant applications
233 int fosa_mutex_unlock(fosa_mutex_t *mutex);
236 /**********************
237 * Condition variables
238 *********************/
243 * Initiatize a condition variable
245 * The condition variable referenced by cond is initialized with
246 * the attributes required by the FOSA implementation.
248 * Returns 0 if successful; otherwise it returns an error code:
249 * FOSA_EAGAIN: the system lacked the necessary resources to create the
251 * FOSA_ENOMEM: Insufficient memory exists to initialize the condition variable
252 * FOSA_EBUSY: The system has detected an attempt to reinitialize the
255 * Alternatively, in case of error the implementation is allowed to
256 * notify it to the system console and then terminate the FRSH
257 * implementation and dependant applications
259 int fosa_cond_init(fosa_cond_t *cond);
262 * fosa_cond_destroy()
264 * Destroy a condition variable
266 * The condition variable pointed to by cond is destroyed
268 * Returns 0 if successful; otherwise it returns an error code:
269 * FOSA_EINVAL: the value of cond is invalid
270 * FOSA_EBUSY: The condition variable is in use (a thread is waiting on it)
272 * Alternatively, in case of error the implementation is allowed to
273 * notify it to the system console and then terminate the FRSH
274 * implementation and dependant applications
276 int fosa_cond_destroy(fosa_cond_t *cond);
281 * Signal a condition variable
283 * This call unblocks at least one of the threads that are waiting on
284 * the condition variable referenced by cond. If there are no threads
285 * waiting, the function has no effect
287 * Returns 0 if successful; otherwise it returns an error code:
288 * FOSA_EINVAL: the value of cond is invalid
290 * Alternatively, in case of error the implementation is allowed to
291 * notify it to the system console and then terminate the FRSH
292 * implementation and dependant applications
294 int fosa_cond_signal(fosa_cond_t *cond);
297 * fosa_cond_broadcast()
299 * Broadcast a condition variable
301 * This call unblocks all of the threads that are waiting on the
302 * condition variable referenced by cond. If there are no threads
303 * waiting, the function has no effect.
305 * Returns 0 if successful; otherwise it returns an error code:
306 * FOSA_EINVAL: the value of cond is invalid
308 * Alternatively, in case of error the implementation is allowed to
309 * notify it to the system console and then terminate the FRSH
310 * implementation and dependant applications
312 int fosa_cond_broadcast(fosa_cond_t *cond);
317 * Wait at a condition variable
319 * This call is used to block on the condition variable referenced by
320 * cond. It shall be called with the mutex referenced by mutex
321 * locked. The function releases the mutex and blocks the calling
322 * thread until the condition is signalled by some other thread and
323 * the calling thread is awakened. Then it locks the mutex and
324 * returns with the mutex locked by the calling thread.
326 * Returns 0 if successful; otherwise it returns an error code:
327 * FOSA_EINVAL: the value of cond or mutex 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
331 * Alternatively, in case of error the implementation is allowed to
332 * notify it to the system console and then terminate the FRSH
333 * implementation and dependant applications
335 int fosa_cond_wait(fosa_cond_t *cond, fosa_mutex_t *mutex);
338 * fosa_cond_timedwait()
340 * Wait at a condition variable, with a timeout
342 * This function is equal to fosa_cond_wait(), except that the maximum
343 * wait time is limited to the absolute time referenced by abstime, as
344 * measured by the FOSA_CLOCK_ABSOLUTE clock.
346 * Returns 0 if successful; otherwise it returns an error code:
347 * FOSA_EINVAL: the value of cond or mutex or abstime is invalid, or different
348 * mutexes were used for concurrent wait operations on cond, or
349 * the mutex was not owned by the calling thread
350 * FOSA_ETIMEDOUT: the timeout expired
352 * Alternatively, except for FOSA_ETIMEDOUT, in case of error the
353 * implementation is allowed to notify it to the system console and
354 * then terminate the FRSH implementation and dependant applications
356 int fosa_cond_timedwait(fosa_cond_t *cond, fosa_mutex_t *mutex,
357 const struct timespec *abstime);
364 #endif /* !FOSA_MUTEX_AND_CONDVARS_H_ */