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
68 * This module defines the types and functions to abstract mutexes and
69 * conditional variables for the FRSH implementation.
75 /*******************************************************
76 * Mutexes with priority ceiling
77 ******************************************************/
82 * Initialize a frsh mutex
84 * The mutex pointed to by mutex is initialized as a mutex using
85 * the priority ceiling protocol. A priority ceiling of prioceiling
86 * is assigned to this mutex.
88 * Returns 0 if successful; otherwise it returns an error code:
89 * EINVAL: the value of prioceiling is invalid
90 * EAGAIN: the system lacked the necessary resources to create the mutex
91 * ENOMEM: Insufficient memory exists to initialize the mutex
92 * EBUSY: The system has detected an attempt to reinitialize the mutex
94 * Alternatively, in case of error the implementation is allowed to
95 * notify it to the system console and then terminate the FRSH
96 * implementation and dependant applications
98 int fosa_mutex_init(frsh_mutex_t *mutex, int prioceiling);
101 * fosa_mutex_destroy()
103 * Destroy a frsh mutex
105 * The mutex pointed to by mutex is destroyed
107 * Returns 0 if successful; otherwise it returns an error code:
108 * EINVAL: the value of mutex is invalid
109 * EBUSY: The mutex is in use (is locked)
111 * Alternatively, in case of error the implementation is allowed to
112 * notify it to the system console and then terminate the FRSH
113 * implementation and dependant applications
115 int fosa_mutex_destroy(frsh_mutex_t *mutex);
118 * fosa_mutex_set_prioceiling()
120 * Dynamically set the priority ceiling of a mutex
122 * This function locks the mutex (blocking the calling thread if
123 * necessary) and after it is locked it changes its priority ceiling
124 * to the value specified by new_ceiling, and then it unlocks the
125 * mutex. The previous value of the ceiling is returned in
128 * Returns 0 if successful; otherwise it returns an error code:
129 * EINVAL: the value of mutex or prioceiling is invalid
131 * Alternatively, in case of error the implementation is allowed to
132 * notify it to the system console and then terminate the FRSH
133 * implementation and dependant applications
135 int fosa_mutex_set_prioceiling
136 (frsh_mutex_t *mutex, int new_ceiling, int *old_ceiling);
139 * fosa_mutex_get_prioceiling()
141 * Dynamically get the priority ceiling of a mutex
143 * This function copies into the variable pointed to by ceiling the
144 * current priority ceiling of the mutex referenced by mutex
146 * Returns 0 if successful; otherwise it returns an error code:
147 * EINVAL: the value of mutex is invalid
149 * Alternatively, in case of error the implementation is allowed to
150 * notify it to the system console and then terminate the FRSH
151 * implementation and dependant applications
153 int fosa_mutex_get_prioceiling(const frsh_mutex_t *mutex, int *ceiling);
160 * This function locks the mutex specified by mutex. If it is already
161 * locked, the calling thread blocks until the mutex becomes
162 * available. The operation returns with the mutex in the locked
163 * state, with the calling thread as its owner.
165 * Returns 0 if successful; otherwise it returns an error code:
166 * EINVAL: the value of mutex is invalid, or the priority of the
167 * calling thread is higher than the priority ceiling of the mutex
168 * EDEADLK: the current thread already owns this mutex
170 * Alternatively, in case of error the implementation is allowed to
171 * notify it to the system console and then terminate the FRSH
172 * implementation and dependant applications
174 int fosa_mutex_lock(frsh_mutex_t *mutex);
177 * fosa_mutex_trylock()
179 * Try locking a mutex
181 * This function is identical to fosa_mutex_lock() except that if the
182 * mutex is already locked the call returns immediately with an error
185 * Returns 0 if successful; otherwise it returns an error code:
186 * EINVAL: the value of mutex is invalid, or the priority of the
187 * calling thread is higher than the priority ceiling of the mutex
188 * EBUSY: the mutex was already locked
190 * Alternatively, except for EBUSY, in case of error the
191 * implementation is allowed to notify it to the system console and
192 * then terminate the FRSH implementation and dependant applications
194 int fosa_mutex_trylock(frsh_mutex_t *mutex);
197 * fosa_mutex_unlock()
201 * This function must be called by the owner of the mutex referenced
202 * by mutex, to unlock it. If there are threads blocked on the mutex
203 * the mutex becomes available and the highest priority thread is
204 * awakened to acquire the mutex.
206 * Returns 0 if successful; otherwise it returns an error code:
207 * EINVAL: the value of mutex is invalid
208 * EPERM: the calling thread is not the owner of the mutex
210 * Alternatively, except for EBUSY, in case of error the
211 * implementation is allowed to notify it to the system console and
212 * then terminate the FRSH implementation and dependant applications
214 int fosa_mutex_unlock(frsh_mutex_t *mutex);
217 /**********************
218 * Condition variables
219 *********************/
224 * Initiatize a condition variable
226 * The condition variable referenced by cond is initialized with
227 * the attributes required by the FOSA implementation.
229 * Returns 0 if successful; otherwise it returns an error code:
230 * EAGAIN: the system lacked the necessary resources to create the
232 * ENOMEM: Insufficient memory exists to initialize the condition variable
233 * EBUSY: The system has detected an attempt to reinitialize the
236 * Alternatively, in case of error the implementation is allowed to
237 * notify it to the system console and then terminate the FRSH
238 * implementation and dependant applications
240 int fosa_cond_init(fosa_cond_t *cond);
243 * fosa_cond_destroy()
245 * Destroy a condition variable
247 * The condition variable pointed to by cond is destroyed
249 * Returns 0 if successful; otherwise it returns an error code:
250 * EINVAL: the value of cond is invalid
251 * EBUSY: The condition variable is in use (a thread is waiting on it)
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_destroy(fosa_cond_t *cond);
262 * Signal a condition variable
264 * This call unblocks at least one of the threads that are waiting on
265 * the condition variable referenced by cond. If there are no threads
266 * waiting, the function has no effect
268 * Returns 0 if successful; otherwise it returns an error code:
269 * EINVAL: the value of cond is invalid
271 * Alternatively, in case of error the implementation is allowed to
272 * notify it to the system console and then terminate the FRSH
273 * implementation and dependant applications
275 int fosa_cond_signal(fosa_cond_t *cond);
278 * fosa_cond_broadcast()
280 * Broadcast a condition variable
282 * This call unblocks all of the threads that are waiting on the
283 * condition variable referenced by cond. If there are no threads
284 * waiting, the function has no effect.
286 * Returns 0 if successful; otherwise it returns an error code:
287 * EINVAL: the value of cond is invalid
289 * Alternatively, in case of error the implementation is allowed to
290 * notify it to the system console and then terminate the FRSH
291 * implementation and dependant applications
293 int fosa_cond_broadcast(fosa_cond_t *cond);
298 * Wait at a condition variable
300 * This call is used to block on the condition variable referenced by
301 * cond. It shall be called with the mutex referenced by mutex
302 * locked. The function releases the mutex and blocks the calling
303 * thread until the condition is signalled by some other thread and
304 * the calling thread is awakened. Then it locks the mutex and
305 * returns with the mutex locked by the calling thread.
307 * Returns 0 if successful; otherwise it returns an error code:
308 * EINVAL: the value of cond or mutex is invalid, or different
309 * mutexes were used for concurrent wait operations on cond, or
310 * the mutex was not owned by the calling thread
312 * Alternatively, in case of error the implementation is allowed to
313 * notify it to the system console and then terminate the FRSH
314 * implementation and dependant applications
316 int fosa_cond_wait(fosa_cond_t *cond, frsh_mutex_t *mutex);
319 * fosa_cond_timedwait()
321 * Wait at a condition variable, with a timeout
323 * This function is equal to fosa_cond_wait(), except that the maximum
324 * wait time is limited to the absolute time referenced by abstime, as
325 * measured by the FOSA_CLOCK_REALTIME clock.
327 * Returns 0 if successful; otherwise it returns an error code:
328 * EINVAL: the value of cond or mutex or abstime is invalid, or different
329 * mutexes were used for concurrent wait operations on cond, or
330 * the mutex was not owned by the calling thread
331 * ETIMEDOUT: the timeout expired
333 * Alternatively, except for ETIMEDOUT, in case of error the
334 * implementation is allowed to notify it to the system console and
335 * then terminate the FRSH implementation and dependant applications
337 int fosa_cond_timedwait(fosa_cond_t *cond, frsh_mutex_t *mutex,
338 const struct timespec abstime);
345 #endif /* !FOSA_MUTEX_AND_CONDVARS_H_ */