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 //==============================================
48 // ******** ****** ******** **********
49 // **///// /** ** **////// /** /**
50 // ** /** ** /** /** /**
51 // ******* /** ** /********* /**********
52 // **//// /** ** ////////** /**//////**
53 // ** /** ** /** /** /**
54 // ** /** ** ******** /** /**
55 // // /******/ //////// // //
57 // FOSA(Frescor Operating System Adaptation layer)
58 //================================================
61 #include <asm/bitops.h>
63 /*************************
64 * Thread identification
65 *************************/
67 bool fosa_thread_equal(frsh_thread_id_t t1, frsh_thread_id_t t2){
68 return pthread_equal(t1,t2);
71 frsh_thread_id_t fosa_thread_self(){
72 return pthread_self();
75 /*************************
76 * Thread creation and termination
77 *************************/
79 int fosa_thread_create (frsh_thread_id_t *tid, const frsh_thread_attr_t *attr,
80 frsh_thread_code_t code, void * arg){
81 return pthread_create(tid,attr,code, arg);
84 int fosa_key_create(int *key) {
85 int idx=ffs(pthread_self()->tsd_bitmap);
90 pthread_self()->tsd_bitmap&=~(1<<idx);
96 int fosa_key_destroy(int key) {
97 pthread_self()->tsd_bitmap|=(1<<key);
102 /**************************************************
103 * Thread-specific data
104 * (extended with access from a different thread)
106 * Several data items (pointers) may be associated with each thread
107 * Each item is identified through a key, an integer value between 0
108 * and FOSA_MAX_KEYS-1. The caller is responsible of allocating and
109 * deallocating the memory area pointed to by the pointer
110 **************************************************/
112 int fosa_thread_set_specific_data (int key, frsh_thread_id_t tid,
114 if ((0<key) && (key<FOSA_MAX_KEYS-1)){
115 tid->tsd[key] = (void *) value;
121 int fosa_thread_get_specific_data(int key, frsh_thread_id_t tid,
123 if ((0<key) && (key<FOSA_MAX_KEYS-1)){
124 //*value=pthread_remote_getspecific(key,tid);
125 *value=tid->tsd[key];
133 /******************************************************************
136 * This implementation of FRSH assumes an underlying fixed priority
137 * scheduler with priorities in a range, with a minimum and a
138 * maximumm, a number of priority levels with at least 31
139 * priorities. A larger number implies a larger priority. In systems
140 * in which the underlying scheduler uses the opposite convention, a
141 * mapping is automatically provided by the OS adaptation layer.
142 *******************************************************************/
144 int fosa_get_priority_max() {
145 return sched_get_priority_max(0);
148 int fosa_get_priority_min(){
149 return sched_get_priority_min(0);
152 int fosa_thread_attr_set_prio(frsh_thread_attr_t *attr, int prio) {
153 if ((sched_get_priority_min(0)<=prio) || (prio<=sched_get_priority_min(0))){
154 attr->sched_param.sched_priority = prio;
160 int fosa_thread_attr_get_prio (const frsh_thread_attr_t *attr, int *prio){
161 *prio = attr->sched_param.sched_priority;
165 int fosa_thread_set_prio(frsh_thread_id_t tid, int prio){
166 if ((sched_get_priority_min(0)<=prio) || (prio<=sched_get_priority_min(0))){
167 pthread_setschedprio(tid,prio);
173 int fosa_thread_get_prio (frsh_thread_id_t tid, int *prio){
174 *prio = tid->sched_param.sched_priority;
180 /*******************************************************************
183 * Signals represent events that may be notified by the system, or
184 * sent explicitly by the application, and for which a thread may
185 * synchronously wait. Signals carry an associated piece of
186 * information (an integer or a pointer) and are queued until they are
187 * accepted. Signals are identified by an integer signal number (of
188 * the type frsh_signal_t) in the range FOSA_SIGNAL_MIN,
189 * FOSA_SIGNAL_MAX. This range is required to have at least <tbd>
191 *******************************************************************/
193 int fosa_set_accepted_signals(frsh_signal_t set[], int size) {
197 rtl_sigfillset(&bitset); // By default all signals will be blocked.
198 for (x=0; x<size; x++)
199 rtl_sigdelset(&bitset, set[x]); // Unblock the "set" of signals.
201 return pthread_sigmask(SIG_SETMASK, &bitset, NULL);
205 int fosa_signal_queue (frsh_signal_t signal, frsh_signal_info_t info,
206 frsh_thread_id_t receiver){
209 value.sival_ptr=info.sival_ptr;
210 return sigqueue(0, signal, value);
213 int fosa_signal_queue_scheduler(frsh_signal_t signal, frsh_signal_info_t info) {
214 union posix_appsched_eventinfo eventinfo;
215 eventinfo.sig.signo=signal;
216 eventinfo.sig.siginfo= (siginfo_t){info.sival_int};
217 generate_event(pthread_self(), fosa_scheduler_thread,
218 POSIX_APPSCHED_SIGNAL, &eventinfo,
219 sizeof(union posix_appsched_eventinfo));
224 int fosa_signal_wait (frsh_signal_t set[], int size, frsh_signal_t *signal_received,
225 frsh_signal_info_t *info){
230 rtl_sigemptyset(&bitset); // No signals to wait for;
231 for (x=0; x<size; x++)
232 rtl_sigaddset(&bitset, set[x]); // Add to the set of signals to be waited for.
233 sigwaitinfo(&bitset, &__info);
234 *signal_received=__info.si_signo;
235 //*info = __info.si_value.sival_ptr;
239 int fosa_signal_timedwait (frsh_signal_t set[], int size, frsh_signal_t *signal_received, frsh_signal_info_t *info, const struct timespec *timeout){
246 rtl_sigemptyset(&bitset); // No signals to wait for;
247 for (x=0; x<size; x++)
248 rtl_sigaddset(&bitset, set[x]); // Add to the set of signals to be waited for.
249 sigtimedwait(&bitset, &__info, timeout);
251 *signal_received=__info.si_signo;
252 // *info = __info.si_value.sival_ptr;
256 int frsh_thread_attr_init(frsh_thread_attr_t *attr) {
257 return pthread_attr_init(attr);
260 int frsh_thread_attr_destroy(frsh_thread_attr_t *attr) {
261 return pthread_attr_destroy(attr);
264 int frsh_thread_attr_set_stacksize(frsh_thread_attr_t *attr,
266 return pthread_attr_setstacksize(attr, stacksize);
269 int frsh_thread_attr_get_stacksize (const frsh_thread_attr_t *attr, size_t *stacksize) {
270 return pthread_attr_getstacksize(attr, stacksize);