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);
99 extern inline int fosa_mutex_init(frsh_mutex_t *mutex, int prioceiling){
100 pthread_mutexattr_t *attr;
101 *attr->prioceiling=prioceiling;
102 return pthread_mutex_init(mutex, attr);
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:
113 * EINVAL: the value of mutex is invalid
114 * EBUSY: The mutex is in use (is locked)
116 * Alternatively, in case of error the implementation is allowed to
117 * notify it to the system console and then terminate the FRSH
118 * implementation and dependant applications
120 //int fosa_mutex_destroy(frsh_mutex_t *mutex);
121 extern inline int fosa_mutex_destroy(frsh_mutex_t *mutex){
122 return pthread_mutex_destroy(mutex);
125 * fosa_mutex_set_prioceiling()
127 * Dynamically set the priority ceiling of a mutex
129 * This function locks the mutex (blocking the calling thread if
130 * necessary) and after it is locked it changes its priority ceiling
131 * to the value specified by new_ceiling, and then it unlocks the
132 * mutex. The previous value of the ceiling is returned in
135 * Returns 0 if successful; otherwise it returns an error code:
136 * EINVAL: the value of mutex or prioceiling is invalid
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);
144 extern inline int fosa_mutex_set_prioceiling (frsh_mutex_t *mutex,
147 return pthread_mutex_setprioceiling(mutex, new_ceiling, old_ceiling);
150 * fosa_mutex_get_prioceiling()
152 * Dynamically get the priority ceiling of a mutex
154 * This function copies into the variable pointed to by ceiling the
155 * current priority ceiling of the mutex referenced by mutex
157 * Returns 0 if successful; otherwise it returns an error code:
158 * EINVAL: the value of mutex is invalid
160 * Alternatively, in case of error the implementation is allowed to
161 * notify it to the system console and then terminate the FRSH
162 * implementation and dependant applications
164 //int fosa_mutex_get_prioceiling(const frsh_mutex_t *mutex, int *ceiling);
165 extern inline int fosa_mutex_get_prioceiling(const frsh_mutex_t *mutex, int *ceiling){
166 return pthread_mutex_getprioceiling(mutex, 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:
179 * EINVAL: the value of mutex is invalid, or the priority of the
180 * calling thread is higher than the priority ceiling of the mutex
181 * EDEADLK: the current thread already owns this mutex
183 * Alternatively, in case of error the implementation is allowed to
184 * notify it to the system console and then terminate the FRSH
185 * implementation and dependant applications
187 //int fosa_mutex_lock(frsh_mutex_t *mutex);
188 extern inline int fosa_mutex_lock(frsh_mutex_t *mutex){
189 return pthread_mutex_lock(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 * EINVAL: the value of mutex is invalid, or the priority of the
203 * calling thread is higher than the priority ceiling of the mutex
204 * EBUSY: the mutex was already locked
206 * Alternatively, except for EBUSY, in case of error the
207 * implementation is allowed to notify it to the system console and
208 * then terminate the FRSH implementation and dependant applications
210 //int fosa_mutex_trylock(frsh_mutex_t *mutex);
211 extern inline int fosa_mutex_trylock(frsh_mutex_t *mutex){
212 return pthread_mutex_trylock(mutex);
215 * fosa_mutex_unlock()
219 * This function must be called by the owner of the mutex referenced
220 * by mutex, to unlock it. If there are threads blocked on the mutex
221 * the mutex becomes available and the highest priority thread is
222 * awakened to acquire the mutex.
224 * Returns 0 if successful; otherwise it returns an error code:
225 * EINVAL: the value of mutex is invalid
226 * EPERM: the calling thread is not the owner of the mutex
228 * Alternatively, except for EBUSY, in case of error the
229 * implementation is allowed to notify it to the system console and
230 * then terminate the FRSH implementation and dependant applications
232 //int fosa_mutex_unlock(frsh_mutex_t *mutex);
233 extern inline int fosa_mutex_unlock(frsh_mutex_t *mutex){
234 return pthread_mutex_unlock(mutex);
237 /**********************
238 * Condition variables
239 *********************/
244 * Initiatize a condition variable
246 * The condition variable referenced by cond is initialized with
247 * the attributes required by the FOSA implementation.
249 * Returns 0 if successful; otherwise it returns an error code:
250 * EAGAIN: the system lacked the necessary resources to create the
252 * ENOMEM: Insufficient memory exists to initialize the condition variable
253 * EBUSY: The system has detected an attempt to reinitialize the
256 * Alternatively, in case of error the implementation is allowed to
257 * notify it to the system console and then terminate the FRSH
258 * implementation and dependant applications
260 //int fosa_cond_init(fosa_cond_t *cond);
261 extern inline int fosa_cond_init(fosa_cond_t *cond){
262 //return pthread_cond_init(pthread_cond_t *cond,
263 // const pthread_condattr_t *attr);
267 * fosa_cond_destroy()
269 * Destroy a condition variable
271 * The condition variable pointed to by cond is destroyed
273 * Returns 0 if successful; otherwise it returns an error code:
274 * EINVAL: the value of cond is invalid
275 * EBUSY: The condition variable is in use (a thread is waiting on it)
277 * Alternatively, in case of error the implementation is allowed to
278 * notify it to the system console and then terminate the FRSH
279 * implementation and dependant applications
281 //int fosa_cond_destroy(fosa_cond_t *cond);
282 extern inline int fosa_cond_destroy(fosa_cond_t *cond){
283 return pthread_cond_destroy(cond);
289 * Signal a condition variable
291 * This call unblocks at least one of the threads that are waiting on
292 * the condition variable referenced by cond. If there are no threads
293 * waiting, the function has no effect
295 * Returns 0 if successful; otherwise it returns an error code:
296 * EINVAL: the value of cond is invalid
298 * Alternatively, in case of error the implementation is allowed to
299 * notify it to the system console and then terminate the FRSH
300 * implementation and dependant applications
302 //int fosa_cond_signal(fosa_cond_t *cond);
303 extern inline int fosa_cond_signal(fosa_cond_t *cond){
304 return pthread_cond_signal(cond);
307 * fosa_cond_broadcast()
309 * Broadcast a condition variable
311 * This call unblocks all of the threads that are waiting on the
312 * condition variable referenced by cond. If there are no threads
313 * waiting, the function has no effect.
315 * Returns 0 if successful; otherwise it returns an error code:
316 * EINVAL: the value of cond is invalid
318 * Alternatively, in case of error the implementation is allowed to
319 * notify it to the system console and then terminate the FRSH
320 * implementation and dependant applications
322 //int fosa_cond_broadcast(fosa_cond_t *cond);
323 extern inline int fosa_cond_broadcast(fosa_cond_t *cond){
324 return pthread_cond_broadcast(cond);
330 * Wait at a condition variable
332 * This call is used to block on the condition variable referenced by
333 * cond. It shall be called with the mutex referenced by mutex
334 * locked. The function releases the mutex and blocks the calling
335 * thread until the condition is signalled by some other thread and
336 * the calling thread is awakened. Then it locks the mutex and
337 * returns with the mutex locked by the calling thread.
339 * Returns 0 if successful; otherwise it returns an error code:
340 * EINVAL: the value of cond or mutex 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
344 * Alternatively, in case of error the implementation is allowed to
345 * notify it to the system console and then terminate the FRSH
346 * implementation and dependant applications
348 //int fosa_cond_wait(fosa_cond_t *cond, frsh_mutex_t *mutex);
349 extern inline int fosa_cond_wait(fosa_cond_t *cond, frsh_mutex_t *mutex){
350 return pthread_cond_wait(cond, mutex);
354 * fosa_cond_timedwait()
356 * Wait at a condition variable, with a timeout
358 * This function is equal to fosa_cond_wait(), except that the maximum
359 * wait time is limited to the absolute time referenced by abstime, as
360 * measured by the FOSA_CLOCK_REALTIME clock.
362 * Returns 0 if successful; otherwise it returns an error code:
363 * EINVAL: the value of cond or mutex or abstime is invalid, or different
364 * mutexes were used for concurrent wait operations on cond, or
365 * the mutex was not owned by the calling thread
366 * ETIMEDOUT: the timeout expired
368 * Alternatively, except for ETIMEDOUT, in case of error the
369 * implementation is allowed to notify it to the system console and
370 * then terminate the FRSH implementation and dependant applications
372 // int fosa_cond_timedwait(fosa_cond_t *cond, frsh_mutex_t *mutex,
373 // const struct timespec abstime);
374 extern inline int fosa_cond_timedwait(fosa_cond_t *cond,
376 const struct timespec abstime){
377 return pthread_cond_timedwait(cond, mutex, abstime);
384 #endif /* !FOSA_MUTEX_AND_CONDVARS_H_ */