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 // -----------------------------------------------------------------------
41 //fosa_mutexes_and_condvars.c
42 //==============================================
43 // ******** ****** ******** **********
44 // **///// /** ** **////// /** /**
45 // ** /** ** /** /** /**
46 // ******* /** ** /********* /**********
47 // **//// /** ** ////////** /**//////**
48 // ** /** ** /** /** /**
49 // ** /** ** ******** /** /**
50 // // /******/ //////// // //
52 // FOSA(Frescor Operating System Adaptation layer)
53 //================================================
55 #include "fosa_mutexes_and_condvars.h"
60 #include <misc/error_checks.h>
64 /*******************************************************
65 * Mutexes with priority ceiling
66 ******************************************************/
71 * Initialize a frsh mutex
73 * The mutex pointed to by mutex is initialized as a mutex using
74 * the priority ceiling protocol. A priority ceiling of prioceiling
75 * is assigned to this mutex.
77 * Returns 0 if successful; otherwise it returns an error code:
78 * EINVAL: the value of prioceiling is invalid
79 * EAGAIN: the system lacked the necessary resources to create the mutex
80 * ENOMEM: Insufficient memory exists to initialize the mutex
81 * EBUSY: The system has detected an attempt to reinitialize the mutex
83 * Alternatively, in case of error the implementation is allowed to
84 * notify it to the system console and then terminate the FRSH
85 * implementation and dependant applications
87 int fosa_mutex_init(frsh_mutex_t *mutex, int prioceiling)
89 pthread_mutexattr_t attr;
92 // create the attributes object and set the mutex to use the
93 // priority ceiling protocol and the desired priority ceiling
94 ret_value=pthread_mutexattr_init(&attr);
95 if (ret_value!=0) return ret_value;
97 CHK(pthread_mutexattr_setprotocol(&attr,PTHREAD_PRIO_PROTECT));
98 ret_value=pthread_mutexattr_setprioceiling(&attr,prioceiling);
102 ret_value=pthread_mutex_init(mutex,&attr);
104 // destroy the mutex attributes object
105 pthread_mutexattr_destroy(&attr);
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:
118 * EINVAL: the value of mutex is invalid
119 * EBUSY: The mutex is in use (is locked)
121 * Alternatively, in case of error the implementation is allowed to
122 * notify it to the system console and then terminate the FRSH
123 * implementation and dependant applications
125 int fosa_mutex_destroy(frsh_mutex_t *mutex)
127 return pthread_mutex_destroy(mutex);
131 * fosa_mutex_set_prioceiling()
133 * Dynamically set the priority ceiling of a mutex
135 * This function locks the mutex (blocking the calling thread if
136 * necessary) and after it is locked it changes its priority ceiling
137 * to the value specified by new_ceiling, and then it unlocks the
138 * mutex. The previous value of the ceiling is returned in
141 * Returns 0 if successful; otherwise it returns an error code:
142 * EINVAL: the value of mutex or prioceiling is invalid
144 * Alternatively, in case of error the implementation is allowed to
145 * notify it to the system console and then terminate the FRSH
146 * implementation and dependant applications
148 int fosa_mutex_set_prioceiling
149 (frsh_mutex_t *mutex, int new_ceiling, int *old_ceiling)
151 return pthread_mutex_setprioceiling(mutex,new_ceiling,old_ceiling);
155 * fosa_mutex_get_prioceiling()
157 * Dynamically get the priority ceiling of a mutex
159 * This function copies into the variable pointed to by ceiling the
160 * current priority ceiling of the mutex referenced by mutex
162 * Returns 0 if successful; otherwise it returns an error code:
163 * EINVAL: the value of mutex is invalid
165 * Alternatively, in case of error the implementation is allowed to
166 * notify it to the system console and then terminate the FRSH
167 * implementation and dependant applications
169 int fosa_mutex_get_prioceiling(const frsh_mutex_t *mutex, int *ceiling)
171 return pthread_mutex_getprioceiling(mutex,ceiling);
179 * This function locks the mutex specified by mutex. If it is already
180 * locked, the calling thread blocks until the mutex becomes
181 * available. The operation returns with the mutex in the locked
182 * state, with the calling thread as its owner.
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 * EDEADLK: the current thread already owns this mutex
189 * Alternatively, in case of error the implementation is allowed to
190 * notify it to the system console and then terminate the FRSH
191 * implementation and dependant applications
193 int fosa_mutex_lock(frsh_mutex_t *mutex)
195 return pthread_mutex_lock(mutex);
199 * fosa_mutex_trylock()
201 * Try locking a mutex
203 * This function is identical to fosa_mutex_lock() except that if the
204 * mutex is already locked the call returns immediately with an error
207 * Returns 0 if successful; otherwise it returns an error code:
208 * EINVAL: the value of mutex is invalid, or the priority of the
209 * calling thread is higher than the priority ceiling of the mutex
210 * EBUSY: the mutex was already locked
212 * Alternatively, except for EBUSY, in case of error the
213 * implementation is allowed to notify it to the system console and
214 * then terminate the FRSH implementation and dependant applications
216 int fosa_mutex_trylock(frsh_mutex_t *mutex)
218 return pthread_mutex_trylock(mutex);
222 * fosa_mutex_unlock()
226 * This function must be called by the owner of the mutex referenced
227 * by mutex, to unlock it. If there are threads blocked on the mutex
228 * the mutex becomes available and the highest priority thread is
229 * awakened to acquire the mutex.
231 * Returns 0 if successful; otherwise it returns an error code:
232 * EINVAL: the value of mutex is invalid
233 * EPERM: the calling thread is not the owner of the mutex
235 * Alternatively, except for EBUSY, in case of error the
236 * implementation is allowed to notify it to the system console and
237 * then terminate the FRSH implementation and dependant applications
239 int fosa_mutex_unlock(frsh_mutex_t *mutex)
241 return pthread_mutex_unlock(mutex);
245 /**********************
246 * Condition variables
247 *********************/
252 * Initiatize a condition variable
254 * The condition variable referenced by cond is initialized with
255 * the attributes required by the FOSA implementation.
257 * Returns 0 if successful; otherwise it returns an error code:
258 * EAGAIN: the system lacked the necessary resources to create the
260 * ENOMEM: Insufficient memory exists to initialize the condition variable
261 * EBUSY: The system has detected an attempt to reinitialize the
264 * Alternatively, in case of error the implementation is allowed to
265 * notify it to the system console and then terminate the FRSH
266 * implementation and dependant applications
268 int fosa_cond_init(fosa_cond_t *cond)
270 // initialize condition variable with default attributes
271 return pthread_cond_init(cond,NULL);
275 * fosa_cond_destroy()
277 * Destroy a condition variable
279 * The condition variable pointed to by cond is destroyed
281 * Returns 0 if successful; otherwise it returns an error code:
282 * EINVAL: the value of cond is invalid
283 * EBUSY: The condition variable is in use (a thread is waiting on it)
285 * Alternatively, in case of error the implementation is allowed to
286 * notify it to the system console and then terminate the FRSH
287 * implementation and dependant applications
289 int fosa_cond_destroy(fosa_cond_t *cond)
291 return pthread_cond_destroy(cond);
297 * Signal a condition variable
299 * This call unblocks at least one of the threads that are waiting on
300 * the 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 * 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_signal(fosa_cond_t *cond)
312 return pthread_cond_signal(cond);
316 * fosa_cond_broadcast()
318 * Broadcast a condition variable
320 * This call unblocks all of the threads that are waiting on the
321 * condition variable referenced by cond. If there are no threads
322 * waiting, the function has no effect.
324 * Returns 0 if successful; otherwise it returns an error code:
325 * EINVAL: the value of cond is invalid
327 * Alternatively, in case of error the implementation is allowed to
328 * notify it to the system console and then terminate the FRSH
329 * implementation and dependant applications
331 int fosa_cond_broadcast(fosa_cond_t *cond)
333 return pthread_cond_broadcast(cond);
339 * Wait at a condition variable
341 * This call is used to block on the condition variable referenced by
342 * cond. It shall be called with the mutex referenced by mutex
343 * locked. The function releases the mutex and blocks the calling
344 * thread until the condition is signalled by some other thread and
345 * the calling thread is awakened. Then it locks the mutex and
346 * returns with the mutex locked by the calling thread.
348 * Returns 0 if successful; otherwise it returns an error code:
349 * EINVAL: the value of cond or mutex is invalid, or different
350 * mutexes were used for concurrent wait operations on cond, or
351 * the mutex was not owned by the calling thread
353 * Alternatively, in case of error the implementation is allowed to
354 * notify it to the system console and then terminate the FRSH
355 * implementation and dependant applications
357 int fosa_cond_wait(fosa_cond_t *cond, frsh_mutex_t *mutex)
359 return pthread_cond_wait(cond,mutex);
363 * fosa_cond_timedwait()
365 * Wait at a condition variable, with a timeout
367 * This function is equal to fosa_cond_wait(), except that the maximum
368 * wait time is limited to the absolute time referenced by abstime, as
369 * measured by the FOSA_CLOCK_REALTIME clock.
371 * Returns 0 if successful; otherwise it returns an error code:
372 * EINVAL: the value of cond or mutex or abstime is invalid, or different
373 * mutexes were used for concurrent wait operations on cond, or
374 * the mutex was not owned by the calling thread
375 * ETIMEDOUT: the timeout expired
377 * Alternatively, except for ETIMEDOUT, in case of error the
378 * implementation is allowed to notify it to the system console and
379 * then terminate the FRSH implementation and dependant applications
381 int fosa_cond_timedwait(fosa_cond_t *cond, frsh_mutex_t *mutex,
382 const struct timespec *abstime)
384 return pthread_cond_timedwait(cond,mutex,abstime);