]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - include/fosa_threads_and_signals.h
cc79e61ff45e315a598b97450db1139204eb1c63
[frescor/fosa.git] / include / fosa_threads_and_signals.h
1 //----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2007 by the FRESCOR consortium:
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
17 //
18 //        The 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 // This file is part of FOSA (Frsh Operating System Abstraction)
32 //
33 // FOSA is free software; you can redistribute it and/or modify it
34 // under terms of the GNU General Public License as published by the
35 // Free Software Foundation; either version 2, or (at your option) any
36 // later version.  FOSA is distributed in the hope that it will be
37 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
38 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 // General Public License for more details. You should have received a
40 // copy of the GNU General Public License along with FOSA; see file
41 // COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
42 // Cambridge, MA 02139, USA.
43 //
44 // As a special exception, including FOSA header files in a file,
45 // instantiating FOSA generics or templates, or linking other files
46 // with FOSA objects to produce an executable application, does not
47 // by itself cause the resulting executable application to be covered
48 // by the GNU General Public License. This exception does not
49 // however invalidate any other reasons why the executable file might be
50 // covered by the GNU Public License.
51 // -----------------------------------------------------------------------
52 //fosa_thread_and_signals.h
53 //==============================================
54 //  ********  ******    ********  **********
55 //  **///// /**    **  **//////  /**     /**
56 //  **      /**    ** /**        /**     /**
57 //  ******* /**    ** /********* /**********
58 //  **////  /**    ** ////////** /**//////**
59 //  **      /**    **        /** /**     /**
60 //  **      /**    **  ********  /**     /**
61 //  //       /******/  ////////   //      // 
62 //
63 // FOSA(Frescor Operating System Adaptation layer)
64 //================================================
65
66
67 #ifndef         FOSA_THREAD_AND_SIGNALS_H_
68 #define         FOSA_THREAD_AND_SIGNALS_H_
69
70 #include "fosa_types.h"
71
72 /**
73  * @defgroup threadandsignals Thread and Signals
74  * @ingroup fosa
75  *
76  * This module defines the functions that manipulate frsh_threads and
77  * frsh_signals inside FRSH implementation.
78  *
79  * Applications can refer to FRSH threads but they cannot create them
80  * directly, instead they must use frsh_thread_create*() which in turn
81  * use fosa_thread_create().
82  *
83  * For signals, we assume that the OS provides a direct mapping
84  * for frsh_signal_t and frsh_signal_info_t in the native interface.
85  *
86  * @{
87  **/
88
89
90
91 /*************************
92  * Thread identification
93  *************************/ 
94
95 /**
96  * fosa_thread_equal()
97  *
98  * Compare two thread identifiers to determine if they refer to the 
99  * same thread
100  **/
101 bool fosa_thread_equal(frsh_thread_id_t t1, frsh_thread_id_t t2);
102
103
104 /**
105  * fosa_thread_self()
106  *
107  * Return the thread id of the calling thread
108  **/
109 frsh_thread_id_t fosa_thread_self();
110
111
112 /*************************
113  * Thread creation and termination
114  *************************/ 
115
116 /**
117  * fosa_thread_create()
118  *
119  * This function creates a new thread using the attributes specified
120  * in attr. If attr is NULL, default attributes are used. The new
121  * thread starts running immediately, executing the function specified
122  * by code, with an argument equal to arg. Upon successful return, the
123  * variable pointed to by tid will contain the identifier of the newly
124  * created thread. The set of signals that may be synchronously
125  * accepted is inherited from the parent thread.
126  *
127  * Returns 0 if successful; otherwise it returs a code error:
128  *
129  *     FOSA_EAGAIN: the system lacks the necessary resources to create a
130  *             new thread or the maximum number of threads has been
131  *             reached
132  *
133  *     FOSA_EINVAL: the value specified by attr is invalid (for instance,
134  *              it has not been correctly initialized)
135  *
136  *     FOSA_EREJECT: the cretion of the thread was rejected by the frsh scheduler
137  *               possibly because of incorrect attributes, or because the 
138  *               requested minimum capacity cannot be guaranteed
139  *
140  **/
141  int fosa_thread_create
142     (frsh_thread_id_t *tid, const frsh_thread_attr_t *attr, 
143      frsh_thread_code_t code, void * arg);
144
145
146 /**
147  * Note: no thread termination primitive is provided. The termination
148  * of a thread will be notified by the system to the FRSH scheduler
149  * through the scheduler API
150  **/
151
152
153 /**************************************************
154  * Thread-specific data
155  *  (extended with access from a different thread)
156  *
157  * Several data items (pointers) may be associated with each thread
158  * Each item is identified through a key, an integer value between 0
159  * and FOSA_MAX_KEYS-1. The caller is responsible of allocating and
160  * deallocating the memory area pointed to by the pointer
161  **************************************************/ 
162
163 /**
164  * fosa_key_create()
165  *
166  * Create a new key for thread specific data.
167  *
168  * Prior to setting data in a key, we need ask the system to create
169  * one for us.  The thread specific data of all the threads is set to
170  * the value NULL until changed to a different value via
171  * fosa_thread_set_specific_data().
172  *
173  * @return 0 if successful \n
174  *   FOSA_EINVAL If we already have reached the FOSA_MAX_KEYS limit.
175  *   FOSA_ENOMEM If there are no enough memory resources to 
176  *               create the key.
177  **/
178 int fosa_key_create(int *key);
179
180 /**
181  * fosa_key_destroy()
182  *
183  * Destroy a key
184  *
185  * This destroys the key and disables its use in the system
186  *
187  * @return 0 if successful \n
188  *   FOSA_EINVAL The key is not initialised or is not in FOSA key range.
189  **/
190 int fosa_key_destroy(int key);
191
192
193 /**
194  * fosa_thread_set_specific_data()
195  *
196  * Set thread-specific data
197  *
198  * For the thread identified by tid, the thread-specifid data field
199  * identified by key will be set to the value specified by value
200  *
201  * Returns 0 if successful; otherwise, an error code is returned
202  *     FOSA_EINVAL: the value of key is not between 0 and FOSA_MAX_KEYS-1
203  *
204  * Alternatively, in case of error the implementation is allowed to
205  * notify it to the system console and then terminate the FRSH
206  * implementation and dependant applications
207  **/
208  int fosa_thread_set_specific_data
209        (int key, frsh_thread_id_t tid, const void * value);
210
211 /**
212  * fosa_thread_get_specific_data()
213  *
214  * Get thread-specific data
215  *
216  * For the thread identified by tid, the thread-specifid data field
217  * identified by key will be copied to the variable pointed to by value
218  *
219  * Returns 0 if successful; otherwise, an error code is returned
220  *     FOSA_EINVAL: the value of key is not between 0 and FOSA_MAX_KEYS-1
221  *
222  * Alternatively, in case of error the implementation is allowed to
223  * notify it to the system console and then terminate the FRSH
224  * implementation and dependant applications
225  **/
226 int fosa_thread_get_specific_data(int key, frsh_thread_id_t tid, 
227                                   void ** value);
228
229
230 /******************************************************************
231  * Thread scheduling
232  * 
233  * This implementation of FRSH assumes an underlying fixed priority
234  * scheduler with priorities in a range, with a minimum and a
235  * maximumm, a number of priority levels with at least 31
236  * priorities. A larger number implies a larger priority. In systems
237  * in which the underlying scheduler uses the opposite convention, a
238  * mapping is automatically provided by the OS adaptation layer.
239  *******************************************************************/
240
241 /**
242  * fosa_get_priority_max()
243  *
244  * Return the maximum priority value used in this implementation
245  **/
246 int fosa_get_priority_max();
247
248 /**
249  * fosa_get_priority_min()
250  *
251  * Return the minimum priority value used in this implementation
252  **/
253 int fosa_get_priority_min();
254
255 /**
256  * fosa_thread_attr_set_prio()
257  *
258  * Change the priority of a thread attributes object
259  *
260  * The priority of the thread attriutes object specified by attr is
261  * set to the value specified by prio. This function has no runtime
262  * effect on the priority, except when the attributes object is used
263  * to create a thread, when it will be created with the specified
264  * priority
265  * 
266  * Returns 0 if successful, or the following error code:
267  *    FOSA_EINVAL: the specified priority value is not between the 
268  *            minimum and the maximum priorities defined in this
269  *            FRSH implementation
270  * Alternatively, in case of error the implementation is allowed to
271  * notify it to the system console and then terminate the FRSH
272  * implementation and dependant applications
273  **/
274 int fosa_thread_attr_set_prio(frsh_thread_attr_t *attr, int prio);
275
276 /**
277  * fosa_thread_attr_get_prio()
278  *
279  * Get the priority from a thread attributes object
280  *
281  * This function sets the variable pointed to by prio to the
282  * priority stored in the thread attributes object attr.
283  * 
284  * Returns 0
285  **/
286  int fosa_thread_attr_get_prio
287           (const frsh_thread_attr_t *attr, int *prio);
288
289 /**
290  * fosa_thread_set_prio()
291  *
292  * Dynamically change the priority of a thread
293  *
294  * The priority of the thread identified by tid is
295  * set to the value specified by prio. 
296  * 
297  * Returns 0 if successful, or the following error code:
298  *    FOSA_EINVAL: the specified priority value is not between the 
299  *            minimum and the maximum priorities defined in this
300  *            FRSH implementation
301  * Alternatively, in case of error the implementation is allowed to
302  * notify it to the system console and then terminate the FRSH
303  * implementation and dependant applications
304  **/
305 int fosa_thread_set_prio(frsh_thread_id_t tid, int prio);
306
307 /**
308  * fosa_thread_get_prio()
309  *
310  * Dynamically get the priority of a thread
311  *
312  * This function sets the variable pointed to by prio to the
313  * priority of the thread identified by tid
314  * 
315  * Returns 0
316  **/
317 int fosa_thread_get_prio (frsh_thread_id_t tid, int *prio);
318
319
320
321 /*******************************************************************
322  * Signals
323  *
324  * Signals represent events that may be notified by the system, or
325  * sent explicitly by the application, and for which a thread may
326  * synchronously wait. Signals carry an associated piece of
327  * information (an integer or a pointer) and are queued until they are
328  * accepted.  Signals are identified by an integer signal number (of
329  * the type frsh_signal_t) in the range FOSA_SIGNAL_MIN,
330  * FOSA_SIGNAL_MAX.  This range is required to have at least <tbd>
331  * values.
332  *******************************************************************/
333
334 /**
335  * fosa_set_accepted_signals()
336  *
337  * Establish the set of signals that may be synchronously accepted 
338  * by the calling thread
339  *
340  * The function uses the array of signal numbers specified by set,
341  * which must be of size equal to size
342  *
343  * Returns 0 if successful; otherwise it returns an error code:
344  *     FOSA_EINVAL: the array contains one or more values which are not
345  *             between FOSA_SIGNAL_MIN and FOSA_SIGNAL_MAX, or size
346  *             is less than 0
347  *
348  * Alternatively, in case of error the implementation is allowed to
349  * notify it to the system console and then terminate the FRSH
350  * implementation and dependant applications
351  **/
352 int fosa_set_accepted_signals(frsh_signal_t set[], int size);
353
354 /**
355  * fosa_signal_queue()
356  *
357  * Queue a signal
358  *
359  * This function is used to explicitly send a signal with a specified
360  * value
361  * 
362  * The signal number specified by signal is sent together with the
363  * information specified by info, to the thread identified by
364  * receiver. In those implementations that do not support queueing a
365  * signal with information to a thread (such as POSIX), the signal may
366  * be sent to any thread that is waiting for this signal via
367  * fosa_signal_wait(). Portability can be ensured by having the receiver
368  * thread be the one who is waiting for the signal. 
369  *
370  * Returns 0 if successful; otherwise it returns an error code:
371  *     FOSA_EINVAL: the signal specified by signal is not
372  *              between FOSA_SIGNAL_MIN and FOSA_SIGNAL_MAX
373  *
374  *     FOSA_EAGAIN: no resources are available to queue the signal; the
375  *             maximum number of queued signals has been reached, or a
376  *             systemwide resource limit has been exceeded
377  *
378  * Alternatively, in case of error the implementation is allowed to
379  * notify it to the system console and then terminate the FRSH
380  * implementation and dependant applications
381  **/
382 int fosa_signal_queue
383        (frsh_signal_t signal, frsh_signal_info_t info,
384         frsh_thread_id_t receiver);
385
386
387
388
389 /**
390  * fosa_signal_wait()
391  *
392  * Wait for a signal
393  * 
394  * The function waits for the arrival of one of the signals in the
395  * array of signal numbers specified by set, which must be of size
396  * equal to size. If there is a signal already queued, the function
397  * returns immediately. If there is no signal of the specified set
398  * queued, the calling thread is suspended until a signal from that
399  * set arrives. Upon return, if signal_received is not NULL the number
400  * of the signal received is stored in the variable pointed to by
401  * signal_received; and if info is not NULL the associated information
402  * is stored in the variable pointed to by info.
403  *
404  * Returns 0 if successful; otherwise it returns an error code:
405  *     FOSA_EINVAL: the array contains one or more values which are not
406  *             between FOSA_SIGNAL_MIN and FOSA_SIGNAL_MAX, or size
407  *             is less than 0
408  *
409  * Alternatively, in case of error the implementation is allowed to
410  * notify it to the system console and then terminate the FRSH
411  * implementation and dependant applications
412  **/
413  int fosa_signal_wait
414       (frsh_signal_t set[], int size, frsh_signal_t *signal_received, 
415        frsh_signal_info_t *info);
416
417 /**
418  * fosa_signal_timedwait()
419  *
420  * Timed wait for a signal
421  * 
422  * This function behaves the same as fosa_signal_wait(), except that
423  * the suspension time is limited to the time interval specified in
424  * the timespec structure referenced by timeout.
425  * 
426  * Returns 0 if successful; otherwise it returns an error code:
427  *     FOSA_EINVAL: the array contains one or more values which are not
428  *             between FOSA_SIGNAL_MIN and FOSA_SIGNAL_MAX, or size
429  *             is less than 0, or timeout is invalid
430  *     FOSA_EAGAIN: The timeout expired
431  *
432  * Alternatively, in case of the FOSA_EINVAL error the implementation is
433  * allowed to notify it to the system console and then terminate the
434  * FRSH implementation and dependant applications
435  **/
436  int fosa_signal_timedwait
437       (frsh_signal_t set[], int size, frsh_signal_t *signal_received, 
438        frsh_signal_info_t *info, const struct timespec *timeout);
439
440 /*}*/
441
442
443 #endif      /* !FOSA_THREAD_AND_SIGNALS_H_ */