2008-01-29 08:50:16 +08:00
|
|
|
/****************************************************************************
|
2014-08-09 06:44:08 +08:00
|
|
|
* sched/task/task_create.c
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2024-09-11 19:45:11 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-03-27 00:40:10 +08: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-02-18 07:21:28 +08:00
|
|
|
*
|
2020-03-27 00:40:10 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2020-03-27 00:40:10 +08: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-02-18 07:21:28 +08:00
|
|
|
*
|
2008-01-29 08:50:16 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2008-01-29 08:50:16 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Included Files
|
2008-01-29 08:50:16 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-03-12 06:19:01 +08:00
|
|
|
#include <nuttx/config.h>
|
2011-04-04 04:41:49 +08:00
|
|
|
|
2007-02-18 07:21:28 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
2007-02-20 06:51:18 +08:00
|
|
|
#include <debug.h>
|
2011-04-04 04:41:49 +08:00
|
|
|
|
2007-02-18 07:21:28 +08:00
|
|
|
#include <nuttx/arch.h>
|
2011-04-04 04:41:49 +08:00
|
|
|
#include <nuttx/kmalloc.h>
|
2020-03-27 00:40:10 +08:00
|
|
|
#include <nuttx/sched.h>
|
2013-12-31 07:55:19 +08:00
|
|
|
#include <nuttx/kthread.h>
|
2023-10-22 17:06:46 +08:00
|
|
|
#include <nuttx/fs/fs.h>
|
2011-04-04 04:41:49 +08:00
|
|
|
|
2014-08-09 07:53:55 +08:00
|
|
|
#include "sched/sched.h"
|
2014-08-09 04:06:42 +08:00
|
|
|
#include "group/group.h"
|
2014-08-09 06:44:08 +08:00
|
|
|
#include "task/task.h"
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2008-01-29 08:50:16 +08:00
|
|
|
/****************************************************************************
|
2022-10-16 21:27:03 +08:00
|
|
|
* Public Functions
|
2008-01-29 08:50:16 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2008-01-29 08:50:16 +08:00
|
|
|
/****************************************************************************
|
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-05 03:42:51 +08:00
|
|
|
* Name: nxthread_create
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2011-04-07 22:39:55 +08:00
|
|
|
* This function creates and activates a new thread of the specified type
|
|
|
|
* with a specified priority and returns its system-assigned ID. It is the
|
2017-10-17 01:38:00 +08:00
|
|
|
* internal, common implementation of task_create() and kthread_create().
|
2012-07-15 03:30:31 +08:00
|
|
|
* See comments with task_create() for further information.
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* name - Name of the new task
|
2013-01-24 06:23:46 +08:00
|
|
|
* ttype - Type of the new task
|
2007-02-18 07:21:28 +08:00
|
|
|
* priority - Priority of the new task
|
2022-10-20 03:27:21 +08:00
|
|
|
* stack_addr - Address of the stack needed
|
|
|
|
* stack_size - Size (in bytes) of the stack needed
|
2007-02-18 07:21:28 +08:00
|
|
|
* entry - Entry point of a new task
|
2018-08-09 07:06:46 +08:00
|
|
|
* arg - A pointer to an array of input parameters. The array
|
2012-07-15 03:30:31 +08:00
|
|
|
* should be terminated with a NULL argv[] value. If no
|
|
|
|
* parameters are required, argv may be NULL.
|
2022-10-16 00:37:58 +08:00
|
|
|
* envp - A pointer to an array of environment strings. Terminated
|
|
|
|
* with a NULL entry.
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2018-02-01 06:11:54 +08:00
|
|
|
* Returns the positive, non-zero process ID of the new task or a negated
|
|
|
|
* errno value to indicate the nature of any failure. If memory is
|
|
|
|
* insufficient or the task cannot be created -ENOMEM will be returned.
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2008-01-29 08:50:16 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2022-10-16 21:27:03 +08:00
|
|
|
int nxthread_create(FAR const char *name, uint8_t ttype, int priority,
|
2022-10-20 03:27:21 +08:00
|
|
|
FAR void *stack_addr, int stack_size, main_t entry,
|
2022-10-16 21:27:03 +08:00
|
|
|
FAR char * const argv[], FAR char * const envp[])
|
2007-02-18 07:21:28 +08:00
|
|
|
{
|
2024-05-17 06:11:52 +08:00
|
|
|
FAR struct tcb_s *tcb;
|
2007-02-18 07:21:28 +08:00
|
|
|
pid_t pid;
|
2011-04-07 22:39:55 +08:00
|
|
|
int ret;
|
2007-02-18 07:21:28 +08:00
|
|
|
|
|
|
|
/* Allocate a TCB for the new task. */
|
|
|
|
|
2024-05-17 06:11:52 +08:00
|
|
|
tcb = kmm_zalloc(ttype == TCB_FLAG_TTYPE_KERNEL ?
|
|
|
|
sizeof(struct tcb_s) : sizeof(struct task_tcb_s));
|
2007-02-18 07:21:28 +08:00
|
|
|
if (!tcb)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
serr("ERROR: Failed to allocate TCB\n");
|
2018-02-01 06:11:54 +08:00
|
|
|
return -ENOMEM;
|
2007-02-18 07:21:28 +08:00
|
|
|
}
|
|
|
|
|
2020-06-29 10:44:33 +08:00
|
|
|
/* Setup the task type */
|
2013-01-26 01:23:38 +08:00
|
|
|
|
2024-05-17 06:11:52 +08:00
|
|
|
tcb->flags = ttype | TCB_FLAG_FREE_TCB;
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2020-06-29 10:44:33 +08:00
|
|
|
/* Initialize the task */
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2024-05-17 06:11:52 +08:00
|
|
|
ret = nxtask_init((FAR struct task_tcb_s *)tcb, name, priority,
|
|
|
|
stack_addr, stack_size, entry, argv, envp, NULL);
|
2013-02-09 05:42:23 +08:00
|
|
|
if (ret < OK)
|
2007-02-18 07:21:28 +08:00
|
|
|
{
|
2020-06-29 10:44:33 +08:00
|
|
|
kmm_free(tcb);
|
|
|
|
return ret;
|
2013-01-26 03:15:05 +08:00
|
|
|
}
|
|
|
|
|
2007-03-12 06:19:01 +08:00
|
|
|
/* Get the assigned pid before we start the task */
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2024-05-17 06:11:52 +08:00
|
|
|
pid = tcb->pid;
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-03-12 06:19:01 +08:00
|
|
|
/* Activate the task */
|
|
|
|
|
2024-05-17 06:11:52 +08:00
|
|
|
nxtask_activate(tcb);
|
2020-05-27 23:15:45 +08:00
|
|
|
|
2023-06-19 04:20:04 +08:00
|
|
|
return pid;
|
2011-04-07 22:39:55 +08:00
|
|
|
}
|
|
|
|
|
2018-02-01 06:11:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxtask_create
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function creates and activates a new task with a specified
|
|
|
|
* priority and returns its system-assigned ID.
|
|
|
|
*
|
|
|
|
* The entry address entry is the address of the "main" function of the
|
|
|
|
* task. This function will be called once the C environment has been
|
|
|
|
* set up. The specified function will be called with four arguments.
|
|
|
|
* Should the specified routine return, a call to exit() will
|
|
|
|
* automatically be made.
|
|
|
|
*
|
|
|
|
* Note that four (and only four) arguments must be passed for the spawned
|
|
|
|
* functions.
|
|
|
|
*
|
|
|
|
* nxtask_create() is identical to the function task_create(), differing
|
|
|
|
* only in its return value: This function does not modify the errno
|
|
|
|
* variable. This is a non-standard, internal OS function and is not
|
|
|
|
* intended for use by application logic. Applications should use
|
|
|
|
* task_create().
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* name - Name of the new task
|
|
|
|
* priority - Priority of the new task
|
2022-10-20 03:27:21 +08:00
|
|
|
* stack_addr - Address of the stack needed
|
|
|
|
* stack_size - Size (in bytes) of the stack needed
|
2018-02-01 06:11:54 +08:00
|
|
|
* entry - Entry point of a new task
|
2018-08-09 07:06:46 +08:00
|
|
|
* arg - A pointer to an array of input parameters. The array
|
2018-02-01 06:11:54 +08:00
|
|
|
* should be terminated with a NULL argv[] value. If no
|
|
|
|
* parameters are required, argv may be NULL.
|
2022-10-16 00:37:58 +08:00
|
|
|
* envp - A pointer to an array of environment strings. Terminated
|
|
|
|
* with a NULL entry.
|
2018-02-01 06:11:54 +08:00
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2018-02-01 06:11:54 +08:00
|
|
|
* Returns the positive, non-zero process ID of the new task or a negated
|
|
|
|
* errno value to indicate the nature of any failure. If memory is
|
|
|
|
* insufficient or the task cannot be created -ENOMEM will be returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-10-20 03:27:21 +08:00
|
|
|
int nxtask_create(FAR const char *name, int priority,
|
|
|
|
FAR void *stack_addr, int stack_size, main_t entry,
|
2022-10-16 00:37:58 +08:00
|
|
|
FAR char * const argv[], FAR char * const envp[])
|
2018-02-01 06:11:54 +08:00
|
|
|
{
|
2022-10-20 03:27:21 +08:00
|
|
|
return nxthread_create(name, TCB_FLAG_TTYPE_TASK, priority, stack_addr,
|
2022-10-16 00:37:58 +08:00
|
|
|
stack_size, entry, argv, envp ? envp : environ);
|
2018-02-01 06:11:54 +08:00
|
|
|
}
|
|
|
|
|
2011-04-07 22:39:55 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: task_create
|
|
|
|
*
|
|
|
|
* Description:
|
2012-07-15 03:30:31 +08:00
|
|
|
* This function creates and activates a new task with a specified
|
|
|
|
* priority and returns its system-assigned ID.
|
2011-04-07 22:39:55 +08:00
|
|
|
*
|
2012-07-15 03:30:31 +08:00
|
|
|
* The entry address entry is the address of the "main" function of the
|
|
|
|
* task. This function will be called once the C environment has been
|
|
|
|
* set up. The specified function will be called with four arguments.
|
|
|
|
* Should the specified routine return, a call to exit() will
|
2011-04-07 22:39:55 +08:00
|
|
|
* automatically be made.
|
|
|
|
*
|
2012-07-15 03:30:31 +08:00
|
|
|
* Note that four (and only four) arguments must be passed for the spawned
|
|
|
|
* functions.
|
2011-04-07 22:39:55 +08:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* name - Name of the new task
|
|
|
|
* priority - Priority of the new task
|
|
|
|
* stack_size - size (in bytes) of the stack needed
|
|
|
|
* entry - Entry point of a new task
|
2018-08-09 07:06:46 +08:00
|
|
|
* arg - A pointer to an array of input parameters. The array
|
2012-07-15 03:30:31 +08:00
|
|
|
* should be terminated with a NULL argv[] value. If no
|
|
|
|
* parameters are required, argv may be NULL.
|
2011-04-07 22:39:55 +08:00
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2011-04-07 22:39:55 +08:00
|
|
|
* Returns the non-zero process ID of the new task or ERROR if memory is
|
2018-02-01 06:11:54 +08:00
|
|
|
* insufficient or the task cannot be created. The errno will be set in
|
|
|
|
* the failure case to indicate the nature of the error.
|
2011-04-07 22:39:55 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-17 05:40:48 +08:00
|
|
|
#ifndef CONFIG_BUILD_KERNEL
|
2022-10-20 03:27:21 +08:00
|
|
|
int task_create_with_stack(FAR const char *name, int priority,
|
|
|
|
FAR void *stack_addr, int stack_size,
|
|
|
|
main_t entry, FAR char * const argv[])
|
2011-04-07 22:39:55 +08:00
|
|
|
{
|
2022-10-20 03:27:21 +08:00
|
|
|
int ret = nxtask_create(name, priority, stack_addr,
|
|
|
|
stack_size, entry, argv, NULL);
|
2018-02-01 06:11:54 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2007-02-18 07:21:28 +08:00
|
|
|
}
|
2022-10-20 03:27:21 +08:00
|
|
|
|
|
|
|
int task_create(FAR const char *name, int priority,
|
|
|
|
int stack_size, main_t entry, FAR char * const argv[])
|
|
|
|
{
|
|
|
|
return task_create_with_stack(name, priority, NULL,
|
|
|
|
stack_size, entry, argv);
|
|
|
|
}
|
2014-09-14 22:22:21 +08:00
|
|
|
#endif
|
2011-04-07 22:39:55 +08:00
|
|
|
|
2021-06-26 02:31:34 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: kthread_create_with_stack
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function creates and activates a kernel thread task with
|
|
|
|
* kernel-mode privileges. It is identical to kthread_create() except
|
|
|
|
* that it get the stack memory from caller.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* name - Name of the new task
|
|
|
|
* priority - Priority of the new task
|
2022-10-20 03:27:21 +08:00
|
|
|
* stack_addr - Stack buffer of the new task
|
2021-06-26 02:31:34 +08:00
|
|
|
* stack_size - Stack size of the new task
|
|
|
|
* entry - Entry point of a new task
|
|
|
|
* arg - A pointer to an array of input parameters. The array
|
|
|
|
* should be terminated with a NULL argv[] value. If no
|
|
|
|
* parameters are required, argv may be NULL.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Returns the positive, non-zero process ID of the new task or a negated
|
|
|
|
* errno value to indicate the nature of any failure. If memory is
|
|
|
|
* insufficient or the task cannot be created -ENOMEM will be returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int kthread_create_with_stack(FAR const char *name, int priority,
|
2022-10-20 03:27:21 +08:00
|
|
|
FAR void *stack_addr, int stack_size,
|
2021-06-26 02:31:34 +08:00
|
|
|
main_t entry, FAR char * const argv[])
|
|
|
|
{
|
|
|
|
return nxthread_create(name, TCB_FLAG_TTYPE_KERNEL, priority,
|
2022-10-20 03:27:21 +08:00
|
|
|
stack_addr, stack_size, entry, argv, NULL);
|
2021-06-26 02:31:34 +08:00
|
|
|
}
|
|
|
|
|
2011-04-07 22:39:55 +08:00
|
|
|
/****************************************************************************
|
2017-10-17 01:38:00 +08:00
|
|
|
* Name: kthread_create
|
2011-04-07 22:39:55 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function creates and activates a kernel thread task with kernel-
|
|
|
|
* mode privileges. It is identical to task_create() except that it
|
|
|
|
* configures the newly started thread to run in kernel model.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2018-02-01 06:11:54 +08:00
|
|
|
* name - Name of the new task
|
|
|
|
* priority - Priority of the new task
|
|
|
|
* stack_size - size (in bytes) of the stack needed
|
|
|
|
* entry - Entry point of a new task
|
2018-08-09 07:06:46 +08:00
|
|
|
* arg - A pointer to an array of input parameters. The array
|
2018-02-01 06:11:54 +08:00
|
|
|
* should be terminated with a NULL argv[] value. If no
|
|
|
|
* parameters are required, argv may be NULL.
|
2011-04-07 22:39:55 +08:00
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2018-02-01 06:11:54 +08:00
|
|
|
* Returns the positive, non-zero process ID of the new task or a negated
|
|
|
|
* errno value to indicate the nature of any failure. If memory is
|
|
|
|
* insufficient or the task cannot be created -ENOMEM will be returned.
|
2011-04-07 22:39:55 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-10-17 01:38:00 +08:00
|
|
|
int kthread_create(FAR const char *name, int priority,
|
2020-05-06 17:24:42 +08:00
|
|
|
int stack_size, main_t entry, FAR char * const argv[])
|
2011-04-07 22:39:55 +08:00
|
|
|
{
|
2021-06-26 02:31:34 +08:00
|
|
|
return kthread_create_with_stack(name, priority,
|
|
|
|
NULL, stack_size, entry, argv);
|
2011-04-07 22:39:55 +08:00
|
|
|
}
|