1 // -----------------------------------------------------------------------
2 // Copyright (C) 2006 - 2009 FRESCOR consortium partners:
4 // Universidad de Cantabria, SPAIN
5 // University of York, UK
6 // Scuola Superiore Sant'Anna, ITALY
7 // Kaiserslautern University, GERMANY
8 // Univ. Politécnica 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 for a link to partners' websites
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
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 // FSF API web pages: http://marte.unican.es/fsf/docs
32 // http://shark.sssup.it/contrib/first/docs/
34 // This file is part of FOSA (Frsh Operating System Adaption)
36 // FOSA is free software; you can redistribute it and/or modify it
37 // under terms of the GNU General Public License as published by the
38 // Free Software Foundation; either version 2, or (at your option) any
39 // later version. FOSA is distributed in the hope that it will be
40 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
41 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
42 // General Public License for more details. You should have received a
43 // copy of the GNU General Public License along with FOSA; see file
44 // COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
45 // Cambridge, MA 02139, USA.
47 // As a special exception, including FOSA header files in a file,
48 // instantiating FOSA generics or templates, or linking other files
49 // with FOSA objects to produce an executable application, does not
50 // by itself cause the resulting executable application to be covered
51 // by the GNU General Public License. This exception does not
52 // however invalidate any other reasons why the executable file might be
53 // covered by the GNU Public License.
54 // -----------------------------------------------------------------------
55 //fosa_mutexes_and_condvars.h
56 //==============================================
57 // ******** ****** ******** **********
58 // **///// /** ** **////// /** /**
59 // ** /** ** /** /** /**
60 // ******* /** ** /********* /**********
61 // **//// /** ** ////////** /**//////**
62 // ** /** ** /** /** /**
63 // ** /** ** ******** /** /**
64 // // /******/ //////// // //
66 // FOSA(Frescor Operating System Adaptation layer)
67 //================================================
69 #ifndef FOSA_MUTEX_AND_CONDVARS_H_
70 #define FOSA_MUTEX_AND_CONDVARS_H_
73 #include "fosa_types.h"
78 * @defgroup mutexesandcondvars Mutexes and Condvars
81 * This module defines the types and functions to abstract mutexes and
82 * conditional variables for the FRSH implementation.
88 /*******************************************************
89 * Mutexes with priority ceiling
90 ******************************************************/
95 * Initialize a frsh mutex
97 * The mutex pointed to by mutex is initialized as a mutex using
98 * the priority ceiling protocol. A priority ceiling of prioceiling
99 * is assigned to this mutex.
101 * Returns 0 if successful; otherwise it returns an error code:
103 * FOSA_EINVAL: the value of prioceiling is invalid \n
104 * FOSA_EAGAIN: the system lacked the necessary resources to create
106 * FOSA_ENOMEM: Insufficient memory exists to initialize the mutex \n
107 * FOSA_EBUSY: The system has detected an attempt to reinitialize the mutex
109 * Alternatively, in case of error the implementation is allowed to
110 * notify it to the system console and then terminate the FRSH
111 * implementation and dependant applications
113 int fosa_mutex_init(fosa_mutex_t *mutex, int prioceiling);
116 * fosa_mutex_destroy()
118 * Destroy a frsh mutex
120 * The mutex pointed to by mutex is destroyed
122 * Returns 0 if successful; otherwise it returns an error code:
124 * FOSA_EINVAL: the value of mutex is invalid \n
125 * FOSA_EBUSY: The mutex is in use (is locked)\n
127 * Alternatively, in case of error the implementation is allowed to
128 * notify it to the system console and then terminate the FRSH
129 * implementation and dependant applications
131 int fosa_mutex_destroy(fosa_mutex_t *mutex);
134 * fosa_mutex_set_prioceiling()
136 * Dynamically set the priority ceiling of a mutex
138 * This function locks the mutex (blocking the calling thread if
139 * necessary) and after it is locked it changes its priority ceiling
140 * to the value specified by new_ceiling, and then it unlocks the
141 * mutex. The previous value of the ceiling is returned in
144 * Returns 0 if successful; otherwise it returns an error code:
146 * FOSA_EINVAL: the value of mutex or prioceiling is invalid \n
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
152 int fosa_mutex_set_prioceiling
153 (fosa_mutex_t *mutex, int new_ceiling, int *old_ceiling);
156 * fosa_mutex_get_prioceiling()
158 * Dynamically get the priority ceiling of a mutex
160 * This function copies into the variable pointed to by ceiling the
161 * current priority ceiling of the mutex referenced by mutex
163 * Returns 0 if successful; otherwise it returns an error code:
165 * FOSA_EINVAL: the value of mutex is invalid \n
167 * Alternatively, in case of error the implementation is allowed to
168 * notify it to the system console and then terminate the FRSH
169 * implementation and dependant applications
171 int fosa_mutex_get_prioceiling(const fosa_mutex_t *mutex, int *ceiling);
178 * This function locks the mutex specified by mutex. If it is already
179 * locked, the calling thread blocks until the mutex becomes
180 * available. The operation returns with the mutex in the locked
181 * state, with the calling thread as its owner.
183 * Returns 0 if successful; otherwise it returns an error code: \n
185 * FOSA_EINVAL: the value of mutex is invalid, or the priority of the
186 * calling thread is higher than the priority ceiling of
189 * FOSA_EDEADLK: the current thread already owns this mutex \n
191 * Alternatively, in case of error the implementation is allowed to
192 * notify it to the system console and then terminate the FRSH
193 * implementation and dependant applications
195 int fosa_mutex_lock(fosa_mutex_t *mutex);
198 * fosa_mutex_trylock()
200 * Try locking a mutex
202 * This function is identical to fosa_mutex_lock() except that if the
203 * mutex is already locked the call returns immediately with an error
206 * Returns 0 if successful; otherwise it returns an error code:
207 * FOSA_EINVAL: the value of mutex is invalid, or the priority of the
208 * calling thread is higher than the priority ceiling of the
210 * FOSA_EBUSY: the mutex was already locked \n
212 * Alternatively, except for FOSA_EBUSY, in case of error the
213 * implementation is allowed to notify it to the system console and
214 * then terminate the FRSH implementation and dependant applications
216 int fosa_mutex_trylock(fosa_mutex_t *mutex);
219 * fosa_mutex_unlock()
223 * This function must be called by the owner of the mutex referenced
224 * by mutex, to unlock it. If there are threads blocked on the mutex
225 * the mutex becomes available and the highest priority thread is
226 * awakened to acquire the mutex.
228 * Returns 0 if successful; otherwise it returns an error code:
229 * FOSA_EINVAL: the value of mutex is invalid
230 * FOSA_EPERM: the calling thread is not the owner of the mutex
232 * Alternatively, except for FOSA_EBUSY, in case of error the
233 * implementation is allowed to notify it to the system console and
234 * then terminate the FRSH implementation and dependant applications
236 int fosa_mutex_unlock(fosa_mutex_t *mutex);
239 /**********************
240 * Condition variables
241 *********************/
246 * Initiatize a condition variable
248 * The condition variable referenced by cond is initialized with
249 * the attributes required by the FOSA implementation.
251 * Returns 0 if successful; otherwise it returns an error code:
252 * FOSA_EAGAIN: the system lacked the necessary resources to create the
254 * FOSA_ENOMEM: Insufficient memory exists to initialize the condition variable
255 * FOSA_EBUSY: The system has detected an attempt to reinitialize the
258 * Alternatively, in case of error the implementation is allowed to
259 * notify it to the system console and then terminate the FRSH
260 * implementation and dependant applications
262 int fosa_cond_init(fosa_cond_t *cond);
265 * fosa_cond_destroy()
267 * Destroy a condition variable
269 * The condition variable pointed to by cond is destroyed
271 * Returns 0 if successful; otherwise it returns an error code:
272 * FOSA_EINVAL: the value of cond is invalid
273 * FOSA_EBUSY: The condition variable is in use (a thread is waiting on it)
275 * Alternatively, in case of error the implementation is allowed to
276 * notify it to the system console and then terminate the FRSH
277 * implementation and dependant applications
279 int fosa_cond_destroy(fosa_cond_t *cond);
284 * Signal a condition variable
286 * This call unblocks at least one of the threads that are waiting on
287 * the condition variable referenced by cond. If there are no threads
288 * waiting, the function has no effect
290 * Returns 0 if successful; otherwise it returns an error code:
291 * FOSA_EINVAL: the value of cond is invalid
293 * Alternatively, in case of error the implementation is allowed to
294 * notify it to the system console and then terminate the FRSH
295 * implementation and dependant applications
297 int fosa_cond_signal(fosa_cond_t *cond);
300 * fosa_cond_broadcast()
302 * Broadcast a condition variable
304 * This call unblocks all of the threads that are waiting on the
305 * condition variable referenced by cond. If there are no threads
306 * waiting, the function has no effect.
308 * Returns 0 if successful; otherwise it returns an error code:
309 * FOSA_EINVAL: the value of cond is invalid
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
315 int fosa_cond_broadcast(fosa_cond_t *cond);
320 * Wait at a condition variable
322 * This call is used to block on the condition variable referenced by
323 * cond. It shall be called with the mutex referenced by mutex
324 * locked. The function releases the mutex and blocks the calling
325 * thread until the condition is signalled by some other thread and
326 * the calling thread is awakened. Then it locks the mutex and
327 * returns with the mutex locked by the calling thread.
329 * Returns 0 if successful; otherwise it returns an error code:
330 * FOSA_EINVAL: the value of cond or mutex is invalid, or different
331 * mutexes were used for concurrent wait operations on cond, or
332 * the mutex was not owned by the calling thread
334 * Alternatively, in case of error the implementation is allowed to
335 * notify it to the system console and then terminate the FRSH
336 * implementation and dependant applications
338 int fosa_cond_wait(fosa_cond_t *cond, fosa_mutex_t *mutex);
341 * fosa_cond_timedwait()
343 * Wait at a condition variable, with a timeout
345 * This function is equal to fosa_cond_wait(), except that the maximum
346 * wait time is limited to the absolute time referenced by abstime, as
347 * measured by the FOSA_CLOCK_ABSOLUTE clock.
349 * Returns 0 if successful; otherwise it returns an error code:
350 * FOSA_EINVAL: the value of cond or mutex or abstime is invalid, or different
351 * mutexes were used for concurrent wait operations on cond, or
352 * the mutex was not owned by the calling thread
353 * FOSA_ETIMEDOUT: the timeout expired
355 * Alternatively, except for FOSA_ETIMEDOUT, in case of error the
356 * implementation is allowed to notify it to the system console and
357 * then terminate the FRSH implementation and dependant applications
359 int fosa_cond_timedwait(fosa_cond_t *cond, fosa_mutex_t *mutex,
360 const fosa_abs_time_t *abstime);
367 #endif /* !FOSA_MUTEX_AND_CONDVARS_H_ */