Tizen RT Libs&Environment  v1.1 D4
sched.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific
15  * language governing permissions and limitations under the License.
16  *
17  ****************************************************************************/
18 
19 /********************************************************************************
20  *
21  * Copyright (C) 2007-2014 Gregory Nutt. All rights reserved.
22  * Author: Gregory Nutt <gnutt@nuttx.org>
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
27  *
28  * 1. Redistributions of source code must retain the above copyright
29  * notice, this list of conditions and the following disclaimer.
30  * 2. Redistributions in binary form must reproduce the above copyright
31  * notice, this list of conditions and the following disclaimer in
32  * the documentation and/or other materials provided with the
33  * distribution.
34  * 3. Neither the name NuttX nor the names of its contributors may be
35  * used to endorse or promote products derived from this software
36  * without specific prior written permission.
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
41  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
42  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
43  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
44  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
45  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
46  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
48  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49  * POSSIBILITY OF SUCH DAMAGE.
50  *
51  ********************************************************************************/
53 
54 #ifndef __INCLUDE_TINYARA_SCHED_H
55 #define __INCLUDE_TINYARA_SCHED_H
56 
57 /********************************************************************************
58  * Included Files
59  ********************************************************************************/
60 
61 #include <tinyara/config.h>
62 
63 #include <sys/types.h>
64 #include <stdint.h>
65 #include <queue.h>
66 #include <signal.h>
67 #include <semaphore.h>
68 #include <pthread.h>
69 #include <mqueue.h>
70 #include <time.h>
71 
72 #include <tinyara/irq.h>
73 #include <tinyara/mm/shm.h>
74 #include <tinyara/fs/fs.h>
75 #include <tinyara/net/net.h>
76 
77 #include <arch/arch.h>
78 
79 /********************************************************************************
80  * Pre-processor Definitions
81  ********************************************************************************/
82 /* Configuration ****************************************************************/
83 /* Task groups currently only supported for retention of child status */
84 
85 #undef HAVE_TASK_GROUP
86 #undef HAVE_GROUP_MEMBERS
87 
88 /* We need a group an group members if we are supporting the parent/child
89  * relationship.
90  */
91 
92 #if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS)
93 #define HAVE_TASK_GROUP 1
94 #define HAVE_GROUP_MEMBERS 1
95 
96 /* We need a group (but not members) if any other resources are shared within
97  * a task group. NOTE: that we essentially always need a task group and that
98  * managing this definition adds a lot of overhead just to handle a corner-
99  * case very minimal system!
100  */
101 
102 #else
103 #if !defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_SCHED_HAVE_PARENT)
104 #define HAVE_TASK_GROUP 1 /* pthreads with parent */
105 #elif !defined(CONFIG_DISABLE_ENVIRON)
106 #define HAVE_TASK_GROUP 1 /* Environment variables */
107 #elif !defined(CONFIG_DISABLE_SIGNALS)
108 #define HAVE_TASK_GROUP 1 /* Signals */
109 #elif defined(CONFIG_SCHED_ATEXIT)
110 #define HAVE_TASK_GROUP 1 /* Group atexit() function */
111 #elif defined(CONFIG_SCHED_ONEXIT)
112 #define HAVE_TASK_GROUP 1 /* Group on_exit() function */
113 #elif defined(CONFIG_SCHED_WAITPID)
114 #define HAVE_TASK_GROUP 1 /* Group waitpid() function */
115 #elif CONFIG_NFILE_DESCRIPTORS > 0
116 #define HAVE_TASK_GROUP 1 /* File descriptors */
117 #elif CONFIG_NFILE_STREAMS > 0
118 #define HAVE_TASK_GROUP 1 /* Standard C buffered I/O */
119 #elif CONFIG_NSOCKET_DESCRIPTORS > 0
120 #define HAVE_TASK_GROUP 1 /* Sockets */
121 #elif !defined(CONFIG_DISABLE_MQUEUE)
122 #define HAVE_TASK_GROUP 1 /* Message queues */
123 #elif defined(CONFIG_ARCH_ADDRENV)
124 #define HAVE_TASK_GROUP 1 /* Address environment */
125 #elif defined(CONFIG_MM_SHM)
126 #define HAVE_TASK_GROUP 1 /* Shared memory */
127 #endif
128 #endif
129 
130 /* In any event, we don't need group members if support for pthreads is disabled */
131 
132 #ifdef CONFIG_DISABLE_PTHREAD
133 #undef HAVE_GROUP_MEMBERS
134 #endif
135 
136 /* Task Management Definitions **************************************************/
137 /* Special task IDS. Any negative PID is invalid. */
138 
139 #define NULL_TASK_PROCESS_ID (pid_t)0
140 #define INVALID_PROCESS_ID (pid_t)-1
141 
142 /* This is the maximum number of times that a lock can be set */
143 
144 #define MAX_LOCK_COUNT 127
145 
146 /* Values for the struct tcb_s flags bits */
147 
148 #define TCB_FLAG_TTYPE_SHIFT (0) /* Bits 0-1: thread type */
149 #define TCB_FLAG_TTYPE_MASK (3 << TCB_FLAG_TTYPE_SHIFT)
150 #define TCB_FLAG_TTYPE_TASK (0 << TCB_FLAG_TTYPE_SHIFT) /* Normal user task */
151 #define TCB_FLAG_TTYPE_PTHREAD (1 << TCB_FLAG_TTYPE_SHIFT) /* User pthread */
152 #define TCB_FLAG_TTYPE_KERNEL (2 << TCB_FLAG_TTYPE_SHIFT) /* Kernel thread */
153 #define TCB_FLAG_NONCANCELABLE (1 << 2) /* Bit 2: Pthread is non-cancelable */
154 #define TCB_FLAG_CANCEL_DEFERRED (1 << 3) /* Bit 3: Deferred (vs asynch) cancellation type */
155 #define TCB_FLAG_CANCEL_PENDING (1 << 4) /* Bit 4: Pthread cancel is pending */
156 #define TCB_FLAG_POLICY_SHIFT (5) /* Bit 5-6: Scheduling policy */
157  #define TCB_FLAG_POLICY_MASK (3 << TCB_FLAG_POLICY_SHIFT)
158  # define TCB_FLAG_SCHED_FIFO (0 << TCB_FLAG_POLICY_SHIFT) /* FIFO scheding policy */
159  # define TCB_FLAG_ROUND_ROBIN (1 << TCB_FLAG_POLICY_SHIFT) /* Round robin scheding policy */
160  # define TCB_FLAG_SCHED_SPORADIC (2 << TCB_FLAG_POLICY_SHIFT) /* Sporadic scheding policy */
161  # define TCB_FLAG_SCHED_OTHER (3 << TCB_FLAG_POLICY_SHIFT) /* Other scheding policy */
162 #define TCB_FLAG_CPU_LOCKED (1 << 7) /* Bit 7: Locked to this CPU */
163 #define TCB_FLAG_EXIT_PROCESSING (1 << 8) /* Bit 8: Exitting */
164  /* Bits 9-15: Available */
165 
166 /* Values for struct task_group tg_flags */
167 
168 #define GROUP_FLAG_NOCLDWAIT (1 << 0) /* Bit 0: Do not retain child exit status */
169 #define GROUP_FLAG_ADDRENV (1 << 1) /* Bit 1: Group has an address environment */
170 #define GROUP_FLAG_PRIVILEGED (1 << 2) /* Bit 2: Group is privileged */
171 
172 /* Values for struct child_status_s ch_flags */
173 
174 #define CHILD_FLAG_TTYPE_SHIFT (0) /* Bits 0-1: child thread type */
175 #define CHILD_FLAG_TTYPE_MASK (3 << CHILD_FLAG_TTYPE_SHIFT)
176 #define CHILD_FLAG_TTYPE_TASK (0 << CHILD_FLAG_TTYPE_SHIFT) /* Normal user task */
177 #define CHILD_FLAG_TTYPE_PTHREAD (1 << CHILD_FLAG_TTYPE_SHIFT) /* User pthread */
178 #define CHILD_FLAG_TTYPE_KERNEL (2 << CHILD_FLAG_TTYPE_SHIFT) /* Kernel thread */
179 #define CHILD_FLAG_EXITED (1 << 0) /* Bit 2: The child thread has exit'ed */
180 
181 /********************************************************************************
182  * Public Type Definitions
183  ********************************************************************************/
184 
185 #ifndef __ASSEMBLY__
186 
187 /* General Task Management Types ************************************************/
188 
195 enum tstate_e {
196  TSTATE_TASK_INVALID = 0, /* INVALID - The TCB is uninitialized */
197  TSTATE_TASK_PENDING, /* READY_TO_RUN - Pending preemption unlock */
198  TSTATE_TASK_READYTORUN, /* READY-TO-RUN - But not running */
199  TSTATE_TASK_RUNNING, /* READY_TO_RUN - And running */
200 
201  TSTATE_TASK_INACTIVE, /* BLOCKED - Initialized but not yet activated */
202  TSTATE_WAIT_SEM, /* BLOCKED - Waiting for a semaphore */
203 #ifndef CONFIG_DISABLE_SIGNALS
204  TSTATE_WAIT_SIG, /* BLOCKED - Waiting for a signal */
205 #endif
206 #ifndef CONFIG_DISABLE_MQUEUE
207  TSTATE_WAIT_MQNOTEMPTY, /* BLOCKED - Waiting for a MQ to become not empty. */
208  TSTATE_WAIT_MQNOTFULL, /* BLOCKED - Waiting for a MQ to become not full. */
209 #endif
210 #ifdef CONFIG_PAGING
211  TSTATE_WAIT_PAGEFILL, /* BLOCKED - Waiting for page fill */
212 #endif
213  NUM_TASK_STATES /* Must be last */
214 };
215 typedef enum tstate_e tstate_t;
216 
217 /* The following definitions are determined by tstate_t */
218 
219 #define FIRST_READY_TO_RUN_STATE TSTATE_TASK_READYTORUN
220 #define LAST_READY_TO_RUN_STATE TSTATE_TASK_RUNNING
221 #define FIRST_BLOCKED_STATE TSTATE_TASK_INACTIVE
222 #define LAST_BLOCKED_STATE (NUM_TASK_STATES-1)
223 
224 /* The following is the form of a thread start-up function */
225 
226 typedef CODE void (*start_t)(void);
227 
231 union entry_u {
233  main_t main;
234 };
235 typedef union entry_u entry_t;
236 
237 /* This is the type of the function called at task startup */
238 
239 #ifdef CONFIG_SCHED_STARTHOOK
240 typedef CODE void (*starthook_t)(FAR void *arg);
241 #endif
242 
243 /* These are the types of the functions that are executed with exit() is called
244  * (if registered via atexit() on on_exit()).
245  */
246 
247 #ifdef CONFIG_SCHED_ATEXIT
248 typedef CODE void (*atexitfunc_t)(void);
249 #endif
250 
251 #ifdef CONFIG_SCHED_ONEXIT
252 typedef CODE void (*onexitfunc_t)(int exitcode, FAR void *arg);
253 #endif
254 
255 /* struct child_status_s *********************************************************/
260 #ifdef CONFIG_SCHED_CHILD_STATUS
262  FAR struct child_status_s *flink;
263 
264  uint8_t ch_flags; /* Child status: See CHILD_FLAG_* definitions */
265  pid_t ch_pid; /* Child task ID */
266  int ch_status; /* Child exit status */
267 };
268 #endif
269 
270 /* struct pthread_cleanup_s ******************************************************/
271 /* This structure describes one element of the pthread cleanup stack */
272 
273 #ifdef CONFIG_PTHREAD_CLEANUP
274 struct pthread_cleanup_s {
275  pthread_cleanup_t pc_cleaner; /* Cleanup callback address */
276  FAR void *pc_arg; /* Argument that accompanies the callback */
277 };
278 #endif
279 
280 /* struct dspace_s ***************************************************************/
281 
286 #ifdef CONFIG_PIC
287 struct dspace_s {
288  /* The life of the structure allocation is determined by this reference
289  * count. This count is number of threads that shared the same D-Space.
290  * This includes the parent task as well as any pthreads created by the
291  * parent task or any of its child threads.
292  */
293 
294  uint16_t crefs;
295 
296  /* This is the allocated D-Space memory region. This may be a physical
297  * address allocated with kmm_malloc(), or it may be virtual address associated
298  * with an address environment (if CONFIG_ARCH_ADDRENV=y).
299  */
300 
301  FAR uint8_t *region;
302 };
303 #endif
304 
305 /* struct task_group_s ***********************************************************/
306 /* All threads created by pthread_create belong in the same task group (along with
307  * the thread of the original task). struct task_group_s is a shared structure
308  * referenced by the TCB of each thread that is a member of the task group.
309  *
310  * This structure should contain *all* resources shared by tasks and threads that
311  * belong to the same task group:
312  *
313  * Child exit status
314  * Environment variables
315  * PIC data space and address environments
316  * File descriptors
317  * FILE streams
318  * Sockets
319  * Address environments.
320  *
321  * Each instance of struct task_group_s is reference counted. Each instance is
322  * created with a reference count of one. The reference incremented when each
323  * thread joins the group and decremented when each thread exits, leaving the
324  * group. When the reference count decrements to zero, the struct task_group_s
325  * is free.
326  */
327 
328 #ifdef HAVE_TASK_GROUP
329 
330 #ifndef CONFIG_DISABLE_PTHREAD
331 struct join_s; /* Forward reference */
332 /* Defined in kernel/pthread/pthread.h */
333 #endif
334 
335 struct task_group_s {
336 #if defined(HAVE_GROUP_MEMBERS) || defined(CONFIG_ARCH_ADDRENV)
337  struct task_group_s *flink; /* Supports a singly linked list */
338  gid_t tg_gid; /* The ID of this task group */
339 #endif
340 #ifdef HAVE_GROUP_MEMBERS
341  gid_t tg_pgid; /* The ID of the parent task group */
342 #endif
343 #if !defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_SCHED_HAVE_PARENT)
344  pid_t tg_task; /* The ID of the task within the group */
345 #endif
346  uint8_t tg_flags; /* See GROUP_FLAG_* definitions */
347 
348  /* Group membership ********************************************************** */
349 
350  uint8_t tg_nmembers; /* Number of members in the group */
351 #ifdef HAVE_GROUP_MEMBERS
352  uint8_t tg_mxmembers; /* Number of members in allocation */
353  FAR pid_t *tg_members; /* Members of the group */
354 #endif
355 
356 #if defined(CONFIG_SCHED_ATEXIT) && !defined(CONFIG_SCHED_ONEXIT)
357  /* atexit support *********************************************************** */
358 
359 #if defined(CONFIG_SCHED_ATEXIT_MAX) && CONFIG_SCHED_ATEXIT_MAX > 1
360  atexitfunc_t tg_atexitfunc[CONFIG_SCHED_ATEXIT_MAX];
361 #else
362  atexitfunc_t tg_atexitfunc; /* Called when exit is called. */
363 #endif
364 #endif
365 
366 #ifdef CONFIG_SCHED_ONEXIT
367  /* on_exit support ********************************************************** */
368 
369 #if defined(CONFIG_SCHED_ONEXIT_MAX) && CONFIG_SCHED_ONEXIT_MAX > 1
370  onexitfunc_t tg_onexitfunc[CONFIG_SCHED_ONEXIT_MAX];
371  FAR void *tg_onexitarg[CONFIG_SCHED_ONEXIT_MAX];
372 #else
373  onexitfunc_t tg_onexitfunc; /* Called when exit is called. */
374  FAR void *tg_onexitarg; /* The argument passed to the function */
375 #endif
376 #endif
377 
378 #if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS)
379  /* Child exit status ********************************************************* */
380 
381  FAR struct child_status_s *tg_children; /* Head of a list of child status */
382 #endif
383 
384 #if defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_SCHED_HAVE_PARENT)
385  /* waitpid support *********************************************************** */
386  /* Simple mechanism used only when there is no support for SIGCHLD */
387 
388  sem_t tg_exitsem; /* Support for waitpid */
389  int *tg_statloc; /* Location to return exit status */
390 #endif
391 
392 #ifndef CONFIG_DISABLE_PTHREAD
393  /* Pthreads ****************************************************************** */
394  /* Pthread join Info: */
395  sem_t tg_joinsem; /* Mutually exclusive access to join data */
396  FAR struct join_s *tg_joinhead; /* Head of a list of join data */
397  FAR struct join_s *tg_jointail; /* Tail of a list of join data */
398  uint8_t tg_nkeys; /* Number pthread keys allocated */
399 #endif
400 
401 #ifndef CONFIG_DISABLE_SIGNALS
402  /* POSIX Signal Control Fields *********************************************** */
403 
404  sq_queue_t sigpendingq; /* List of pending signals */
405 #endif
406 
407 #ifndef CONFIG_DISABLE_ENVIRON
408  /* Environment variables ***************************************************** */
409 
410  size_t tg_envsize; /* Size of environment string allocation */
411  FAR char *tg_envp; /* Allocated environment strings */
412 #endif
413 
414  /* PIC data space and address environments *********************************** */
415  /* Logically the PIC data space belongs here (see struct dspace_s). The
416  * current logic needs review: There are differences in the away that the
417  * life of the PIC data is managed.
418  */
419 
420 #if CONFIG_NFILE_DESCRIPTORS > 0
421  /* File descriptors ********************************************************** */
422 
423  struct filelist tg_filelist; /* Maps file descriptor to file */
424 #endif
425 
426 #if CONFIG_NFILE_STREAMS > 0
427  /* FILE streams ************************************************************** */
428  /* In a flat, single-heap build. The stream list is allocated with this
429  * structure. But kernel mode with a kernel allocator, it must be separately
430  * allocated using a user-space allocator.
431  */
432 
433 #if (defined(CONFIG_BUILD_PROTECTED) || defined(CONFIG_BUILD_KERNEL)) && \
434  defined(CONFIG_MM_KERNEL_HEAP)
435  FAR struct streamlist *tg_streamlist;
436 #else
437  struct streamlist tg_streamlist; /* Holds C buffered I/O info */
438 #endif
439 #endif
440 
441 #if CONFIG_NSOCKET_DESCRIPTORS > 0
442  /* Sockets ******************************************************************* */
443 
444  struct socketlist tg_socketlist; /* Maps socket descriptor to socket */
445 #endif
446 
447 #ifndef CONFIG_DISABLE_MQUEUE
448  /* POSIX Named Message Queue Fields ****************************************** */
449 
450  sq_queue_t tg_msgdesq; /* List of opened message queues */
451 #endif
452 
453 #ifdef CONFIG_ARCH_ADDRENV
454  /* Address Environment ******************************************************* */
455 
456  group_addrenv_t tg_addrenv; /* Task group address environment */
457 #endif
458 
459 #ifdef CONFIG_MM_SHM
460  /* Shared Memory ************************************************************* */
461 
462  struct group_shm_s tg_shm; /* Task shared memory logic */
463 #endif
464 };
465 #endif
466 
467 /* struct tcb_s ******************************************************************/
468 
469 FAR struct wdog_s; /* Forward reference */
474 struct tcb_s {
475  /* Fields used to support list management ************************************ */
476 
477  FAR struct tcb_s *flink; /* Doubly linked list */
478  FAR struct tcb_s *blink;
479 
480  /* Task Group **************************************************************** */
481 
482 #ifdef HAVE_TASK_GROUP
483  FAR struct task_group_s *group; /* Pointer to shared task group data */
484 #endif
485 
486  /* Task Management Fields **************************************************** */
487 
488  pid_t pid; /* This is the ID of the thread */
489 
490 #ifdef CONFIG_SCHED_HAVE_PARENT /* Support parent-child relationship */
491 #ifndef HAVE_GROUP_MEMBERS /* Don't know pids of group members */
492  pid_t ppid; /* This is the ID of the parent thread */
493 #ifndef CONFIG_SCHED_CHILD_STATUS /* Retain child thread status */
494  uint16_t nchildren; /* This is the number active children */
495 #endif
496 #endif
497 #endif /* CONFIG_SCHED_HAVE_PARENT */
498 
499  start_t start; /* Thread start function */
500  entry_t entry; /* Entry Point into the thread */
501  uint8_t sched_priority; /* Current priority of the thread */
502 
503 #ifdef CONFIG_PRIORITY_INHERITANCE
504 #if CONFIG_SEM_NNESTPRIO > 0
505  uint8_t npend_reprio; /* Number of nested reprioritizations */
506  uint8_t pend_reprios[CONFIG_SEM_NNESTPRIO];
507 #endif
508  uint8_t base_priority; /* "Normal" priority of the thread */
509 #endif
510 
511  uint8_t task_state; /* Current state of the thread */
512  uint16_t flags; /* Misc. general status flags */
513  int16_t lockcount; /* 0=preemptable (not-locked) */
514 #ifdef CONFIG_CANCELLATION_POINTS
515  int16_t cpcount; /* Nested cancellation point count */
516 #endif
517 
518 #if CONFIG_RR_INTERVAL > 0
519  int timeslice; /* RR timeslice interval remaining */
520 #endif
521  FAR struct wdog_s *waitdog; /* All timed waits used this wdog */
522 
523  /* Stack-Related Fields ****************************************************** */
524 
525  size_t adj_stack_size; /* Stack size after adjustment */
526  /* for hardware, processor, etc. */
527  /* (for debug purposes only) */
528  FAR void *stack_alloc_ptr; /* Pointer to allocated stack */
529  /* Need to deallocate stack */
530  FAR void *adj_stack_ptr; /* Adjusted stack_alloc_ptr for HW */
531  /* The initial stack pointer value */
532 
533 #ifdef CONFIG_MPU_STACKGUARD
534  FAR void *stack_guard; /* address of the stack guard */
535  size_t guard_size; /* size of the guard region */
536 #endif
537  /* External Module Support *************************************************** */
538 
539 #ifdef CONFIG_PIC
540  FAR struct dspace_s *dspace; /* Allocated area for .bss and .data */
541 #endif
542 
543  /* POSIX Semaphore Control Fields ******************************************** */
544 
545  sem_t *waitsem; /* Semaphore ID waiting on */
546 
547  /* POSIX Signal Control Fields *********************************************** */
548 
549 #ifndef CONFIG_DISABLE_SIGNALS
550  sigset_t sigprocmask; /* Signals that are blocked */
551  sigset_t sigwaitmask; /* Waiting for pending signals */
552  sq_queue_t sigactionq; /* List of actions for signals */
553  sq_queue_t sigpendactionq; /* List of pending signal actions */
554  sq_queue_t sigpostedq; /* List of posted signals */
555  siginfo_t sigunbinfo; /* Signal info when task unblocked */
556 #endif
557 
558  /* POSIX Named Message Queue Fields ****************************************** */
559 
560 #ifndef CONFIG_DISABLE_MQUEUE
561  FAR struct mqueue_inode_s *msgwaitq; /* Waiting for this message queue */
562 #endif
563 
564  /* Library related fields **************************************************** */
565 
566  int pterrno; /* Current per-thread errno */
567 
568  /* State save areas ********************************************************** */
569  /* The form and content of these fields are platform-specific. */
570 
571  struct xcptcontext xcp; /* Interrupt register save area */
572 
573 #if CONFIG_TASK_NAME_SIZE > 0
574  char name[CONFIG_TASK_NAME_SIZE + 1]; /* Task name (with NUL terminator) */
575 #endif
576 
577 #ifdef CONFIG_DEBUG_MM_HEAPINFO
581 #endif
582 };
583 
584 /* struct task_tcb_s *************************************************************/
593 struct task_tcb_s {
594  /* Common TCB fields ********************************************************* */
595 
596  struct tcb_s cmn; /* Common TCB fields */
597 
598  /* Task Management Fields **************************************************** */
599 
600 #ifdef CONFIG_SCHED_STARTHOOK
601  starthook_t starthook; /* Task startup function */
602  FAR void *starthookarg; /* The argument passed to the function */
603 #endif
604 
605  /* Values needed to restart a task ******************************************* */
606 
607  uint8_t init_priority; /* Initial priority of the task */
608  FAR char **argv; /* Name+start-up parameters */
609 };
610 
611 /* struct pthread_tcb_s **********************************************************/
612 
613 #ifndef CONFIG_DISABLE_PTHREAD
614 
623  /* Common TCB fields ********************************************************* */
624 
625  struct tcb_s cmn; /* Common TCB fields */
626 
627  /* Task Management Fields **************************************************** */
628 
629  pthread_addr_t arg; /* Startup argument */
630  FAR void *joininfo; /* Detach-able info to support join */
631 
632  /* Robust mutex support *********************************************/
633 
634 #ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
635  FAR struct pthread_mutex_s *mhead; /* List of mutexes held by thread */
636 #endif
637 
638  /* Clean-up stack ***************************************************/
639 
640 #ifdef CONFIG_PTHREAD_CLEANUP
641  /* tos - The index to the next avaiable entry at the top of the stack.
642  * stack - The pre-allocated clean-up stack memory.
643  */
644 
645  uint8_t tos;
646  struct pthread_cleanup_s stack[CONFIG_PTHREAD_CLEANUP_STACKSIZE];
647 #endif
648 
649  /* POSIX Thread Specific Data ************************************************ */
650 
651 #if CONFIG_NPTHREAD_KEYS > 0
652  FAR void *pthread_data[CONFIG_NPTHREAD_KEYS];
653 #endif
654 #if defined(CONFIG_BUILD_PROTECTED)
655  struct pthread_region_s *region;
656 #endif
657 };
658 #endif /* !CONFIG_DISABLE_PTHREAD */
659 
660 /* This is the callback type used by sched_foreach() */
661 
662 typedef void (*sched_foreach_t)(FAR struct tcb_s *tcb, FAR void *arg);
663 
664 #endif /* __ASSEMBLY__ */
665 
666 /********************************************************************************
667  * Public Data
668  ********************************************************************************/
669 
670 #ifndef __ASSEMBLY__
671 #undef EXTERN
672 #if defined(__cplusplus)
673 #define EXTERN extern "C"
674 extern "C" {
675 #else
676 #define EXTERN extern
677 #endif
678 
679 /********************************************************************************
680  * Public Function Prototypes
681  ********************************************************************************/
682 
683 /* TCB helpers ******************************************************************/
684 
696 FAR struct tcb_s *sched_self(void);
697 
698 /* sched_foreach
699  */
712 void sched_foreach(sched_foreach_t handler, FAR void *arg);
713 
725 FAR struct tcb_s *sched_gettcb(pid_t pid);
726 
727 /* File system helpers **********************************************************/
728 /* These functions all extract lists from the group structure assocated with the
729  * currently executing task.
730  */
731 
732 #if CONFIG_NFILE_DESCRIPTORS > 0
733 
737 FAR struct filelist *sched_getfiles(void);
741 #if CONFIG_NFILE_STREAMS > 0
742 
749 FAR struct streamlist *sched_getstreams(void);
750 #endif /* CONFIG_NFILE_STREAMS */
751 #endif /* CONFIG_NFILE_DESCRIPTORS */
752 
753 #if CONFIG_NSOCKET_DESCRIPTORS > 0
754 FAR struct socketlist *sched_getsockets(void);
755 #endif /* CONFIG_NSOCKET_DESCRIPTORS */
756 
757 /********************************************************************************
758  * Name: task_starthook
759  *
760  * Description:
761  * Configure a start hook... a function that will be called on the thread
762  * of the new task before the new task's main entry point is called.
763  * The start hook is useful, for example, for setting up automatic
764  * configuration of C++ constructors.
765  *
766  * Inputs:
767  * tcb - The new, unstarted task task that needs the start hook
768  * starthook - The pointer to the start hook function
769  * arg - The argument to pass to the start hook function.
770  *
771  * Return:
772  * None
773  *
774  ********************************************************************************/
775 
776 #ifdef CONFIG_SCHED_STARTHOOK
777 
781 void task_starthook(FAR struct task_tcb_s *tcb, starthook_t starthook, FAR void *arg);
785 #endif
786 
787 /********************************************************************************
788  * Internal vfork support. The overall sequence is:
789  *
790  * 1) User code calls vfork(). vfork() is provided in architecture-specific
791  * code.
792  * 2) vfork()and calls task_vforksetup().
793  * 3) task_vforksetup() allocates and configures the child task's TCB. This
794  * consists of:
795  * - Allocation of the child task's TCB.
796  * - Initialization of file descriptors and streams
797  * - Configuration of environment variables
798  * - Setup the intput parameters for the task.
799  * - Initialization of the TCB (including call to up_initial_state()
800  * 4) vfork() provides any additional operating context. vfork must:
801  * - Allocate and initialize the stack
802  * - Initialize special values in any CPU registers that were not
803  * already configured by up_initial_state()
804  * 5) vfork() then calls task_vforkstart()
805  * 6) task_vforkstart() then executes the child thread.
806  *
807  * task_vforkabort() may be called if an error occurs between steps 3 and 6.
808  *
809  ********************************************************************************/
814 FAR struct task_tcb_s *task_vforksetup(start_t retaddr);
818 pid_t task_vforkstart(FAR struct task_tcb_s *child);
822 void task_vforkabort(FAR struct task_tcb_s *child, int errcode);
826 #undef EXTERN
827 #if defined(__cplusplus)
828 }
829 #endif
830 #endif /* __ASSEMBLY__ */
831 
832 #endif /* __INCLUDE_TINYARA_SCHED_H */
sigset_t sigwaitmask
Definition: sched.h:551
int curr_alloc_size
Definition: sched.h:578
int16_t lockcount
Definition: sched.h:513
FAR struct tcb_s * sched_gettcb(pid_t pid)
Give a task ID, look up the corresponding TCB.
uint8_t tg_mxmembers
Definition: sched.h:352
This structure describes a reference counted D-Space region. This must be a separately allocated "bre...
Definition: sched.h:287
uint8_t base_priority
Definition: sched.h:508
start_t start
Definition: sched.h:499
sq_queue_t sigpendactionq
Definition: sched.h:553
Structure of pthread mutex configuration.
Definition: pthread.h:311
CODE void(* start_t)(void)
Definition: sched.h:226
void(* sched_foreach_t)(FAR struct tcb_s *tcb, FAR void *arg)
Definition: sched.h:662
FAR struct wdog_s * waitdog
Definition: sched.h:521
int timeslice
Definition: sched.h:519
FAR struct child_status_s * tg_children
Definition: sched.h:381
FAR void * pthread_addr_t
Definition: pthread.h:248
pid_t tg_task
Definition: sched.h:344
struct xcptcontext xcp
Definition: sched.h:571
size_t tg_envsize
Definition: sched.h:410
struct tcb_s cmn
Definition: sched.h:625
main_t main
Definition: sched.h:233
This structure is used to maintin information about child tasks. pthreads work differently, they have join information. This is only for child tasks.
Definition: sched.h:261
structure for header queue
Definition: queue.h:113
CODE void(* starthook_t)(FAR void *arg)
Definition: sched.h:240
FAR pid_t * tg_members
Definition: sched.h:353
starthook_t starthook
Definition: sched.h:601
FAR void * joininfo
Definition: sched.h:630
FAR void * tg_onexitarg
Definition: sched.h:374
sq_queue_t sigactionq
Definition: sched.h:552
int ch_status
Definition: sched.h:266
uint8_t task_state
Definition: sched.h:511
Pthread APIs.
enum tstate_e tstate_t
Definition: sched.h:215
uint8_t tg_nkeys
Definition: sched.h:398
uint8_t tg_flags
Definition: sched.h:346
siginfo_t sigunbinfo
Definition: sched.h:555
FAR struct join_s * tg_jointail
Definition: sched.h:397
size_t adj_stack_size
Definition: sched.h:525
Structure for using to pass parameters to/from signal handlers.
Definition: signal.h:241
sem_t * waitsem
Definition: sched.h:545
int peak_alloc_size
Definition: sched.h:579
FAR char ** argv
Definition: sched.h:608
Queue APIs.
struct filelist tg_filelist
Definition: sched.h:423
FAR void * pthread_data[CONFIG_NPTHREAD_KEYS]
Definition: sched.h:652
This is the common part of the task control block (TCB). The TCB is the heart of the TinyAra task-con...
Definition: sched.h:474
pid_t pid
Definition: sched.h:488
pthread_addr_t arg
Definition: sched.h:629
Signal APIs.
Mqueue APIs.
FAR void * starthookarg
Definition: sched.h:602
sq_queue_t sigpostedq
Definition: sched.h:554
FAR struct join_s * tg_joinhead
Definition: sched.h:396
int pterrno
Definition: sched.h:566
uint16_t flags
Definition: sched.h:512
FAR struct tcb_s * blink
Definition: sched.h:478
gid_t tg_gid
Definition: sched.h:338
FAR struct tcb_s * sched_self(void)
returns the TCB of the currently running task (i.e., the caller)
Structure for Task Group Information.
Definition: sched.h:335
CODE void(* atexitfunc_t)(void)
Definition: sched.h:248
entry_t entry
Definition: sched.h:500
uint8_t ch_flags
Definition: sched.h:264
tstate_e
This is the type of the task_state field of the TCB. NOTE: the order and content of this enumeration ...
Definition: sched.h:195
onexitfunc_t tg_onexitfunc
Definition: sched.h:373
FAR void * stack_alloc_ptr
Definition: sched.h:528
FAR struct task_group_s * group
Definition: sched.h:483
FAR char * tg_envp
Definition: sched.h:411
FAR struct pthread_mutex_s * mhead
Definition: sched.h:635
gid_t tg_pgid
Definition: sched.h:341
Semaphore APIs.
void sched_foreach(sched_foreach_t handler, FAR void *arg)
enumerate over each task and provide the TCB of each task or thread to a callback function...
struct task_group_s * flink
Definition: sched.h:337
FAR struct mqueue_inode_s * msgwaitq
Definition: sched.h:561
This is the particular form of the task control block (TCB) structure used by pthreads. There are two TCB forms: one for pthreads and one for tasks. Both share the common TCB fields (which must appear at the top of the structure) plus additional fields unique to tasks and threads. Having separate structures for tasks and pthreads adds some complexity, but saves memory in that it prevents pthreads from being burdened with the overhead required for tasks (and vice versa).
Definition: sched.h:622
sq_queue_t sigpendingq
Definition: sched.h:404
uint8_t tg_nmembers
Definition: sched.h:350
uint8_t init_priority
Definition: sched.h:607
Structure of generic semaphore.
Definition: semaphore.h:113
pthread_addr_t(* pthread_startroutine_t)(pthread_addr_t)
Definition: pthread.h:250
sem_t tg_joinsem
Definition: sched.h:395
This is the entry point into the main thread of the task or into a created pthread within the task...
Definition: sched.h:231
uint16_t crefs
Definition: sched.h:294
FAR struct child_status_s * flink
Definition: sched.h:262
FAR struct dspace_s * dspace
Definition: sched.h:540
uint8_t sched_priority
Definition: sched.h:501
uint32_t sigset_t
Definition: signal.h:214
FAR void * adj_stack_ptr
Definition: sched.h:530
int num_alloc_free
Definition: sched.h:580
struct tcb_s cmn
Definition: sched.h:596
sigset_t sigprocmask
Definition: sched.h:550
FAR struct tcb_s * flink
Definition: sched.h:477
Time APIs.
This is the particular form of the task control block (TCB) structure used by tasks (and kernel threa...
Definition: sched.h:593
Structure of pthread region configuration.
Definition: pthread.h:257
CODE void(* onexitfunc_t)(int exitcode, FAR void *arg)
Definition: sched.h:252
FAR uint8_t * region
Definition: sched.h:301
pthread_startroutine_t pthread
Definition: sched.h:232
pid_t ch_pid
Definition: sched.h:265
char name[CONFIG_TASK_NAME_SIZE+1]
Definition: sched.h:574
sq_queue_t tg_msgdesq
Definition: sched.h:450