2008-01-30 19:49:31 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* sched/os_internal.h
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2013-01-23 00:42:51 +01:00
|
|
|
* Copyright (C) 2007-2013 Gregory Nutt. All rights reserved.
|
2011-10-19 21:35:21 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2008-01-30 19:49:31 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-02-18 00:21:28 +01:00
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2008-01-30 19:49:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-14 21:30:31 +02:00
|
|
|
#ifndef __SCHED_OS_INTERNAL_H
|
|
|
|
#define __SCHED_OS_INTERNAL_H
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 19:49:31 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2008-01-30 19:49:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <nuttx/config.h>
|
2009-12-14 22:15:18 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <sys/types.h>
|
2009-12-14 22:15:18 +01:00
|
|
|
#include <stdbool.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <queue.h>
|
|
|
|
#include <sched.h>
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
|
2008-01-30 19:49:31 +01:00
|
|
|
/****************************************************************************
|
2009-12-14 22:15:18 +01:00
|
|
|
* Pre-processor Definitions
|
2008-01-30 19:49:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2011-10-19 21:35:21 +02:00
|
|
|
/* OS CRASH CODES: All must lie in the range 0-99 */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
enum os_crash_codes_e
|
|
|
|
{
|
|
|
|
OSERR_NOERROR = 0, /* No error */
|
|
|
|
OSERR_NOTIMPLEMENTED, /* Feature is not implemented */
|
|
|
|
OSERR_INTERNAL, /* Internal logic error */
|
|
|
|
OSERR_UNEXPECTEDISR, /* Received unexpected interrupt */
|
|
|
|
OSERR_UNDEFINEDINSN, /* Undefined instruction */
|
|
|
|
OSERR_ERREXCEPTION, /* Other CPU-detected errors */
|
|
|
|
OSERR_OUTOFMEMORY, /* Insufficient memory */
|
|
|
|
OSERR_OUTOFMESSAGES, /* Out of messages */
|
|
|
|
OSERR_NOIDLETASK, /* There is no idle task */
|
|
|
|
OSERR_MQNONEMPTYCOUNT, /* Expected waiter for non-empty queue */
|
|
|
|
OSERR_MQNOTFULLCOUNT, /* Expected waiter for non-full queue */
|
2011-08-07 14:58:54 +02:00
|
|
|
OSERR_MQNOWAITER, /* Expected a queue for the waiter */
|
2007-02-18 00:21:28 +01:00
|
|
|
OSERR_BADWAITSEM, /* Already waiting for a semaphore */
|
|
|
|
OSERR_BADMSGTYPE, /* Tried to free a bad message type */
|
|
|
|
OSERR_FAILEDTOADDSIGNAL, /* Failed to add pending signal */
|
|
|
|
OSERR_FAILEDTOREMOVESIGNAL, /* Failed to remove pending signal */
|
|
|
|
OSERR_TIMEOUTNOTCB, /* Timed out, but not TCB registered */
|
|
|
|
OSERR_NOPENDINGSIGNAL, /* Expected a signal to be pending */
|
|
|
|
OSERR_BADDELETESTATE, /* Bad state in task deletion */
|
|
|
|
OSERR_WDOGNOTFOUND, /* Active watchdog not found */
|
|
|
|
OSERR_EXITFROMINTERRUPT, /* Interrupt code attempted to exit */
|
|
|
|
OSERR_BADUNBLOCKSTATE, /* Attempt to unblock from bad state */
|
|
|
|
OSERR_BADBLOCKSTATE, /* Attempt to block from bad state */
|
2008-01-09 15:20:31 +01:00
|
|
|
OSERR_BADREPRIORITIZESTATE /* Attempt to reprioritize in bad state or priority */
|
2007-02-18 00:21:28 +01:00
|
|
|
};
|
|
|
|
|
2013-01-13 19:53:00 +01:00
|
|
|
/* Special task IDS. Any negative PID is invalid. */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-01-13 19:53:00 +01:00
|
|
|
#define NULL_TASK_PROCESS_ID (pid_t)0
|
|
|
|
#define INVALID_PROCESS_ID (pid_t)-1
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Although task IDs can take the (positive, non-zero)
|
|
|
|
* range of pid_t, the number of tasks that will be supported
|
2007-03-01 22:05:55 +01:00
|
|
|
* at any one time is (artificially) limited by the CONFIG_MAX_TASKS
|
|
|
|
* configuration setting. Limiting the number of tasks speeds certain
|
2007-02-18 00:21:28 +01:00
|
|
|
* OS functions (this is the only limitation in the number of
|
|
|
|
* tasks built into the design).
|
|
|
|
*/
|
|
|
|
|
2007-03-01 22:05:55 +01:00
|
|
|
#define MAX_TASKS_MASK (CONFIG_MAX_TASKS-1)
|
2007-02-18 00:21:28 +01:00
|
|
|
#define PIDHASH(pid) ((pid) & MAX_TASKS_MASK)
|
|
|
|
|
2011-04-07 16:39:55 +02:00
|
|
|
/* One processor family supported by NuttX has a single, fixed hardware stack.
|
|
|
|
* That is the 8051 family. So for that family only, there is a variant form
|
|
|
|
* of kernel_thread() that does not take a stack size parameter. The following
|
|
|
|
* helper macro is provided to work around the ugliness of that exception.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_CUSTOM_STACK
|
2011-04-07 23:22:06 +02:00
|
|
|
# define KERNEL_THREAD(n,p,s,e,a) kernel_thread(n,p,s,e,a)
|
2011-04-07 16:39:55 +02:00
|
|
|
#else
|
2011-04-07 23:22:06 +02:00
|
|
|
# define KERNEL_THREAD(n,p,s,e,a) kernel_thread(n,p,e,a)
|
2011-04-07 16:39:55 +02:00
|
|
|
#endif
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* A more efficient ways to access the errno */
|
|
|
|
|
|
|
|
#define SET_ERRNO(e) \
|
2013-02-04 19:46:28 +01:00
|
|
|
{ struct tcb_s *rtcb = struct tcb_s*)g_readytorun.head; rtcb->pterrno = (e); }
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
#define _SET_TCB_ERRNO(t,e) \
|
2008-02-01 22:23:19 +01:00
|
|
|
{ (t)->pterrno = (e); }
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 19:49:31 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Type Definitions
|
2008-01-30 19:49:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* This structure defines the format of the hash table that
|
|
|
|
* is used to (1) determine if a task ID is unique, and (2)
|
|
|
|
* to map a process ID to its corresponding TCB.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct pidhash_s
|
|
|
|
{
|
2013-02-04 19:46:28 +01:00
|
|
|
FAR struct tcb_s *tcb;
|
|
|
|
pid_t pid;
|
2007-02-18 00:21:28 +01:00
|
|
|
};
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
typedef struct pidhash_s pidhash_t;
|
|
|
|
|
|
|
|
/* This structure defines an element of the g_tasklisttable[].
|
|
|
|
* This table is used to map a task_state enumeration to the
|
|
|
|
* corresponding task list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct tasklist_s
|
|
|
|
{
|
2007-03-14 19:58:21 +01:00
|
|
|
DSEG volatile dq_queue_t *list; /* Pointer to the task list */
|
2009-12-14 22:15:18 +01:00
|
|
|
bool prioritized; /* true if the list is prioritized */
|
2007-02-18 00:21:28 +01:00
|
|
|
};
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
typedef struct tasklist_s tasklist_t;
|
|
|
|
|
2008-01-30 19:49:31 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Global Variables
|
2008-01-30 19:49:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 19:49:31 +01:00
|
|
|
/* Declared in os_start.c ***************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-14 21:30:31 +02:00
|
|
|
/* The state of a task is indicated both by the task_state field of the TCB
|
|
|
|
* and by a series of task lists. All of these tasks lists are declared
|
|
|
|
* below. Although it is not always necessary, most of these lists are
|
|
|
|
* prioritized so that common list handling logic can be used (only the
|
|
|
|
* g_readytorun, the g_pendingtasks, and the g_waitingforsemaphore lists need
|
2007-02-18 00:21:28 +01:00
|
|
|
* to be prioritized).
|
|
|
|
*/
|
|
|
|
|
2012-07-14 21:30:31 +02:00
|
|
|
/* This is the list of all tasks that are ready to run. The head of this
|
|
|
|
* list is the currently active task; the tail of this list is always the
|
|
|
|
* IDLE task.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2007-03-14 19:58:21 +01:00
|
|
|
extern volatile dq_queue_t g_readytorun;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-14 21:30:31 +02:00
|
|
|
/* This is the list of all tasks that are ready-to-run, but cannot be placed
|
|
|
|
* in the g_readytorun list because: (1) They are higher priority than the
|
|
|
|
* currently active task at the head of the g_readytorun list, and (2) the
|
|
|
|
* currently active task has disabled pre-emption.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2007-03-14 19:58:21 +01:00
|
|
|
extern volatile dq_queue_t g_pendingtasks;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* This is the list of all tasks that are blocked waiting for a semaphore */
|
|
|
|
|
2007-03-14 19:58:21 +01:00
|
|
|
extern volatile dq_queue_t g_waitingforsemaphore;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* This is the list of all tasks that are blocked waiting for a signal */
|
|
|
|
|
2007-03-01 00:45:56 +01:00
|
|
|
#ifndef CONFIG_DISABLE_SIGNALS
|
2007-03-14 19:58:21 +01:00
|
|
|
extern volatile dq_queue_t g_waitingforsignal;
|
2007-03-01 00:45:56 +01:00
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* This is the list of all tasks that are blocked waiting for a message
|
|
|
|
* queue to become non-empty.
|
|
|
|
*/
|
|
|
|
|
2007-03-01 00:45:56 +01:00
|
|
|
#ifndef CONFIG_DISABLE_MQUEUE
|
2007-03-14 19:58:21 +01:00
|
|
|
extern volatile dq_queue_t g_waitingformqnotempty;
|
2007-03-01 00:45:56 +01:00
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* This is the list of all tasks that are blocked waiting for a message
|
|
|
|
* queue to become non-full.
|
|
|
|
*/
|
|
|
|
|
2007-03-01 00:45:56 +01:00
|
|
|
#ifndef CONFIG_DISABLE_MQUEUE
|
2007-03-14 19:58:21 +01:00
|
|
|
extern volatile dq_queue_t g_waitingformqnotfull;
|
2007-03-01 00:45:56 +01:00
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2010-08-15 17:02:45 +02:00
|
|
|
/* This is the list of all tasks that are blocking waiting for a page fill */
|
|
|
|
|
|
|
|
#ifdef CONFIG_PAGING
|
|
|
|
extern volatile dq_queue_t g_waitingforfill;
|
|
|
|
#endif
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* This the list of all tasks that have been initialized, but not yet
|
|
|
|
* activated. NOTE: This is the only list that is not prioritized.
|
|
|
|
*/
|
|
|
|
|
2007-03-14 19:58:21 +01:00
|
|
|
extern volatile dq_queue_t g_inactivetasks;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-10 16:32:33 +01:00
|
|
|
/* These are lists of dayed memory deallocations that need to be handled
|
|
|
|
* within the IDLE loop or worker thread. These deallocations get queued
|
|
|
|
* by sched_kufree and sched_kfree() if the OS needs to deallocate memory
|
|
|
|
* while it is within an interrupt handler.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2013-03-10 16:32:33 +01:00
|
|
|
extern volatile sq_queue_t g_delayed_kufree;
|
|
|
|
|
|
|
|
#if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
|
|
|
|
extern volatile sq_queue_t g_delayed_kfree;
|
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* This is the value of the last process ID assigned to a task */
|
|
|
|
|
2007-03-14 19:58:21 +01:00
|
|
|
extern volatile pid_t g_lastpid;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* The following hash table is used for two things:
|
2007-03-01 22:05:55 +01:00
|
|
|
*
|
2012-07-14 21:30:31 +02:00
|
|
|
* 1. This hash table greatly speeds the determination of a new unique
|
|
|
|
* process ID for a task, and
|
2007-02-18 00:21:28 +01:00
|
|
|
* 2. Is used to quickly map a process ID into a TCB.
|
2007-03-01 22:05:55 +01:00
|
|
|
*
|
2012-07-14 21:30:31 +02:00
|
|
|
* It has the side effects of using more memory and limiting the number
|
|
|
|
* of tasks to CONFIG_MAX_TASKS.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2007-03-01 22:05:55 +01:00
|
|
|
extern pidhash_t g_pidhash[CONFIG_MAX_TASKS];
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-14 21:30:31 +02:00
|
|
|
/* This is a table of task lists. This table is indexed by the task state
|
|
|
|
* enumeration type (tstate_t) and provides a pointer to the associated
|
|
|
|
* static task list (if there is one) as well as a boolean indication as to
|
|
|
|
* if the list is an ordered list or not.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
extern const tasklist_t g_tasklisttable[NUM_TASK_STATES];
|
|
|
|
|
2008-01-30 19:49:31 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Function Prototypes
|
2008-01-30 19:49:31 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-14 21:30:31 +02:00
|
|
|
int os_bringup(void);
|
2013-01-26 00:21:27 +01:00
|
|
|
#ifdef CONFIG_SCHED_CHILD_STATUS
|
|
|
|
void weak_function task_initialize(void);
|
|
|
|
#endif
|
2012-07-14 21:30:31 +02:00
|
|
|
void task_start(void);
|
2013-02-04 22:24:00 +01:00
|
|
|
int task_schedsetup(FAR struct task_tcb_s *tcb, int priority, start_t start,
|
2013-01-23 23:23:46 +01:00
|
|
|
main_t main, uint8_t ttype);
|
2013-02-04 22:24:00 +01:00
|
|
|
int task_argsetup(FAR struct task_tcb_s *tcb, FAR const char *name, FAR char * const argv[]);
|
2013-02-04 19:46:28 +01:00
|
|
|
void task_exithook(FAR struct tcb_s *tcb, int status);
|
2012-07-14 21:30:31 +02:00
|
|
|
int task_deletecurrent(void);
|
2013-02-06 16:43:28 +01:00
|
|
|
void task_recover(FAR struct tcb_s *tcb);
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2011-04-07 16:39:55 +02:00
|
|
|
#ifndef CONFIG_CUSTOM_STACK
|
2012-07-14 21:30:31 +02:00
|
|
|
int kernel_thread(FAR const char *name, int priority, int stack_size,
|
2013-02-02 20:31:30 +01:00
|
|
|
main_t entry, FAR char * const argv[]);
|
2011-04-07 16:39:55 +02:00
|
|
|
#else
|
2012-07-14 21:30:31 +02:00
|
|
|
int kernel_thread(FAR const char *name, int priority, main_t entry,
|
2013-02-02 20:31:30 +01:00
|
|
|
FAR char * const argv[]);
|
2011-04-07 16:39:55 +02:00
|
|
|
#endif
|
2013-02-04 19:46:28 +01:00
|
|
|
bool sched_addreadytorun(FAR struct tcb_s *rtrtcb);
|
|
|
|
bool sched_removereadytorun(FAR struct tcb_s *rtrtcb);
|
|
|
|
bool sched_addprioritized(FAR struct tcb_s *newTcb, DSEG dq_queue_t *list);
|
2012-07-14 21:30:31 +02:00
|
|
|
bool sched_mergepending(void);
|
2013-02-04 19:46:28 +01:00
|
|
|
void sched_addblocked(FAR struct tcb_s *btcb, tstate_t task_state);
|
|
|
|
void sched_removeblocked(FAR struct tcb_s *btcb);
|
|
|
|
int sched_setpriority(FAR struct tcb_s *tcb, int sched_priority);
|
2009-03-11 00:52:46 +01:00
|
|
|
#ifdef CONFIG_PRIORITY_INHERITANCE
|
2013-02-04 19:46:28 +01:00
|
|
|
int sched_reprioritize(FAR struct tcb_s *tcb, int sched_priority);
|
2009-03-11 00:52:46 +01:00
|
|
|
#else
|
|
|
|
# define sched_reprioritize(tcb,sched_priority) sched_setpriority(tcb,sched_priority)
|
|
|
|
#endif
|
2013-02-04 19:46:28 +01:00
|
|
|
FAR struct tcb_s *sched_gettcb(pid_t pid);
|
|
|
|
bool sched_verifytcb(FAR struct tcb_s *tcb);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-20 19:22:21 +01:00
|
|
|
int sched_releasetcb(FAR struct tcb_s *tcb, uint8_t ttype);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-14 21:30:31 +02:00
|
|
|
#endif /* __SCHED_OS_INTERNAL_H */
|