1 // -----------------------------------------------------------------------
2 // Copyright (C) 2006 - 2007 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. Politecnica 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
23 // This file is part of the FRSH implementation
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)
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.
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
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
46 // -----------------------------------------------------------------------
47 //fosa_thread_and_signals.h
48 //==============================================
49 // ******** ****** ******** **********
50 // **///// /** ** **////// /** /**
51 // ** /** ** /** /** /**
52 // ******* /** ** /********* /**********
53 // **//// /** ** ////////** /**//////**
54 // ** /** ** /** /** /**
55 // ** /** ** ******** /** /**
56 // // /******/ //////// // //
58 // FOSA(Frescor Operating System Adaptation layer)
59 //================================================
62 #ifndef FOSA_THREAD_AND_SIGNALS_H_
63 #define FOSA_THREAD_AND_SIGNALS_H_
66 * @defgroup threadandsignals Thread and Signals
69 * This module defines the functions that manipulate frsh_threads and
70 * frsh_signals inside FRSH implementation.
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().
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.
84 /*************************
85 * Thread identification
86 *************************/
91 * Compare two thread identifiers to determine if they refer to the
94 bool fosa_thread_equal(frsh_thread_id_t t1, frsh_thread_id_t t2);
100 * Return the thread id of the calling thread
102 frsh_thread_id_t fosa_thread_self();
105 /*************************
106 * Thread creation and termination
107 *************************/
110 * fosa_thread_create()
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.
120 * Returns 0 if successful; otherwise it returs a code error:
122 * EAGAIN: the system lacks the necessary resources to create a
123 * new thread or the maximum number of threads has been
126 * EINVAL: the value specified by attr is invalid (for instance,
127 * it has not been correctly initialized)
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
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);
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
146 /**************************************************
147 * Thread-specific data
148 * (extended with access from a different thread)
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 **************************************************/
159 * Create a new key for thread specific data.
161 * Prior to setting data in a key, we need ask the system to create
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
169 int fosa_key_create(int *key);
176 * This destroys the key and isables its use in the system
178 * @return 0 if successful \n
179 * FOSA_EINVAL The key is not initialised or is not in FOSA key range.
181 int fosa_key_destroy(int key);
185 * fosa_thread_set_specific_data()
187 * Set thread-specific data
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
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
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
199 int fosa_thread_set_specific_data
200 (int key, frsh_thread_id_t tid, const void * value);
203 * fosa_thread_get_specific_data()
205 * Get thread-specific data
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
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
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
217 int fosa_thread_get_specific_data(int key, frsh_thread_id_t tid,
221 /******************************************************************
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 *******************************************************************/
233 * fosa_get_priority_max()
235 * Return the maximum priority value used in this implementation
237 int fosa_get_priority_max();
240 * fosa_get_priority_min()
242 * Return the minimum priority value used in this implementation
244 int fosa_get_priority_min();
247 * fosa_thread_attr_set_prio()
249 * Change the priority of a thread attributes object
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
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
265 int fosa_thread_attr_set_prio(frsh_thread_attr_t *attr, int prio);
268 * fosa_thread_attr_get_prio()
270 * Get the priority from a thread attributes object
272 * This function sets the variable pointed to by prio to the
273 * priority stored in the thread attributes object attr.
277 int fosa_thread_attr_get_prio
278 (const frsh_thread_attr_t *attr, size_t *prio);
281 * fosa_thread_set_prio()
283 * Dynamically change the priority of a thread
285 * The priority of the thread identified by tid is
286 * set to the value specified by prio.
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
296 int fosa_thread_set_prio(frsh_thread_id_t tid, int prio);
299 * fosa_thread_get_prio()
301 * Dynamically get the priority of a thread
303 * This function sets the variable pointed to by prio to the
304 * priority of the thread identified by tid
308 int fosa_thread_get_prio (frsh_thread_id_t tid, int *prio);
312 /*******************************************************************
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>
323 *******************************************************************/
326 * fosa_set_accepted_signals()
328 * Establish the set of signals that may be synchronously accepted
329 * by the calling thread
331 * The function uses the array of signal numbers specified by set,
332 * which must be of size equal to size
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
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
343 int fosa_set_accepted_signals(frsh_signal_t set[], int size);
346 * fosa_signal_queue()
350 * This function is used to explicitly send a signal with a specified
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.
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
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
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
373 int fosa_signal_queue
374 (frsh_signal_t signal, frsh_signal_info_t info,
375 frsh_thread_id_t receiver);
379 * fosa_signal_queue_scheduler()
381 * Queue a signal destinated to the scheduler
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.
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.
391 * Besides for the scheduler we don't have always a destinator
392 * thread_id needed in frsh_signal_queue for OSE.
394 * So the fosa implementation will solve this issue internally.
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
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
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
408 int fosa_signal_queue_scheduler(frsh_signal_t signal, frsh_signal_info_t info);
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.
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
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
436 (frsh_signal_t set[], int size, frsh_signal_t *signal_received,
437 frsh_signal_info_t *info);
440 * fosa_signal_timedwait()
442 * Timed wait for a signal
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.
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
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
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);
465 #endif /* !FOSA_THREAD_AND_SIGNALS_H_ */