1 // -----------------------------------------------------------------------
2 // Copyright (C) 2006 - 2008 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. Politécnica 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
24 // based on previous work (FSF) done in the FIRST project
26 // Copyright (C) 2005 Mälardalen University, SWEDEN
27 // Scuola Superiore S.Anna, ITALY
28 // Universidad de Cantabria, SPAIN
29 // University of York, UK
31 // FSF API web pages: http://marte.unican.es/fsf/docs
32 // http://shark.sssup.it/contrib/first/docs/
34 // This file is part of FOSA (Frsh Operating System Adaption)
36 // FOSA is free software; you can redistribute it and/or modify it
37 // under terms of the GNU General Public License as published by the
38 // Free Software Foundation; either version 2, or (at your option) any
39 // later version. FOSA is distributed in the hope that it will be
40 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
41 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
42 // General Public License for more details. You should have received a
43 // copy of the GNU General Public License along with FOSA; see file
44 // COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
45 // Cambridge, MA 02139, USA.
47 // As a special exception, including FOSA header files in a file,
48 // instantiating FOSA generics or templates, or linking other files
49 // with FOSA objects to produce an executable application, does not
50 // by itself cause the resulting executable application to be covered
51 // by the GNU General Public License. This exception does not
52 // however invalidate any other reasons why the executable file might be
53 // covered by the GNU Public License.
54 // -----------------------------------------------------------------------
55 //==============================================
56 // ******** ****** ******** **********
57 // **///// /** ** **////// /** /**
58 // ** /** ** /** /** /**
59 // ******* /** ** /********* /**********
60 // **//// /** ** ////////** /**//////**
61 // ** /** ** /** /** /**
62 // ** /** ** ******** /** /**
63 // // /******/ //////// // //
65 // FOSA(Frescor Operating System Adaptation layer)
66 //================================================
68 #include <fosa_configuration_parameters.h>
69 #include <fosa_threads_and_signals.h>
70 #include <fosa_time.h>
74 /*************************
75 * Thread identification
76 *************************/
77 bool fosa_thread_equal(fosa_thread_id_t t1, fosa_thread_id_t t2)
79 return pthread_equal (t1, t2);
82 fosa_thread_id_t fosa_thread_self()
84 return pthread_self();
87 /*************************
89 *************************/
91 int fosa_thread_attr_init(fosa_thread_attr_t *attr)
93 return pthread_attr_init (attr);
96 int fosa_thread_attr_destroy(fosa_thread_attr_t *attr)
98 return pthread_attr_destroy (attr);
101 int fosa_thread_attr_set_stacksize(fosa_thread_attr_t *attr,
104 return pthread_attr_setstacksize (attr, stacksize);
108 int fosa_thread_attr_get_stacksize(const fosa_thread_attr_t *attr,
111 return pthread_attr_getstacksize (attr, stacksize);
115 /*************************
116 * Thread creation and termination
117 *************************/
118 int fosa_thread_create
119 (fosa_thread_id_t *tid,
120 const fosa_thread_attr_t *attr,
121 fosa_thread_code_t code,
124 return pthread_create (tid, attr, code, arg);
128 /**************************************************
129 * Thread-specific data
130 * (extended with access from a different thread)
131 **************************************************/
132 int fosa_key_create(int *key)
134 switch (pthread_key_create (key, NULL)) {
145 if (*key >= FOSA_MAX_KEYS + 1) {
146 pthread_key_delete (*key);
153 int fosa_key_destroy(int key)
155 if (key < 0 || key >= FOSA_MAX_KEYS + 1)
158 return pthread_key_delete (key);
161 int fosa_thread_set_specific_data
162 (int key, fosa_thread_id_t tid, const void * value)
164 if (key < 0 || key >= FOSA_MAX_KEYS + 1)
167 return pthread_setspecific_for (key, tid, value);
170 int fosa_thread_get_specific_data(int key, fosa_thread_id_t tid,
173 if (key < 0 || key >= FOSA_MAX_KEYS + 1)
176 return pthread_getspecific_from (key, tid, value);
179 /**********************
181 **********************/
182 // PaRTiKle uses decreasing values for increasing priority (0 -> maxprio)
184 inline int fosa2prtk (int prio, int urg)
186 return ((prio & 0x3f) << 4) + (urg & 0xf);
189 static inline int prtk2fprio (int prio)
191 return (prio >> 4) & 0x3f;
194 static inline int prtk2furg (int prio)
200 int fosa_get_priority_max()
202 return prtk2fprio (sched_get_priority_min (SCHED_FIFO));
205 int fosa_get_priority_min()
207 return prtk2fprio (sched_get_priority_max (SCHED_FIFO));
210 int fosa_thread_attr_set_prio(fosa_thread_attr_t *attr, int prio)
212 struct sched_param sp;
214 if (prio > fosa_get_priority_max () || prio < fosa_get_priority_min ())
217 sp.sched_priority = sched_get_priority_min (SCHED_FIFO) - fosa2prtk (prio, 0);
219 return pthread_attr_setschedparam (attr, &sp);
222 int fosa_thread_attr_get_prio
223 (const fosa_thread_attr_t *attr, int *prio)
225 struct sched_param sp;
227 pthread_attr_getschedparam (attr, &sp);
229 *prio = prtk2fprio (sched_get_priority_min (SCHED_FIFO) - sp.sched_priority);
234 int fosa_thread_set_prio(fosa_thread_id_t tid, int prio)
236 struct sched_param sp;
239 if (prio > fosa_get_priority_max () || prio < fosa_get_priority_min ())
242 pthread_getschedparam (tid, &policy, &sp);
243 sp.sched_priority = sched_get_priority_min (SCHED_FIFO) - fosa2prtk (prio, 0);
245 return pthread_setschedparam (tid, policy, &sp);
248 int fosa_thread_get_prio (fosa_thread_id_t tid, int *prio)
250 struct sched_param sp;
253 pthread_getschedparam (tid, &policy, &sp);
254 *prio = sched_get_priority_min (SCHED_FIFO) - sp.sched_priority;
263 int fosa_set_accepted_signals(fosa_signal_t set[], int size)
267 struct sigaction act;
272 sigemptyset (&accept_set);
273 act.sa_mask = accept_set;
274 act.sa_flags = SA_SIGINFO;
275 act.sa_handler = SIG_DFL;
277 for (i = 0; i < size; i ++) {
278 if (FOSA_SIGNAL_MIN > set [i] || set [i] > FOSA_SIGNAL_MAX)
281 sigaction (set [i], &act, NULL);
282 sigaddset (&accept_set, set [i]);
285 return pthread_sigmask (SIG_BLOCK, &accept_set, NULL);
289 int fosa_signal_queue
290 (fosa_signal_t signal, fosa_signal_info_t info,
291 fosa_thread_id_t receiver)
293 union sigval nfo = (union sigval) info.sival_ptr;
295 if (sigqueue (1, signal, nfo))
303 (fosa_signal_t set[], int size, fosa_signal_t *signal_received,
304 fosa_signal_info_t *info)
306 return fosa_signal_timedwait (set, size, signal_received, info, NULL);
309 int fosa_signal_timedwait
310 (fosa_signal_t set[], int size, fosa_signal_t *signal_received,
311 fosa_signal_info_t *info, const fosa_rel_time_t *timeout)
316 struct timespec tout = fosa_rel_time_to_timespec (*timeout);
322 for (i = 0; i < size; i ++) {
323 if (FOSA_SIGNAL_MIN > set [i] || set [i] > FOSA_SIGNAL_MAX)
325 sigaddset (&wset, set [i]);
328 sig = sigtimedwait (&wset, &nfo, &tout);
333 *info = (fosa_signal_info_t) nfo.si_value.sival_ptr;
336 *signal_received = sig;