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 * @defgroup mutexesandcondvars Mutexes and Condvars
67 * This module defines the types and functions to abstract mutexes and
68 * conditional variables for the FRSH implementation.
74 /*******************************************************
75 * Mutexes with priority ceiling
76 ******************************************************/
81 * Initialize a frsh mutex
83 * The mutex pointed to by mutex is initialized as a mutex using
84 * the priority ceiling protocol. A priority ceiling of prioceiling
85 * is assigned to this mutex.
87 * Returns 0 if successful; otherwise it returns an error code:
88 * EINVAL: the value of prioceiling is invalid
89 * EAGAIN: the system lacked the necessary resources to create the mutex
90 * ENOMEM: Insufficient memory exists to initialize the mutex
91 * EBUSY: The system has detected an attempt to reinitialize the mutex
93 * Alternatively, in case of error the implementation is allowed to
94 * notify it to the system console and then terminate the FRSH
95 * implementation and dependant applications
97 int fosa_mutex_init(frsh_mutex_t *mutex, int prioceiling);
100 * fosa_mutex_destroy()
102 * Destroy a frsh mutex
104 * The mutex pointed to by mutex is destroyed
106 * Returns 0 if successful; otherwise it returns an error code:
107 * EINVAL: the value of mutex is invalid
108 * EBUSY: The mutex is in use (is locked)
110 * Alternatively, in case of error the implementation is allowed to
111 * notify it to the system console and then terminate the FRSH
112 * implementation and dependant applications
114 int fosa_mutex_destroy(frsh_mutex_t *mutex);
117 * fosa_mutex_set_prioceiling()
119 * Dynamically set the priority ceiling of a mutex
121 * This function locks the mutex (blocking the calling thread if
122 * necessary) and after it is locked it changes its priority ceiling
123 * to the value specified by new_ceiling, and then it unlocks the
124 * mutex. The previous value of the ceiling is returned in
127 * Returns 0 if successful; otherwise it returns an error code:
128 * EINVAL: the value of mutex or prioceiling is invalid
130 * Alternatively, in case of error the implementation is allowed to
131 * notify it to the system console and then terminate the FRSH
132 * implementation and dependant applications
134 int fosa_mutex_set_prioceiling
135 (frsh_mutex_t *mutex, int new_ceiling, int *old_ceiling);
138 * fosa_mutex_get_prioceiling()
140 * Dynamically get the priority ceiling of a mutex
142 * This function copies into the variable pointed to by ceiling the
143 * current priority ceiling of the mutex referenced by mutex
145 * Returns 0 if successful; otherwise it returns an error code:
146 * EINVAL: the value of mutex is invalid
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_get_prioceiling(const frsh_mutex_t *mutex, int *ceiling);
159 * This function locks the mutex specified by mutex. If it is already
160 * locked, the calling thread blocks until the mutex becomes
161 * available. The operation returns with the mutex in the locked
162 * state, with the calling thread as its owner.
164 * Returns 0 if successful; otherwise it returns an error code:
165 * EINVAL: the value of mutex is invalid, or the priority of the
166 * calling thread is higher than the priority ceiling of the mutex
167 * EDEADLK: the current thread already owns this mutex
169 * Alternatively, in case of error the implementation is allowed to
170 * notify it to the system console and then terminate the FRSH
171 * implementation and dependant applications
173 int fosa_mutex_lock(frsh_mutex_t *mutex);
176 * fosa_mutex_trylock()
178 * Try locking a mutex
180 * This function is identical to fosa_mutex_lock() except that if the
181 * mutex is already locked the call returns immediately with an error
184 * Returns 0 if successful; otherwise it returns an error code:
185 * EINVAL: the value of mutex is invalid, or the priority of the
186 * calling thread is higher than the priority ceiling of the mutex
187 * EBUSY: the mutex was already locked
189 * Alternatively, except for EBUSY, in case of error the
190 * implementation is allowed to notify it to the system console and
191 * then terminate the FRSH implementation and dependant applications
193 int fosa_mutex_trylock(frsh_mutex_t *mutex);
196 * fosa_mutex_unlock()
200 * This function must be called by the owner of the mutex referenced
201 * by mutex, to unlock it. If there are threads blocked on the mutex
202 * the mutex becomes available and the highest priority thread is
203 * awakened to acquire the mutex.
205 * Returns 0 if successful; otherwise it returns an error code:
206 * EINVAL: the value of mutex is invalid
207 * EPERM: the calling thread is not the owner of the mutex
209 * Alternatively, except for 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_unlock(frsh_mutex_t *mutex);
216 /**********************
217 * Condition variables
218 *********************/
223 * Initiatize a condition variable
225 * The condition variable referenced by cond is initialized with
226 * the attributes required by the FOSA implementation.
228 * Returns 0 if successful; otherwise it returns an error code:
229 * EAGAIN: the system lacked the necessary resources to create the
231 * ENOMEM: Insufficient memory exists to initialize the condition variable
232 * EBUSY: The system has detected an attempt to reinitialize the
235 * Alternatively, in case of error the implementation is allowed to
236 * notify it to the system console and then terminate the FRSH
237 * implementation and dependant applications
239 int fosa_cond_init(fosa_cond_t *cond);
242 * fosa_cond_destroy()
244 * Destroy a condition variable
246 * The condition variable pointed to by cond is destroyed
248 * Returns 0 if successful; otherwise it returns an error code:
249 * EINVAL: the value of cond is invalid
250 * EBUSY: The condition variable is in use (a thread is waiting on it)
252 * Alternatively, in case of error the implementation is allowed to
253 * notify it to the system console and then terminate the FRSH
254 * implementation and dependant applications
256 int fosa_cond_destroy(fosa_cond_t *cond);
261 * Signal a condition variable
263 * This call unblocks at least one of the threads that are waiting on
264 * the condition variable referenced by cond. If there are no threads
265 * waiting, the function has no effect
267 * Returns 0 if successful; otherwise it returns an error code:
268 * EINVAL: the value of cond is invalid
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_signal(fosa_cond_t *cond);
277 * fosa_cond_broadcast()
279 * Broadcast a condition variable
281 * This call unblocks all of the threads that are waiting on the
282 * 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 * 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_broadcast(fosa_cond_t *cond);
297 * Wait at a condition variable
299 * This call is used to block on the condition variable referenced by
300 * cond. It shall be called with the mutex referenced by mutex
301 * locked. The function releases the mutex and blocks the calling
302 * thread until the condition is signalled by some other thread and
303 * the calling thread is awakened. Then it locks the mutex and
304 * returns with the mutex locked by the calling thread.
306 * Returns 0 if successful; otherwise it returns an error code:
307 * EINVAL: the value of cond or mutex is invalid, or different
308 * mutexes were used for concurrent wait operations on cond, or
309 * the mutex was not owned by the calling thread
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_wait(fosa_cond_t *cond, frsh_mutex_t *mutex);
318 * fosa_cond_timedwait()
320 * Wait at a condition variable, with a timeout
322 * This function is equal to fosa_cond_wait(), except that the maximum
323 * wait time is limited to the absolute time referenced by abstime, as
324 * measured by the FOSA_CLOCK_REALTIME clock.
326 * Returns 0 if successful; otherwise it returns an error code:
327 * EINVAL: the value of cond or mutex or abstime 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
330 * ETIMEDOUT: the timeout expired
332 * Alternatively, except for ETIMEDOUT, in case of error the
333 * implementation is allowed to notify it to the system console and
334 * then terminate the FRSH implementation and dependant applications
336 int fosa_cond_timedwait(fosa_cond_t *cond, frsh_mutex_t *mutex,
337 const struct timespec abstime);
344 #endif /* !FOSA_MUTEX_AND_CONDVARS_H_ */