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