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 #include "fosa_time.h"
62 #include "fosa_mutexes_and_condvars.h"
64 #ifdef OMK_FOR_USER /* If compiled by OMK, use the config */
65 #include "fosa_config.h"
68 /*******************************************************
69 * Mutexes with priority/bandwidth inheritance
70 ******************************************************/
75 * Initialize a frsh mutex
77 * The mutex pointed to by mutex is initialized as a mutex using
78 * the priority ceiling protocol. A priority ceiling of prioceiling
79 * is assigned to this mutex.
81 * Returns 0 if successful; otherwise it returns an error code:
82 * EINVAL: the value of prioceiling is invalid
83 * EAGAIN: the system lacked the necessary resources to create the mutex
84 * ENOMEM: Insufficient memory exists to initialize the mutex
85 * EBUSY: The system has detected an attempt to reinitialize the mutex
87 int fosa_mutex_init(fosa_mutex_t *mutex, int prioceiling)
90 pthread_mutexattr_t attr;
92 if ((error = pthread_mutexattr_init(&attr)) != 0)
95 #ifndef CONFIG_NO_PRIO_INHERIT /* Valgrind doesn't support this attribute */
96 if ((error = pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT)) != 0)
99 return pthread_mutex_init(mutex, &attr);
103 * fosa_mutex_destroy()
105 * Destroy a frsh mutex
107 * The mutex pointed to by mutex is destroyed
109 * Returns 0 if successful; otherwise it returns an error code:
110 * EINVAL: the value of mutex is invalid
111 * EBUSY: The mutex is in use (is locked)
113 int fosa_mutex_destroy(fosa_mutex_t *mutex)
115 return pthread_mutex_destroy(mutex);
119 * fosa_mutex_set_prioceiling()
121 * Dynamically set the priority ceiling of a mutex
123 * Since in this implementation we use BandWidth Inheritance defining the
124 * ceiling of a mutex is meaningless, and so the function always returns
127 int fosa_mutex_set_prioceiling(fosa_mutex_t *mutex,
135 * fosa_mutex_get_prioceiling()
137 * Dynamically get the priority ceiling of a mutex
139 * Since in this implementation we use BandWidth Inheritance defining the
140 * ceiling of a mutex is meaningless, and so the function always returns
143 int fosa_mutex_get_prioceiling(const fosa_mutex_t *mutex, int *ceiling)
153 * This function locks the mutex specified by mutex. If it is already
154 * locked, the calling thread blocks until the mutex becomes
155 * available. The operation returns with the mutex in the locked
156 * state, with the calling thread as its owner.
158 * Returns 0 if successful; otherwise it returns an error code:
159 * EINVAL: the value of mutex is invalid, or the priority of the
160 * calling thread is higher than the priority ceiling of the mutex
161 * EDEADLK: the current thread already owns this mutex
163 int fosa_mutex_lock(fosa_mutex_t *mutex)
165 return pthread_mutex_lock(mutex);
169 * fosa_mutex_trylock()
171 * Try locking a mutex
173 * This function is identical to fosa_mutex_lock() except that if the
174 * mutex is already locked the call returns immediately with an error
177 * Returns 0 if successful; otherwise it returns an error code:
178 * EINVAL: the value of mutex is invalid, or the priority of the
179 * calling thread is higher than the priority ceiling of the mutex
180 * EBUSY: the mutex was already locked
182 int fosa_mutex_trylock(fosa_mutex_t *mutex)
184 return pthread_mutex_trylock(mutex);
188 * fosa_mutex_unlock()
192 * This function must be called by the owner of the mutex referenced
193 * by mutex, to unlock it. If there are threads blocked on the mutex
194 * the mutex becomes available and the highest priority thread is
195 * awakened to acquire the mutex.
197 * Returns 0 if successful; otherwise it returns an error code:
198 * EINVAL: the value of mutex is invalid
199 * EPERM: the calling thread is not the owner of the mutex
201 int fosa_mutex_unlock(fosa_mutex_t *mutex)
203 return pthread_mutex_unlock(mutex);
206 /**********************
207 * Condition variables
208 *********************/
213 * Initiatize a condition variable
215 * The condition variable referenced by cond is initialized with
216 * the attributes required by the FOSA implementation.
218 * Returns 0 if successful; otherwise it returns an error code:
219 * EAGAIN: the system lacked the necessary resources to create the
221 * ENOMEM: Insufficient memory exists to initialize the condition variable
222 * EBUSY: The system has detected an attempt to reinitialize the
225 int fosa_cond_init(fosa_cond_t *cond)
227 return pthread_cond_init(cond, NULL);
231 * fosa_cond_destroy()
233 * Destroy a condition variable
235 * The condition variable pointed to by cond is destroyed
237 * Returns 0 if successful; otherwise it returns an error code:
238 * EINVAL: the value of cond is invalid
239 * EBUSY: The condition variable is in use (a thread is waiting on it)
241 int fosa_cond_destroy(fosa_cond_t *cond)
243 return pthread_cond_destroy(cond);
249 * Signal a condition variable
251 * This call unblocks at least one of the threads that are waiting on
252 * the condition variable referenced by cond. If there are no threads
253 * waiting, the function has no effect
255 * Returns 0 if successful; otherwise it returns an error code:
256 * EINVAL: the value of cond is invalid
258 int fosa_cond_signal(fosa_cond_t *cond)
260 return pthread_cond_signal(cond);
264 * fosa_cond_broadcast()
266 * Broadcast a condition variable
268 * This call unblocks all of the threads that are waiting on the
269 * condition variable referenced by cond. If there are no threads
270 * waiting, the function has no effect.
272 * Returns 0 if successful; otherwise it returns an error code:
273 * EINVAL: the value of cond is invalid
275 int fosa_cond_broadcast(fosa_cond_t *cond)
277 return pthread_cond_broadcast(cond);
283 * Wait at a condition variable
285 * This call is used to block on the condition variable referenced by
286 * cond. It shall be called with the mutex referenced by mutex
287 * locked. The function releases the mutex and blocks the calling
288 * thread until the condition is signalled by some other thread and
289 * the calling thread is awakened. Then it locks the mutex and
290 * returns with the mutex locked by the calling thread.
292 * Returns 0 if successful; otherwise it returns an error code:
293 * EINVAL: the value of cond or mutex is invalid, or different
294 * mutexes were used for concurrent wait operations on cond, or
295 * the mutex was not owned by the calling thread
297 int fosa_cond_wait(fosa_cond_t *cond, fosa_mutex_t *mutex)
299 return pthread_cond_wait(cond, mutex);
303 * fosa_cond_timedwait()
305 * Wait at a condition variable, with a timeout
307 * This function is equal to fosa_cond_wait(), except that the maximum
308 * wait time is limited to the absolute time referenced by abstime, as
309 * measured by the FOSA_CLOCK_ABSOLUTE clock.
311 * Returns 0 if successful; otherwise it returns an error code:
312 * EINVAL: the value of cond or mutex or abstime is invalid, or different
313 * mutexes were used for concurrent wait operations on cond, or
314 * the mutex was not owned by the calling thread
315 * ETIMEDOUT: the timeout expired
317 int fosa_cond_timedwait(fosa_cond_t *cond,
319 const fosa_abs_time_t *abstime)
321 struct timespec abstime_tspec;
323 abstime_tspec = fosa_abs_time_to_timespec(*abstime);
325 return pthread_cond_timedwait(cond, mutex, &abstime_tspec);