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