]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_rtlinux/fosa_threads_and_signals.c
Updating header text in FOSA files for the incoming final project
[frescor/fosa.git] / src_rtlinux / fosa_threads_and_signals.c
1 // -----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2009 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. Politécnica  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 //
24 //  based on previous work (FSF) done in the FIRST project
25 //
26 //   Copyright (C) 2005  Mälardalen University, SWEDEN
27 //                       Scuola Superiore S.Anna, ITALY
28 //                       Universidad de Cantabria, SPAIN
29 //                       University of York, UK
30 //
31 //   FSF API web pages: http://marte.unican.es/fsf/docs
32 //                      http://shark.sssup.it/contrib/first/docs/
33 //
34 //   This file is part of FOSA (Frsh Operating System Adaption)
35 //
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.
46 //
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 //  //       /******/  ////////   //      // 
64 //
65 // FOSA(Frescor Operating System Adaptation layer)
66 //================================================
67
68 #include <fosa.h>
69 #include <asm/bitops.h>
70
71 /*************************
72  * Thread identification
73  *************************/ 
74
75 bool fosa_thread_equal(frsh_thread_id_t t1, frsh_thread_id_t t2){
76      return pthread_equal(t1,t2);
77 }
78
79 frsh_thread_id_t fosa_thread_self(){
80      return pthread_self();
81 }
82
83 /*************************
84  * Thread creation and termination
85  *************************/ 
86
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);
90 }
91
92 int fosa_key_create(int *key) {
93     int idx=ffs(pthread_self()->tsd_bitmap);
94
95     if (idx<0)
96         return -FOSA_EINVAL;
97
98     pthread_self()->tsd_bitmap&=~(1<<idx);
99
100     *key=idx;
101     return 0;
102 }
103
104 int fosa_key_destroy(int key) {
105     pthread_self()->tsd_bitmap|=(1<<key);
106     return 0;
107 }
108
109
110 /**************************************************
111  * Thread-specific data
112  *  (extended with access from a different thread)
113  *
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  **************************************************/ 
119
120 int fosa_thread_set_specific_data (int key, frsh_thread_id_t tid, 
121                                            const void * value){
122      if ((0<key) && (key<FOSA_MAX_KEYS-1)){
123        tid->tsd[key] = (void *) value;
124        return 0;
125      }
126      return EINVAL;
127 }
128
129 int fosa_thread_get_specific_data(int key, frsh_thread_id_t tid, 
130                                   void ** value){
131      if ((0<key) && (key<FOSA_MAX_KEYS-1)){
132        //*value=pthread_remote_getspecific(key,tid);
133        *value=tid->tsd[key];
134        return 0;
135      }
136      return EINVAL;
137
138 }
139
140
141 /******************************************************************
142  * Thread scheduling
143  * 
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  *******************************************************************/
151
152 int fosa_get_priority_max() {
153      return sched_get_priority_max(0);
154 }
155
156 int fosa_get_priority_min(){
157      return sched_get_priority_min(0);
158 }
159
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;
163     return 0;
164   }
165   return EINVAL;
166 }
167
168 int fosa_thread_attr_get_prio (const frsh_thread_attr_t *attr, int *prio){
169      *prio = attr->sched_param.sched_priority;
170      return 0;
171 }
172
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);
176     return 0;
177   }
178   return EINVAL;
179 }
180
181 int fosa_thread_get_prio (frsh_thread_id_t tid, int *prio){
182      *prio = tid->sched_param.sched_priority;
183      return 0;
184 }
185
186
187
188 /*******************************************************************
189  * Signals
190  *
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>
198  * values.
199  *******************************************************************/
200
201 int fosa_set_accepted_signals(frsh_signal_t set[], int size) {
202      int x;
203      rtl_sigset_t bitset;
204      
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.
208      
209      return pthread_sigmask(SIG_SETMASK, &bitset, NULL);
210 }
211
212
213 int fosa_signal_queue (frsh_signal_t signal, frsh_signal_info_t info,
214                        frsh_thread_id_t receiver){
215      union sigval value;
216      
217      value.sival_ptr=info.sival_ptr;
218      return sigqueue(0, signal, value);
219 }
220
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));
228      rtl_schedule();
229      return 0;
230 }
231
232 int fosa_signal_wait (frsh_signal_t set[], int size, frsh_signal_t *signal_received, 
233                                     frsh_signal_info_t *info){
234   int x;
235   rtl_sigset_t bitset;
236   siginfo_t __info;
237   
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;
244   return 0;
245 }
246
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){
248      
249      
250      int x;
251      rtl_sigset_t bitset;
252      siginfo_t __info;
253      
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);
258
259      *signal_received=__info.si_signo;
260 //     *info = __info.si_value.sival_ptr;
261      return 0;
262 }
263
264 int frsh_thread_attr_init(frsh_thread_attr_t *attr) {
265     return pthread_attr_init(attr);
266 }
267
268 int frsh_thread_attr_destroy(frsh_thread_attr_t *attr) {
269     return pthread_attr_destroy(attr);
270 }
271
272 int frsh_thread_attr_set_stacksize(frsh_thread_attr_t *attr, 
273                                    size_t stacksize) {
274     return pthread_attr_setstacksize(attr, stacksize);
275 }
276
277 int frsh_thread_attr_get_stacksize (const frsh_thread_attr_t *attr, size_t *stacksize) {
278     return pthread_attr_getstacksize(attr, stacksize);
279 }