]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_partikle/fosa_mutexes_and_condvars.c
ce75869e3b6425e0bf0fc6f20e110223c2023231
[frescor/fosa.git] / src_partikle / 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 //==============================================
48 //  ********  ******    ********  **********
49 //  **///// /**    **  **//////  /**     /**
50 //  **      /**    ** /**        /**     /**
51 //  ******* /**    ** /********* /**********
52 //  **////  /**    ** ////////** /**//////**
53 //  **      /**    **        /** /**     /**
54 //  **      /**    **  ********  /**     /**
55 //  //       /******/  ////////   //      // 
56 //
57 // FOSA(Frescor Operating System Adaptation layer)
58 //================================================
59
60 #include <fosa_mutexes_and_condvars.h>
61
62 /*******************************************************
63  * Mutexes with priority ceiling
64  ******************************************************/
65 int fosa_mutex_init(fosa_mutex_t *mutex, int prioceiling)
66 {
67         pthread_mutexattr_t attr;
68         int err;
69         
70         err = pthread_mutexattr_init (&attr);
71         if (err)
72                 return err;
73         
74         pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ERRORCHECK);
75         pthread_mutexattr_setprotocol (&attr, PTHREAD_PRIO_PROTECT);
76         err = pthread_mutexattr_setprioceiling (&attr, prioceiling);
77         if (err)
78                 return err;
79         
80         return pthread_mutex_init (mutex, &attr);
81 }
82                 
83 int fosa_mutex_destroy(fosa_mutex_t *mutex)
84 {
85         return pthread_mutex_destroy (mutex);
86 }
87
88 int fosa_mutex_set_prioceiling
89                 (fosa_mutex_t *mutex, int new_ceiling, int *old_ceiling)
90 {
91         return pthread_mutex_setprioceiling (mutex, new_ceiling, old_ceiling);
92 }
93
94 int fosa_mutex_get_prioceiling(const fosa_mutex_t *mutex, int *ceiling)
95 {
96         return pthread_mutex_getprioceiling (mutex, ceiling);
97 }
98
99 int fosa_mutex_lock(fosa_mutex_t *mutex)
100 {
101         return pthread_mutex_lock (mutex);
102 }
103
104 int fosa_mutex_trylock(fosa_mutex_t *mutex)
105 {
106         return pthread_mutex_trylock (mutex);
107 }
108
109 int fosa_mutex_unlock(fosa_mutex_t *mutex)
110 {
111         return pthread_mutex_unlock (mutex);
112 }
113
114
115 /**********************
116  * Condition variables
117  *********************/
118 int fosa_cond_init(fosa_cond_t *cond)
119 {
120         pthread_condattr_t attr;
121         int err;
122         
123         err = pthread_condattr_init (&attr);
124         if (err)
125                 return err;
126         
127         return pthread_cond_init (cond, NULL);
128 }
129
130 int fosa_cond_destroy(fosa_cond_t *cond)
131 {
132         return pthread_cond_destroy (cond);
133 }
134
135 int fosa_cond_signal(fosa_cond_t *cond)
136 {
137         return pthread_cond_signal (cond);
138 }
139
140 int fosa_cond_broadcast(fosa_cond_t *cond)
141 {
142         return pthread_cond_broadcast (cond);
143 }
144
145 int fosa_cond_wait(fosa_cond_t *cond, fosa_mutex_t *mutex)
146 {
147         return pthread_cond_wait (cond, mutex);
148 }
149
150 int fosa_cond_timedwait 
151                 (fosa_cond_t *cond, 
152                  fosa_mutex_t *mutex,
153                  const struct timespec *abstime)
154 {
155         int err;
156         
157         err = pthread_cond_timedwait (cond, mutex, abstime);
158         if (err == ETIMEDOUT)
159                 return ETIMEDOUT;
160
161         if (err)
162                 return EINVAL;
163         
164         return 0;
165 }