1 // -----------------------------------------------------------------------
2 // Copyright (C) 2006 - 2008 FRESCOR consortium partners:
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
11 // Thales Communication S.A. FRANCE
12 // Visual Tools S.A. SPAIN
13 // Rapita Systems Ltd UK
16 // See http://www.frescor.org for a link to partners' websites
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
24 // based on previous work (FSF) done in the FIRST project
26 // Copyright (C) 2005 Mälardalen University, SWEDEN
27 // Scuola Superiore S.Anna, ITALY
28 // Universidad de Cantabria, SPAIN
29 // University of York, UK
31 // FSF API web pages: http://marte.unican.es/fsf/docs
32 // http://shark.sssup.it/contrib/first/docs/
34 // This file is part of FOSA (Frsh Operating System Adaption)
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.
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 //fosa_thread_and_signals.h
56 //==============================================
57 // ******** ****** ******** **********
58 // **///// /** ** **////// /** /**
59 // ** /** ** /** /** /**
60 // ******* /** ** /********* /**********
61 // **//// /** ** ////////** /**//////**
62 // ** /** ** /** /** /**
63 // ** /** ** ******** /** /**
64 // // /******/ //////// // //
66 // FOSA(Frescor Operating System Adaptation layer)
67 //================================================
70 #ifndef FOSA_THREAD_AND_SIGNALS_H_
71 #define FOSA_THREAD_AND_SIGNALS_H_
73 #include "fosa_types.h"
80 * @defgroup threadandsignals Thread and Signals
83 * This module defines the functions that manipulate fosa_threads and
84 * fosa_signals inside FRSH implementation.
86 * Applications can refer to FRSH threads but they cannot create them
87 * directly, instead they must use frsh_thread_create*() which in turn
88 * use fosa_thread_create().
90 * For signals, we assume that the OS provides a direct mapping
91 * for fosa_signal_t and fosa_signal_info_t in the native interface.
98 /*************************
99 * Thread identification
100 *************************/
103 * fosa_thread_equal()
105 * Compare two thread identifiers to determine if they refer to the
108 bool fosa_thread_equal(fosa_thread_id_t t1, fosa_thread_id_t t2);
114 * Return the thread id of the calling thread
116 fosa_thread_id_t fosa_thread_self();
119 /*************************
121 *************************/
124 * fosa_thread_attr_init()
126 * Initialize a thread attributes object
128 * This function initializes the object pointed to by attr to all
129 * the default values defined by FRSH
131 * @return 0 if successful; otherwise it returns \n
132 * FOSA_ENOMEM: insufficient memory exists to initialize the thread
135 int fosa_thread_attr_init(fosa_thread_attr_t *attr);
139 * fosa_thread_attr_destroy()
141 * Destroy a thread attributes object
143 * This function is used to destroy the thread attributes object,
144 * pointed to by attr, and deallocate any system resources allocated for it
148 int fosa_thread_attr_destroy(fosa_thread_attr_t *attr);
152 * fosa_thread_attr_set_stacksize()
154 * Set the thread minimum stack size in a thread attributes object
156 * This function sets the minimum stack size of the thread attributes
157 * object attr to the value given by stacksize, in bytes. This
158 * function has no runtime effect on the stack size, except when the
159 * attributes object is used to create a thread, when it will be
160 * created with the specified minimum stack size
162 * @return 0 if successful, or the following error code:
163 * FOSA_EINVAL: the specified stacksize value is not supported in
164 * this implementation
166 int fosa_thread_attr_set_stacksize(fosa_thread_attr_t *attr,
170 * fosa_thread_attr_get_stacksize()
172 * Get the thread minimum stack size from a thread attributes object
174 * This function sets the variable pointed to by stacksize to the
175 * minimum stack size stored in the thread attributes object attr.
179 int fosa_thread_attr_get_stacksize(const fosa_thread_attr_t *attr,
183 /*************************
184 * Thread creation and termination
185 *************************/
188 * fosa_thread_create()
190 * This function creates a new thread using the attributes specified
191 * in attr. If attr is NULL, default attributes are used. The new
192 * thread starts running immediately, executing the function specified
193 * by code, with an argument equal to arg. Upon successful return, the
194 * variable pointed to by tid will contain the identifier of the newly
195 * created thread. The set of signals that may be synchronously
196 * accepted is inherited from the parent thread.
198 * Returns 0 if successful; otherwise it returs a code error:
200 * FOSA_EAGAIN: the system lacks the necessary resources to create a
201 * new thread or the maximum number of threads has been
204 * FOSA_EINVAL: the value specified by attr is invalid (for instance,
205 * it has not been correctly initialized)
207 * FOSA_EREJECT: the cretion of the thread was rejected by the frsh scheduler
208 * possibly because of incorrect attributes, or because the
209 * requested minimum capacity cannot be guaranteed
212 int fosa_thread_create
213 (fosa_thread_id_t *tid, const fosa_thread_attr_t *attr,
214 fosa_thread_code_t code, void * arg);
218 * Note: no thread termination primitive is provided. The termination
219 * of a thread will be notified by the system to the FRSH scheduler
220 * through the scheduler API
224 /**************************************************
225 * Thread-specific data
226 * (extended with access from a different thread)
228 * Several data items (pointers) may be associated with each thread
229 * Each item is identified through a key, an integer value between 0
230 * and FOSA_MAX_KEYS-1. The caller is responsible of allocating and
231 * deallocating the memory area pointed to by the pointer
232 **************************************************/
237 * Create a new key for thread specific data.
239 * Prior to setting data in a key, we need ask the system to create
240 * one for us. The thread specific data of all the threads is set to
241 * the value NULL until changed to a different value via
242 * fosa_thread_set_specific_data().
244 * @return 0 if successful \n
245 * FOSA_EINVAL If we already have reached the FOSA_MAX_KEYS limit.
246 * FOSA_ENOMEM If there are no enough memory resources to
249 int fosa_key_create(int *key);
256 * This destroys the key and disables its use in the system
258 * @return 0 if successful \n
259 * FOSA_EINVAL The key is not initialised or is not in FOSA key range.
261 int fosa_key_destroy(int key);
265 * fosa_thread_set_specific_data()
267 * Set thread-specific data
269 * For the thread identified by tid, the thread-specifid data field
270 * identified by key will be set to the value specified by value
272 * Returns 0 if successful; otherwise, an error code is returned
273 * FOSA_EINVAL: the value of key is not between 0 and FOSA_MAX_KEYS-1
275 * Alternatively, in case of error the implementation is allowed to
276 * notify it to the system console and then terminate the FRSH
277 * implementation and dependant applications
279 int fosa_thread_set_specific_data
280 (int key, fosa_thread_id_t tid, const void * value);
283 * fosa_thread_get_specific_data()
285 * Get thread-specific data
287 * For the thread identified by tid, the thread-specifid data field
288 * identified by key will be copied to the variable pointed to by value
290 * Returns 0 if successful; otherwise, an error code is returned
291 * FOSA_EINVAL: the value of key is not between 0 and FOSA_MAX_KEYS-1
293 * Alternatively, in case of error the implementation is allowed to
294 * notify it to the system console and then terminate the FRSH
295 * implementation and dependant applications
297 int fosa_thread_get_specific_data(int key, fosa_thread_id_t tid,
301 /******************************************************************
304 * This implementation of FRSH assumes an underlying fixed priority
305 * scheduler with priorities in a range, with a minimum and a
306 * maximumm, a number of priority levels with at least 31
307 * priorities. A larger number implies a larger priority. In systems
308 * in which the underlying scheduler uses the opposite convention, a
309 * mapping is automatically provided by the OS adaptation layer.
310 *******************************************************************/
313 * fosa_get_priority_max()
315 * Return the maximum priority value used in this implementation
317 int fosa_get_priority_max();
320 * fosa_get_priority_min()
322 * Return the minimum priority value used in this implementation
324 int fosa_get_priority_min();
327 * fosa_thread_attr_set_prio()
329 * Change the priority of a thread attributes object
331 * The priority of the thread attriutes object specified by attr is
332 * set to the value specified by prio. This function has no runtime
333 * effect on the priority, except when the attributes object is used
334 * to create a thread, when it will be created with the specified
337 * Returns 0 if successful, or the following error code:
338 * FOSA_EINVAL: the specified priority value is not between the
339 * minimum and the maximum priorities defined in this
340 * FRSH implementation
341 * Alternatively, in case of error the implementation is allowed to
342 * notify it to the system console and then terminate the FRSH
343 * implementation and dependant applications
345 int fosa_thread_attr_set_prio(fosa_thread_attr_t *attr, int prio);
348 * fosa_thread_attr_get_prio()
350 * Get the priority from a thread attributes object
352 * This function sets the variable pointed to by prio to the
353 * priority stored in the thread attributes object attr.
357 int fosa_thread_attr_get_prio
358 (const fosa_thread_attr_t *attr, int *prio);
361 * fosa_thread_set_prio()
363 * Dynamically change the priority of a thread
365 * The priority of the thread identified by tid is
366 * set to the value specified by prio.
368 * Returns 0 if successful, or the following error code:
369 * FOSA_EINVAL: the specified priority value is not between the
370 * minimum and the maximum priorities defined in this
371 * FRSH implementation
372 * Alternatively, in case of error the implementation is allowed to
373 * notify it to the system console and then terminate the FRSH
374 * implementation and dependant applications
376 int fosa_thread_set_prio(fosa_thread_id_t tid, int prio);
379 * fosa_thread_get_prio()
381 * Dynamically get the priority of a thread
383 * This function sets the variable pointed to by prio to the
384 * priority of the thread identified by tid
388 int fosa_thread_get_prio (fosa_thread_id_t tid, int *prio);
392 /*******************************************************************
395 * Signals represent events that may be notified by the system, or
396 * sent explicitly by the application, and for which a thread may
397 * synchronously wait. Signals carry an associated piece of
398 * information (an integer or a pointer) and are queued until they are
399 * accepted. Signals are identified by an integer signal number (of
400 * the type fosa_signal_t) in the range FOSA_SIGNAL_MIN,
401 * FOSA_SIGNAL_MAX. This range is required to have at least <tbd>
403 *******************************************************************/
406 * fosa_set_accepted_signals()
408 * Establish the set of signals that may be synchronously accepted
409 * by the calling thread
411 * The function uses the array of signal numbers specified by set,
412 * which must be of size equal to size
414 * Returns 0 if successful; otherwise it returns an error code:
415 * FOSA_EINVAL: the array contains one or more values which are not
416 * between FOSA_SIGNAL_MIN and FOSA_SIGNAL_MAX, or size
419 * Alternatively, in case of error the implementation is allowed to
420 * notify it to the system console and then terminate the FRSH
421 * implementation and dependant applications
423 int fosa_set_accepted_signals(fosa_signal_t set[], int size);
426 * fosa_signal_queue()
430 * This function is used to explicitly send a signal with a specified
433 * The signal number specified by signal is sent together with the
434 * information specified by info, to the thread identified by
435 * receiver. In those implementations that do not support queueing a
436 * signal with information to a thread (such as POSIX), the signal may
437 * be sent to any thread that is waiting for this signal via
438 * fosa_signal_wait(). Portability can be ensured by having the receiver
439 * thread be the one who is waiting for the signal.
441 * Returns 0 if successful; otherwise it returns an error code:
442 * FOSA_EINVAL: the signal specified by signal is not
443 * between FOSA_SIGNAL_MIN and FOSA_SIGNAL_MAX
445 * FOSA_EAGAIN: no resources are available to queue the signal; the
446 * maximum number of queued signals has been reached, or a
447 * systemwide resource limit has been exceeded
449 * Alternatively, in case of error the implementation is allowed to
450 * notify it to the system console and then terminate the FRSH
451 * implementation and dependant applications
453 int fosa_signal_queue
454 (fosa_signal_t signal, fosa_signal_info_t info,
455 fosa_thread_id_t receiver);
465 * The function waits for the arrival of one of the signals in the
466 * array of signal numbers specified by set, which must be of size
467 * equal to size. If there is a signal already queued, the function
468 * returns immediately. If there is no signal of the specified set
469 * queued, the calling thread is suspended until a signal from that
470 * set arrives. Upon return, if signal_received is not NULL the number
471 * of the signal received is stored in the variable pointed to by
472 * signal_received; and if info is not NULL the associated information
473 * is stored in the variable pointed to by info.
475 * Returns 0 if successful; otherwise it returns an error code:
476 * FOSA_EINVAL: the array contains one or more values which are not
477 * between FOSA_SIGNAL_MIN and FOSA_SIGNAL_MAX, or size
480 * Alternatively, in case of error the implementation is allowed to
481 * notify it to the system console and then terminate the FRSH
482 * implementation and dependant applications
485 (fosa_signal_t set[], int size, fosa_signal_t *signal_received,
486 fosa_signal_info_t *info);
489 * fosa_signal_timedwait()
491 * Timed wait for a signal
493 * This function behaves the same as fosa_signal_wait(), except that
494 * the suspension time is limited to the time interval specified in
495 * the fosa_rel_time_t value referenced by timeout.
497 * Returns 0 if successful; otherwise it returns an error code:
498 * FOSA_EINVAL: the array contains one or more values which are not
499 * between FOSA_SIGNAL_MIN and FOSA_SIGNAL_MAX, or size
500 * is less than 0, or timeout is invalid
501 * FOSA_EAGAIN: The timeout expired
503 * Alternatively, in case of the FOSA_EINVAL error the implementation is
504 * allowed to notify it to the system console and then terminate the
505 * FRSH implementation and dependant applications
507 int fosa_signal_timedwait
508 (fosa_signal_t set[], int size, fosa_signal_t *signal_received,
509 fosa_signal_info_t *info, const fosa_rel_time_t *timeout);
515 #endif /* !FOSA_THREAD_AND_SIGNALS_H_ */