sched.h 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  1. /********************************************************************************
  2. * include/nuttx/sched.h
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ********************************************************************************/
  20. #ifndef __INCLUDE_NUTTX_SCHED_H
  21. #define __INCLUDE_NUTTX_SCHED_H
  22. /********************************************************************************
  23. * Included Files
  24. ********************************************************************************/
  25. #include <nuttx/config.h>
  26. #include <sys/types.h>
  27. #include <stdint.h>
  28. #include <queue.h>
  29. #include <signal.h>
  30. #include <semaphore.h>
  31. #include <pthread.h>
  32. #include <mqueue.h>
  33. #include <time.h>
  34. #include <nuttx/clock.h>
  35. #include <nuttx/irq.h>
  36. #include <nuttx/wdog.h>
  37. #include <nuttx/mm/shm.h>
  38. #include <nuttx/fs/fs.h>
  39. #include <nuttx/net/net.h>
  40. #include <arch/arch.h>
  41. /********************************************************************************
  42. * Pre-processor Definitions
  43. ********************************************************************************/
  44. /* Configuration ****************************************************************/
  45. /* Task groups currently only supported for retention of child status */
  46. #undef HAVE_GROUP_MEMBERS
  47. /* We need a group an group members if we are supporting the parent/child
  48. * relationship.
  49. */
  50. #if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS)
  51. # define HAVE_GROUP_MEMBERS 1
  52. #endif
  53. /* We don't need group members if support for pthreads is disabled */
  54. #ifdef CONFIG_DISABLE_PTHREAD
  55. # undef HAVE_GROUP_MEMBERS
  56. #endif
  57. /* Sporadic scheduling */
  58. #ifndef CONFIG_SCHED_SPORADIC_MAXREPL
  59. # define CONFIG_SCHED_SPORADIC_MAXREPL 3
  60. #endif
  61. /* Task Management Definitions **************************************************/
  62. /* Special task IDS. Any negative PID is invalid. */
  63. #define NULL_TASK_PROCESS_ID (pid_t)0
  64. #define INVALID_PROCESS_ID (pid_t)-1
  65. /* This is the maximum number of times that a lock can be set */
  66. #define MAX_LOCK_COUNT 127
  67. /* Values for the struct tcb_s flags bits */
  68. #define TCB_FLAG_TTYPE_SHIFT (0) /* Bits 0-1: thread type */
  69. #define TCB_FLAG_TTYPE_MASK (3 << TCB_FLAG_TTYPE_SHIFT)
  70. # define TCB_FLAG_TTYPE_TASK (0 << TCB_FLAG_TTYPE_SHIFT) /* Normal user task */
  71. # define TCB_FLAG_TTYPE_PTHREAD (1 << TCB_FLAG_TTYPE_SHIFT) /* User pthread */
  72. # define TCB_FLAG_TTYPE_KERNEL (2 << TCB_FLAG_TTYPE_SHIFT) /* Kernel thread */
  73. #define TCB_FLAG_NONCANCELABLE (1 << 2) /* Bit 2: Pthread is non-cancelable */
  74. #define TCB_FLAG_CANCEL_DEFERRED (1 << 3) /* Bit 3: Deferred (vs asynch) cancellation type */
  75. #define TCB_FLAG_CANCEL_PENDING (1 << 4) /* Bit 4: Pthread cancel is pending */
  76. #define TCB_FLAG_POLICY_SHIFT (5) /* Bit 5-6: Scheduling policy */
  77. #define TCB_FLAG_POLICY_MASK (3 << TCB_FLAG_POLICY_SHIFT)
  78. # define TCB_FLAG_SCHED_FIFO (0 << TCB_FLAG_POLICY_SHIFT) /* FIFO scheding policy */
  79. # define TCB_FLAG_SCHED_RR (1 << TCB_FLAG_POLICY_SHIFT) /* Round robin scheding policy */
  80. # define TCB_FLAG_SCHED_SPORADIC (2 << TCB_FLAG_POLICY_SHIFT) /* Sporadic scheding policy */
  81. # define TCB_FLAG_SCHED_OTHER (3 << TCB_FLAG_POLICY_SHIFT) /* Other scheding policy */
  82. #define TCB_FLAG_CPU_LOCKED (1 << 7) /* Bit 7: Locked to this CPU */
  83. #define TCB_FLAG_SIGNAL_ACTION (1 << 8) /* Bit 8: In a signal handler */
  84. #define TCB_FLAG_SYSCALL (1 << 9) /* Bit 9: In a system call */
  85. #define TCB_FLAG_EXIT_PROCESSING (1 << 10) /* Bit 10: Exitting */
  86. /* Bits 11-15: Available */
  87. /* Values for struct task_group tg_flags */
  88. #define GROUP_FLAG_NOCLDWAIT (1 << 0) /* Bit 0: Do not retain child exit status */
  89. #define GROUP_FLAG_ADDRENV (1 << 1) /* Bit 1: Group has an address environment */
  90. #define GROUP_FLAG_PRIVILEGED (1 << 2) /* Bit 2: Group is privileged */
  91. #define GROUP_FLAG_DELETED (1 << 3) /* Bit 3: Group has been deleted but not yet freed */
  92. /* Bits 4-7: Available */
  93. /* Values for struct child_status_s ch_flags */
  94. #define CHILD_FLAG_TTYPE_SHIFT (0) /* Bits 0-1: child thread type */
  95. #define CHILD_FLAG_TTYPE_MASK (3 << CHILD_FLAG_TTYPE_SHIFT)
  96. # define CHILD_FLAG_TTYPE_TASK (0 << CHILD_FLAG_TTYPE_SHIFT) /* Normal user task */
  97. # define CHILD_FLAG_TTYPE_PTHREAD (1 << CHILD_FLAG_TTYPE_SHIFT) /* User pthread */
  98. # define CHILD_FLAG_TTYPE_KERNEL (2 << CHILD_FLAG_TTYPE_SHIFT) /* Kernel thread */
  99. #define CHILD_FLAG_EXITED (1 << 2) /* Bit 2: The child thread has exit'ed */
  100. /* Bits 3-7: Available */
  101. /* Sporadic scheduler flags */
  102. #define SPORADIC_FLAG_ALLOCED (1 << 0) /* Bit 0: Timer is allocated */
  103. #define SPORADIC_FLAG_MAIN (1 << 1) /* Bit 1: The main timer */
  104. #define SPORADIC_FLAG_REPLENISH (1 << 2) /* Bit 2: Replenishment cycle */
  105. /* Bits 3-7: Available */
  106. /* Most internal nxsched_* interfaces are not available in the user space in
  107. * PROTECTED and KERNEL builds. In that context, the application semaphore
  108. * interfaces must be used. The differences between the two sets of
  109. * interfaces are: (1) the nxsched_* interfaces do not cause cancellation
  110. * points and (2) they do not modify the errno variable.
  111. *
  112. * This is only important when compiling libraries (libc or libnx) that are
  113. * used both by the OS (libkc.a and libknx.a) or by the applications
  114. * (libc.a and libnx.a). In that case, the correct interface must be
  115. * used for the build context.
  116. *
  117. * REVISIT: In the flat build, the same functions must be used both by
  118. * the OS and by applications. We have to use the normal user functions
  119. * in this case or we will fail to set the errno or fail to create the
  120. * cancellation point.
  121. */
  122. #if !defined(CONFIG_BUILD_FLAT) && defined(__KERNEL__)
  123. # define _SCHED_GETPARAM(t,p) nxsched_get_param(t,p)
  124. # define _SCHED_SETPARAM(t,p) nxsched_set_param(t,p)
  125. # define _SCHED_GETSCHEDULER(t) nxsched_get_scheduler(t)
  126. # define _SCHED_SETSCHEDULER(t,s,p) nxsched_set_scheduler(t,s,p)
  127. # define _SCHED_GETAFFINITY(t,c,m) nxsched_get_affinity(t,c,m)
  128. # define _SCHED_SETAFFINITY(t,c,m) nxsched_set_affinity(t,c,m)
  129. # define _SCHED_ERRNO(r) (-(r))
  130. # define _SCHED_ERRVAL(r) (r)
  131. #else
  132. # define _SCHED_GETPARAM(t,p) sched_getparam(t,p)
  133. # define _SCHED_SETPARAM(t,p) sched_setparam(t,p)
  134. # define _SCHED_GETSCHEDULER(t) sched_getscheduler(t)
  135. # define _SCHED_SETSCHEDULER(t,s,p) sched_setscheduler(t,s,p)
  136. # define _SCHED_GETAFFINITY(t,c,m) sched_getaffinity(t,c,m)
  137. # define _SCHED_SETAFFINITY(t,c,m) sched_setaffinity(t,c,m)
  138. # define _SCHED_ERRNO(r) errno
  139. # define _SCHED_ERRVAL(r) (-errno)
  140. #endif
  141. /* The number of callback can be saved */
  142. #if defined(CONFIG_SCHED_ONEXIT_MAX)
  143. # define CONFIG_SCHED_EXIT_MAX CONFIG_SCHED_ONEXIT_MAX
  144. #elif defined(CONFIG_SCHED_ATEXIT_MAX)
  145. # define CONFIG_SCHED_EXIT_MAX CONFIG_SCHED_ATEXIT_MAX
  146. #endif
  147. #if defined(CONFIG_SCHED_EXIT_MAX) && CONFIG_SCHED_EXIT_MAX < 1
  148. # error "CONFIG_SCHED_EXIT_MAX < 1"
  149. #endif
  150. /********************************************************************************
  151. * Public Type Definitions
  152. ********************************************************************************/
  153. #ifndef __ASSEMBLY__
  154. /* General Task Management Types ************************************************/
  155. /* This is the type of the task_state field of the TCB. NOTE: the order and
  156. * content of this enumeration is critical since there are some OS tables indexed
  157. * by these values. The range of values is assumed to fit into a uint8_t in
  158. * struct tcb_s.
  159. */
  160. enum tstate_e
  161. {
  162. TSTATE_TASK_INVALID = 0, /* INVALID - The TCB is uninitialized */
  163. TSTATE_TASK_PENDING, /* READY_TO_RUN - Pending preemption unlock */
  164. TSTATE_TASK_READYTORUN, /* READY-TO-RUN - But not running */
  165. #ifdef CONFIG_SMP
  166. TSTATE_TASK_ASSIGNED, /* READY-TO-RUN - Not running, but assigned to a CPU */
  167. #endif
  168. TSTATE_TASK_RUNNING, /* READY_TO_RUN - And running */
  169. TSTATE_TASK_INACTIVE, /* BLOCKED - Initialized but not yet activated */
  170. TSTATE_WAIT_SEM, /* BLOCKED - Waiting for a semaphore */
  171. TSTATE_WAIT_SIG, /* BLOCKED - Waiting for a signal */
  172. #ifndef CONFIG_DISABLE_MQUEUE
  173. TSTATE_WAIT_MQNOTEMPTY, /* BLOCKED - Waiting for a MQ to become not empty. */
  174. TSTATE_WAIT_MQNOTFULL, /* BLOCKED - Waiting for a MQ to become not full. */
  175. #endif
  176. #ifdef CONFIG_PAGING
  177. TSTATE_WAIT_PAGEFILL, /* BLOCKED - Waiting for page fill */
  178. #endif
  179. #ifdef CONFIG_SIG_SIGSTOP_ACTION
  180. TSTATE_TASK_STOPPED, /* BLOCKED - Waiting for SIGCONT */
  181. #endif
  182. NUM_TASK_STATES /* Must be last */
  183. };
  184. typedef enum tstate_e tstate_t;
  185. /* The following definitions are determined by tstate_t. Ordering of values
  186. * in the enumeration is important!
  187. */
  188. #define FIRST_READY_TO_RUN_STATE TSTATE_TASK_READYTORUN
  189. #define LAST_READY_TO_RUN_STATE TSTATE_TASK_RUNNING
  190. #define FIRST_ASSIGNED_STATE TSTATE_TASK_ASSIGNED
  191. #define LAST_ASSIGNED_STATE TSTATE_TASK_RUNNING
  192. #define FIRST_BLOCKED_STATE TSTATE_TASK_INACTIVE
  193. #define LAST_BLOCKED_STATE (NUM_TASK_STATES-1)
  194. /* The following is the form of a thread start-up function */
  195. typedef CODE void (*start_t)(void);
  196. /* This is the entry point into the main thread of the task or into a created
  197. * pthread within the task.
  198. */
  199. union entry_u
  200. {
  201. pthread_startroutine_t pthread;
  202. main_t main;
  203. };
  204. typedef union entry_u entry_t;
  205. /* This is the type of the function called at task startup */
  206. #ifdef CONFIG_SCHED_STARTHOOK
  207. typedef CODE void (*starthook_t)(FAR void *arg);
  208. #endif
  209. /* These are the types of the functions that are executed with exit() is called
  210. * (if registered via atexit() on on_exit()).
  211. */
  212. #ifdef CONFIG_SCHED_ATEXIT
  213. typedef CODE void (*atexitfunc_t)(void);
  214. #endif
  215. #ifdef CONFIG_SCHED_ONEXIT
  216. typedef CODE void (*onexitfunc_t)(int exitcode, FAR void *arg);
  217. #endif
  218. /* struct sporadic_s ************************************************************/
  219. #ifdef CONFIG_SCHED_SPORADIC
  220. /* This structure represents one replenishment interval. This is what is
  221. * received by each timeout handler.
  222. */
  223. struct sporadic_s;
  224. struct replenishment_s
  225. {
  226. FAR struct tcb_s *tcb; /* The parent TCB structure */
  227. struct wdog_s timer; /* Timer dedicated to this interval */
  228. uint32_t budget; /* Current budget time */
  229. uint8_t flags; /* See SPORADIC_FLAG_* definitions */
  230. };
  231. /* This structure is an allocated "plug-in" to the main TCB structure. It is
  232. * allocated when the sporadic scheduling policy is assigned to a thread. Thus,
  233. * in the context of numerous threads of varying policies, there the overhead
  234. * from this significant allocation is only borne by the threads with the
  235. * sporadic scheduling policy.
  236. */
  237. struct sporadic_s
  238. {
  239. bool suspended; /* Thread is currently suspended */
  240. uint8_t hi_priority; /* Sporadic high priority */
  241. uint8_t low_priority; /* Sporadic low priority */
  242. uint8_t max_repl; /* Maximum number of replenishments */
  243. uint8_t nrepls; /* Number of active replenishments */
  244. uint32_t repl_period; /* Sporadic replenishment period */
  245. uint32_t budget; /* Sporadic execution budget period */
  246. clock_t eventtime; /* Time thread suspended or [re-]started */
  247. /* This is the last interval timer activated */
  248. FAR struct replenishment_s *active;
  249. /* This is the list of replenishment interval timers */
  250. struct replenishment_s replenishments[CONFIG_SCHED_SPORADIC_MAXREPL];
  251. };
  252. #endif /* CONFIG_SCHED_SPORADIC */
  253. /* struct child_status_s ********************************************************/
  254. /* This structure is used to maintain information about child tasks. pthreads
  255. * work differently, they have join information. This is only for child tasks.
  256. */
  257. #ifdef CONFIG_SCHED_CHILD_STATUS
  258. struct child_status_s
  259. {
  260. FAR struct child_status_s *flink;
  261. uint8_t ch_flags; /* Child status: See CHILD_FLAG_* defns */
  262. pid_t ch_pid; /* Child task ID */
  263. int ch_status; /* Child exit status */
  264. };
  265. #endif
  266. /* struct pthread_cleanup_s *****************************************************/
  267. /* This structure describes one element of the pthread cleanup stack */
  268. #ifdef CONFIG_PTHREAD_CLEANUP
  269. struct pthread_cleanup_s
  270. {
  271. pthread_cleanup_t pc_cleaner; /* Cleanup callback address */
  272. FAR void *pc_arg; /* Argument that accompanies the callback */
  273. };
  274. #endif
  275. /* type tls_ndxset_t ************************************************************/
  276. /* Smallest addressable type that can hold the entire configured number of TLS
  277. * data indexes.
  278. */
  279. #if CONFIG_TLS_NELEM > 0
  280. # if CONFIG_TLS_NELEM > 32
  281. # error Too many TLS elements
  282. # elif CONFIG_TLS_NELEM > 16
  283. typedef uint32_t tls_ndxset_t;
  284. # elif CONFIG_TLS_NELEM > 8
  285. typedef uint16_t tls_ndxset_t;
  286. # else
  287. typedef uint8_t tls_ndxset_t;
  288. # endif
  289. #endif
  290. /* type grpid_t *****************************************************************/
  291. /* The task group ID */
  292. typedef int16_t grpid_t;
  293. /* struct dspace_s **************************************************************/
  294. /* This structure describes a reference counted D-Space region. This must be a
  295. * separately allocated "break-away" structure that can be owned by a task and
  296. * any pthreads created by the task.
  297. */
  298. #ifdef CONFIG_PIC
  299. struct dspace_s
  300. {
  301. /* The life of the structure allocation is determined by this reference
  302. * count. This count is number of threads that shared the same D-Space.
  303. * This includes the parent task as well as any pthreads created by the
  304. * parent task or any of its child threads.
  305. */
  306. uint16_t crefs;
  307. /* This is the allocated D-Space memory region. This may be a physical
  308. * address allocated with kmm_malloc(), or it may be virtual address associated
  309. * with an address environment (if CONFIG_ARCH_ADDRENV=y).
  310. */
  311. FAR uint8_t *region;
  312. };
  313. #endif
  314. /* struct stackinfo_s ***********************************************************/
  315. /* Used to report stack information */
  316. struct stackinfo_s
  317. {
  318. size_t adj_stack_size; /* Stack size after adjustment */
  319. /* for hardware, processor, etc. */
  320. /* (for debug purposes only) */
  321. FAR void *stack_alloc_ptr; /* Pointer to allocated stack */
  322. /* Needed to deallocate stack */
  323. FAR void *adj_stack_ptr; /* Adjusted stack_alloc_ptr for HW */
  324. /* The initial stack pointer value */
  325. };
  326. /* struct exitinfo_s ************************************************************/
  327. struct exitinfo_s
  328. {
  329. union
  330. {
  331. #ifdef CONFIG_SCHED_ATEXIT
  332. atexitfunc_t at;
  333. #endif
  334. #ifdef CONFIG_SCHED_ONEXIT
  335. onexitfunc_t on;
  336. #endif
  337. } func;
  338. #ifdef CONFIG_SCHED_ONEXIT
  339. FAR void *arg;
  340. #endif
  341. };
  342. /* struct task_group_s **********************************************************/
  343. /* All threads created by pthread_create belong in the same task group (along
  344. * with the thread of the original task). struct task_group_s is a shared
  345. * structure referenced by the TCB of each thread that is a member of the task
  346. * group.
  347. *
  348. * This structure should contain *all* resources shared by tasks and threads that
  349. * belong to the same task group:
  350. *
  351. * Child exit status
  352. * Environment variables
  353. * PIC data space and address environments
  354. * File descriptors
  355. * FILE streams
  356. * Sockets
  357. * Address environments.
  358. *
  359. * Each instance of struct task_group_s is reference counted. Each instance is
  360. * created with a reference count of one. The reference incremented when each
  361. * thread joins the group and decremented when each thread exits, leaving the
  362. * group. When the reference count decrements to zero, the struct task_group_s
  363. * is free.
  364. */
  365. #ifndef CONFIG_DISABLE_PTHREAD
  366. struct join_s; /* Forward reference */
  367. /* Defined in sched/pthread/pthread.h */
  368. #endif
  369. #ifdef CONFIG_BINFMT_LOADABLE
  370. struct binary_s; /* Forward reference */
  371. /* Defined in include/nuttx/binfmt/binfmt.h */
  372. #endif
  373. struct task_group_s
  374. {
  375. #if defined(HAVE_GROUP_MEMBERS) || defined(CONFIG_ARCH_ADDRENV)
  376. struct task_group_s *flink; /* Supports a singly linked list */
  377. grpid_t tg_grpid; /* The ID of this task group */
  378. #endif
  379. #ifdef HAVE_GROUP_MEMBERS
  380. grpid_t tg_pgrpid; /* The ID of the parent task group */
  381. #endif
  382. #if !defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_SCHED_HAVE_PARENT)
  383. pid_t tg_task; /* The ID of the task within the group */
  384. #endif
  385. uint8_t tg_flags; /* See GROUP_FLAG_* definitions */
  386. /* User identity **************************************************************/
  387. #ifdef CONFIG_SCHED_USER_IDENTITY
  388. uid_t tg_uid; /* User identity */
  389. gid_t tg_gid; /* User group identity */
  390. #endif
  391. /* Group membership ***********************************************************/
  392. uint8_t tg_nmembers; /* Number of members in the group */
  393. #ifdef HAVE_GROUP_MEMBERS
  394. uint8_t tg_mxmembers; /* Number of members in allocation */
  395. FAR pid_t *tg_members; /* Members of the group */
  396. #endif
  397. /* [at|on]exit support ********************************************************/
  398. #ifdef CONFIG_SCHED_EXIT_MAX
  399. struct exitinfo_s tg_exit[CONFIG_SCHED_EXIT_MAX];
  400. #endif
  401. #ifdef CONFIG_BINFMT_LOADABLE
  402. /* Loadable module support ****************************************************/
  403. FAR struct binary_s *tg_bininfo; /* Describes resources used by program */
  404. #endif
  405. #ifdef CONFIG_SCHED_HAVE_PARENT
  406. /* Child exit status **********************************************************/
  407. #ifdef CONFIG_SCHED_CHILD_STATUS
  408. FAR struct child_status_s *tg_children; /* Head of a list of child status */
  409. #endif
  410. #ifndef HAVE_GROUP_MEMBERS
  411. /* REVISIT: What if parent thread exits? Should use tg_pgrpid. */
  412. pid_t tg_ppid; /* This is the ID of the parent thread */
  413. #ifndef CONFIG_SCHED_CHILD_STATUS
  414. uint16_t tg_nchildren; /* This is the number active children */
  415. #endif
  416. #endif /* HAVE_GROUP_MEMBERS */
  417. #endif /* CONFIG_SCHED_HAVE_PARENT */
  418. #if defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_SCHED_HAVE_PARENT)
  419. /* waitpid support ************************************************************/
  420. /* Simple mechanism used only when there is no support for SIGCHLD */
  421. uint8_t tg_nwaiters; /* Number of waiters */
  422. uint8_t tg_waitflags; /* User flags for waitpid behavior */
  423. sem_t tg_exitsem; /* Support for waitpid */
  424. FAR int *tg_statloc; /* Location to return exit status */
  425. #endif
  426. #ifndef CONFIG_DISABLE_PTHREAD
  427. /* Pthreads *******************************************************************/
  428. /* Pthread join Info: */
  429. sem_t tg_joinsem; /* Mutually exclusive access to join data */
  430. FAR struct join_s *tg_joinhead; /* Head of a list of join data */
  431. FAR struct join_s *tg_jointail; /* Tail of a list of join data */
  432. #endif
  433. /* Thread local storage *******************************************************/
  434. #if CONFIG_TLS_NELEM > 0
  435. tls_ndxset_t tg_tlsset; /* Set of TLS data indexes allocated */
  436. #endif
  437. /* POSIX Signal Control Fields ************************************************/
  438. sq_queue_t tg_sigactionq; /* List of actions for signals */
  439. sq_queue_t tg_sigpendingq; /* List of pending signals */
  440. #ifdef CONFIG_SIG_DEFAULT
  441. sigset_t tg_sigdefault; /* Set of signals set to the default action */
  442. #endif
  443. #ifndef CONFIG_DISABLE_ENVIRON
  444. /* Environment variables ******************************************************/
  445. size_t tg_envsize; /* Size of environment string allocation */
  446. FAR char *tg_envp; /* Allocated environment strings */
  447. #endif
  448. #ifndef CONFIG_DISABLE_POSIX_TIMERS
  449. /* Interval timer *************************************************************/
  450. timer_t itimer;
  451. #endif
  452. /* PIC data space and address environments ************************************/
  453. /* Logically the PIC data space belongs here (see struct dspace_s). The
  454. * current logic needs review: There are differences in the away that the
  455. * life of the PIC data is managed.
  456. */
  457. /* File descriptors ***********************************************************/
  458. struct filelist tg_filelist; /* Maps file descriptor to file */
  459. #if CONFIG_NFILE_STREAMS > 0
  460. /* FILE streams ***************************************************************/
  461. /* In a flat, single-heap build. The stream list is allocated with this
  462. * structure. But kernel mode with a kernel allocator, it must be separately
  463. * allocated using a user-space allocator.
  464. */
  465. #ifdef CONFIG_MM_KERNEL_HEAP
  466. FAR struct streamlist *tg_streamlist;
  467. #else
  468. struct streamlist tg_streamlist; /* Holds C buffered I/O info */
  469. #endif
  470. #endif
  471. #ifdef CONFIG_NET
  472. /* Sockets ********************************************************************/
  473. struct socketlist tg_socketlist; /* Maps socket descriptor to socket */
  474. #endif
  475. #ifndef CONFIG_DISABLE_MQUEUE
  476. /* POSIX Named Message Queue Fields *******************************************/
  477. sq_queue_t tg_msgdesq; /* List of opened message queues */
  478. #endif
  479. #ifdef CONFIG_ARCH_ADDRENV
  480. /* Address Environment ********************************************************/
  481. group_addrenv_t tg_addrenv; /* Task group address environment */
  482. #endif
  483. #ifdef CONFIG_MM_SHM
  484. /* Shared Memory **************************************************************/
  485. struct group_shm_s tg_shm; /* Task shared memory logic */
  486. #endif
  487. };
  488. /* struct tcb_s *****************************************************************/
  489. /* This is the common part of the task control block (TCB). The TCB is the heart
  490. * of the NuttX task-control logic. Each task or thread is represented by a TCB
  491. * that includes these common definitions.
  492. */
  493. struct tcb_s
  494. {
  495. /* Fields used to support list management *************************************/
  496. FAR struct tcb_s *flink; /* Doubly linked list */
  497. FAR struct tcb_s *blink;
  498. /* Task Group *****************************************************************/
  499. FAR struct task_group_s *group; /* Pointer to shared task group data */
  500. /* Task Management Fields *****************************************************/
  501. pid_t pid; /* This is the ID of the thread */
  502. start_t start; /* Thread start function */
  503. entry_t entry; /* Entry Point into the thread */
  504. uint8_t sched_priority; /* Current priority of the thread */
  505. uint8_t init_priority; /* Initial priority of the thread */
  506. #ifdef CONFIG_PRIORITY_INHERITANCE
  507. #if CONFIG_SEM_NNESTPRIO > 0
  508. uint8_t npend_reprio; /* Number of nested reprioritizations */
  509. uint8_t pend_reprios[CONFIG_SEM_NNESTPRIO];
  510. #endif
  511. uint8_t base_priority; /* "Normal" priority of the thread */
  512. #endif
  513. uint8_t task_state; /* Current state of the thread */
  514. #ifdef CONFIG_SMP
  515. uint8_t cpu; /* CPU index if running or assigned */
  516. cpu_set_t affinity; /* Bit set of permitted CPUs */
  517. #endif
  518. uint16_t flags; /* Misc. general status flags */
  519. int16_t lockcount; /* 0=preemptible (not-locked) */
  520. #ifdef CONFIG_IRQCOUNT
  521. int16_t irqcount; /* 0=Not in critical section */
  522. #endif
  523. #ifdef CONFIG_CANCELLATION_POINTS
  524. int16_t cpcount; /* Nested cancellation point count */
  525. #endif
  526. int16_t errcode; /* Used to pass error information */
  527. #if CONFIG_RR_INTERVAL > 0 || defined(CONFIG_SCHED_SPORADIC)
  528. int32_t timeslice; /* RR timeslice OR Sporadic budget */
  529. /* interval remaining */
  530. #endif
  531. #ifdef CONFIG_SCHED_SPORADIC
  532. FAR struct sporadic_s *sporadic; /* Sporadic scheduling parameters */
  533. #endif
  534. struct wdog_s waitdog; /* All timed waits use this timer */
  535. /* Stack-Related Fields *******************************************************/
  536. size_t adj_stack_size; /* Stack size after adjustment */
  537. /* for hardware, processor, etc. */
  538. /* (for debug purposes only) */
  539. FAR void *stack_alloc_ptr; /* Pointer to allocated stack */
  540. /* Needed to deallocate stack */
  541. FAR void *adj_stack_ptr; /* Adjusted stack_alloc_ptr for HW */
  542. /* The initial stack pointer value */
  543. /* External Module Support ****************************************************/
  544. #ifdef CONFIG_PIC
  545. FAR struct dspace_s *dspace; /* Allocated area for .bss and .data */
  546. #endif
  547. /* POSIX Semaphore Control Fields *********************************************/
  548. sem_t *waitsem; /* Semaphore ID waiting on */
  549. /* POSIX Signal Control Fields ************************************************/
  550. sigset_t sigprocmask; /* Signals that are blocked */
  551. sigset_t sigwaitmask; /* Waiting for pending signals */
  552. sq_queue_t sigpendactionq; /* List of pending signal actions */
  553. sq_queue_t sigpostedq; /* List of posted signals */
  554. siginfo_t sigunbinfo; /* Signal info when task unblocked */
  555. /* POSIX Named Message Queue Fields *******************************************/
  556. #ifndef CONFIG_DISABLE_MQUEUE
  557. FAR struct mqueue_inode_s *msgwaitq; /* Waiting for this message queue */
  558. #endif
  559. /* Robust mutex support *******************************************************/
  560. #if !defined(CONFIG_DISABLE_PTHREAD) && !defined(CONFIG_PTHREAD_MUTEX_UNSAFE)
  561. FAR struct pthread_mutex_s *mhead; /* List of mutexes held by thread */
  562. #endif
  563. /* Clean-up stack *************************************************************/
  564. #ifdef CONFIG_PTHREAD_CLEANUP
  565. /* tos - The index to the next available entry at the top of the stack.
  566. * stack - The pre-allocated clean-up stack memory.
  567. */
  568. uint8_t tos;
  569. struct pthread_cleanup_s stack[CONFIG_PTHREAD_CLEANUP_STACKSIZE];
  570. #endif
  571. /* Pre-emption monitor support ************************************************/
  572. #ifdef CONFIG_SCHED_CRITMONITOR
  573. uint32_t premp_start; /* Time when preemption disabled */
  574. uint32_t premp_max; /* Max time preemption disabled */
  575. uint32_t crit_start; /* Time critical section entered */
  576. uint32_t crit_max; /* Max time in critical section */
  577. #endif
  578. /* State save areas ***********************************************************/
  579. /* The form and content of these fields are platform-specific. */
  580. struct xcptcontext xcp; /* Interrupt register save area */
  581. #if CONFIG_TASK_NAME_SIZE > 0
  582. char name[CONFIG_TASK_NAME_SIZE + 1]; /* Task name (with NUL terminator) */
  583. #endif
  584. };
  585. /* struct task_tcb_s ************************************************************/
  586. /* This is the particular form of the task control block (TCB) structure used by
  587. * tasks (and kernel threads). There are two TCB forms: one for pthreads and
  588. * one for tasks. Both share the common TCB fields (which must appear at the
  589. * top of the structure) plus additional fields unique to tasks and threads.
  590. * Having separate structures for tasks and pthreads adds some complexity, but
  591. * saves memory in that it prevents pthreads from being burdened with the
  592. * overhead required for tasks (and vice versa).
  593. */
  594. struct task_tcb_s
  595. {
  596. /* Common TCB fields **********************************************************/
  597. struct tcb_s cmn; /* Common TCB fields */
  598. /* Task Management Fields *****************************************************/
  599. #ifdef CONFIG_SCHED_STARTHOOK
  600. starthook_t starthook; /* Task startup function */
  601. FAR void *starthookarg; /* The argument passed to the function */
  602. #endif
  603. /* [Re-]start name + start-up parameters **************************************/
  604. FAR char **argv; /* Name+start-up parameters */
  605. };
  606. /* struct pthread_tcb_s *********************************************************/
  607. /* This is the particular form of the task control block (TCB) structure used by
  608. * pthreads. There are two TCB forms: one for pthreads and one for tasks. Both
  609. * share the common TCB fields (which must appear at the top of the structure)
  610. * plus additional fields unique to tasks and threads. Having separate
  611. * structures for tasks and pthreads adds some complexity, but saves memory in
  612. * that it prevents pthreads from being burdened with the overhead required for
  613. * tasks (and vice versa).
  614. */
  615. #ifndef CONFIG_DISABLE_PTHREAD
  616. struct pthread_tcb_s
  617. {
  618. /* Common TCB fields **********************************************************/
  619. struct tcb_s cmn; /* Common TCB fields */
  620. /* Task Management Fields *****************************************************/
  621. pthread_addr_t arg; /* Startup argument */
  622. FAR void *joininfo; /* Detach-able info to support join */
  623. };
  624. #endif /* !CONFIG_DISABLE_PTHREAD */
  625. /* This is the callback type used by nxsched_foreach() */
  626. typedef CODE void (*nxsched_foreach_t)(FAR struct tcb_s *tcb, FAR void *arg);
  627. #endif /* __ASSEMBLY__ */
  628. /********************************************************************************
  629. * Public Data
  630. ********************************************************************************/
  631. #ifndef __ASSEMBLY__
  632. #undef EXTERN
  633. #if defined(__cplusplus)
  634. #define EXTERN extern "C"
  635. extern "C"
  636. {
  637. #else
  638. #define EXTERN extern
  639. #endif
  640. #ifdef CONFIG_SCHED_CRITMONITOR
  641. /* Maximum time with pre-emption disabled or within critical section. */
  642. #ifdef CONFIG_SMP_NCPUS
  643. EXTERN uint32_t g_premp_max[CONFIG_SMP_NCPUS];
  644. EXTERN uint32_t g_crit_max[CONFIG_SMP_NCPUS];
  645. #else
  646. EXTERN uint32_t g_premp_max[1];
  647. EXTERN uint32_t g_crit_max[1];
  648. #endif
  649. #endif /* CONFIG_SCHED_CRITMONITOR */
  650. /********************************************************************************
  651. * Public Function Prototypes
  652. ********************************************************************************/
  653. /********************************************************************************
  654. * Name: nxsched_self
  655. *
  656. * Description:
  657. * Return the current threads TCB. Basically, this function just wraps the
  658. * head of the ready-to-run list and manages access to the TCB from outside
  659. * of the sched/ sub-directory.
  660. *
  661. ********************************************************************************/
  662. FAR struct tcb_s *nxsched_self(void);
  663. /********************************************************************************
  664. * Name: nxsched_foreach
  665. *
  666. * Description:
  667. * Enumerate over each task and provide the TCB of each task to a user
  668. * callback functions.
  669. *
  670. * NOTE: This function examines the TCB and calls each handler within a
  671. * critical section. However, that critical section is released and
  672. * reacquired for each TCB. When it is released, there may be changes in
  673. * tasking. If the caller requires absolute stability through the
  674. * traversal, then the caller should establish the critical section BEFORE
  675. * calling this function.
  676. *
  677. * Input Parameters:
  678. * handler - The function to be called with the TCB of
  679. * each task
  680. *
  681. * Returned Value:
  682. * None
  683. *
  684. ********************************************************************************/
  685. void nxsched_foreach(nxsched_foreach_t handler, FAR void *arg);
  686. /********************************************************************************
  687. * Name: nxsched_get_tcb
  688. *
  689. * Description:
  690. * Given a task ID, this function will return the a pointer to the
  691. * corresponding TCB (or NULL if there is no such task ID).
  692. *
  693. * NOTE: This function holds a critical section while examining TCB data
  694. * data structures but releases that critical section before returning.
  695. * When it is released, the TCB may become unstable. If the caller
  696. * requires absolute stability while using the TCB, then the caller
  697. * should establish the critical section BEFORE calling this function and
  698. * hold that critical section as long as necessary.
  699. *
  700. ********************************************************************************/
  701. FAR struct tcb_s *nxsched_get_tcb(pid_t pid);
  702. /********************************************************************************
  703. * Name: nxsched_releasepid
  704. *
  705. * Description:
  706. * When a task is destroyed, this function must be called to make its
  707. * process ID available for re-use.
  708. *
  709. ********************************************************************************/
  710. int nxsched_release_tcb(FAR struct tcb_s *tcb, uint8_t ttype);
  711. /* File system helpers **********************************************************/
  712. /* These functions all extract lists from the group structure associated with the
  713. * currently executing task.
  714. */
  715. FAR struct filelist *nxsched_get_files(void);
  716. #if CONFIG_NFILE_STREAMS > 0
  717. FAR struct streamlist *nxsched_get_streams(void);
  718. #endif /* CONFIG_NFILE_STREAMS */
  719. #ifdef CONFIG_NET
  720. FAR struct socketlist *nxsched_get_sockets(void);
  721. #endif
  722. /********************************************************************************
  723. * Name: nxtask_init
  724. *
  725. * Description:
  726. * This function initializes a Task Control Block (TCB) in preparation for
  727. * starting a new thread. It performs a subset of the functionality of
  728. * task_create()
  729. *
  730. * Unlike task_create():
  731. * 1. Allocate the TCB. The pre-allocated TCB is passed in argv.
  732. * 2. Allocate the stack. The pre-allocated stack is passed in argv.
  733. * 3. Activate the task. This must be done by calling nxtask_activate().
  734. *
  735. * Certain fields of the pre-allocated TCB may be set to change the
  736. * nature of the created task. For example:
  737. *
  738. * - Task type may be set in the TCB flags to create kernel thread
  739. *
  740. * Input Parameters:
  741. * tcb - Address of the new task's TCB
  742. * name - Name of the new task (not used)
  743. * priority - Priority of the new task
  744. * stack - Start of the pre-allocated stack
  745. * stack_size - Size (in bytes) of the stack allocated
  746. * entry - Application start point of the new task
  747. * argv - A pointer to an array of input parameters. The array
  748. * should be terminated with a NULL argv[] value. If no
  749. * parameters are required, argv may be NULL.
  750. *
  751. * Returned Value:
  752. * OK on success; negative error value on failure appropriately. (See
  753. * nxtask_setup_scheduler() for possible failure conditions). On failure,
  754. * the caller is responsible for freeing the stack memory and for calling
  755. * nxsched_release_tcb() to free the TCB (which could be in most any
  756. * state).
  757. *
  758. ********************************************************************************/
  759. int nxtask_init(FAR struct task_tcb_s *tcb, const char *name, int priority,
  760. FAR void *stack, uint32_t stack_size, main_t entry,
  761. FAR char * const argv[]);
  762. /********************************************************************************
  763. * Name: nxtask_uninit
  764. *
  765. * Description:
  766. * Undo all operations on a TCB performed by task_init() and release the
  767. * TCB by calling kmm_free(). This is intended primarily to support
  768. * error recovery operations after a successful call to task_init() such
  769. * was when a subsequent call to task_activate fails.
  770. *
  771. * Caution: Freeing of the TCB itself might be an unexpected side-effect.
  772. *
  773. * Input Parameters:
  774. * tcb - Address of the TCB initialized by task_init()
  775. *
  776. * Returned Value:
  777. * OK on success; negative error value on failure appropriately.
  778. *
  779. ********************************************************************************/
  780. void nxtask_uninit(FAR struct task_tcb_s *tcb);
  781. /********************************************************************************
  782. * Name: nxtask_activate
  783. *
  784. * Description:
  785. * This function activates tasks initialized by nxtask_setup_scheduler().
  786. * Without activation, a task is ineligible for execution by the
  787. * scheduler.
  788. *
  789. * Input Parameters:
  790. * tcb - The TCB for the task for the task (same as the nxtask_init
  791. * argument).
  792. *
  793. * Returned Value:
  794. * None
  795. *
  796. ********************************************************************************/
  797. void nxtask_activate(FAR struct tcb_s *tcb);
  798. /********************************************************************************
  799. * Name: nxtask_starthook
  800. *
  801. * Description:
  802. * Configure a start hook... a function that will be called on the thread
  803. * of the new task before the new task's main entry point is called.
  804. * The start hook is useful, for example, for setting up automatic
  805. * configuration of C++ constructors.
  806. *
  807. * Input Parameters:
  808. * tcb - The new, unstarted task task that needs the start hook
  809. * starthook - The pointer to the start hook function
  810. * arg - The argument to pass to the start hook function.
  811. *
  812. * Returned Value:
  813. * None
  814. *
  815. ********************************************************************************/
  816. #ifdef CONFIG_SCHED_STARTHOOK
  817. void nxtask_starthook(FAR struct task_tcb_s *tcb, starthook_t starthook,
  818. FAR void *arg);
  819. #endif
  820. /********************************************************************************
  821. * Name: nxtask_startup
  822. *
  823. * Description:
  824. * This function is the user-space, task startup function. It is called
  825. * from up_task_start() in user-mode.
  826. *
  827. * Input Parameters:
  828. * entrypt - The user-space address of the task entry point
  829. * argc and argv - Standard arguments for the task entry point
  830. *
  831. * Returned Value:
  832. * None. This function does not return.
  833. *
  834. ********************************************************************************/
  835. #ifndef CONFIG_BUILD_KERNEL
  836. void nxtask_startup(main_t entrypt, int argc, FAR char *argv[]);
  837. #endif
  838. /********************************************************************************
  839. * Internal vfork support. The overall sequence is:
  840. *
  841. * 1) User code calls vfork(). vfork() is provided in architecture-specific
  842. * code.
  843. * 2) vfork()and calls nxtask_setup_vfork().
  844. * 3) nxtask_setup_vfork() allocates and configures the child task's TCB. This
  845. * consists of:
  846. * - Allocation of the child task's TCB.
  847. * - Initialization of file descriptors and streams
  848. * - Configuration of environment variables
  849. * - Setup the input parameters for the task.
  850. * - Initialization of the TCB (including call to up_initial_state()
  851. * 4) vfork() provides any additional operating context. vfork must:
  852. * - Allocate and initialize the stack
  853. * - Initialize special values in any CPU registers that were not
  854. * already configured by up_initial_state()
  855. * 5) vfork() then calls nxtask_start_vfork()
  856. * 6) nxtask_start_vfork() then executes the child thread.
  857. *
  858. * nxtask_abort_vfork() may be called if an error occurs between steps 3 and 6.
  859. *
  860. ********************************************************************************/
  861. FAR struct task_tcb_s *nxtask_setup_vfork(start_t retaddr, size_t *argsize);
  862. pid_t nxtask_start_vfork(FAR struct task_tcb_s *child);
  863. void nxtask_abort_vfork(FAR struct task_tcb_s *child, int errcode);
  864. /********************************************************************************
  865. * Name: group_exitinfo
  866. *
  867. * Description:
  868. * This function may be called to when a task is loaded into memory. It
  869. * will setup the to automatically unload the module when the task exits.
  870. *
  871. * Input Parameters:
  872. * pid - The task ID of the newly loaded task
  873. * bininfo - This structure allocated with kmm_malloc(). This memory
  874. * persists until the task exits and will be used unloads
  875. * the module from memory.
  876. *
  877. * Returned Value:
  878. * This is a NuttX internal function so it follows the convention that
  879. * 0 (OK) is returned on success and a negated errno is returned on
  880. * failure.
  881. *
  882. ********************************************************************************/
  883. #ifdef CONFIG_BINFMT_LOADABLE
  884. struct binary_s; /* Forward reference */
  885. int group_exitinfo(pid_t pid, FAR struct binary_s *bininfo);
  886. #endif
  887. /********************************************************************************
  888. * Name: nxsched_resume_scheduler
  889. *
  890. * Description:
  891. * Called by architecture specific implementations that block task execution.
  892. * This function prepares the scheduler for the thread that is about to be
  893. * restarted.
  894. *
  895. * Input Parameters:
  896. * tcb - The TCB of the thread to be restarted.
  897. *
  898. * Returned Value:
  899. * None
  900. *
  901. ********************************************************************************/
  902. #if CONFIG_RR_INTERVAL > 0 || defined(CONFIG_SCHED_RESUMESCHEDULER)
  903. void nxsched_resume_scheduler(FAR struct tcb_s *tcb);
  904. #else
  905. # define nxsched_resume_scheduler(tcb)
  906. #endif
  907. /********************************************************************************
  908. * Name: nxsched_suspend_scheduler
  909. *
  910. * Description:
  911. * Called by architecture specific implementations to resume task execution.
  912. * This function performs scheduler operations for the thread that is about to
  913. * be suspended.
  914. *
  915. * Input Parameters:
  916. * tcb - The TCB of the thread to be restarted.
  917. *
  918. * Returned Value:
  919. * None
  920. *
  921. ********************************************************************************/
  922. #ifdef CONFIG_SCHED_SUSPENDSCHEDULER
  923. void nxsched_suspend_scheduler(FAR struct tcb_s *tcb);
  924. #else
  925. # define nxsched_suspend_scheduler(tcb)
  926. #endif
  927. /********************************************************************************
  928. * Name: nxsched_get_param
  929. *
  930. * Description:
  931. * This function gets the scheduling priority of the task specified by
  932. * pid. It is identical in function, differing only in its return value:
  933. * This function does not modify the errno variable.
  934. *
  935. * This is a non-standard, internal OS function and is not intended for
  936. * use by application logic. Applications should use the standard
  937. * sched_getparam().
  938. *
  939. * Input Parameters:
  940. * pid - the task ID of the task. If pid is zero, the priority
  941. * of the calling task is returned.
  942. * param - A structure whose member sched_priority is the integer
  943. * priority. The task's priority is copied to the sched_priority
  944. * element of this structure.
  945. *
  946. * Returned Value:
  947. * 0 (OK) if successful, otherwise a negated errno value is returned to
  948. * indicate the nature of the failure..
  949. *
  950. * This function can fail if param is null (EINVAL) or if pid does
  951. * not correspond to any task (ESRCH).
  952. *
  953. ********************************************************************************/
  954. struct sched_param; /* Forward reference */
  955. int nxsched_get_param (pid_t pid, FAR struct sched_param *param);
  956. /********************************************************************************
  957. * Name: nxsched_set_param
  958. *
  959. * Description:
  960. * This function sets the priority of a specified task. It is identical
  961. * to the function sched_setparam(), differing only in its return value:
  962. * This function does not modify the errno variable.
  963. *
  964. * NOTE: Setting a task's priority to the same value has a similar effect
  965. * to sched_yield() -- The task will be moved to after all other tasks
  966. * with the same priority.
  967. *
  968. * This is a non-standard, internal OS function and is not intended for
  969. * use by application logic. Applications should use the standard
  970. * sched_setparam().
  971. *
  972. * Input Parameters:
  973. * pid - the task ID of the task to reprioritize. If pid is zero, the
  974. * priority of the calling task is changed.
  975. * param - A structure whose member sched_priority is the integer priority.
  976. * The range of valid priority numbers is from SCHED_PRIORITY_MIN
  977. * through SCHED_PRIORITY_MAX.
  978. *
  979. * Returned Value:
  980. * 0 (OK) if successful, otherwise a negated errno value is returned to
  981. * indicate the nature of the failure..
  982. *
  983. * EINVAL The parameter 'param' is invalid or does not make sense for the
  984. * current scheduling policy.
  985. * EPERM The calling task does not have appropriate privileges.
  986. * ESRCH The task whose ID is pid could not be found.
  987. *
  988. ********************************************************************************/
  989. struct sched_param; /* Forward reference */
  990. int nxsched_set_param(pid_t pid, FAR const struct sched_param *param);
  991. /********************************************************************************
  992. * Name: nxsched_get_scheduler
  993. *
  994. * Description:
  995. * sched_getscheduler() returns the scheduling policy currently
  996. * applied to the task identified by pid. If pid equals zero, the
  997. * policy of the calling task will be retrieved.
  998. *
  999. * This functions is identical to the function sched_getscheduler(),
  1000. * differing only in its return value: This function does not modify
  1001. * the errno variable.
  1002. *
  1003. * This is a non-standard, internal OS function and is not intended for
  1004. * use by application logic. Applications should use the standard
  1005. * sched_getscheduler().
  1006. *
  1007. * Input Parameters:
  1008. * pid - the task ID of the task to query. If pid is zero, the
  1009. * calling task is queried.
  1010. *
  1011. * Returned Value:
  1012. * On success, sched_getscheduler() returns the policy for the task
  1013. * (either SCHED_FIFO or SCHED_RR). On error, a negated errno value
  1014. * returned:
  1015. *
  1016. * ESRCH The task whose ID is pid could not be found.
  1017. *
  1018. ********************************************************************************/
  1019. int nxsched_get_scheduler(pid_t pid);
  1020. /********************************************************************************
  1021. * Name: nxsched_set_scheduler
  1022. *
  1023. * Description:
  1024. * nxsched_set_scheduler() sets both the scheduling policy and the priority
  1025. * for the task identified by pid. If pid equals zero, the scheduler of
  1026. * the calling task will be set. The parameter 'param' holds the priority
  1027. * of the thread under the new policy.
  1028. *
  1029. * nxsched_set_scheduler() is identical to the function sched_getparam(),
  1030. * differing only in its return value: This function does not modify the
  1031. * errno variable.
  1032. *
  1033. * This is a non-standard, internal OS function and is not intended for
  1034. * use by application logic. Applications should use the standard
  1035. * sched_getparam().
  1036. *
  1037. * Input Parameters:
  1038. * pid - the task ID of the task to modify. If pid is zero, the calling
  1039. * task is modified.
  1040. * policy - Scheduling policy requested (either SCHED_FIFO or SCHED_RR)
  1041. * param - A structure whose member sched_priority is the new priority.
  1042. * The range of valid priority numbers is from SCHED_PRIORITY_MIN
  1043. * through SCHED_PRIORITY_MAX.
  1044. *
  1045. * Returned Value:
  1046. * On success, nxsched_set_scheduler() returns OK (zero). On error, a
  1047. * negated errno value is returned:
  1048. *
  1049. * EINVAL The scheduling policy is not one of the recognized policies.
  1050. * ESRCH The task whose ID is pid could not be found.
  1051. *
  1052. ********************************************************************************/
  1053. int nxsched_set_scheduler(pid_t pid, int policy,
  1054. FAR const struct sched_param *param);
  1055. /********************************************************************************
  1056. * Name: nxsched_get_affinity
  1057. *
  1058. * Description:
  1059. * nxsched_get_affinity() writes the affinity mask of the thread whose ID
  1060. * is pid into the cpu_set_t pointed to by mask. The cpusetsize
  1061. * argument specifies the size (in bytes) of mask. If pid is zero, then
  1062. * the mask of the calling thread is returned.
  1063. *
  1064. * nxsched_get_affinity() is identical to the function sched_getaffinity(),
  1065. * differing only in its return value: This function does not modify the
  1066. * errno variable.
  1067. *
  1068. * This is a non-standard, internal OS function and is not intended for
  1069. * use by application logic. Applications should use the standard
  1070. * sched_getparam().
  1071. *
  1072. * Input Parameters:
  1073. * pid - The ID of thread whose affinity set will be retrieved.
  1074. * cpusetsize - Size of mask. MUST be sizeofcpu_set_t().
  1075. * mask - The location to return the thread's new affinity set.
  1076. *
  1077. * Returned Value:
  1078. * Zero (OK) if successful. Otherwise, a negated errno value is returned:
  1079. *
  1080. * ESRCH The task whose ID is pid could not be found.
  1081. *
  1082. ********************************************************************************/
  1083. #ifdef CONFIG_SMP
  1084. int nxsched_get_affinity(pid_t pid, size_t cpusetsize, FAR cpu_set_t *mask);
  1085. #endif
  1086. /********************************************************************************
  1087. * Name: nxsched_set_affinity
  1088. *
  1089. * Description:
  1090. * sched_setaffinity() sets the CPU affinity mask of the thread whose ID
  1091. * is pid to the value specified by mask. If pid is zero, then the
  1092. * calling thread is used. The argument cpusetsize is the length (i
  1093. * bytes) of the data pointed to by mask. Normally this argument would
  1094. * be specified as sizeof(cpu_set_t).
  1095. *
  1096. * If the thread specified by pid is not currently running on one of the
  1097. * CPUs specified in mask, then that thread is migrated to one of the
  1098. * CPUs specified in mask.
  1099. *
  1100. * nxsched_set_affinity() is identical to the function sched_setparam(),
  1101. * differing only in its return value: This function does not modify
  1102. * the errno variable. This is a non-standard, internal OS function and
  1103. * is not intended for use by application logic. Applications should
  1104. * use the standard sched_setparam().
  1105. *
  1106. * Input Parameters:
  1107. * pid - The ID of thread whose affinity set will be modified.
  1108. * cpusetsize - Size of mask. MUST be sizeofcpu_set_t().
  1109. * mask - The location to return the thread's new affinity set.
  1110. *
  1111. * Returned Value:
  1112. * Zero (OK) if successful. Otherwise, a negated errno value is returned:
  1113. *
  1114. * ESRCH The task whose ID is pid could not be found.
  1115. *
  1116. ********************************************************************************/
  1117. #ifdef CONFIG_SMP
  1118. int nxsched_set_affinity(pid_t pid, size_t cpusetsize,
  1119. FAR const cpu_set_t *mask);
  1120. #endif
  1121. /********************************************************************************
  1122. * Name: nxsched_get_stackinfo
  1123. *
  1124. * Description:
  1125. * Report information about a thread's stack allocation.
  1126. *
  1127. * Input Parameters:
  1128. * pid - Identifies the thread to query. Zero is interpreted as the
  1129. * the calling thread
  1130. * stackinfo - User-provided location to return the stack information.
  1131. *
  1132. * Returned Value:
  1133. * Zero (OK) if successful. Otherwise, a negated errno value is returned.
  1134. *
  1135. * -ENOENT Returned if pid does not refer to an active task
  1136. * -EACCES The calling thread does not have privileges to access the
  1137. * stack of the thread associated with the pid.
  1138. *
  1139. ********************************************************************************/
  1140. int nxsched_get_stackinfo(pid_t pid, FAR struct stackinfo_s *stackinfo);
  1141. /********************************************************************************
  1142. * Name: nx_wait/nx_waitid/nx_waitpid
  1143. ********************************************************************************/
  1144. #ifdef CONFIG_SCHED_WAITPID
  1145. pid_t nx_wait(FAR int *stat_loc);
  1146. int nx_waitid(int idtype, id_t id, FAR siginfo_t *info, int options);
  1147. pid_t nx_waitpid(pid_t pid, FAR int *stat_loc, int options);
  1148. #endif
  1149. #undef EXTERN
  1150. #if defined(__cplusplus)
  1151. }
  1152. #endif
  1153. #endif /* __ASSEMBLY__ */
  1154. #endif /* __INCLUDE_NUTTX_SCHED_H */