1 // -----------------------------------------------------------------------
2 // Copyright (C) 2006 - 2007 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. 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 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
23 // This file is part of the FRSH implementation
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)
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.
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
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
46 // -----------------------------------------------------------------------
47 //fosa_mutexes_and_condvars.h
48 //==============================================
49 // ******** ****** ******** **********
50 // **///// /** ** **////// /** /**
51 // ** /** ** /** /** /**
52 // ******* /** ** /********* /**********
53 // **//// /** ** ////////** /**//////**
54 // ** /** ** /** /** /**
55 // ** /** ** ******** /** /**
56 // // /******/ //////// // //
58 // FOSA(Frescor Operating System Adaptation layer)
59 //================================================
61 #ifndef FOSA_MUTEX_AND_CONDVARS_H_
62 #define FOSA_MUTEX_AND_CONDVARS_H_
65 #include "fosa_types.h"
68 * @defgroup mutexesandcondvars Mutexes and Condvars
71 * This module defines the types and functions to abstract mutexes and
72 * conditional variables for the FRSH implementation.
78 /*******************************************************
79 * Mutexes with priority ceiling
80 ******************************************************/
85 * Initialize a frsh mutex
87 * The mutex pointed to by mutex is initialized as a mutex using
88 * the priority ceiling protocol. A priority ceiling of prioceiling
89 * is assigned to this mutex.
91 * Returns 0 if successful; otherwise it returns an error code:
93 * FOSA_EINVAL: the value of prioceiling is invalid \n
94 * FOSA_EAGAIN: the system lacked the necessary resources to create
96 * FOSA_ENOMEM: Insufficient memory exists to initialize the mutex \n
97 * FOSA_EBUSY: The system has detected an attempt to reinitialize the mutex
99 * Alternatively, in case of error the implementation is allowed to
100 * notify it to the system console and then terminate the FRSH
101 * implementation and dependant applications
103 int fosa_mutex_init(frsh_mutex_t *mutex, int prioceiling);
106 * fosa_mutex_destroy()
108 * Destroy a frsh mutex
110 * The mutex pointed to by mutex is destroyed
112 * Returns 0 if successful; otherwise it returns an error code:
114 * FOSA_EINVAL: the value of mutex is invalid \n
115 * FOSA_EBUSY: The mutex is in use (is locked)\n
117 * Alternatively, in case of error the implementation is allowed to
118 * notify it to the system console and then terminate the FRSH
119 * implementation and dependant applications
121 int fosa_mutex_destroy(frsh_mutex_t *mutex);
124 * fosa_mutex_set_prioceiling()
126 * Dynamically set the priority ceiling of a mutex
128 * This function locks the mutex (blocking the calling thread if
129 * necessary) and after it is locked it changes its priority ceiling
130 * to the value specified by new_ceiling, and then it unlocks the
131 * mutex. The previous value of the ceiling is returned in
134 * Returns 0 if successful; otherwise it returns an error code:
136 * FOSA_EINVAL: the value of mutex or prioceiling is invalid \n
138 * Alternatively, in case of error the implementation is allowed to
139 * notify it to the system console and then terminate the FRSH
140 * implementation and dependant applications
142 int fosa_mutex_set_prioceiling
143 (frsh_mutex_t *mutex, int new_ceiling, int *old_ceiling);
146 * fosa_mutex_get_prioceiling()
148 * Dynamically get the priority ceiling of a mutex
150 * This function copies into the variable pointed to by ceiling the
151 * current priority ceiling of the mutex referenced by mutex
153 * Returns 0 if successful; otherwise it returns an error code:
155 * FOSA_EINVAL: the value of mutex is invalid \n
157 * Alternatively, in case of error the implementation is allowed to
158 * notify it to the system console and then terminate the FRSH
159 * implementation and dependant applications
161 int fosa_mutex_get_prioceiling(const frsh_mutex_t *mutex, int *ceiling);
168 * This function locks the mutex specified by mutex. If it is already
169 * locked, the calling thread blocks until the mutex becomes
170 * available. The operation returns with the mutex in the locked
171 * state, with the calling thread as its owner.
173 * Returns 0 if successful; otherwise it returns an error code: \n
175 * FOSA_EINVAL: the value of mutex is invalid, or the priority of the
176 * calling thread is higher than the priority ceiling of
179 * FOSA_EDEADLK: the current thread already owns this mutex \n
181 * Alternatively, in case of error the implementation is allowed to
182 * notify it to the system console and then terminate the FRSH
183 * implementation and dependant applications
185 int fosa_mutex_lock(frsh_mutex_t *mutex);
188 * fosa_mutex_trylock()
190 * Try locking a mutex
192 * This function is identical to fosa_mutex_lock() except that if the
193 * mutex is already locked the call returns immediately with an error
196 * Returns 0 if successful; otherwise it returns an error code:
197 * FOSA_EINVAL: the value of mutex is invalid, or the priority of the
198 * calling thread is higher than the priority ceiling of the
200 * FOSA_EBUSY: the mutex was already locked \n
202 * Alternatively, except for FOSA_EBUSY, in case of error the
203 * implementation is allowed to notify it to the system console and
204 * then terminate the FRSH implementation and dependant applications
206 int fosa_mutex_trylock(frsh_mutex_t *mutex);
209 * fosa_mutex_unlock()
213 * This function must be called by the owner of the mutex referenced
214 * by mutex, to unlock it. If there are threads blocked on the mutex
215 * the mutex becomes available and the highest priority thread is
216 * awakened to acquire the mutex.
218 * Returns 0 if successful; otherwise it returns an error code:
219 * FOSA_EINVAL: the value of mutex is invalid
220 * FOSA_EPERM: the calling thread is not the owner of the mutex
222 * Alternatively, except for FOSA_EBUSY, in case of error the
223 * implementation is allowed to notify it to the system console and
224 * then terminate the FRSH implementation and dependant applications
226 int fosa_mutex_unlock(frsh_mutex_t *mutex);
229 /**********************
230 * Condition variables
231 *********************/
236 * Initiatize a condition variable
238 * The condition variable referenced by cond is initialized with
239 * the attributes required by the FOSA implementation.
241 * Returns 0 if successful; otherwise it returns an error code:
242 * FOSA_EAGAIN: the system lacked the necessary resources to create the
244 * FOSA_ENOMEM: Insufficient memory exists to initialize the condition variable
245 * FOSA_EBUSY: The system has detected an attempt to reinitialize the
248 * Alternatively, in case of error the implementation is allowed to
249 * notify it to the system console and then terminate the FRSH
250 * implementation and dependant applications
252 int fosa_cond_init(fosa_cond_t *cond);
255 * fosa_cond_destroy()
257 * Destroy a condition variable
259 * The condition variable pointed to by cond is destroyed
261 * Returns 0 if successful; otherwise it returns an error code:
262 * FOSA_EINVAL: the value of cond is invalid
263 * FOSA_EBUSY: The condition variable is in use (a thread is waiting on it)
265 * Alternatively, in case of error the implementation is allowed to
266 * notify it to the system console and then terminate the FRSH
267 * implementation and dependant applications
269 int fosa_cond_destroy(fosa_cond_t *cond);
274 * Signal a condition variable
276 * This call unblocks at least one of the threads that are waiting on
277 * the condition variable referenced by cond. If there are no threads
278 * waiting, the function has no effect
280 * Returns 0 if successful; otherwise it returns an error code:
281 * FOSA_EINVAL: the value of cond is invalid
283 * Alternatively, in case of error the implementation is allowed to
284 * notify it to the system console and then terminate the FRSH
285 * implementation and dependant applications
287 int fosa_cond_signal(fosa_cond_t *cond);
290 * fosa_cond_broadcast()
292 * Broadcast a condition variable
294 * This call unblocks all of the threads that are waiting on the
295 * condition variable referenced by cond. If there are no threads
296 * waiting, the function has no effect.
298 * Returns 0 if successful; otherwise it returns an error code:
299 * FOSA_EINVAL: the value of cond is invalid
301 * Alternatively, in case of error the implementation is allowed to
302 * notify it to the system console and then terminate the FRSH
303 * implementation and dependant applications
305 int fosa_cond_broadcast(fosa_cond_t *cond);
310 * Wait at a condition variable
312 * This call is used to block on the condition variable referenced by
313 * cond. It shall be called with the mutex referenced by mutex
314 * locked. The function releases the mutex and blocks the calling
315 * thread until the condition is signalled by some other thread and
316 * the calling thread is awakened. Then it locks the mutex and
317 * returns with the mutex locked by the calling thread.
319 * Returns 0 if successful; otherwise it returns an error code:
320 * FOSA_EINVAL: the value of cond or mutex is invalid, or different
321 * mutexes were used for concurrent wait operations on cond, or
322 * the mutex was not owned by the calling thread
324 * Alternatively, in case of error the implementation is allowed to
325 * notify it to the system console and then terminate the FRSH
326 * implementation and dependant applications
328 int fosa_cond_wait(fosa_cond_t *cond, frsh_mutex_t *mutex);
331 * fosa_cond_timedwait()
333 * Wait at a condition variable, with a timeout
335 * This function is equal to fosa_cond_wait(), except that the maximum
336 * wait time is limited to the absolute time referenced by abstime, as
337 * measured by the FOSA_CLOCK_REALTIME clock.
339 * Returns 0 if successful; otherwise it returns an error code:
340 * FOSA_EINVAL: the value of cond or mutex or abstime is invalid, or different
341 * mutexes were used for concurrent wait operations on cond, or
342 * the mutex was not owned by the calling thread
343 * FOSA_ETIMEDOUT: the timeout expired
345 * Alternatively, except for FOSA_ETIMEDOUT, in case of error the
346 * implementation is allowed to notify it to the system console and
347 * then terminate the FRSH implementation and dependant applications
349 int fosa_cond_timedwait(fosa_cond_t *cond, frsh_mutex_t *mutex,
350 const struct timespec *abstime);
357 #endif /* !FOSA_MUTEX_AND_CONDVARS_H_ */