2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
2014-08-09 00:44:08 +02:00
|
|
|
* sched/task/task_init.c
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
2020-03-26 17:40:10 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
2020-03-26 17:40:10 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
2020-03-26 17:40:10 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
2009-06-17 22:25:27 +02:00
|
|
|
****************************************************************************/
|
2007-03-02 22:27:47 +01:00
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
2007-03-02 22:27:47 +01:00
|
|
|
* Included Files
|
2009-06-17 22:25:27 +02:00
|
|
|
****************************************************************************/
|
2007-03-02 22:27:47 +01:00
|
|
|
|
2007-03-11 23:19:01 +01:00
|
|
|
#include <nuttx/config.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
|
2007-03-02 22:27:47 +01:00
|
|
|
#include <sys/types.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <stdint.h>
|
2007-03-02 22:27:47 +01:00
|
|
|
#include <sched.h>
|
2020-05-27 17:32:14 +02:00
|
|
|
#include <queue.h>
|
2013-01-25 18:23:38 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
2007-03-11 18:37:47 +01:00
|
|
|
#include <nuttx/arch.h>
|
2020-03-26 17:40:10 +01:00
|
|
|
#include <nuttx/sched.h>
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2014-08-08 22:06:42 +02:00
|
|
|
#include "group/group.h"
|
2014-08-09 00:44:08 +02:00
|
|
|
#include "task/task.h"
|
2007-03-02 22:27:47 +01:00
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
2007-03-02 22:27:47 +01:00
|
|
|
* Public Functions
|
2009-06-17 22:25:27 +02:00
|
|
|
****************************************************************************/
|
2007-03-02 22:27:47 +01:00
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
2020-05-25 23:30:40 +02:00
|
|
|
* Name: nxtask_init
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2009-06-17 22:25:27 +02:00
|
|
|
* This function initializes a Task Control Block (TCB) in preparation for
|
|
|
|
* starting a new thread. It performs a subset of the functionality of
|
|
|
|
* task_create()
|
2007-03-11 23:19:01 +01:00
|
|
|
*
|
2009-06-17 22:25:27 +02:00
|
|
|
* Unlike task_create():
|
2020-03-08 13:51:34 +01:00
|
|
|
* 1. Allocate the TCB. The pre-allocated TCB is passed in argv.
|
|
|
|
* 2. Allocate the stack. The pre-allocated stack is passed in argv.
|
2020-05-25 23:30:40 +02:00
|
|
|
* 3. Activate the task. This must be done by calling nxtask_activate().
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
2020-05-27 19:33:20 +02:00
|
|
|
* Certain fields of the pre-allocated TCB may be set to change the
|
|
|
|
* nature of the created task. For example:
|
|
|
|
*
|
2020-05-28 15:39:46 +02:00
|
|
|
* - Task type may be set in the TCB flags to create kernel thread
|
2020-05-27 19:33:20 +02:00
|
|
|
* - If a custom stack is used, i.e., one allocated, managed, and freed
|
|
|
|
* by the caller, then TCB_FLAG_CUSTOM_STACK should be set in the
|
|
|
|
* TCB flags.
|
|
|
|
*
|
2007-03-02 22:27:47 +01:00
|
|
|
* Input Parameters:
|
|
|
|
* tcb - Address of the new task's TCB
|
|
|
|
* name - Name of the new task (not used)
|
|
|
|
* priority - Priority of the new task
|
2007-03-11 23:19:01 +01:00
|
|
|
* stack - Start of the pre-allocated stack
|
|
|
|
* stack_size - Size (in bytes) of the stack allocated
|
2007-03-02 22:27:47 +01:00
|
|
|
* entry - Application start point of the new task
|
2020-03-08 13:51:34 +01:00
|
|
|
* argv - A pointer to an array of input parameters. The array
|
2009-06-17 22:25:27 +02:00
|
|
|
* should be terminated with a NULL argv[] value. If no
|
|
|
|
* parameters are required, argv may be NULL.
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2020-05-03 13:56:49 +02:00
|
|
|
* OK on success; negative error value on failure appropriately. (See
|
2020-05-16 18:05:46 +02:00
|
|
|
* nxtask_setup_scheduler() for possible failure conditions). On failure,
|
|
|
|
* the caller is responsible for freeing the stack memory and for calling
|
2020-05-09 17:05:29 +02:00
|
|
|
* nxsched_release_tcb() to free the TCB (which could be in most any
|
|
|
|
* state).
|
2007-03-02 22:27:47 +01:00
|
|
|
*
|
2009-06-17 22:25:27 +02:00
|
|
|
****************************************************************************/
|
2007-03-02 22:27:47 +01:00
|
|
|
|
2020-05-25 23:30:40 +02:00
|
|
|
int nxtask_init(FAR struct tcb_s *tcb, const char *name, int priority,
|
2020-05-27 17:32:14 +02:00
|
|
|
FAR uint32_t *stack, uint32_t stack_size,
|
|
|
|
main_t entry, FAR char * const argv[])
|
2007-03-02 22:27:47 +01:00
|
|
|
{
|
2013-02-04 22:24:00 +01:00
|
|
|
FAR struct task_tcb_s *ttcb = (FAR struct task_tcb_s *)tcb;
|
2009-12-13 19:01:46 +01:00
|
|
|
int ret;
|
2007-03-11 23:19:01 +01:00
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
/* Only tasks and kernel threads can be initialized in this way */
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
|
|
|
DEBUGASSERT(tcb &&
|
2017-08-15 01:19:27 +02:00
|
|
|
(tcb->flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_PTHREAD);
|
2013-02-04 22:24:00 +01:00
|
|
|
#endif
|
|
|
|
|
2013-01-25 18:23:38 +01:00
|
|
|
/* Create a new task group */
|
|
|
|
|
2014-09-03 22:58:24 +02:00
|
|
|
ret = group_allocate(ttcb, tcb->flags);
|
2013-01-25 18:23:38 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
/* Associate file descriptors with the new task */
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
ret = group_setuptaskfiles(ttcb);
|
2013-01-25 18:23:38 +01:00
|
|
|
if (ret < 0)
|
2009-06-17 22:25:27 +02:00
|
|
|
{
|
2013-01-25 18:23:38 +01:00
|
|
|
goto errout_with_group;
|
2009-06-17 22:25:27 +02:00
|
|
|
}
|
|
|
|
|
2007-03-11 23:19:01 +01:00
|
|
|
/* Configure the user provided stack region */
|
|
|
|
|
|
|
|
up_use_stack(tcb, stack, stack_size);
|
|
|
|
|
|
|
|
/* Initialize the task control block */
|
|
|
|
|
2020-05-16 17:06:29 +02:00
|
|
|
ret = nxtask_setup_scheduler(ttcb, priority, nxtask_start, entry,
|
This commit renames all internal OS functions defined under sched/task so that they begin with the prefix. For example, nxtask_exit() vs. task_exit().
Squashed commit of the following:
Trivial, cosmetic
sched/, arch/, and include: Rename task_vforkstart() as nxtask_vforkstart()
sched/, arch/, and include: Rename task_vforkabort() as nxtask_vforkabort()
sched/, arch/, and include: Rename task_vforksetup() as nxtask_vfork_setup()
sched/: Rename notify_cancellation() as nxnotify_cancellation()
sched/: Rename task_recover() to nxtask_recover()
sched/task, sched/pthread/, Documentation/: Rename task_argsetup() and task_terminate() to nxtask_argsetup() and nxtask_terminate(), respectively.
sched/task: Rename task_schedsetup() to nxtask_schedsetup()
sched/ (plus some binfmt/, include/, and arch/): Rename task_start() and task_starthook() to nxtask_start() and nxtask_starthook().
arch/ and sched/: Rename task_exit() and task_exithook() to nxtask_exit() and nxtask_exithook(), respectively.
sched/task: Rename all internal, static, functions to begin with the nx prefix.
2019-02-04 20:42:51 +01:00
|
|
|
TCB_FLAG_TTYPE_TASK);
|
2013-01-25 18:23:38 +01:00
|
|
|
if (ret < OK)
|
2007-03-11 23:19:01 +01:00
|
|
|
{
|
2013-01-26 00:21:27 +01:00
|
|
|
goto errout_with_group;
|
2007-03-11 23:19:01 +01:00
|
|
|
}
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2013-01-25 18:23:38 +01:00
|
|
|
/* Setup to pass parameters to the new task */
|
|
|
|
|
2020-05-16 17:06:29 +02:00
|
|
|
nxtask_setup_arguments(ttcb, name, argv);
|
2013-01-25 20:15:05 +01:00
|
|
|
|
|
|
|
/* Now we have enough in place that we can join the group */
|
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
ret = group_initialize(ttcb);
|
2013-01-25 20:15:05 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2013-01-26 00:21:27 +01:00
|
|
|
goto errout_with_group;
|
2013-01-25 20:15:05 +01:00
|
|
|
}
|
2019-02-11 19:09:26 +01:00
|
|
|
|
2013-01-25 18:23:38 +01:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_group:
|
|
|
|
group_leave(tcb);
|
|
|
|
|
|
|
|
errout:
|
2020-05-03 13:56:49 +02:00
|
|
|
return ret;
|
2009-06-17 22:25:27 +02:00
|
|
|
}
|
2020-05-27 17:32:14 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxtask_uninit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Undo all operations on a TCB performed by task_init() and release the
|
|
|
|
* TCB by calling kmm_free(). This is intended primarily to support
|
|
|
|
* error recovery operations after a successful call to task_init() such
|
|
|
|
* was when a subsequent call to task_activate fails.
|
|
|
|
*
|
|
|
|
* Caution: Freeing of the TCB itself might be an unexpected side-effect.
|
2020-05-27 19:33:20 +02:00
|
|
|
* The stack will also be freed UNLESS TCB_FLAG_CUSTOM_STACK was set in
|
|
|
|
* in the tcb->flags field when nxtask_init() was called.
|
2020-05-27 17:32:14 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* tcb - Address of the TCB initialized by task_init()
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; negative error value on failure appropriately.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void nxtask_uninit(FAR struct tcb_s *tcb)
|
|
|
|
{
|
|
|
|
/* The TCB was added to the inactive task list by
|
|
|
|
* nxtask_setup_scheduler().
|
|
|
|
*/
|
|
|
|
|
|
|
|
dq_rem((FAR dq_entry_t *)tcb, (FAR dq_queue_t *)&g_inactivetasks);
|
|
|
|
|
|
|
|
/* Release all resources associated with the TCB... Including the TCB
|
|
|
|
* itself.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nxsched_release_tcb((FAR struct tcb_s *)tcb,
|
|
|
|
tcb->flags & TCB_FLAG_TTYPE_MASK);
|
|
|
|
}
|