]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_rtlinux/fosa_threads_and_signals.c
226708387f34e03f4a735534b2172f1e4520cfe1
[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 #include <asm/bitops.h>
62
63 /*************************
64  * Thread identification
65  *************************/ 
66
67 bool fosa_thread_equal(frsh_thread_id_t t1, frsh_thread_id_t t2){
68      return pthread_equal(t1,t2);
69 }
70
71 frsh_thread_id_t fosa_thread_self(){
72      return pthread_self();
73 }
74
75 /*************************
76  * Thread creation and termination
77  *************************/ 
78
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);
82 }
83
84 int fosa_key_create(int *key) {
85     int idx=ffs(pthread_self()->tsd_bitmap);
86
87     if (idx<0)
88         return -FOSA_EINVAL;
89
90     pthread_self()->tsd_bitmap&=~(1<<idx);
91
92     *key=idx;
93     return 0;
94 }
95
96 int fosa_key_destroy(int key) {
97     pthread_self()->tsd_bitmap|=(1<<key);
98     return 0;
99 }
100
101
102 /**************************************************
103  * Thread-specific data
104  *  (extended with access from a different thread)
105  *
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  **************************************************/ 
111
112 int fosa_thread_set_specific_data (int key, frsh_thread_id_t tid, 
113                                            const void * value){
114      if ((0<key) && (key<FOSA_MAX_KEYS-1)){
115        tid->tsd[key] = (void *) value;
116        return 0;
117      }
118      return EINVAL;
119 }
120
121 int fosa_thread_get_specific_data(int key, frsh_thread_id_t tid, 
122                                   void ** value){
123      if ((0<key) && (key<FOSA_MAX_KEYS-1)){
124        //*value=pthread_remote_getspecific(key,tid);
125        *value=tid->tsd[key];
126        return 0;
127      }
128      return EINVAL;
129
130 }
131
132
133 /******************************************************************
134  * Thread scheduling
135  * 
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  *******************************************************************/
143
144 int fosa_get_priority_max() {
145      return sched_get_priority_max(0);
146 }
147
148 int fosa_get_priority_min(){
149      return sched_get_priority_min(0);
150 }
151
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;
155     return 0;
156   }
157   return EINVAL;
158 }
159
160 int fosa_thread_attr_get_prio (const frsh_thread_attr_t *attr, int *prio){
161      *prio = attr->sched_param.sched_priority;
162      return 0;
163 }
164
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);
168     return 0;
169   }
170   return EINVAL;
171 }
172
173 int fosa_thread_get_prio (frsh_thread_id_t tid, int *prio){
174      *prio = tid->sched_param.sched_priority;
175      return 0;
176 }
177
178
179
180 /*******************************************************************
181  * Signals
182  *
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>
190  * values.
191  *******************************************************************/
192
193 int fosa_set_accepted_signals(frsh_signal_t set[], int size) {
194      int x;
195      rtl_sigset_t bitset;
196      
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.
200      
201      return pthread_sigmask(SIG_SETMASK, &bitset, NULL);
202 }
203
204
205 int fosa_signal_queue (frsh_signal_t signal, frsh_signal_info_t info,
206                        frsh_thread_id_t receiver){
207      union sigval value;
208      
209      value.sival_ptr=info.sival_ptr;
210      return sigqueue(0, signal, value);
211 }
212
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));
220      rtl_schedule();
221      return 0;
222 }
223
224 int fosa_signal_wait (frsh_signal_t set[], int size, frsh_signal_t *signal_received, 
225                                     frsh_signal_info_t *info){
226   int x;
227   rtl_sigset_t bitset;
228   siginfo_t __info;
229   
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;
236   return 0;
237 }
238
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){
240      
241      
242      int x;
243      rtl_sigset_t bitset;
244      siginfo_t __info;
245      
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);
250
251      *signal_received=__info.si_signo;
252 //     *info = __info.si_value.sival_ptr;
253      return 0;
254 }
255
256 int frsh_thread_attr_init(frsh_thread_attr_t *attr) {
257     return pthread_attr_init(attr);
258 }
259
260 int frsh_thread_attr_destroy(frsh_thread_attr_t *attr) {
261     return pthread_attr_destroy(attr);
262 }
263
264 int frsh_thread_attr_set_stacksize(frsh_thread_attr_t *attr, 
265                                    size_t stacksize) {
266     return pthread_attr_setstacksize(attr, stacksize);
267 }
268
269 int frsh_thread_attr_get_stacksize (const frsh_thread_attr_t *attr, size_t *stacksize) {
270     return pthread_attr_getstacksize(attr, stacksize);
271 }