TizenRT Libs&Environment  v2.0 M2
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 /* Values for flag of pthread key allocated */
182 
183 #define KEY_NOT_INUSE (0)
184 #define KEY_INUSE (1)
185 
186 /********************************************************************************
187  * Public Type Definitions
188  ********************************************************************************/
189 
190 #ifndef __ASSEMBLY__
191 
192 /* General Task Management Types ************************************************/
193 
200 enum tstate_e {
201  TSTATE_TASK_INVALID = 0, /* INVALID - The TCB is uninitialized */
202  TSTATE_TASK_PENDING, /* READY_TO_RUN - Pending preemption unlock */
203  TSTATE_TASK_READYTORUN, /* READY-TO-RUN - But not running */
204  TSTATE_TASK_RUNNING, /* READY_TO_RUN - And running */
205 
206  TSTATE_TASK_INACTIVE, /* BLOCKED - Initialized but not yet activated */
207  TSTATE_WAIT_SEM, /* BLOCKED - Waiting for a semaphore */
208 #ifndef CONFIG_DISABLE_SIGNALS
209  TSTATE_WAIT_SIG, /* BLOCKED - Waiting for a signal */
210 #endif
211 #ifndef CONFIG_DISABLE_MQUEUE
212  TSTATE_WAIT_MQNOTEMPTY, /* BLOCKED - Waiting for a MQ to become not empty. */
213  TSTATE_WAIT_MQNOTFULL, /* BLOCKED - Waiting for a MQ to become not full. */
214 #endif
215 #ifdef CONFIG_PAGING
216  TSTATE_WAIT_PAGEFILL, /* BLOCKED - Waiting for page fill */
217 #endif
218  NUM_TASK_STATES /* Must be last */
219 };
220 typedef enum tstate_e tstate_t;
221 
222 /* The following definitions are determined by tstate_t */
223 
224 #define FIRST_READY_TO_RUN_STATE TSTATE_TASK_READYTORUN
225 #define LAST_READY_TO_RUN_STATE TSTATE_TASK_RUNNING
226 #define FIRST_BLOCKED_STATE TSTATE_TASK_INACTIVE
227 #define LAST_BLOCKED_STATE (NUM_TASK_STATES-1)
228 
229 /* The following is the form of a thread start-up function */
230 
231 typedef CODE void (*start_t)(void);
232 
236 union entry_u {
238  main_t main;
239 };
240 typedef union entry_u entry_t;
241 
242 /* This is the type of the function called at task startup */
243 
244 #ifdef CONFIG_SCHED_STARTHOOK
245 typedef CODE void (*starthook_t)(FAR void *arg);
246 #endif
247 
248 /* These are the types of the functions that are executed with exit() is called
249  * (if registered via atexit() on on_exit()).
250  */
251 
252 #ifdef CONFIG_SCHED_ATEXIT
253 typedef CODE void (*atexitfunc_t)(void);
254 #endif
255 
256 #ifdef CONFIG_SCHED_ONEXIT
257 typedef CODE void (*onexitfunc_t)(int exitcode, FAR void *arg);
258 #endif
259 
260 /* struct child_status_s *********************************************************/
265 #ifdef CONFIG_SCHED_CHILD_STATUS
267  FAR struct child_status_s *flink;
268 
269  uint8_t ch_flags; /* Child status: See CHILD_FLAG_* definitions */
270  pid_t ch_pid; /* Child task ID */
271  int ch_status; /* Child exit status */
272 };
273 #endif
274 
275 /* struct pthread_cleanup_s ******************************************************/
276 /* This structure describes one element of the pthread cleanup stack */
277 
278 #ifdef CONFIG_PTHREAD_CLEANUP
279 struct pthread_cleanup_s {
280  pthread_cleanup_t pc_cleaner; /* Cleanup callback address */
281  FAR void *pc_arg; /* Argument that accompanies the callback */
282 };
283 #endif
284 
285 /* struct dspace_s ***************************************************************/
286 
291 #ifdef CONFIG_PIC
292 struct dspace_s {
293  /* The life of the structure allocation is determined by this reference
294  * count. This count is number of threads that shared the same D-Space.
295  * This includes the parent task as well as any pthreads created by the
296  * parent task or any of its child threads.
297  */
298 
299  uint16_t crefs;
300 
301  /* This is the allocated D-Space memory region. This may be a physical
302  * address allocated with kmm_malloc(), or it may be virtual address associated
303  * with an address environment (if CONFIG_ARCH_ADDRENV=y).
304  */
305 
306  FAR uint8_t *region;
307 };
308 #endif
309 
310 /* struct task_group_s ***********************************************************/
311 /* All threads created by pthread_create belong in the same task group (along with
312  * the thread of the original task). struct task_group_s is a shared structure
313  * referenced by the TCB of each thread that is a member of the task group.
314  *
315  * This structure should contain *all* resources shared by tasks and threads that
316  * belong to the same task group:
317  *
318  * Child exit status
319  * Environment variables
320  * PIC data space and address environments
321  * File descriptors
322  * FILE streams
323  * Sockets
324  * Address environments.
325  *
326  * Each instance of struct task_group_s is reference counted. Each instance is
327  * created with a reference count of one. The reference incremented when each
328  * thread joins the group and decremented when each thread exits, leaving the
329  * group. When the reference count decrements to zero, the struct task_group_s
330  * is free.
331  */
332 
333 #ifdef HAVE_TASK_GROUP
334 
335 #ifndef CONFIG_DISABLE_PTHREAD
336 struct join_s; /* Forward reference */
337 /* Defined in kernel/pthread/pthread.h */
338 #endif
339 
340 struct task_group_s {
341 #if defined(HAVE_GROUP_MEMBERS) || defined(CONFIG_ARCH_ADDRENV)
342  struct task_group_s *flink; /* Supports a singly linked list */
343  gid_t tg_gid; /* The ID of this task group */
344 #endif
345 #ifdef HAVE_GROUP_MEMBERS
346  gid_t tg_pgid; /* The ID of the parent task group */
347 #endif
348 #if !defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_SCHED_HAVE_PARENT)
349  pid_t tg_task; /* The ID of the task within the group */
350 #endif
351  uint8_t tg_flags; /* See GROUP_FLAG_* definitions */
352 
353  /* Group membership ********************************************************** */
354 
355  uint8_t tg_nmembers; /* Number of members in the group */
356 #ifdef HAVE_GROUP_MEMBERS
357  uint8_t tg_mxmembers; /* Number of members in allocation */
358  FAR pid_t *tg_members; /* Members of the group */
359 #endif
360 
361 #if defined(CONFIG_SCHED_ATEXIT) && !defined(CONFIG_SCHED_ONEXIT)
362  /* atexit support *********************************************************** */
363 
364  sq_queue_t tg_atexitfunc;
365 #endif
366 
367 #ifdef CONFIG_SCHED_ONEXIT
368  /* on_exit support ********************************************************** */
370 #endif
371 
372 #if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS)
373  /* Child exit status ********************************************************* */
374 
375  FAR struct child_status_s *tg_children; /* Head of a list of child status */
376 #endif
377 
378 #if defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_SCHED_HAVE_PARENT)
379  /* waitpid support *********************************************************** */
380  /* Simple mechanism used only when there is no support for SIGCHLD */
381 
382  sem_t tg_exitsem; /* Support for waitpid */
383  int *tg_statloc; /* Location to return exit status */
384 #endif
385 
386 #ifndef CONFIG_DISABLE_PTHREAD
387  /* Pthreads ****************************************************************** */
388  /* Pthread join Info: */
389  sem_t tg_joinsem; /* Mutually exclusive access to join data */
390  FAR struct join_s *tg_joinhead; /* Head of a list of join data */
391  FAR struct join_s *tg_jointail; /* Tail of a list of join data */
392 #if CONFIG_NPTHREAD_KEYS > 0
393  uint8_t tg_key[PTHREAD_KEYS_MAX]; /* flag of pthread keys allocated */
394  pthread_destructor_t tg_destructor[PTHREAD_KEYS_MAX]; /* Address list of each destructor */
395 #endif
396 #endif
397 
398 #ifndef CONFIG_DISABLE_SIGNALS
399  /* POSIX Signal Control Fields *********************************************** */
400 
401  sq_queue_t sigpendingq; /* List of pending signals */
402 #endif
403 
404 #ifndef CONFIG_DISABLE_ENVIRON
405  /* Environment variables ***************************************************** */
406 
407  size_t tg_envsize; /* Size of environment string allocation */
408  FAR char *tg_envp; /* Allocated environment strings */
409 #endif
410 
411  /* PIC data space and address environments *********************************** */
412  /* Logically the PIC data space belongs here (see struct dspace_s). The
413  * current logic needs review: There are differences in the away that the
414  * life of the PIC data is managed.
415  */
416 
417 #if CONFIG_NFILE_DESCRIPTORS > 0
418  /* File descriptors ********************************************************** */
419 
420  struct filelist tg_filelist; /* Maps file descriptor to file */
421 #endif
422 
423 #if CONFIG_NFILE_STREAMS > 0
424  /* FILE streams ************************************************************** */
425  /* In a flat, single-heap build. The stream list is allocated with this
426  * structure. But kernel mode with a kernel allocator, it must be separately
427  * allocated using a user-space allocator.
428  */
429 
430 #if (defined(CONFIG_BUILD_PROTECTED) || defined(CONFIG_BUILD_KERNEL)) && \
431  defined(CONFIG_MM_KERNEL_HEAP)
432  FAR struct streamlist *tg_streamlist;
433 #else
434  struct streamlist tg_streamlist; /* Holds C buffered I/O info */
435 #endif
436 #endif
437 
438 #if CONFIG_NSOCKET_DESCRIPTORS > 0
439  /* Sockets ******************************************************************* */
440 
441  struct socketlist tg_socketlist; /* Maps socket descriptor to socket */
442 #endif
443 
444 #ifndef CONFIG_DISABLE_MQUEUE
445  /* POSIX Named Message Queue Fields ****************************************** */
446 
447  sq_queue_t tg_msgdesq; /* List of opened message queues */
448 #endif
449 
450 #ifdef CONFIG_ARCH_ADDRENV
451  /* Address Environment ******************************************************* */
452 
453  group_addrenv_t tg_addrenv; /* Task group address environment */
454 #endif
455 
456 #ifdef CONFIG_MM_SHM
457  /* Shared Memory ************************************************************* */
458 
459  struct group_shm_s tg_shm; /* Task shared memory logic */
460 #endif
461 };
462 #endif
463 
464 /* struct tcb_s ******************************************************************/
465 
466 FAR struct wdog_s; /* Forward reference */
471 struct tcb_s {
472  /* Fields used to support list management ************************************ */
473 
474  FAR struct tcb_s *flink; /* Doubly linked list */
475  FAR struct tcb_s *blink;
476 
477  /* Task Group **************************************************************** */
478 
479 #ifdef HAVE_TASK_GROUP
480  FAR struct task_group_s *group; /* Pointer to shared task group data */
481 #endif
482 
483  /* Task Management Fields **************************************************** */
484 
485  pid_t pid; /* This is the ID of the thread */
486 
487 #ifdef CONFIG_SCHED_HAVE_PARENT /* Support parent-child relationship */
488 #ifndef HAVE_GROUP_MEMBERS /* Don't know pids of group members */
489  pid_t ppid; /* This is the ID of the parent thread */
490 #ifndef CONFIG_SCHED_CHILD_STATUS /* Retain child thread status */
491  uint16_t nchildren; /* This is the number active children */
492 #endif
493 #endif
494 #endif /* CONFIG_SCHED_HAVE_PARENT */
495 
496  start_t start; /* Thread start function */
497  entry_t entry; /* Entry Point into the thread */
498  uint8_t sched_priority; /* Current priority of the thread */
499 
500 #ifdef CONFIG_PRIORITY_INHERITANCE
501 #if CONFIG_SEM_NNESTPRIO > 0
502  uint8_t npend_reprio; /* Number of nested reprioritizations */
503  uint8_t pend_reprios[CONFIG_SEM_NNESTPRIO];
504 #endif
505  uint8_t base_priority; /* "Normal" priority of the thread */
506 #endif
507 
508  uint8_t task_state; /* Current state of the thread */
509  uint16_t flags; /* Misc. general status flags */
510  int16_t lockcount; /* 0=preemptable (not-locked) */
511 #ifdef CONFIG_CANCELLATION_POINTS
512  int16_t cpcount; /* Nested cancellation point count */
513 #endif
514 
515 #if CONFIG_RR_INTERVAL > 0
516  int timeslice; /* RR timeslice interval remaining */
517 #endif
518  FAR struct wdog_s *waitdog; /* All timed waits used this wdog */
519 
520  /* Stack-Related Fields ****************************************************** */
521 
522  size_t adj_stack_size; /* Stack size after adjustment */
523  /* for hardware, processor, etc. */
524  /* (for debug purposes only) */
525  FAR void *stack_alloc_ptr; /* Pointer to allocated stack */
526  /* Need to deallocate stack */
527  FAR void *adj_stack_ptr; /* Adjusted stack_alloc_ptr for HW */
528  /* The initial stack pointer value */
529 
530 #ifdef CONFIG_MPU_STACKGUARD
531  FAR void *stack_guard; /* address of the stack guard */
532  size_t guard_size; /* size of the guard region */
533 #endif
534  /* External Module Support *************************************************** */
535 
536 #ifdef CONFIG_PIC
537  FAR struct dspace_s *dspace; /* Allocated area for .bss and .data */
538 #endif
539 
540  /* POSIX Semaphore Control Fields ******************************************** */
541 
542  sem_t *waitsem; /* Semaphore ID waiting on */
543 
544  /* POSIX Signal Control Fields *********************************************** */
545 
546 #ifndef CONFIG_DISABLE_SIGNALS
547  sigset_t sigprocmask; /* Signals that are blocked */
548  sigset_t sigwaitmask; /* Waiting for pending signals */
549  sq_queue_t sigactionq; /* List of actions for signals */
550  sq_queue_t sigpendactionq; /* List of pending signal actions */
551  sq_queue_t sigpostedq; /* List of posted signals */
552  siginfo_t sigunbinfo; /* Signal info when task unblocked */
553 #endif
554 
555  /* POSIX Named Message Queue Fields ****************************************** */
556 
557 #ifndef CONFIG_DISABLE_MQUEUE
558  FAR struct mqueue_inode_s *msgwaitq; /* Waiting for this message queue */
559 #endif
560 
561  /* Library related fields **************************************************** */
562 
563  int pterrno; /* Current per-thread errno */
564 
565  /* State save areas ********************************************************** */
566  /* The form and content of these fields are platform-specific. */
567 
568  struct xcptcontext xcp; /* Interrupt register save area */
569 
570 #if CONFIG_TASK_NAME_SIZE > 0
571  char name[CONFIG_TASK_NAME_SIZE + 1]; /* Task name (with NUL terminator) */
572 #endif
573 
574 #ifdef CONFIG_DEBUG_MM_HEAPINFO
578 #endif
579 };
580 
581 /* struct task_tcb_s *************************************************************/
590 struct task_tcb_s {
591  /* Common TCB fields ********************************************************* */
592 
593  struct tcb_s cmn; /* Common TCB fields */
594 
595  /* Task Management Fields **************************************************** */
596 
597 #ifdef CONFIG_SCHED_STARTHOOK
598  starthook_t starthook; /* Task startup function */
599  FAR void *starthookarg; /* The argument passed to the function */
600 #endif
601 
602  /* Values needed to restart a task ******************************************* */
603 
604  uint8_t init_priority; /* Initial priority of the task */
605  FAR char **argv; /* Name+start-up parameters */
606 };
607 
608 /* struct pthread_tcb_s **********************************************************/
609 
610 #ifndef CONFIG_DISABLE_PTHREAD
611 
620  /* Common TCB fields ********************************************************* */
621 
622  struct tcb_s cmn; /* Common TCB fields */
623 
624  /* Task Management Fields **************************************************** */
625 
626  pthread_addr_t arg; /* Startup argument */
627  FAR void *joininfo; /* Detach-able info to support join */
628 
629  /* Robust mutex support *********************************************/
630 
631 #ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
632  FAR struct pthread_mutex_s *mhead; /* List of mutexes held by thread */
633 #endif
634 
635  /* Clean-up stack ***************************************************/
636 
637 #ifdef CONFIG_PTHREAD_CLEANUP
638  /* tos - The index to the next avaiable entry at the top of the stack.
639  * stack - The pre-allocated clean-up stack memory.
640  */
641 
642  uint8_t tos;
643  struct pthread_cleanup_s stack[CONFIG_PTHREAD_CLEANUP_STACKSIZE];
644 #endif
645 
646  /* POSIX Thread Specific Data ************************************************ */
647 
648 #if CONFIG_NPTHREAD_KEYS > 0
649  void *key_data[PTHREAD_KEYS_MAX];
650 #endif
651 #if defined(CONFIG_BUILD_PROTECTED)
652  struct pthread_region_s *region;
653 #endif
654 };
655 #endif /* !CONFIG_DISABLE_PTHREAD */
656 
657 /* This is the callback type used by sched_foreach() */
658 
659 typedef void (*sched_foreach_t)(FAR struct tcb_s *tcb, FAR void *arg);
660 
661 #endif /* __ASSEMBLY__ */
662 
663 /********************************************************************************
664  * Public Data
665  ********************************************************************************/
666 
667 #ifndef __ASSEMBLY__
668 #undef EXTERN
669 #if defined(__cplusplus)
670 #define EXTERN extern "C"
671 extern "C" {
672 #else
673 #define EXTERN extern
674 #endif
675 
676 /********************************************************************************
677  * Public Function Prototypes
678  ********************************************************************************/
679 
680 /* TCB helpers ******************************************************************/
681 
693 FAR struct tcb_s *sched_self(void);
694 
695 /* sched_foreach
696  */
709 void sched_foreach(sched_foreach_t handler, FAR void *arg);
710 
722 FAR struct tcb_s *sched_gettcb(pid_t pid);
723 
724 /* File system helpers **********************************************************/
725 /* These functions all extract lists from the group structure assocated with the
726  * currently executing task.
727  */
728 
729 #if CONFIG_NFILE_DESCRIPTORS > 0
730 
734 FAR struct filelist *sched_getfiles(void);
738 #if CONFIG_NFILE_STREAMS > 0
739 
746 FAR struct streamlist *sched_getstreams(void);
747 #endif /* CONFIG_NFILE_STREAMS */
748 #endif /* CONFIG_NFILE_DESCRIPTORS */
749 
750 #if CONFIG_NSOCKET_DESCRIPTORS > 0
751 FAR struct socketlist *sched_getsockets(void);
752 #endif /* CONFIG_NSOCKET_DESCRIPTORS */
753 
754 /********************************************************************************
755  * Name: task_starthook
756  *
757  * Description:
758  * Configure a start hook... a function that will be called on the thread
759  * of the new task before the new task's main entry point is called.
760  * The start hook is useful, for example, for setting up automatic
761  * configuration of C++ constructors.
762  *
763  * Inputs:
764  * tcb - The new, unstarted task task that needs the start hook
765  * starthook - The pointer to the start hook function
766  * arg - The argument to pass to the start hook function.
767  *
768  * Return:
769  * None
770  *
771  ********************************************************************************/
772 
773 #ifdef CONFIG_SCHED_STARTHOOK
774 
778 void task_starthook(FAR struct task_tcb_s *tcb, starthook_t starthook, FAR void *arg);
782 #endif
783 
784 /********************************************************************************
785  * Internal vfork support. The overall sequence is:
786  *
787  * 1) User code calls vfork(). vfork() is provided in architecture-specific
788  * code.
789  * 2) vfork()and calls task_vforksetup().
790  * 3) task_vforksetup() allocates and configures the child task's TCB. This
791  * consists of:
792  * - Allocation of the child task's TCB.
793  * - Initialization of file descriptors and streams
794  * - Configuration of environment variables
795  * - Setup the intput parameters for the task.
796  * - Initialization of the TCB (including call to up_initial_state()
797  * 4) vfork() provides any additional operating context. vfork must:
798  * - Allocate and initialize the stack
799  * - Initialize special values in any CPU registers that were not
800  * already configured by up_initial_state()
801  * 5) vfork() then calls task_vforkstart()
802  * 6) task_vforkstart() then executes the child thread.
803  *
804  * task_vforkabort() may be called if an error occurs between steps 3 and 6.
805  *
806  ********************************************************************************/
811 FAR struct task_tcb_s *task_vforksetup(start_t retaddr);
815 pid_t task_vforkstart(FAR struct task_tcb_s *child);
819 void task_vforkabort(FAR struct task_tcb_s *child, int errcode);
823 #undef EXTERN
824 #if defined(__cplusplus)
825 }
826 #endif
827 #endif /* __ASSEMBLY__ */
828 
829 #endif /* __INCLUDE_TINYARA_SCHED_H */
sigset_t sigwaitmask
Definition: sched.h:548
int curr_alloc_size
Definition: sched.h:575
int16_t lockcount
Definition: sched.h:510
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:357
This structure describes a reference counted D-Space region. This must be a separately allocated "bre...
Definition: sched.h:292
uint8_t base_priority
Definition: sched.h:505
start_t start
Definition: sched.h:496
sq_queue_t sigpendactionq
Definition: sched.h:550
Structure of pthread mutex configuration.
Definition: pthread.h:318
CODE void(* start_t)(void)
Definition: sched.h:231
void(* sched_foreach_t)(FAR struct tcb_s *tcb, FAR void *arg)
Definition: sched.h:659
FAR struct wdog_s * waitdog
Definition: sched.h:518
int timeslice
Definition: sched.h:516
FAR struct child_status_s * tg_children
Definition: sched.h:375
FAR void * pthread_addr_t
Definition: pthread.h:255
pid_t tg_task
Definition: sched.h:349
size_t tg_envsize
Definition: sched.h:407
main_t main
Definition: sched.h:238
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:266
structure for header queue
Definition: queue.h:113
#define PTHREAD_KEYS_MAX
Definition: pthread.h:210
CODE void(* starthook_t)(FAR void *arg)
Definition: sched.h:245
FAR pid_t * tg_members
Definition: sched.h:358
starthook_t starthook
Definition: sched.h:598
FAR void * joininfo
Definition: sched.h:627
sq_queue_t sigactionq
Definition: sched.h:549
int ch_status
Definition: sched.h:271
uint8_t task_state
Definition: sched.h:508
Pthread APIs.
enum tstate_e tstate_t
Definition: sched.h:220
uint8_t tg_flags
Definition: sched.h:351
siginfo_t sigunbinfo
Definition: sched.h:552
FAR struct join_s * tg_jointail
Definition: sched.h:391
size_t adj_stack_size
Definition: sched.h:522
Structure for using to pass parameters to/from signal handlers.
Definition: signal.h:297
sem_t * waitsem
Definition: sched.h:542
int peak_alloc_size
Definition: sched.h:576
FAR char ** argv
Definition: sched.h:605
Queue APIs.
This is the common part of the task control block (TCB). The TCB is the heart of the TinyAra task-con...
Definition: sched.h:471
pid_t pid
Definition: sched.h:485
pthread_addr_t arg
Definition: sched.h:626
Signal APIs.
Mqueue APIs.
CODE void(* pthread_destructor_t)(void *arg)
Definition: pthread.h:254
FAR void * starthookarg
Definition: sched.h:599
sq_queue_t sigpostedq
Definition: sched.h:551
FAR struct join_s * tg_joinhead
Definition: sched.h:390
int pterrno
Definition: sched.h:563
uint16_t flags
Definition: sched.h:509
FAR struct tcb_s * blink
Definition: sched.h:475
gid_t tg_gid
Definition: sched.h:343
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:340
CODE void(* atexitfunc_t)(void)
Definition: sched.h:253
entry_t entry
Definition: sched.h:497
uint8_t ch_flags
Definition: sched.h:269
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:200
FAR void * stack_alloc_ptr
Definition: sched.h:525
FAR struct task_group_s * group
Definition: sched.h:480
FAR char * tg_envp
Definition: sched.h:408
FAR struct pthread_mutex_s * mhead
Definition: sched.h:632
gid_t tg_pgid
Definition: sched.h:346
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...
sq_queue_t tg_onexitfunc
Definition: sched.h:369
struct task_group_s * flink
Definition: sched.h:342
FAR struct mqueue_inode_s * msgwaitq
Definition: sched.h:558
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:619
sq_queue_t sigpendingq
Definition: sched.h:401
uint8_t tg_nmembers
Definition: sched.h:355
uint8_t init_priority
Definition: sched.h:604
Structure of generic semaphore.
Definition: semaphore.h:113
pthread_addr_t(* pthread_startroutine_t)(pthread_addr_t)
Definition: pthread.h:257
sem_t tg_joinsem
Definition: sched.h:389
This is the entry point into the main thread of the task or into a created pthread within the task...
Definition: sched.h:236
uint16_t crefs
Definition: sched.h:299
FAR struct child_status_s * flink
Definition: sched.h:267
FAR struct dspace_s * dspace
Definition: sched.h:537
uint8_t sched_priority
Definition: sched.h:498
uint32_t sigset_t
Definition: signal.h:270
FAR void * adj_stack_ptr
Definition: sched.h:527
int num_alloc_free
Definition: sched.h:577
sigset_t sigprocmask
Definition: sched.h:547
FAR struct tcb_s * flink
Definition: sched.h:474
Time APIs.
This is the particular form of the task control block (TCB) structure used by tasks (and kernel threa...
Definition: sched.h:590
Structure of pthread region configuration.
Definition: pthread.h:264
CODE void(* onexitfunc_t)(int exitcode, FAR void *arg)
Definition: sched.h:257
FAR uint8_t * region
Definition: sched.h:306
pthread_startroutine_t pthread
Definition: sched.h:237
pid_t ch_pid
Definition: sched.h:270
sq_queue_t tg_msgdesq
Definition: sched.h:447