]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_aquosa/fosa_mutexes_and_condvars.c
Small changes and bugfixes in signal handling, mutexes and condition variables.
[frescor/fosa.git] / src_aquosa / fosa_mutexes_and_condvars.c
1 // -----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2007 FRESCOR consortium partners:
3 //
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
10 //    ENEA                                   SWEDEN
11 //    Thales Communication S.A.              FRANCE
12 //    Visual Tools S.A.                      SPAIN
13 //    Rapita Systems Ltd                     UK
14 //    Evidence                               ITALY
15 //    
16 //    See http://www.frescor.org for a link to partners' websites
17 //
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
21 //        made of this code.
22 //
23 //  This file is part of the FRSH implementation
24 //
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)
28 //  any later version.
29 //
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.
34 //
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
38 //  02111-1307, USA.
39 //
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
45 //  Public License.
46 // -----------------------------------------------------------------------
47 //fosa_mutexes_and_condvars.h
48 //==============================================
49 //  ********  ******    ********  **********
50 //  **///// /**    **  **//////  /**     /**
51 //  **      /**    ** /**        /**     /**
52 //  ******* /**    ** /********* /**********
53 //  **////  /**    ** ////////** /**//////**
54 //  **      /**    **        /** /**     /**
55 //  **      /**    **  ********  /**     /**
56 //  //       /******/  ////////   //      // 
57 //
58 // FOSA(Frescor Operating System Adaptation layer)
59 //================================================
60
61 #include "fosa_time.h"
62 #include "fosa_configuration_parameters.h"
63 #include "fosa_mutexes_and_condvars.h"
64
65 #ifdef OMK_FOR_USER             /* If compiled by OMK, use the config */
66 #include "fosa_config.h"
67 #endif
68
69 /*******************************************************
70  * Mutexes with priority/bandwidth inheritance
71  ******************************************************/
72
73 /**
74  * fosa_mutex_init()
75  *
76  * Initialize a frsh mutex
77  *
78  * The mutex pointed to by mutex is initialized as a mutex using
79  * the priority ceiling protocol. A priority ceiling of prioceiling
80  * is assigned to this mutex.
81  *
82  * Returns 0 if successful; otherwise it returns an error code:
83  *    EINVAL: the value of prioceiling is invalid
84  *    EAGAIN: the system lacked the necessary resources to create the mutex
85  *    ENOMEM: Insufficient memory exists to initialize the mutex
86  *    EBUSY:  The system has detected an attempt to reinitialize the mutex
87  **/
88 int fosa_mutex_init(fosa_mutex_t *mutex, int prioceiling)
89 {
90         int ret;
91         pthread_mutexattr_t attr;
92
93         ret = pthread_mutexattr_init(&attr);
94         if (ret) return errno;
95
96 #ifndef CONFIG_NO_PRIO_INHERIT  /* Valgrind doesn't support this attribute */
97         ret = pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
98         if (ret) return errno;
99 #endif
100
101         ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
102         if (ret) return errno;
103
104         return pthread_mutex_init(mutex, &attr);
105 }
106
107 /**
108  * fosa_mutex_destroy()
109  *
110  * Destroy a frsh mutex
111  *
112  * The mutex pointed to by mutex is destroyed
113  *
114  * Returns 0 if successful; otherwise it returns an error code:
115  *    EINVAL: the value of mutex is invalid
116  *    EBUSY:  The mutex is in use (is locked)
117  **/
118 int fosa_mutex_destroy(fosa_mutex_t *mutex)
119 {
120         return pthread_mutex_destroy(mutex);
121 }
122
123 /**
124  * fosa_mutex_set_prioceiling()
125  *
126  * Dynamically set the priority ceiling of a mutex
127  *
128  * Since in this implementation we use BandWidth Inheritance defining the
129  * ceiling of a mutex is meaningless, and so the function always returns
130  * EINVAL
131  **/
132 int fosa_mutex_set_prioceiling(fosa_mutex_t *mutex,
133                                int new_ceiling,
134                                int *old_ceiling)
135 {
136         return FOSA_EINVAL;
137 }
138
139 /**
140  * fosa_mutex_get_prioceiling()
141  *
142  * Dynamically get the priority ceiling of a mutex
143  *
144  * Since in this implementation we use BandWidth Inheritance defining the
145  * ceiling of a mutex is meaningless, and so the function always returns
146  * EINVAL
147  **/
148 int fosa_mutex_get_prioceiling(const fosa_mutex_t *mutex, int *ceiling)
149 {
150         return FOSA_EINVAL;
151 }
152
153 /**
154  * fosa_mutex_lock()
155  *
156  * Lock a mutex
157  *
158  * This function locks the mutex specified by mutex. If it is already
159  * locked, the calling thread blocks until the mutex becomes
160  * available. The operation returns with the mutex in the locked
161  * state, with the calling thread as its owner.
162  *
163  * Returns 0 if successful; otherwise it returns an error code:
164  *    EINVAL: the value of mutex is invalid, or the priority of the
165  *            calling thread is higher than the priority ceiling of the mutex
166  *    EDEADLK: the current thread already owns this mutex
167  **/
168 int fosa_mutex_lock(fosa_mutex_t *mutex)
169 {
170         return pthread_mutex_lock(mutex);
171 }
172
173 /**
174  * fosa_mutex_trylock()
175  *
176  * Try locking a mutex
177  *
178  * This function is identical to fosa_mutex_lock() except that if the
179  * mutex is already locked the call returns immediately with an error
180  * indication.
181  *
182  * Returns 0 if successful; otherwise it returns an error code:
183  *    EINVAL: the value of mutex is invalid, or the priority of the
184  *            calling thread is higher than the priority ceiling of the mutex
185  *    EBUSY: the mutex was already locked
186  **/
187 int fosa_mutex_trylock(fosa_mutex_t *mutex)
188 {
189         return pthread_mutex_trylock(mutex);
190 }
191
192 /**
193  * fosa_mutex_unlock()
194  *
195  * Unlock a mutex
196  *
197  * This function must be called by the owner of the mutex referenced
198  * by mutex, to unlock it. If there are threads blocked on the mutex
199  * the mutex becomes available and the highest priority thread is
200  * awakened to acquire the mutex.
201  *
202  * Returns 0 if successful; otherwise it returns an error code:
203  *     EINVAL: the value of mutex is invalid
204  *     EPERM: the calling thread is not the owner of the mutex
205  **/
206 int fosa_mutex_unlock(fosa_mutex_t *mutex)
207 {
208         return pthread_mutex_unlock(mutex);
209 }
210
211 /**********************
212  * Condition variables
213  *********************/
214
215 /**
216  * fosa_cond_init()
217  *
218  * Initiatize a condition variable
219  *
220  * The condition variable referenced by cond is initialized with
221  * the attributes required by the FOSA implementation.
222  *
223  *  Returns 0 if successful; otherwise it returns an error code:
224  *     EAGAIN: the system lacked the necessary resources to create the
225  *             condition variable
226  *     ENOMEM: Insufficient memory exists to initialize the condition variable
227  *     EBUSY:  The system has detected an attempt to reinitialize the
228  *             condition variable
229  **/
230 int fosa_cond_init(fosa_cond_t *cond)
231 {
232         int ret;
233         pthread_condattr_t attr;
234
235         ret = pthread_condattr_init(&attr);
236         if (ret) return errno;
237
238         ret = pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
239         if (ret) return errno;
240
241         return pthread_cond_init(cond, &attr);
242 }
243
244 /**
245  * fosa_cond_destroy()
246  *
247  * Destroy a condition variable
248  *
249  * The condition variable pointed to by cond is destroyed
250  *
251  * Returns 0 if successful; otherwise it returns an error code:
252  *     EINVAL: the value of cond is invalid
253  *     EBUSY:  The condition variable is in use (a thread is waiting on it)
254  **/
255 int fosa_cond_destroy(fosa_cond_t *cond)
256 {
257         return pthread_cond_destroy(cond);
258 }
259
260 /**
261  * fosa_cond_signal()
262  *
263  * Signal a condition variable
264  *
265  * This call unblocks at least one of the threads that are waiting on
266  * the condition variable referenced by cond. If there are no threads
267  * waiting, the function has no effect
268  *
269  * Returns 0 if successful; otherwise it returns an error code:
270  *     EINVAL: the value of cond is invalid
271  **/
272 int fosa_cond_signal(fosa_cond_t *cond)
273 {
274         return pthread_cond_signal(cond);
275 }
276
277 /**
278  * fosa_cond_broadcast()
279  *
280  * Broadcast a condition variable
281  *
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.
285  *
286  * Returns 0 if successful; otherwise it returns an error code:
287  *     EINVAL: the value of cond is invalid
288  **/
289 int fosa_cond_broadcast(fosa_cond_t *cond)
290 {
291         return pthread_cond_broadcast(cond);
292 }
293
294 /**
295  * fosa_cond_wait()
296  *
297  * Wait at a condition variable
298  *
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.
305  *
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
310  **/
311 int fosa_cond_wait(fosa_cond_t *cond, fosa_mutex_t *mutex)
312 {
313         return pthread_cond_wait(cond, mutex);
314 }
315
316 /**
317  * fosa_cond_timedwait()
318  *
319  * Wait at a condition variable, with a timeout
320  *
321  * This function is equal to fosa_cond_wait(), except that the maximum
322  * wait time is limited to the absolute time referenced by abstime, as
323  * measured by the FOSA_CLOCK_ABSOLUTE clock.
324  *
325  * Returns 0 if successful; otherwise it returns an error code:
326  *     EINVAL: the value of cond or mutex or abstime is invalid, or different
327  *             mutexes were used for concurrent wait operations on cond, or
328  *             the mutex was not owned by the calling thread
329  *     ETIMEDOUT: the timeout expired
330  **/
331 int fosa_cond_timedwait(fosa_cond_t *cond,
332         fosa_mutex_t *mutex,
333         const fosa_abs_time_t *abstime)
334 {
335         struct timespec abstime_tspec;
336
337         abstime_tspec = fosa_abs_time_to_timespec(*abstime);
338         return pthread_cond_timedwait(cond, mutex, &abstime_tspec);
339 }
340