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
|
|
|
*
|
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-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>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2013-01-25 18:23:38 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
2007-03-11 18:37:47 +01:00
|
|
|
#include <nuttx/arch.h>
|
2022-09-25 17:08:38 +02:00
|
|
|
#include <nuttx/queue.h>
|
2020-03-26 17:40:10 +01:00
|
|
|
#include <nuttx/sched.h>
|
2023-02-22 04:08:22 +01:00
|
|
|
#include <nuttx/trace.h>
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2022-04-04 12:11:06 +02:00
|
|
|
#include "environ/environ.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"
|
2022-05-27 18:43:49 +02:00
|
|
|
#include "tls/tls.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
|
|
|
*
|
2007-03-02 22:27:47 +01:00
|
|
|
* Input Parameters:
|
2021-06-15 07:28:40 +02:00
|
|
|
* tcb - Address of the new task's TCB
|
|
|
|
* name - Name of the new task (not used)
|
|
|
|
* priority - Priority of the new task
|
|
|
|
* stack - Start of the pre-allocated stack
|
|
|
|
* stack_size - Size (in bytes) of the stack allocated
|
|
|
|
* entry - Application start point of the new task
|
|
|
|
* argv - 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.
|
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
|
|
|
|
2021-06-15 07:28:40 +02:00
|
|
|
int nxtask_init(FAR struct task_tcb_s *tcb, const char *name, int priority,
|
2020-06-29 05:23:01 +02:00
|
|
|
FAR void *stack, uint32_t stack_size,
|
2022-04-04 12:11:06 +02:00
|
|
|
main_t entry, FAR char * const argv[],
|
2023-11-13 13:59:42 +01:00
|
|
|
FAR char * const envp[],
|
|
|
|
FAR const posix_spawn_file_actions_t *actions)
|
2007-03-02 22:27:47 +01:00
|
|
|
{
|
2020-06-29 09:52:16 +02:00
|
|
|
uint8_t ttype = tcb->cmn.flags & TCB_FLAG_TTYPE_MASK;
|
2009-12-13 19:01:46 +01:00
|
|
|
int ret;
|
2007-03-11 23:19:01 +01:00
|
|
|
|
2023-02-22 04:08:22 +01:00
|
|
|
sched_trace_begin();
|
|
|
|
|
2021-03-24 18:34:47 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
2013-02-04 22:24:00 +01:00
|
|
|
/* Only tasks and kernel threads can be initialized in this way */
|
|
|
|
|
2020-06-29 04:44:33 +02:00
|
|
|
DEBUGASSERT(tcb && ttype != TCB_FLAG_TTYPE_PTHREAD);
|
2013-02-04 22:24:00 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-27 09:28:19 +01:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2023-04-13 10:48:06 +02:00
|
|
|
/* Kernel threads do not own any address environment */
|
2023-01-27 09:28:19 +01:00
|
|
|
|
2024-05-08 06:22:59 +02:00
|
|
|
if (ttype == TCB_FLAG_TTYPE_KERNEL)
|
2023-01-27 09:28:19 +01:00
|
|
|
{
|
2023-04-13 10:48:06 +02:00
|
|
|
tcb->cmn.addrenv_own = NULL;
|
2023-01-27 09:28:19 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-01-25 18:23:38 +01:00
|
|
|
/* Create a new task group */
|
|
|
|
|
2024-03-04 02:19:27 +01:00
|
|
|
ret = group_initialize(tcb, tcb->cmn.flags);
|
2013-01-25 18:23:38 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-02-22 04:08:22 +01:00
|
|
|
sched_trace_end();
|
2023-04-13 10:48:06 +02:00
|
|
|
return ret;
|
2013-01-25 18:23:38 +01:00
|
|
|
}
|
|
|
|
|
sched/pthread/join: refactor pthread join to support join task
1. add support to join main task
| static pthread_t self;
|
| static void *join_task(void *arg)
| {
| int ret;
| ret = pthread_join(self, NULL); <--- /* Fix Task could not be joined */
| return NULL;
| }
|
| int main(int argc, char *argv[])
| {
| pthread_t thread;
|
| self = pthread_self();
|
| pthread_create(&thread, NULL, join_task, NULL);
| sleep(1);
|
| pthread_exit(NULL);
| return 0;
| }
2. Detach active thread will not alloc for additional join, just update the task flag.
3. Remove the return value waiting lock logic (data_sem),
the return value will be stored in the waiting tcb.
4. Revise the return value of pthread_join(), consistent with linux
e.g:
Joining a detached and canceled thread should return EINVAL, not ESRCH
https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_join.html
[EINVAL]
The value specified by thread does not refer to a joinable thread.
NOTE:
This PR will not increase stack usage, but struct tcb_s will increase 32 bytes.
Signed-off-by: chao an <anchao@lixiang.com>
2024-03-12 08:10:11 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
|
|
|
/* Initialize the task join */
|
|
|
|
|
|
|
|
nxtask_joininit(&tcb->cmn);
|
|
|
|
#endif
|
|
|
|
|
2022-04-04 12:11:06 +02:00
|
|
|
/* Duplicate the parent tasks environment */
|
|
|
|
|
|
|
|
ret = env_dup(tcb->cmn.group, envp);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_group;
|
|
|
|
}
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
/* Associate file descriptors with the new task */
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2023-11-13 13:59:42 +01:00
|
|
|
ret = group_setuptaskfiles(tcb, actions, true);
|
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
|
|
|
}
|
|
|
|
|
2020-06-29 04:44:33 +02:00
|
|
|
if (stack)
|
|
|
|
{
|
|
|
|
/* Use pre-allocated stack */
|
2007-03-11 23:19:01 +01:00
|
|
|
|
2020-06-29 09:52:16 +02:00
|
|
|
ret = up_use_stack(&tcb->cmn, stack, stack_size);
|
2020-06-29 04:44:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Allocate the stack for the TCB */
|
|
|
|
|
2022-05-28 23:38:37 +02:00
|
|
|
ret = up_create_stack(&tcb->cmn, stack_size, ttype);
|
2020-06-29 04:44:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < OK)
|
|
|
|
{
|
|
|
|
goto errout_with_group;
|
|
|
|
}
|
2007-03-11 23:19:01 +01:00
|
|
|
|
2021-04-12 17:44:08 +02:00
|
|
|
/* Initialize thread local storage */
|
2021-04-03 14:33:16 +02:00
|
|
|
|
2022-05-27 18:43:49 +02:00
|
|
|
ret = tls_init_info(&tcb->cmn);
|
|
|
|
if (ret < OK)
|
2021-04-12 17:44:08 +02:00
|
|
|
{
|
|
|
|
goto errout_with_group;
|
|
|
|
}
|
2021-03-24 18:34:47 +01:00
|
|
|
|
2020-10-22 09:45:59 +02:00
|
|
|
/* Initialize the task control block */
|
|
|
|
|
|
|
|
ret = nxtask_setup_scheduler(tcb, priority, nxtask_start,
|
|
|
|
entry, ttype);
|
|
|
|
if (ret < OK)
|
|
|
|
{
|
|
|
|
goto errout_with_group;
|
|
|
|
}
|
|
|
|
|
2013-01-25 18:23:38 +01:00
|
|
|
/* Setup to pass parameters to the new task */
|
|
|
|
|
2022-05-31 10:58:07 +02:00
|
|
|
ret = nxtask_setup_arguments(tcb, name, argv);
|
|
|
|
if (ret < OK)
|
|
|
|
{
|
|
|
|
goto errout_with_group;
|
|
|
|
}
|
2013-01-25 20:15:05 +01:00
|
|
|
|
|
|
|
/* Now we have enough in place that we can join the group */
|
|
|
|
|
2024-03-04 02:19:27 +01:00
|
|
|
group_postinitialize(tcb);
|
2023-02-22 04:08:22 +01:00
|
|
|
sched_trace_end();
|
2022-03-01 19:24:05 +01:00
|
|
|
return ret;
|
2013-01-25 18:23:38 +01:00
|
|
|
|
|
|
|
errout_with_group:
|
2020-06-29 09:52:16 +02:00
|
|
|
if (!stack && tcb->cmn.stack_alloc_ptr)
|
2020-06-29 04:44:33 +02:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_BUILD_KERNEL
|
|
|
|
/* If the exiting thread is not a kernel thread, then it has an
|
|
|
|
* address environment. Don't bother to release the stack memory
|
|
|
|
* in this case... There is no point since the memory lies in the
|
|
|
|
* user memory region that will be destroyed anyway (and the
|
|
|
|
* address environment has probably already been destroyed at
|
|
|
|
* this point.. so we would crash if we even tried it). But if
|
2024-05-08 06:22:59 +02:00
|
|
|
* this is a privileged group, then we still have to release the
|
2020-06-29 04:44:33 +02:00
|
|
|
* memory using the kernel allocator.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ttype == TCB_FLAG_TTYPE_KERNEL)
|
|
|
|
#endif
|
|
|
|
{
|
2020-06-29 09:52:16 +02:00
|
|
|
up_release_stack(&tcb->cmn, ttype);
|
2020-06-29 04:44:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
sched/pthread/join: refactor pthread join to support join task
1. add support to join main task
| static pthread_t self;
|
| static void *join_task(void *arg)
| {
| int ret;
| ret = pthread_join(self, NULL); <--- /* Fix Task could not be joined */
| return NULL;
| }
|
| int main(int argc, char *argv[])
| {
| pthread_t thread;
|
| self = pthread_self();
|
| pthread_create(&thread, NULL, join_task, NULL);
| sleep(1);
|
| pthread_exit(NULL);
| return 0;
| }
2. Detach active thread will not alloc for additional join, just update the task flag.
3. Remove the return value waiting lock logic (data_sem),
the return value will be stored in the waiting tcb.
4. Revise the return value of pthread_join(), consistent with linux
e.g:
Joining a detached and canceled thread should return EINVAL, not ESRCH
https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_join.html
[EINVAL]
The value specified by thread does not refer to a joinable thread.
NOTE:
This PR will not increase stack usage, but struct tcb_s will increase 32 bytes.
Signed-off-by: chao an <anchao@lixiang.com>
2024-03-12 08:10:11 +01:00
|
|
|
nxtask_joindestroy(&tcb->cmn);
|
|
|
|
|
2020-06-29 09:52:16 +02:00
|
|
|
group_leave(&tcb->cmn);
|
2023-01-27 09:28:19 +01:00
|
|
|
|
2023-02-22 04:08:22 +01:00
|
|
|
sched_trace_end();
|
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
|
2024-05-08 06:22:59 +02:00
|
|
|
* error recovery operations after a successful call to task_init()
|
|
|
|
* when a subsequent call to task_activate fails.
|
2020-05-27 17:32:14 +02:00
|
|
|
*
|
|
|
|
* Caution: Freeing of the TCB itself might be an unexpected side-effect.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* tcb - Address of the TCB initialized by task_init()
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; negative error value on failure appropriately.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-06-29 09:52:16 +02:00
|
|
|
void nxtask_uninit(FAR struct task_tcb_s *tcb)
|
2020-05-27 17:32:14 +02:00
|
|
|
{
|
|
|
|
/* The TCB was added to the inactive task list by
|
|
|
|
* nxtask_setup_scheduler().
|
|
|
|
*/
|
|
|
|
|
2024-03-19 05:33:08 +01:00
|
|
|
dq_rem((FAR dq_entry_t *)tcb, list_inactivetasks());
|
2020-05-27 17:32:14 +02:00
|
|
|
|
|
|
|
/* Release all resources associated with the TCB... Including the TCB
|
|
|
|
* itself.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nxsched_release_tcb((FAR struct tcb_s *)tcb,
|
2020-06-29 09:52:16 +02:00
|
|
|
tcb->cmn.flags & TCB_FLAG_TTYPE_MASK);
|
2020-05-27 17:32:14 +02:00
|
|
|
}
|