]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_rtlinux/fosa_threads_and_signals.c
rtlinux version of fosa_app_def_sched.c already compiles
[frescor/fosa.git] / src_rtlinux / fosa_threads_and_signals.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.h>
61
62 /*************************
63  * Thread identification
64  *************************/ 
65
66 bool fosa_thread_equal(frsh_thread_id_t t1, frsh_thread_id_t t2){
67      return pthread_equal(t1,t2);
68 }
69
70 frsh_thread_id_t fosa_thread_self(){
71      return pthread_self();
72 }
73
74 /*************************
75  * Thread creation and termination
76  *************************/ 
77
78 int fosa_thread_create (frsh_thread_id_t *tid, const frsh_thread_attr_t *attr, 
79                         frsh_thread_code_t code, void * arg){
80      return pthread_create(tid,attr,code, arg);
81 }
82
83
84 /**************************************************
85  * Thread-specific data
86  *  (extended with access from a different thread)
87  *
88  * Several data items (pointers) may be associated with each thread
89  * Each item is identified through a key, an integer value between 0
90  * and FOSA_MAX_KEYS-1. The caller is responsible of allocating and
91  * deallocating the memory area pointed to by the pointer
92  **************************************************/ 
93
94 int fosa_thread_set_specific_data (int key, frsh_thread_id_t tid, 
95                                            const void * value){
96      if ((0<key) && (key<FOSA_MAX_KEYS-1)){
97        tid->tsd[key] = (void *) value;
98        return 0;
99      }
100      return EINVAL;
101 }
102
103 int fosa_thread_get_specific_data(int key, frsh_thread_id_t tid, 
104                                   void ** value){
105      if ((0<key) && (key<FOSA_MAX_KEYS-1)){
106        //*value=pthread_remote_getspecific(key,tid);
107        *value=tid->tsd[key];
108        return 0;
109      }
110      return EINVAL;
111
112 }
113
114
115 /******************************************************************
116  * Thread scheduling
117  * 
118  * This implementation of FRSH assumes an underlying fixed priority
119  * scheduler with priorities in a range, with a minimum and a
120  * maximumm, a number of priority levels with at least 31
121  * priorities. A larger number implies a larger priority. In systems
122  * in which the underlying scheduler uses the opposite convention, a
123  * mapping is automatically provided by the OS adaptation layer.
124  *******************************************************************/
125
126 int fosa_get_priority_max() {
127      return sched_get_priority_max(0);
128 }
129
130 int fosa_get_priority_min(){
131      return sched_get_priority_min(0);
132 }
133
134 int fosa_thread_attr_set_prio(frsh_thread_attr_t *attr, int prio) {
135   if ((sched_get_priority_min(0)<=prio) || (prio<=sched_get_priority_min(0))){
136     attr->sched_param.sched_priority = prio;
137     return 0;
138   }
139   return EINVAL;
140 }
141
142 int fosa_thread_attr_get_prio (const frsh_thread_attr_t *attr, int *prio){
143      *prio = attr->sched_param.sched_priority;
144      return 0;
145 }
146
147 int fosa_thread_set_prio(frsh_thread_id_t tid, int prio){
148   if ((sched_get_priority_min(0)<=prio) || (prio<=sched_get_priority_min(0))){
149     pthread_setschedprio(tid,prio);
150     return 0;
151   }
152   return EINVAL;
153 }
154
155 int fosa_thread_get_prio (frsh_thread_id_t tid, int *prio){
156      *prio = tid->sched_param.sched_priority;
157      return 0;
158 }
159
160
161
162 /*******************************************************************
163  * Signals
164  *
165  * Signals represent events that may be notified by the system, or
166  * sent explicitly by the application, and for which a thread may
167  * synchronously wait. Signals carry an associated piece of
168  * information (an integer or a pointer) and are queued until they are
169  * accepted.  Signals are identified by an integer signal number (of
170  * the type frsh_signal_t) in the range FOSA_SIGNAL_MIN,
171  * FOSA_SIGNAL_MAX.  This range is required to have at least <tbd>
172  * values.
173  *******************************************************************/
174
175 int fosa_set_accepted_signals(frsh_signal_t set[], int size) {
176      int x;
177      // MISSED
178      rtl_sigset_t bitset;
179      
180      rtl_sigfillset(&bitset); // By default all signals will be blocked.
181      for (x=0; x<size; x++)
182           rtl_sigdelset(&bitset, set[x]); // Unblock the "set" of signals.
183      
184      return pthread_sigmask(SIG_SETMASK, &bitset, NULL);
185 }
186
187
188 int fosa_signal_queue (frsh_signal_t signal, frsh_signal_info_t info,
189                        frsh_thread_id_t receiver){
190      union sigval value;
191      
192      value.sival_ptr=info.sival_ptr;
193      return sigqueue(0, signal, value);
194 }
195
196 int fosa_signal_queue_scheduler(frsh_signal_t signal, frsh_signal_info_t info) {
197      union posix_appsched_eventinfo eventinfo;
198      eventinfo.sig.signo=signal;
199      eventinfo.sig.siginfo= (siginfo_t){info.sival_int};
200      generate_event(pthread_self(), fosa_scheduler_thread, 
201                     POSIX_APPSCHED_SIGNAL, &eventinfo, 
202                     sizeof(union posix_appsched_eventinfo));
203      return 0;
204 }
205
206 int fosa_signal_wait (frsh_signal_t set[], int size, frsh_signal_t *signal_received, 
207                                     frsh_signal_info_t *info){
208   int x;
209   rtl_sigset_t bitset;
210   siginfo_t __info;
211   
212   rtl_sigemptyset(&bitset); // No signals to wait for;
213   for (x=0; x<size; x++)
214     rtl_sigaddset(&bitset, set[x]); // Add to the set of signals to be waited for.
215   sigwaitinfo(&bitset, &__info);
216   *signal_received=__info.si_signo;
217   //*info = __info.si_value.sival_ptr;
218   return 0;
219 }
220
221 int fosa_signal_timedwait (frsh_signal_t set[], int size, frsh_signal_t *signal_received, frsh_signal_info_t *info, const struct timespec *timeout){
222      
223      
224      int x;
225      rtl_sigset_t bitset;
226      siginfo_t __info;
227      
228      rtl_sigemptyset(&bitset); // No signals to wait for;
229      for (x=0; x<size; x++)
230           rtl_sigaddset(&bitset, set[x]); // Add to the set of signals to be waited for.
231     sigtimedwait(&bitset, &__info, timeout);
232
233      *signal_received=__info.si_signo;
234 //     *info = __info.si_value.sival_ptr;
235      return 0;
236 }
237
238
239