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 //================================================
69 #include <asm/bitops.h>
71 /*************************
72 * Thread identification
73 *************************/
75 bool fosa_thread_equal(frsh_thread_id_t t1, frsh_thread_id_t t2){
76 return pthread_equal(t1,t2);
79 frsh_thread_id_t fosa_thread_self(){
80 return pthread_self();
83 /*************************
84 * Thread creation and termination
85 *************************/
87 int fosa_thread_create (frsh_thread_id_t *tid, const frsh_thread_attr_t *attr,
88 frsh_thread_code_t code, void * arg){
89 return pthread_create(tid,attr,code, arg);
92 int fosa_key_create(int *key) {
93 int idx=ffs(pthread_self()->tsd_bitmap);
98 pthread_self()->tsd_bitmap&=~(1<<idx);
104 int fosa_key_destroy(int key) {
105 pthread_self()->tsd_bitmap|=(1<<key);
110 /**************************************************
111 * Thread-specific data
112 * (extended with access from a different thread)
114 * Several data items (pointers) may be associated with each thread
115 * Each item is identified through a key, an integer value between 0
116 * and FOSA_MAX_KEYS-1. The caller is responsible of allocating and
117 * deallocating the memory area pointed to by the pointer
118 **************************************************/
120 int fosa_thread_set_specific_data (int key, frsh_thread_id_t tid,
122 if ((0<key) && (key<FOSA_MAX_KEYS-1)){
123 tid->tsd[key] = (void *) value;
129 int fosa_thread_get_specific_data(int key, frsh_thread_id_t tid,
131 if ((0<key) && (key<FOSA_MAX_KEYS-1)){
132 //*value=pthread_remote_getspecific(key,tid);
133 *value=tid->tsd[key];
141 /******************************************************************
144 * This implementation of FRSH assumes an underlying fixed priority
145 * scheduler with priorities in a range, with a minimum and a
146 * maximumm, a number of priority levels with at least 31
147 * priorities. A larger number implies a larger priority. In systems
148 * in which the underlying scheduler uses the opposite convention, a
149 * mapping is automatically provided by the OS adaptation layer.
150 *******************************************************************/
152 int fosa_get_priority_max() {
153 return sched_get_priority_max(0);
156 int fosa_get_priority_min(){
157 return sched_get_priority_min(0);
160 int fosa_thread_attr_set_prio(frsh_thread_attr_t *attr, int prio) {
161 if ((sched_get_priority_min(0)<=prio) || (prio<=sched_get_priority_min(0))){
162 attr->sched_param.sched_priority = prio;
168 int fosa_thread_attr_get_prio (const frsh_thread_attr_t *attr, int *prio){
169 *prio = attr->sched_param.sched_priority;
173 int fosa_thread_set_prio(frsh_thread_id_t tid, int prio){
174 if ((sched_get_priority_min(0)<=prio) || (prio<=sched_get_priority_min(0))){
175 pthread_setschedprio(tid,prio);
181 int fosa_thread_get_prio (frsh_thread_id_t tid, int *prio){
182 *prio = tid->sched_param.sched_priority;
188 /*******************************************************************
191 * Signals represent events that may be notified by the system, or
192 * sent explicitly by the application, and for which a thread may
193 * synchronously wait. Signals carry an associated piece of
194 * information (an integer or a pointer) and are queued until they are
195 * accepted. Signals are identified by an integer signal number (of
196 * the type frsh_signal_t) in the range FOSA_SIGNAL_MIN,
197 * FOSA_SIGNAL_MAX. This range is required to have at least <tbd>
199 *******************************************************************/
201 int fosa_set_accepted_signals(frsh_signal_t set[], int size) {
205 rtl_sigfillset(&bitset); // By default all signals will be blocked.
206 for (x=0; x<size; x++)
207 rtl_sigdelset(&bitset, set[x]); // Unblock the "set" of signals.
209 return pthread_sigmask(SIG_SETMASK, &bitset, NULL);
213 int fosa_signal_queue (frsh_signal_t signal, frsh_signal_info_t info,
214 frsh_thread_id_t receiver){
217 value.sival_ptr=info.sival_ptr;
218 return sigqueue(0, signal, value);
221 int fosa_signal_queue_scheduler(frsh_signal_t signal, frsh_signal_info_t info) {
222 union posix_appsched_eventinfo eventinfo;
223 eventinfo.sig.signo=signal;
224 eventinfo.sig.siginfo= (siginfo_t){info.sival_int};
225 generate_event(pthread_self(), fosa_scheduler_thread,
226 POSIX_APPSCHED_SIGNAL, &eventinfo,
227 sizeof(union posix_appsched_eventinfo));
232 int fosa_signal_wait (frsh_signal_t set[], int size, frsh_signal_t *signal_received,
233 frsh_signal_info_t *info){
238 rtl_sigemptyset(&bitset); // No signals to wait for;
239 for (x=0; x<size; x++)
240 rtl_sigaddset(&bitset, set[x]); // Add to the set of signals to be waited for.
241 sigwaitinfo(&bitset, &__info);
242 *signal_received=__info.si_signo;
243 //*info = __info.si_value.sival_ptr;
247 int fosa_signal_timedwait (frsh_signal_t set[], int size, frsh_signal_t *signal_received, frsh_signal_info_t *info, const struct timespec *timeout){
254 rtl_sigemptyset(&bitset); // No signals to wait for;
255 for (x=0; x<size; x++)
256 rtl_sigaddset(&bitset, set[x]); // Add to the set of signals to be waited for.
257 sigtimedwait(&bitset, &__info, timeout);
259 *signal_received=__info.si_signo;
260 // *info = __info.si_value.sival_ptr;
264 int frsh_thread_attr_init(frsh_thread_attr_t *attr) {
265 return pthread_attr_init(attr);
268 int frsh_thread_attr_destroy(frsh_thread_attr_t *attr) {
269 return pthread_attr_destroy(attr);
272 int frsh_thread_attr_set_stacksize(frsh_thread_attr_t *attr,
274 return pthread_attr_setstacksize(attr, stacksize);
277 int frsh_thread_attr_get_stacksize (const frsh_thread_attr_t *attr, size_t *stacksize) {
278 return pthread_attr_getstacksize(attr, stacksize);