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