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"
73 * @defgroup mutexesandcondvars Mutexes and Condvars
76 * This module defines the types and functions to abstract mutexes and
77 * conditional variables for the FRSH implementation.
83 /*******************************************************
84 * Mutexes with priority ceiling
85 ******************************************************/
90 * Initialize a frsh mutex
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.
96 * Returns 0 if successful; otherwise it returns an error code:
98 * FOSA_EINVAL: the value of prioceiling is invalid \n
99 * FOSA_EAGAIN: the system lacked the necessary resources to create
101 * FOSA_ENOMEM: Insufficient memory exists to initialize the mutex \n
102 * FOSA_EBUSY: The system has detected an attempt to reinitialize the mutex
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
108 int fosa_mutex_init(fosa_mutex_t *mutex, int prioceiling);
111 * fosa_mutex_destroy()
113 * Destroy a frsh mutex
115 * The mutex pointed to by mutex is destroyed
117 * Returns 0 if successful; otherwise it returns an error code:
119 * FOSA_EINVAL: the value of mutex is invalid \n
120 * FOSA_EBUSY: The mutex is in use (is locked)\n
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
126 int fosa_mutex_destroy(fosa_mutex_t *mutex);
129 * fosa_mutex_set_prioceiling()
131 * Dynamically set the priority ceiling of a mutex
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
139 * Returns 0 if successful; otherwise it returns an error code:
141 * FOSA_EINVAL: the value of mutex or prioceiling is invalid \n
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
147 int fosa_mutex_set_prioceiling
148 (fosa_mutex_t *mutex, int new_ceiling, int *old_ceiling);
151 * fosa_mutex_get_prioceiling()
153 * Dynamically get the priority ceiling of a mutex
155 * This function copies into the variable pointed to by ceiling the
156 * current priority ceiling of the mutex referenced by mutex
158 * Returns 0 if successful; otherwise it returns an error code:
160 * FOSA_EINVAL: the value of mutex is invalid \n
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
166 int fosa_mutex_get_prioceiling(const fosa_mutex_t *mutex, int *ceiling);
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.
178 * Returns 0 if successful; otherwise it returns an error code: \n
180 * FOSA_EINVAL: the value of mutex is invalid, or the priority of the
181 * calling thread is higher than the priority ceiling of
184 * FOSA_EDEADLK: the current thread already owns this mutex \n
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
190 int fosa_mutex_lock(fosa_mutex_t *mutex);
193 * fosa_mutex_trylock()
195 * Try locking a mutex
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
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
205 * FOSA_EBUSY: the mutex was already locked \n
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
211 int fosa_mutex_trylock(fosa_mutex_t *mutex);
214 * fosa_mutex_unlock()
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.
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
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
231 int fosa_mutex_unlock(fosa_mutex_t *mutex);
234 /**********************
235 * Condition variables
236 *********************/
241 * Initiatize a condition variable
243 * The condition variable referenced by cond is initialized with
244 * the attributes required by the FOSA implementation.
246 * Returns 0 if successful; otherwise it returns an error code:
247 * FOSA_EAGAIN: the system lacked the necessary resources to create the
249 * FOSA_ENOMEM: Insufficient memory exists to initialize the condition variable
250 * FOSA_EBUSY: The system has detected an attempt to reinitialize the
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
257 int fosa_cond_init(fosa_cond_t *cond);
260 * fosa_cond_destroy()
262 * Destroy a condition variable
264 * The condition variable pointed to by cond is destroyed
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)
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
274 int fosa_cond_destroy(fosa_cond_t *cond);
279 * Signal a condition variable
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
285 * Returns 0 if successful; otherwise it returns an error code:
286 * FOSA_EINVAL: the value of cond is invalid
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
292 int fosa_cond_signal(fosa_cond_t *cond);
295 * fosa_cond_broadcast()
297 * Broadcast a condition variable
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.
303 * Returns 0 if successful; otherwise it returns an error code:
304 * FOSA_EINVAL: the value of cond is invalid
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
310 int fosa_cond_broadcast(fosa_cond_t *cond);
315 * Wait at a condition variable
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.
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
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
333 int fosa_cond_wait(fosa_cond_t *cond, fosa_mutex_t *mutex);
336 * fosa_cond_timedwait()
338 * Wait at a condition variable, with a timeout
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.
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
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
354 int fosa_cond_timedwait(fosa_cond_t *cond, fosa_mutex_t *mutex,
355 const struct timespec *abstime);
362 #endif /* !FOSA_MUTEX_AND_CONDVARS_H_ */