TNeoKernel  v1.03
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tn_tasks.h
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * TNeoKernel: real-time kernel initially based on TNKernel
4  *
5  * TNKernel: copyright © 2004, 2013 Yuri Tiomkin.
6  * PIC32-specific routines: copyright © 2013, 2014 Anders Montonen.
7  * TNeoKernel: copyright © 2014 Dmitry Frank.
8  *
9  * TNeoKernel was born as a thorough review and re-implementation of
10  * TNKernel. The new kernel has well-formed code, inherited bugs are fixed
11  * as well as new features being added, and it is tested carefully with
12  * unit-tests.
13  *
14  * API is changed somewhat, so it's not 100% compatible with TNKernel,
15  * hence the new name: TNeoKernel.
16  *
17  * Permission to use, copy, modify, and distribute this software in source
18  * and binary forms and its documentation for any purpose and without fee
19  * is hereby granted, provided that the above copyright notice appear
20  * in all copies and that both that copyright notice and this permission
21  * notice appear in supporting documentation.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE DMITRY FRANK AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DMITRY FRANK OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33  * THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  ******************************************************************************/
36 
37 /**
38  * \file
39  *
40  * Various task services: create, sleep, wake up, terminate, etc.
41  *
42  */
43 
44 #ifndef _TN_TASKS_H
45 #define _TN_TASKS_H
46 
47 /*******************************************************************************
48  * INCLUDED FILES
49  ******************************************************************************/
50 
51 #include "tn_sys.h"
52 #include "tn_list.h"
53 #include "tn_common.h"
54 
55 #include "tn_eventgrp.h"
56 #include "tn_dqueue.h"
57 #include "tn_fmem.h"
58 #include "tn_timer.h"
59 
60 
61 
62 #ifdef __cplusplus
63 extern "C" { /*}*/
64 #endif
65 
66 /*******************************************************************************
67  * PUBLIC TYPES
68  ******************************************************************************/
69 
70 /**
71  * Task state
72  */
74  /// This state should never be publicly available.
75  /// It may be stored in task_state only temporarily,
76  /// while some system service is in progress.
78  ///
79  /// Task is ready to run (it doesn't mean that it is running at the moment)
81  ///
82  /// Task is waiting. The reason of waiting can be obtained from
83  /// `task_wait_reason` field of the `struct TN_Task`.
84  ///
85  /// @see `enum #TN_WaitReason`
86  TN_TASK_STATE_WAIT = (1 << 1),
87  ///
88  /// Task is suspended (by some other task)
90  ///
91  /// Task was previously waiting, and after this it was suspended
93  ///
94  /// Task isn't yet activated or it was terminated by `tn_task_terminate()`.
96 };
97 
98 
99 /**
100  * Task wait reason
101  */
103  ///
104  /// task isn't waiting for anything
106  ///
107  /// task has called `tn_task_sleep()`
109  ///
110  /// task waits to acquire a semaphore
111  /// @see tn_sem.h
113  ///
114  /// task waits for some event in the event group to be set
115  /// @see tn_eventgrp.h
117  ///
118  /// task wants to put some data to the data queue, and there's no space
119  /// in the queue.
120  /// @see tn_dqueue.h
122  ///
123  /// task wants to receive some data to the data queue, and there's no data
124  /// in the queue
125  /// @see tn_dqueue.h
127  ///
128  /// task wants to lock a mutex with priority ceiling
129  /// @see tn_mutex.h
131  ///
132  /// task wants to lock a mutex with priority inheritance
133  /// @see tn_mutex.h
135  ///
136  /// task wants to get memory block from memory pool, and there's no free
137  /// memory blocks
138  /// @see tn_fmem.h
140 };
141 
142 /**
143  * Options for `tn_task_create()`
144  */
146  ///
147  /// whether task should be activated right after it is created.
148  /// If this flag is not set, user must activate task manually by calling
149  /// `tn_task_activate()`.
151  ///
152  /// for internal kernel usage only: this option must be provided
153  /// when creating idle task
155 };
156 
157 /**
158  * Options for `tn_task_exit()`
159  */
161  ///
162  /// whether task should be deleted right after it is exited.
163  /// If this flag is not set, user must either delete it manually by
164  /// calling `tn_task_delete()` or re-activate it by calling
165  /// `tn_task_activate()`.
167 };
168 
169 
170 /**
171  * Task
172  */
173 struct TN_Task {
174  /// pointer to task's current top of the stack;
175  /// Note that this field **must** be a first field in the struct,
176  /// this fact is exploited by platform-specific routines.
178  ///
179  /// queue is used to include task in ready/wait lists
180  struct TN_ListItem task_queue;
181  //
182  // queue is used to include task in timer list
183  //struct TN_ListItem timer_queue;
184  ///
185  /// timer object to implement task waiting for timeout
186  struct TN_Timer timer;
187  ///
188  /// pointer to object's (semaphore, mutex, event, etc) wait list in which
189  /// task is included for waiting
190  struct TN_ListItem *pwait_queue;
191  ///
192  /// queue is used to include task in creation list
193  /// (currently, this list is used for statistics only)
194  struct TN_ListItem create_queue;
195 
196 #if TN_USE_MUTEXES
197  ///
198  /// list of all mutexes that are locked by task
199  struct TN_ListItem mutex_queue;
200 #if TN_MUTEX_DEADLOCK_DETECT
201  ///
202  /// list of other tasks involved in deadlock. This list is non-empty
203  /// only in emergency cases, and it is here to help you fix your bug
204  /// that led to deadlock.
205  ///
206  /// @see `#TN_MUTEX_DEADLOCK_DETECT`
207  struct TN_ListItem deadlock_list;
208 #endif
209 #endif
210 
211  /// base top of the stack for this task
213  ///
214  /// size of task's stack (in `sizeof(TN_UWord)`, not bytes)
216  ///
217  /// pointer to task's body function given to `tn_task_create()`
219  ///
220  /// pointer to task's parameter given to `tn_task_create()`
222  ///
223  /// base priority of the task (actual current priority may be higher than
224  /// base priority because of mutex)
226  ///
227  /// current task priority
228  int priority;
229  ///
230  /// id for object validity verification
232  ///
233  /// task state
235  ///
236  /// reason for waiting (relevant if only `task_state` is
237  /// $(TN_TASK_STATE_WAIT) or $(TN_TASK_STATE_WAITSUSP))
239  ///
240  /// waiting result code (reason why waiting finished)
242  //
243  // remaining time until timeout; may be `#TN_WAIT_INFINITE`.
244  //TN_Timeout tick_count;
245  ///
246  /// time slice counter
248 #if 0
249  ///
250  /// last operation result code, might be used if some service
251  /// does not return that code directly
252  int last_rc;
253 #endif
254  ///
255  /// subsystem-specific fields that are used while task waits for something.
256  /// Do note that these fields are grouped by union, so, they must not
257  /// interfere with each other. It's quite ok here because task can't wait
258  /// for different things.
259  union {
260  /// fields specific to tn_eventgrp.h
262  ///
263  /// fields specific to tn_dqueue.h
265  ///
266  /// fields specific to tn_fmem.h
268  } subsys_wait;
269 
270 #if TN_DEBUG
271  /// task name for debug purposes, user may want to set it by hand
272  const char *name;
273 #endif
274 
275  /// Internal flag used to optimize mutex priority algorithms.
276  /// For the comments on it, see file tn_mutex.c,
277  /// function `_mutex_do_unlock()`.
279 
280  /// Flag indicates that task waited for something
281  /// This flag is set automatially in `_tn_task_set_waiting()`
282  /// Must be cleared manually before calling any service that could sleep,
283  /// if the caller is interested in the relevant value of this flag.
284  unsigned waited : 1;
285 
286 
287 // Other implementation specific fields may be added below
288 
289 };
290 
291 
292 
293 /*******************************************************************************
294  * GLOBAL VARIABLES
295  ******************************************************************************/
296 
297 /*******************************************************************************
298  * DEFINITIONS
299  ******************************************************************************/
300 
301 
302 
303 
304 /*******************************************************************************
305  * PUBLIC FUNCTION PROTOTYPES
306  ******************************************************************************/
307 
308 /**
309  * Construct task and probably start it (depends on options, see below).
310  * `id_task` member should not contain `#TN_ID_TASK`, otherwise,
311  * `#TN_RC_WPARAM` is returned.
312  *
313  * Usage example:
314  *
315  * \code{.c}
316  * #define MY_TASK_STACK_SIZE (TN_MIN_STACK_SIZE + 200)
317  * #define MY_TASK_PRIORITY 5
318  *
319  * struct TN_Task my_task;
320  *
321  * //-- define stack array, we use convenience macro TN_STACK_ARR_DEF()
322  * // for that
323  * TN_STACK_ARR_DEF(my_task_stack, MY_TASK_STACK_SIZE);
324  *
325  * void my_task_body(void *param)
326  * {
327  * //-- an endless loop
328  * for (;;){
329  * tn_task_sleep(1);
330  *
331  * //-- probably do something useful
332  * }
333  * }
334  * \endcode
335  *
336  *
337  *
338  * And then, somewhere from other task or from the callback
339  * `#TN_CBUserTaskCreate` given to `tn_sys_start()` :
340  * \code{.c}
341  * enum TN_RCode rc = tn_task_create(
342  * &my_task,
343  * my_task_body,
344  * MY_TASK_PRIORITY,
345  * my_task_stack,
346  * MY_TASK_STACK_SIZE,
347  * NULL, //-- parameter isn't used
348  * TN_TASK_CREATE_OPT_START //-- start task on creation
349  * );
350  *
351  * if (rc != TN_RC_OK){
352  * //-- handle error
353  * }
354  * \endcode
355  *
356  * $(TN_CALL_FROM_TASK)
357  * $(TN_LEGEND_LINK)
358  *
359  * @param task
360  * Ready-allocated `struct TN_Task` structure. `id_task` member should not
361  * contain `#TN_ID_TASK`, otherwise `#TN_RC_WPARAM` is returned.
362  * @param task_func
363  * Pointer to task body function.
364  * @param priority
365  * Priority for new task. **NOTE**: the lower value, the higher priority.
366  * Must be > `0` and < `(#TN_PRIORITIES_CNT - 1)`.
367  * @param task_stack_low_addr
368  * Pointer to the stack for task.
369  * User must either use the macro `TN_STACK_ARR_DEF()` for the definition
370  * of stack array, or allocate it manually as an array of `#TN_UWord` with
371  * `#TN_ARCH_STK_ATTR_BEFORE` and `#TN_ARCH_STK_ATTR_AFTER` macros.
372  * @param task_stack_size
373  * Size of task stack array, in words (`#TN_UWord`), not in bytes.
374  * @param param
375  * Parameter that is passed to `task_func`.
376  * @param opts
377  * Options for task creation, refer to `enum #TN_TaskCreateOpt`
378  *
379  * @return
380  * * `#TN_RC_OK` on success;
381  * * `#TN_RC_WCONTEXT` if called from wrong context;
382  * * `#TN_RC_WPARAM` if wrong params were given;
383  *
384  * @see `#TN_ARCH_STK_ATTR_BEFORE`
385  * @see `#TN_ARCH_STK_ATTR_AFTER`
386  */
388  struct TN_Task *task,
389  TN_TaskBody *task_func,
390  int priority,
391  TN_UWord *task_stack_low_addr,
392  int task_stack_size,
393  void *param,
394  enum TN_TaskCreateOpt opts
395  );
396 
397 
398 /**
399  * If the task is $(TN_TASK_STATE_RUNNABLE), it is moved to the
400  * $(TN_TASK_STATE_SUSPEND) state. If the task is in the $(TN_TASK_STATE_WAIT)
401  * state, it is moved to the $(TN_TASK_STATE_WAITSUSP) state. (waiting +
402  * suspended)
403  *
404  * $(TN_CALL_FROM_TASK)
405  * $(TN_CAN_SWITCH_CONTEXT)
406  * $(TN_LEGEND_LINK)
407  *
408  * @param task Task to suspend
409  *
410  * @return
411  * * `#TN_RC_OK` on success;
412  * * `#TN_RC_WCONTEXT` if called from wrong context;
413  * * `#TN_RC_WSTATE` if task is already suspended or dormant;
414  * * If `#TN_CHECK_PARAM` is non-zero, additional return codes
415  * are available: `#TN_RC_WPARAM` and `#TN_RC_INVALID_OBJ`.
416  *
417  * @see `enum #TN_TaskState`
418  */
419 enum TN_RCode tn_task_suspend(struct TN_Task *task);
420 
421 /**
422  * Release task from $(TN_TASK_STATE_SUSPEND) state. If the given task is in
423  * the $(TN_TASK_STATE_SUSPEND) state, it is moved to $(TN_TASK_STATE_RUNNABLE)
424  * state; afterwards it has the lowest precedence among runnable tasks with the
425  * same priority. If the task is in $(TN_TASK_STATE_WAITSUSP) state, it is
426  * moved to $(TN_TASK_STATE_WAIT) state.
427  *
428  * $(TN_CALL_FROM_TASK)
429  * $(TN_CAN_SWITCH_CONTEXT)
430  * $(TN_LEGEND_LINK)
431  *
432  * @param task Task to release from suspended state
433  *
434  * @return
435  * * `#TN_RC_OK` on success;
436  * * `#TN_RC_WCONTEXT` if called from wrong context;
437  * * `#TN_RC_WSTATE` if task is not suspended;
438  * * If `#TN_CHECK_PARAM` is non-zero, additional return codes
439  * are available: `#TN_RC_WPARAM` and `#TN_RC_INVALID_OBJ`.
440  *
441  * @see enum TN_TaskState
442  */
443 enum TN_RCode tn_task_resume(struct TN_Task *task);
444 
445 /**
446  * Put current task to sleep for at most timeout ticks. When the timeout
447  * expires and the task was not suspended during the sleep, it is switched to
448  * runnable state. If the timeout value is `#TN_WAIT_INFINITE` and the task was
449  * not suspended during the sleep, the task will sleep until another function
450  * call (like `tn_task_wakeup()` or similar) will make it runnable.
451  *
452  * $(TN_CALL_FROM_TASK)
453  * $(TN_CAN_SWITCH_CONTEXT)
454  * $(TN_CAN_SLEEP)
455  * $(TN_LEGEND_LINK)
456  *
457  * @param timeout
458  * Refer to `#TN_Timeout`
459  *
460  * @returns
461  * * `#TN_RC_TIMEOUT` if task has slept specified timeout;
462  * * `#TN_RC_OK` if task was woken up from other task by `tn_task_wakeup()`
463  * * `#TN_RC_FORCED` if task was released from wait forcibly by
464  * `tn_task_release_wait()`
465  * * `#TN_RC_WCONTEXT` if called from wrong context
466  *
467  * @see TN_Timeout
468  */
469 enum TN_RCode tn_task_sleep(TN_Timeout timeout);
470 
471 /**
472  * Wake up task from sleep.
473  *
474  * Task is woken up if only it sleeps because of call to `tn_task_sleep()`.
475  * If task sleeps for some another reason, task won't be woken up,
476  * and `tn_task_wakeup()` returns `#TN_RC_WSTATE`.
477  *
478  * After this call, `tn_task_sleep()` returns `#TN_RC_OK`.
479  *
480  * $(TN_CALL_FROM_TASK)
481  * $(TN_CAN_SWITCH_CONTEXT)
482  * $(TN_LEGEND_LINK)
483  *
484  * @param task sleeping task to wake up
485  *
486  * @return
487  * * `#TN_RC_OK` if successful
488  * * `#TN_RC_WSTATE` if task is not sleeping, or it is sleeping for
489  * some reason other than `tn_task_sleep()` call.
490  * * `#TN_RC_WCONTEXT` if called from wrong context;
491  * * If `#TN_CHECK_PARAM` is non-zero, additional return codes
492  * are available: `#TN_RC_WPARAM` and `#TN_RC_INVALID_OBJ`.
493  *
494  */
495 enum TN_RCode tn_task_wakeup(struct TN_Task *task);
496 
497 /**
498  * The same as `tn_task_wakeup()` but for using in the ISR.
499  *
500  * $(TN_CALL_FROM_ISR)
501  * $(TN_CAN_SWITCH_CONTEXT)
502  * $(TN_LEGEND_LINK)
503  */
504 enum TN_RCode tn_task_iwakeup(struct TN_Task *task);
505 
506 /**
507  * Activate task that is in $(TN_TASK_STATE_DORMANT) state, that is, it was
508  * either just created by `tn_task_create()` without
509  * `#TN_TASK_CREATE_OPT_START` option, or terminated.
510  *
511  * Task is moved from $(TN_TASK_STATE_DORMANT) state to the
512  * $(TN_TASK_STATE_RUNNABLE) state.
513  *
514  * $(TN_CALL_FROM_TASK)
515  * $(TN_CAN_SWITCH_CONTEXT)
516  * $(TN_LEGEND_LINK)
517  *
518  * @param task dormant task to activate
519  *
520  * @return
521  * * `#TN_RC_OK` if successful
522  * * `#TN_RC_WSTATE` if task is not dormant
523  * * `#TN_RC_WCONTEXT` if called from wrong context;
524  * * If `#TN_CHECK_PARAM` is non-zero, additional return codes
525  * are available: `#TN_RC_WPARAM` and `#TN_RC_INVALID_OBJ`.
526  *
527  * @see TN_TaskState
528  */
529 enum TN_RCode tn_task_activate(struct TN_Task *task);
530 
531 /**
532  * The same as `tn_task_activate()` but for using in the ISR.
533  *
534  * $(TN_CALL_FROM_ISR)
535  * $(TN_CAN_SWITCH_CONTEXT)
536  * $(TN_LEGEND_LINK)
537  */
538 enum TN_RCode tn_task_iactivate(struct TN_Task *task);
539 
540 /**
541  * Release task from $(TN_TASK_STATE_WAIT) state, independently of the reason
542  * of waiting.
543  *
544  * If task is in $(TN_TASK_STATE_WAIT) state, it is moved to
545  * $(TN_TASK_STATE_RUNNABLE) state. If task is in $(TN_TASK_STATE_WAITSUSP)
546  * state, it is moved to $(TN_TASK_STATE_SUSPEND) state.
547  *
548  * `#TN_RC_FORCED` is returned to the waiting task.
549  *
550  * $(TN_CALL_FROM_TASK)
551  * $(TN_CAN_SWITCH_CONTEXT)
552  * $(TN_LEGEND_LINK)
553  *
554  * \attention Usage of this function is discouraged, since the need for
555  * it indicates bad software design
556  *
557  * @param task task waiting for anything
558  *
559  * @return
560  * * `#TN_RC_OK` if successful
561  * * `#TN_RC_WSTATE` if task is not waiting for anything
562  * * `#TN_RC_WCONTEXT` if called from wrong context;
563  * * If `#TN_CHECK_PARAM` is non-zero, additional return codes
564  * are available: `#TN_RC_WPARAM` and `#TN_RC_INVALID_OBJ`.
565  *
566  *
567  * @see TN_TaskState
568  */
569 enum TN_RCode tn_task_release_wait(struct TN_Task *task);
570 
571 /**
572  * The same as `tn_task_release_wait()` but for using in the ISR.
573  *
574  * $(TN_CALL_FROM_ISR)
575  * $(TN_CAN_SWITCH_CONTEXT)
576  * $(TN_LEGEND_LINK)
577  */
578 enum TN_RCode tn_task_irelease_wait(struct TN_Task *task);
579 
580 /**
581  * This function terminates the currently running task. The task is moved to
582  * the $(TN_TASK_STATE_DORMANT) state.
583  *
584  * After exiting, the task may be either deleted by the `tn_task_delete()`
585  * function call or reactivated by the `tn_task_activate()` /
586  * `tn_task_iactivate()` function call. In this case task starts execution from
587  * beginning (as after creation/activation). The task will have the lowest
588  * precedence among all tasks with the same priority in the
589  * $(TN_TASK_STATE_RUNNABLE) state.
590  *
591  * If this function is invoked with `#TN_TASK_EXIT_OPT_DELETE` option set, the
592  * task will be deleted after termination and cannot be reactivated (needs
593  * recreation).
594  *
595  * $(TN_CALL_FROM_TASK)
596  * $(TN_CAN_SWITCH_CONTEXT)
597  * $(TN_LEGEND_LINK)
598  *
599  * @return
600  * Returns if only called from wrong context. Normally, it never returns
601  * (since calling task becomes terminated)
602  *
603  * @see `#TN_TASK_EXIT_OPT_DELETE`
604  * @see `tn_task_delete()`
605  * @see `tn_task_activate()`
606  * @see `tn_task_iactivate()`
607  */
608 void tn_task_exit(enum TN_TaskExitOpt opts);
609 
610 
611 /**
612  * This function is similar to `tn_task_exit()` but it terminates any task
613  * other than currently running one.
614  *
615  * After task is terminated, the task may be either deleted by the
616  * `tn_task_delete()` function call or reactivated by the `tn_task_activate()`
617  * / `tn_task_iactivate()` function call. In this case task starts execution
618  * from beginning (as after creation/activation). The task will have the
619  * lowest precedence among all tasks with the same priority in the
620  * $(TN_TASK_STATE_RUNNABLE) state.
621  *
622  * $(TN_CALL_FROM_TASK)
623  * $(TN_CAN_SWITCH_CONTEXT)
624  * $(TN_LEGEND_LINK)
625  *
626  * @param task task to terminate
627  *
628  * @return
629  * * `#TN_RC_OK` if successful
630  * * `#TN_RC_WSTATE` if task is already dormant
631  * * `#TN_RC_WCONTEXT` if called from wrong context;
632  * * If `#TN_CHECK_PARAM` is non-zero, additional return codes
633  * are available: `#TN_RC_WPARAM` and `#TN_RC_INVALID_OBJ`.
634  */
635 enum TN_RCode tn_task_terminate(struct TN_Task *task);
636 
637 /**
638  * This function deletes the task specified by the task. The task must be in
639  * the $(TN_TASK_STATE_DORMANT) state, otherwise `#TN_RC_WCONTEXT` will be
640  * returned.
641  *
642  * This function resets the `id_task` field in the task structure to 0 and
643  * removes the task from the system tasks list. The task can not be reactivated
644  * after this function call (the task must be recreated).
645  *
646  * $(TN_CALL_FROM_TASK)
647  * $(TN_LEGEND_LINK)
648  *
649  * @param task dormant task to delete
650  *
651  * @return
652  * * `#TN_RC_OK` if successful
653  * * `#TN_RC_WSTATE` if task is not dormant
654  * * `#TN_RC_WCONTEXT` if called from wrong context;
655  * * If `#TN_CHECK_PARAM` is non-zero, additional return codes
656  * are available: `#TN_RC_WPARAM` and `#TN_RC_INVALID_OBJ`.
657  *
658  */
659 enum TN_RCode tn_task_delete(struct TN_Task *task);
660 
661 /**
662  * Get current state of the task; note that returned state is a bitmask,
663  * that is, states could be combined with each other.
664  *
665  * Currently, only $(TN_TASK_STATE_WAIT) and $(TN_TASK_STATE_SUSPEND) states
666  * are allowed to be set together. Nevertheless, it would be probably good
667  * idea to test individual bits in the returned value instead of plain
668  * comparing values.
669  *
670  * Note that if something goes wrong, variable pointed to by `p_state`
671  * isn't touched.
672  *
673  * $(TN_CALL_FROM_TASK)
674  * $(TN_LEGEND_LINK)
675  *
676  * @param task
677  * task to get state of
678  * @param p_state
679  * pointer to the location where to store state of the task
680  *
681  * @return state of the task
682  */
684  struct TN_Task *task,
685  enum TN_TaskState *p_state
686  );
687 
688 /**
689  * Set new priority for task.
690  * If priority is 0, then task's base_priority is set.
691  *
692  * $(TN_CALL_FROM_TASK)
693  * $(TN_LEGEND_LINK)
694  *
695  * \attention this function is obsolete and will probably be removed
696  */
697 enum TN_RCode tn_task_change_priority(struct TN_Task *task, int new_priority);
698 
699 
700 
701 #ifdef __cplusplus
702 } /* extern "C" */
703 #endif
704 
705 #endif // _TN_TASKS_H
706 
707 
708 /*******************************************************************************
709  * end of file
710  ******************************************************************************/
711 
712 
enum TN_RCode tn_task_activate(struct TN_Task *task)
Activate task that is in DORMANT state, that is, it was either just created by tn_task_create() witho...
TN_WaitReason
Task wait reason.
Definition: tn_tasks.h:102
task wants to lock a mutex with priority inheritance
Definition: tn_tasks.h:134
const char * name
task name for debug purposes, user may want to set it by hand
Definition: tn_tasks.h:272
enum TN_RCode tn_task_release_wait(struct TN_Task *task)
Release task from WAIT state, independently of the reason of waiting.
enum TN_RCode tn_task_iwakeup(struct TN_Task *task)
The same as tn_task_wakeup() but for using in the ISR.
enum TN_RCode tn_task_terminate(struct TN_Task *task)
This function is similar to tn_task_exit() but it terminates any task other than currently running on...
Task is suspended (by some other task)
Definition: tn_tasks.h:89
enum TN_RCode tn_task_create(struct TN_Task *task, TN_TaskBody *task_func, int priority, TN_UWord *task_stack_low_addr, int task_stack_size, void *param, enum TN_TaskCreateOpt opts)
Construct task and probably start it (depends on options, see below).
Task.
Definition: tn_tasks.h:173
TN_RCode
Result code returned by kernel services.
Definition: tn_common.h:100
DQueue-specific fields related to waiting task, to be included in struct TN_Task. ...
Definition: tn_dqueue.h:140
TN_ObjId
Magic number for object validity verification.
Definition: tn_common.h:87
unsigned long TN_Timeout
The value representing maximum number of system ticks to wait.
Definition: tn_common.h:203
EventGrp-specific fields related to waiting task, to be included in struct TN_Task.
Definition: tn_eventgrp.h:168
enum TN_WaitReason task_wait_reason
reason for waiting (relevant if only task_state is WAIT or WAIT+SUSPEND)
Definition: tn_tasks.h:238
Task was previously waiting, and after this it was suspended.
Definition: tn_tasks.h:92
task wants to receive some data to the data queue, and there's no data in the queue ...
Definition: tn_tasks.h:126
Definitions used through the whole kernel.
enum TN_TaskState task_state
task state
Definition: tn_tasks.h:234
enum TN_RCode tn_task_delete(struct TN_Task *task)
This function deletes the task specified by the task.
Task is waiting.
Definition: tn_tasks.h:86
TN_TaskCreateOpt
Options for tn_task_create()
Definition: tn_tasks.h:145
struct TN_EGrpTaskWait eventgrp
fields specific to tn_eventgrp.h
Definition: tn_tasks.h:261
TN_UWord * base_stack_top
base top of the stack for this task
Definition: tn_tasks.h:212
int stack_size
size of task's stack (in sizeof(TN_UWord), not bytes)
Definition: tn_tasks.h:215
TN_TaskBody * task_func_addr
pointer to task's body function given to tn_task_create()
Definition: tn_tasks.h:218
int tslice_count
time slice counter
Definition: tn_tasks.h:247
enum TN_RCode tn_task_change_priority(struct TN_Task *task, int new_priority)
Set new priority for task.
struct TN_ListItem deadlock_list
list of other tasks involved in deadlock.
Definition: tn_tasks.h:207
Timer is a kernel object that is used to ask the kernel to call some user-provided function at a part...
enum TN_RCode tn_task_wakeup(struct TN_Task *task)
Wake up task from sleep.
TN_TaskState
Task state.
Definition: tn_tasks.h:73
This state should never be publicly available.
Definition: tn_tasks.h:77
Timer.
Definition: tn_timer.h:202
Fixed memory blocks pool.
enum TN_RCode tn_task_suspend(struct TN_Task *task)
If the task is RUNNABLE, it is moved to the SUSPEND state.
void tn_task_exit(enum TN_TaskExitOpt opts)
This function terminates the currently running task.
whether task should be deleted right after it is exited.
Definition: tn_tasks.h:166
enum TN_RCode tn_task_irelease_wait(struct TN_Task *task)
The same as tn_task_release_wait() but for using in the ISR.
enum TN_RCode tn_task_resume(struct TN_Task *task)
Release task from SUSPEND state.
int base_priority
base priority of the task (actual current priority may be higher than base priority because of mutex)...
Definition: tn_tasks.h:225
Event group.
Task isn't yet activated or it was terminated by tn_task_terminate().
Definition: tn_tasks.h:95
task has called tn_task_sleep()
Definition: tn_tasks.h:108
enum TN_ObjId id_task
id for object validity verification
Definition: tn_tasks.h:231
Task is ready to run (it doesn't mean that it is running at the moment)
Definition: tn_tasks.h:80
TN_UWord * stack_top
pointer to task's current top of the stack; Note that this field must be a first field in the struct...
Definition: tn_tasks.h:177
task wants to get memory block from memory pool, and there's no free memory blocks ...
Definition: tn_tasks.h:139
struct TN_DQueueTaskWait dqueue
fields specific to tn_dqueue.h
Definition: tn_tasks.h:264
unsigned waited
Flag indicates that task waited for something This flag is set automatially in _tn_task_set_waiting()...
Definition: tn_tasks.h:284
int priority
current task priority
Definition: tn_tasks.h:228
enum TN_RCode tn_task_iactivate(struct TN_Task *task)
The same as tn_task_activate() but for using in the ISR.
struct TN_ListItem * pwait_queue
pointer to object's (semaphore, mutex, event, etc) wait list in which task is included for waiting ...
Definition: tn_tasks.h:190
struct TN_ListItem task_queue
queue is used to include task in ready/wait lists
Definition: tn_tasks.h:180
for internal kernel usage only: this option must be provided when creating idle task ...
Definition: tn_tasks.h:154
task wants to lock a mutex with priority ceiling
Definition: tn_tasks.h:130
struct TN_FMemTaskWait fmem
fields specific to tn_fmem.h
Definition: tn_tasks.h:267
struct TN_Timer timer
timer object to implement task waiting for timeout
Definition: tn_tasks.h:186
Kernel system routines: system start, tick processing, time slice managing.
union TN_Task::@0 subsys_wait
subsystem-specific fields that are used while task waits for something.
task wants to put some data to the data queue, and there's no space in the queue. ...
Definition: tn_tasks.h:121
enum TN_RCode tn_task_state_get(struct TN_Task *task, enum TN_TaskState *p_state)
Get current state of the task; note that returned state is a bitmask, that is, states could be combin...
void( TN_TaskBody)(void *param)
Prototype for task body function.
Definition: tn_common.h:164
task waits to acquire a semaphore
Definition: tn_tasks.h:112
enum TN_RCode tn_task_sleep(TN_Timeout timeout)
Put current task to sleep for at most timeout ticks.
task isn't waiting for anything
Definition: tn_tasks.h:105
unsigned priority_already_updated
Internal flag used to optimize mutex priority algorithms.
Definition: tn_tasks.h:278
whether task should be activated right after it is created.
Definition: tn_tasks.h:150
void * task_func_param
pointer to task's parameter given to tn_task_create()
Definition: tn_tasks.h:221
FMem-specific fields related to waiting task, to be included in struct TN_Task.
Definition: tn_fmem.h:112
unsigned int TN_UWord
Unsigned integer type whose size is equal to the size of CPU register.
task waits for some event in the event group to be set
Definition: tn_tasks.h:116
A data queue is a FIFO that stores pointer (of type void *) in each cell, called (in uITRON style) a ...
struct TN_ListItem create_queue
queue is used to include task in creation list (currently, this list is used for statistics only) ...
Definition: tn_tasks.h:194
TN_TaskExitOpt
Options for tn_task_exit()
Definition: tn_tasks.h:160
enum TN_RCode task_wait_rc
waiting result code (reason why waiting finished)
Definition: tn_tasks.h:241
struct TN_ListItem mutex_queue
list of all mutexes that are locked by task
Definition: tn_tasks.h:199