1 // -----------------------------------------------------------------------
2 // Copyright (C) 2006 - 2009 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 #define TRACE(str, args...) printf ("%d: %s: " str, __LINE__, __func__, ##args)
76 /*************************
77 * Thread identification
78 *************************/
79 bool fosa_thread_equal(fosa_thread_id_t t1, fosa_thread_id_t t2)
81 return pthread_equal (t1, t2);
84 fosa_thread_id_t fosa_thread_self()
86 return pthread_self();
89 /*************************
91 *************************/
93 int fosa_thread_attr_init(fosa_thread_attr_t *attr)
95 return pthread_attr_init (attr);
98 int fosa_thread_attr_destroy(fosa_thread_attr_t *attr)
100 return pthread_attr_destroy (attr);
103 int fosa_thread_attr_set_stacksize(fosa_thread_attr_t *attr,
106 return pthread_attr_setstacksize (attr, stacksize);
110 int fosa_thread_attr_get_stacksize(const fosa_thread_attr_t *attr,
113 return pthread_attr_getstacksize (attr, stacksize);
117 /*************************
118 * Thread creation and termination
119 *************************/
120 int fosa_thread_create
121 (fosa_thread_id_t *tid,
122 const fosa_thread_attr_t *attr,
123 fosa_thread_code_t code,
126 return pthread_create (tid, attr, code, arg);
130 /**************************************************
131 * Thread-specific data
132 * (extended with access from a different thread)
133 **************************************************/
134 int fosa_key_create(int *key)
136 switch (pthread_key_create (key, NULL)) {
147 if (*key >= FOSA_MAX_KEYS + 1) {
148 pthread_key_delete (*key);
155 int fosa_key_destroy(int key)
157 if (key < 0 || key >= FOSA_MAX_KEYS + 1)
160 return pthread_key_delete (key);
163 int fosa_thread_set_specific_data
164 (int key, fosa_thread_id_t tid, const void * value)
166 if (key < 0 || key >= FOSA_MAX_KEYS + 1)
169 return pthread_setspecific_for (key, tid, value);
172 int fosa_thread_get_specific_data(int key, fosa_thread_id_t tid,
175 if (key < 0 || key >= FOSA_MAX_KEYS + 1)
178 return pthread_getspecific_from (key, tid, value);
181 /**********************
183 **********************/
184 // PaRTiKle uses decreasing values for increasing priority (0 -> maxprio)
186 inline int fosa2prtk (int prio, int urg)
188 return ((prio & 0x3f) << 4) + (urg & 0xf);
191 static inline int prtk2fprio (int prio)
193 return (prio >> 4) & 0x3f;
196 static inline int prtk2furg (int prio)
202 int fosa_get_priority_max()
204 return prtk2fprio (sched_get_priority_min (SCHED_FIFO));
207 int fosa_get_priority_min()
209 return prtk2fprio (sched_get_priority_max (SCHED_FIFO));
212 int fosa_thread_attr_set_prio(fosa_thread_attr_t *attr, int prio)
214 struct sched_param sp;
216 if (prio > fosa_get_priority_max () || prio < fosa_get_priority_min ())
219 sp.sched_priority = sched_get_priority_min (SCHED_FIFO) - fosa2prtk (prio, 0);
221 return pthread_attr_setschedparam (attr, &sp);
224 int fosa_thread_attr_get_prio
225 (const fosa_thread_attr_t *attr, int *prio)
227 struct sched_param sp;
229 pthread_attr_getschedparam (attr, &sp);
231 *prio = prtk2fprio (sched_get_priority_min (SCHED_FIFO) - sp.sched_priority);
236 int fosa_thread_set_prio(fosa_thread_id_t tid, int prio)
238 struct sched_param sp;
241 if (prio > fosa_get_priority_max () || prio < fosa_get_priority_min ())
244 pthread_getschedparam (tid, &policy, &sp);
245 sp.sched_priority = sched_get_priority_min (SCHED_FIFO) - fosa2prtk (prio, 0);
247 return pthread_setschedparam (tid, policy, &sp);
250 int fosa_thread_get_prio (fosa_thread_id_t tid, int *prio)
252 struct sched_param sp;
255 pthread_getschedparam (tid, &policy, &sp);
256 *prio = sched_get_priority_min (SCHED_FIFO) - sp.sched_priority;
265 int fosa_set_accepted_signals(fosa_signal_t set[], int size)
269 struct sigaction act;
271 // printf ("%d: %s: limits=[%d, %d]\n", __LINE__, __FUNCTION__,
272 // FOSA_SIGNAL_MIN, FOSA_SIGNAL_MAX);
277 sigemptyset (&accept_set);
278 act.sa_mask = accept_set;
279 act.sa_flags = SA_SIGINFO;
280 act.sa_handler = SIG_DFL;
282 // printf ("%d: %s: add signals\n", __LINE__, __FUNCTION__);
283 for (i = 0; i < size; i ++) {
284 // printf ("%d: %s: signal=%d \n", __LINE__, __FUNCTION__, set[i]);
286 if (FOSA_SIGNAL_MIN > set [i] || set [i] > FOSA_SIGNAL_MAX)
289 // printf ("%d: %s: sigaction\n", __LINE__, __FUNCTION__);
290 sigaction (set [i], &act, NULL);
291 sigaddset (&accept_set, set [i]);
294 // printf ("%d: %s: pthread_sigmask\n", __LINE__, __FUNCTION__);
295 assert (!pthread_sigmask (SIG_BLOCK, &accept_set, NULL));
300 int fosa_signal_queue
301 (fosa_signal_t signal, fosa_signal_info_t info,
302 fosa_thread_id_t receiver)
304 union sigval nfo = (union sigval) info.sival_ptr;
306 if (sigqueue (1, signal, nfo))
314 (fosa_signal_t set[], int size, fosa_signal_t *signal_received,
315 fosa_signal_info_t *info)
317 return fosa_signal_timedwait (set, size, signal_received, info, NULL);
321 int fosa_signal_timedwait
322 (fosa_signal_t set[], int size, fosa_signal_t *signal_received,
323 fosa_signal_info_t *info, const fosa_rel_time_t *timeout)
333 for (i = 0; i < size; i ++) {
334 if (FOSA_SIGNAL_MIN > set [i] || set [i] > FOSA_SIGNAL_MAX)
336 sigaddset (&wset, set [i]);
339 sig = sigtimedwait (&wset, &nfo, timeout);
344 *info = (fosa_signal_info_t) nfo.si_value.sival_ptr;
347 *signal_received = sig;