2008-01-30 21:59:12 +00:00
|
|
|
/****************************************************************************
|
2014-08-08 14:21:48 -06:00
|
|
|
* sched/pthread/pthread_create.c
|
2007-02-17 23:21:28 +00:00
|
|
|
*
|
2024-09-11 13:45:11 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*
|
2020-05-16 10:05:46 -06: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-17 23:21:28 +00:00
|
|
|
*
|
2020-05-16 10:05:46 -06:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-17 23:21:28 +00:00
|
|
|
*
|
2020-05-16 10:05:46 -06: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-17 23:21:28 +00:00
|
|
|
*
|
2008-01-30 21:59:12 +00:00
|
|
|
****************************************************************************/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2008-01-30 21:59:12 +00:00
|
|
|
/****************************************************************************
|
2007-02-17 23:21:28 +00:00
|
|
|
* Included Files
|
2008-01-30 21:59:12 +00:00
|
|
|
****************************************************************************/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-14 21:15:18 +00:00
|
|
|
|
2007-02-17 23:21:28 +00:00
|
|
|
#include <sys/types.h>
|
2009-12-14 21:15:18 +00:00
|
|
|
#include <stdbool.h>
|
2007-03-20 16:51:12 +00:00
|
|
|
#include <string.h>
|
2007-02-17 23:21:28 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <sched.h>
|
2026-04-01 05:11:15 +05:30
|
|
|
#include <nuttx/debug.h>
|
2015-07-24 09:03:21 -06:00
|
|
|
#include <assert.h>
|
2007-02-17 23:21:28 +00:00
|
|
|
#include <errno.h>
|
2011-03-31 01:42:50 +00:00
|
|
|
|
2022-09-25 23:08:38 +08:00
|
|
|
#include <nuttx/queue.h>
|
2018-01-30 11:07:36 -06:00
|
|
|
#include <nuttx/sched.h>
|
2016-11-03 12:42:02 -06:00
|
|
|
#include <nuttx/arch.h>
|
2016-11-03 12:58:02 -06:00
|
|
|
#include <nuttx/semaphore.h>
|
2007-02-17 23:21:28 +00:00
|
|
|
#include <nuttx/kmalloc.h>
|
2011-03-31 01:42:50 +00:00
|
|
|
#include <nuttx/pthread.h>
|
2009-12-14 21:15:18 +00: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 15:10:11 +08:00
|
|
|
#include "task/task.h"
|
2014-08-08 17:53:55 -06:00
|
|
|
#include "sched/sched.h"
|
2014-08-08 14:06:42 -06:00
|
|
|
#include "group/group.h"
|
2014-08-08 14:43:02 -06:00
|
|
|
#include "clock/clock.h"
|
2014-08-08 12:55:02 -06:00
|
|
|
#include "pthread/pthread.h"
|
2022-05-28 00:43:49 +08:00
|
|
|
#include "tls/tls.h"
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2008-01-30 21:59:12 +00:00
|
|
|
/****************************************************************************
|
2015-07-23 14:36:49 -06:00
|
|
|
* Public Data
|
2008-01-30 21:59:12 +00:00
|
|
|
****************************************************************************/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2014-09-07 10:46:58 -06:00
|
|
|
/* Default pthread attributes (see include/nuttx/pthread.h). When configured
|
|
|
|
|
* to build separate kernel- and user-address spaces, this global is
|
|
|
|
|
* duplicated in each address spaced. This copy can only be shared within
|
|
|
|
|
* the kernel address space.
|
|
|
|
|
*/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2014-09-07 10:46:58 -06:00
|
|
|
const pthread_attr_t g_default_pthread_attr = PTHREAD_ATTR_INITIALIZER;
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2008-01-30 21:59:12 +00:00
|
|
|
/****************************************************************************
|
2007-02-17 23:21:28 +00:00
|
|
|
* Private Functions
|
2008-01-30 21:59:12 +00:00
|
|
|
****************************************************************************/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2008-01-30 21:59:12 +00:00
|
|
|
/****************************************************************************
|
2020-06-29 08:26:29 -06:00
|
|
|
* Name: pthread_tcb_setup
|
2007-03-13 23:03:12 +00:00
|
|
|
*
|
|
|
|
|
* Description:
|
2020-06-29 08:26:29 -06:00
|
|
|
* This function sets up parameters in the Task Control Block (TCB) in
|
2012-07-14 19:30:31 +00:00
|
|
|
* preparation for starting a new thread.
|
2007-03-13 23:03:12 +00:00
|
|
|
*
|
2020-06-29 08:26:29 -06:00
|
|
|
* pthread_tcb_setup() is called from nxtask_init() and nxtask_start() to
|
2020-04-09 20:46:24 +08:00
|
|
|
* create a new task (with arguments cloned via strdup) or pthread_create()
|
|
|
|
|
* which has one argument passed by value (distinguished by the pthread
|
|
|
|
|
* boolean argument).
|
2007-03-13 23:03:12 +00:00
|
|
|
*
|
|
|
|
|
* Input Parameters:
|
|
|
|
|
* tcb - Address of the new task's TCB
|
2020-06-29 08:26:29 -06:00
|
|
|
* trampoline - User space pthread startup function
|
2013-02-04 21:24:00 +00:00
|
|
|
* arg - The argument to provide to the pthread on startup.
|
2007-03-13 23:03:12 +00:00
|
|
|
*
|
2018-02-01 10:00:02 -06:00
|
|
|
* Returned Value:
|
2009-05-19 19:30:57 +00:00
|
|
|
* None
|
2007-03-13 23:03:12 +00:00
|
|
|
*
|
2008-01-30 21:59:12 +00:00
|
|
|
****************************************************************************/
|
2007-03-13 23:03:12 +00:00
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
static inline void pthread_tcb_setup(FAR struct tcb_s *ptcb,
|
2023-01-29 16:27:04 +08:00
|
|
|
FAR struct tcb_s *parent,
|
2020-06-29 08:26:29 -06:00
|
|
|
pthread_trampoline_t trampoline,
|
2022-01-25 17:22:17 +08:00
|
|
|
pthread_addr_t arg)
|
2007-03-13 23:03:12 +00:00
|
|
|
{
|
2025-02-18 15:19:02 +08:00
|
|
|
FAR struct pthread_entry_s *entry;
|
2007-03-13 23:03:12 +00:00
|
|
|
#if CONFIG_TASK_NAME_SIZE > 0
|
|
|
|
|
/* Copy the pthread name into the TCB */
|
|
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
strlcpy(ptcb->name, parent->name, CONFIG_TASK_NAME_SIZE);
|
2007-03-13 23:03:12 +00:00
|
|
|
#endif /* CONFIG_TASK_NAME_SIZE */
|
|
|
|
|
|
|
|
|
|
/* For pthreads, args are strictly pass-by-value; that actual
|
|
|
|
|
* type wrapped by pthread_addr_t is unknown.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
entry = (FAR struct pthread_entry_s *)(ptcb + 1);
|
|
|
|
|
entry->trampoline = trampoline;
|
|
|
|
|
entry->arg = arg;
|
2007-03-13 23:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
2008-01-30 21:59:12 +00:00
|
|
|
/****************************************************************************
|
2007-02-17 23:21:28 +00:00
|
|
|
* Name: pthread_start
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
2013-02-03 16:43:58 +00:00
|
|
|
* This function is the low level entry point into the pthread
|
2007-02-17 23:21:28 +00:00
|
|
|
*
|
2018-03-13 09:52:27 -06:00
|
|
|
* Input Parameters:
|
2007-02-17 23:21:28 +00:00
|
|
|
* None
|
|
|
|
|
*
|
2008-01-30 21:59:12 +00:00
|
|
|
****************************************************************************/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
|
|
|
|
static void pthread_start(void)
|
|
|
|
|
{
|
2025-02-18 15:19:02 +08:00
|
|
|
FAR struct tcb_s *ptcb = this_task();
|
|
|
|
|
FAR struct pthread_entry_s *entry =
|
|
|
|
|
(FAR struct pthread_entry_s *)(ptcb + 1);
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2016-10-01 11:38:22 -06:00
|
|
|
/* The priority of this thread may have been boosted to avoid priority
|
|
|
|
|
* inversion problems. If that is the case, then drop to the correct
|
|
|
|
|
* execution priority.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
if (ptcb->sched_priority > ptcb->init_priority)
|
2016-10-01 11:38:22 -06:00
|
|
|
{
|
2025-02-18 15:19:02 +08:00
|
|
|
DEBUGVERIFY(nxsched_set_priority(ptcb, ptcb->init_priority));
|
2016-10-01 11:38:22 -06:00
|
|
|
}
|
|
|
|
|
|
2013-03-16 17:37:40 +00:00
|
|
|
/* Pass control to the thread entry point. In the kernel build this has to
|
|
|
|
|
* be handled differently if we are starting a user-space pthread; we have
|
|
|
|
|
* to switch to user-mode before calling into the pthread.
|
|
|
|
|
*/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
DEBUGASSERT(entry->trampoline != NULL && ptcb->entry.pthread != NULL);
|
2020-06-29 08:26:29 -06:00
|
|
|
|
2020-04-30 03:21:23 +08:00
|
|
|
#ifdef CONFIG_BUILD_FLAT
|
2025-02-18 15:19:02 +08:00
|
|
|
entry->trampoline(ptcb->entry.pthread, entry->arg);
|
2020-04-30 03:21:23 +08:00
|
|
|
#else
|
2025-02-18 15:19:02 +08:00
|
|
|
up_pthread_start(entry->trampoline, ptcb->entry.pthread, entry->arg);
|
2013-03-16 17:37:40 +00:00
|
|
|
#endif
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2020-06-29 08:26:29 -06:00
|
|
|
/* The thread has returned (should never happen) */
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2020-06-29 08:26:29 -06:00
|
|
|
DEBUGPANIC();
|
2022-01-25 17:22:17 +08:00
|
|
|
pthread_exit(NULL);
|
2007-02-17 23:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
2008-01-30 21:59:12 +00:00
|
|
|
/****************************************************************************
|
2007-02-17 23:21:28 +00:00
|
|
|
* Public Functions
|
2008-01-30 21:59:12 +00:00
|
|
|
****************************************************************************/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2008-01-30 21:59:12 +00:00
|
|
|
/****************************************************************************
|
2020-06-29 08:26:29 -06:00
|
|
|
* Name: nx_pthread_create
|
2007-02-17 23:21:28 +00:00
|
|
|
*
|
|
|
|
|
* Description:
|
2020-06-29 08:26:29 -06:00
|
|
|
* This function creates and activates a new thread with specified
|
2012-07-14 19:30:31 +00:00
|
|
|
* attributes.
|
2007-02-17 23:21:28 +00:00
|
|
|
*
|
|
|
|
|
* Input Parameters:
|
2021-05-06 16:01:16 +08:00
|
|
|
* trampoline - The user space startup function
|
|
|
|
|
* thread - The pthread handle to be used
|
|
|
|
|
* attr - It points to a pthread_attr_t structure whose contents are
|
|
|
|
|
* used at thread creation time to determine attributes
|
|
|
|
|
* for the new thread
|
|
|
|
|
* entry - The new thread starts execution by invoking entry
|
|
|
|
|
* arg - It is passed as the sole argument of entry
|
2009-05-19 19:30:57 +00:00
|
|
|
*
|
2018-02-01 10:00:02 -06:00
|
|
|
* Returned Value:
|
2009-05-19 19:30:57 +00:00
|
|
|
* OK (0) on success; a (non-negated) errno value on failure. The errno
|
|
|
|
|
* variable is not set.
|
|
|
|
|
*
|
2008-01-30 21:59:12 +00:00
|
|
|
****************************************************************************/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2020-06-29 08:26:29 -06:00
|
|
|
int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
|
|
|
|
|
FAR const pthread_attr_t *attr,
|
2022-01-25 17:22:17 +08:00
|
|
|
pthread_startroutine_t entry, pthread_addr_t arg)
|
2007-02-17 23:21:28 +00:00
|
|
|
{
|
2024-06-06 15:03:40 +08:00
|
|
|
pthread_attr_t default_attr = g_default_pthread_attr;
|
2025-02-18 15:19:02 +08:00
|
|
|
FAR struct tcb_s *ptcb;
|
2015-07-25 12:50:53 -06:00
|
|
|
struct sched_param param;
|
2023-01-25 17:37:28 +02:00
|
|
|
FAR struct tcb_s *parent;
|
2007-02-17 23:21:28 +00:00
|
|
|
int policy;
|
2013-01-25 17:23:38 +00:00
|
|
|
int errcode;
|
2013-01-25 19:15:05 +00:00
|
|
|
int ret;
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2020-06-29 08:26:29 -06:00
|
|
|
DEBUGASSERT(trampoline != NULL);
|
|
|
|
|
|
2023-06-25 14:37:31 +08:00
|
|
|
parent = this_task();
|
|
|
|
|
DEBUGASSERT(parent != NULL);
|
|
|
|
|
|
2007-02-17 23:21:28 +00:00
|
|
|
/* If attributes were not supplied, use the default attributes */
|
|
|
|
|
|
|
|
|
|
if (!attr)
|
|
|
|
|
{
|
2023-06-29 17:08:26 +08:00
|
|
|
/* Inherit parent priority by default. except idle */
|
|
|
|
|
|
|
|
|
|
if (!is_idle_task(parent))
|
|
|
|
|
{
|
|
|
|
|
default_attr.priority = parent->sched_priority;
|
|
|
|
|
}
|
2023-06-25 14:37:31 +08:00
|
|
|
|
|
|
|
|
attr = &default_attr;
|
2007-02-17 23:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Allocate a TCB for the new task. */
|
|
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
ptcb = kmm_zalloc(sizeof(struct tcb_s) + sizeof(struct pthread_entry_s));
|
2007-02-17 23:21:28 +00:00
|
|
|
if (!ptcb)
|
|
|
|
|
{
|
2016-06-11 15:50:49 -06:00
|
|
|
serr("ERROR: Failed to allocate TCB\n");
|
2009-05-19 19:30:57 +00:00
|
|
|
return ENOMEM;
|
2007-02-17 23:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
ptcb->flags |= TCB_FLAG_FREE_TCB;
|
2024-03-06 17:00:22 +08: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 15:10:11 +08:00
|
|
|
/* Initialize the task join */
|
|
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
nxtask_joininit(ptcb);
|
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 15:10:11 +08:00
|
|
|
|
2013-01-25 19:15:05 +00:00
|
|
|
/* Bind the parent's group to the new TCB (we have not yet joined the
|
|
|
|
|
* group).
|
|
|
|
|
*/
|
2013-01-25 17:23:38 +00:00
|
|
|
|
2024-06-06 15:03:40 +08:00
|
|
|
group_bind(ptcb);
|
2013-01-25 17:23:38 +00:00
|
|
|
|
2014-08-24 06:42:11 -06:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2014-08-22 12:32:34 -06:00
|
|
|
/* Share the address environment of the parent task group. */
|
|
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
ret = addrenv_join(this_task(), ptcb);
|
2012-12-11 21:42:15 +00:00
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
2013-01-25 17:23:38 +00:00
|
|
|
errcode = -ret;
|
|
|
|
|
goto errout_with_tcb;
|
2012-12-11 21:42:15 +00:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-08-19 23:32:48 +08:00
|
|
|
if (attr->detachstate == PTHREAD_CREATE_DETACHED)
|
|
|
|
|
{
|
2025-02-18 15:19:02 +08:00
|
|
|
ptcb->flags |= TCB_FLAG_DETACHED;
|
2020-08-19 23:32:48 +08:00
|
|
|
}
|
|
|
|
|
|
2020-10-22 16:45:59 +09:00
|
|
|
if (attr->stackaddr)
|
|
|
|
|
{
|
|
|
|
|
/* Use pre-allocated stack */
|
|
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
ret = up_use_stack(ptcb, attr->stackaddr, attr->stacksize);
|
2020-10-22 16:45:59 +09:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* Allocate the stack for the TCB */
|
|
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
ret = up_create_stack(ptcb,
|
2025-07-31 12:09:19 +02:00
|
|
|
attr->stacksize + attr->guardsize,
|
2020-10-22 16:45:59 +09:00
|
|
|
TCB_FLAG_TTYPE_PTHREAD);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ret != OK)
|
|
|
|
|
{
|
|
|
|
|
errcode = ENOMEM;
|
2022-12-16 15:42:18 +08:00
|
|
|
goto errout_with_tcb;
|
2020-10-22 16:45:59 +09:00
|
|
|
}
|
|
|
|
|
|
2025-05-06 18:32:18 +08:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_ARCH_KERNEL_STACK)
|
2023-05-31 14:39:09 +03:00
|
|
|
/* Allocate the kernel stack */
|
|
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
ret = up_addrenv_kstackalloc(ptcb);
|
2023-05-31 14:39:09 +03:00
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
errcode = ENOMEM;
|
|
|
|
|
goto errout_with_tcb;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-07-25 12:50:53 -06:00
|
|
|
/* Should we use the priority and scheduler specified in the pthread
|
|
|
|
|
* attributes? Or should we use the current thread's priority and
|
|
|
|
|
* scheduler?
|
2007-02-17 23:21:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (attr->inheritsched == PTHREAD_INHERIT_SCHED)
|
|
|
|
|
{
|
2015-07-23 13:16:32 -06:00
|
|
|
/* Get the priority (and any other scheduling parameters) for this
|
|
|
|
|
* thread.
|
|
|
|
|
*/
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2020-05-09 08:04:45 -06:00
|
|
|
ret = nxsched_get_param(0, ¶m);
|
2018-01-30 11:07:36 -06:00
|
|
|
if (ret < 0)
|
2007-02-27 21:17:21 +00:00
|
|
|
{
|
2018-01-30 11:07:36 -06:00
|
|
|
errcode = -ret;
|
2022-12-16 15:42:18 +08:00
|
|
|
goto errout_with_tcb;
|
2007-02-27 21:17:21 +00:00
|
|
|
}
|
2007-02-17 23:21:28 +00:00
|
|
|
|
|
|
|
|
/* Get the scheduler policy for this thread */
|
|
|
|
|
|
2020-05-09 08:04:45 -06:00
|
|
|
policy = nxsched_get_scheduler(0);
|
2018-01-30 11:07:36 -06:00
|
|
|
if (policy < 0)
|
2007-02-17 23:21:28 +00:00
|
|
|
{
|
2018-01-30 11:07:36 -06:00
|
|
|
errcode = -policy;
|
2022-12-16 15:42:18 +08:00
|
|
|
goto errout_with_tcb;
|
2007-02-17 23:21:28 +00:00
|
|
|
}
|
2015-07-25 12:50:53 -06:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* Use the scheduler policy and policy the attributes */
|
2015-07-23 13:16:32 -06:00
|
|
|
|
2015-07-25 12:50:53 -06:00
|
|
|
policy = attr->policy;
|
|
|
|
|
param.sched_priority = attr->priority;
|
2015-07-23 13:16:32 -06:00
|
|
|
|
2015-07-25 12:50:53 -06:00
|
|
|
#ifdef CONFIG_SCHED_SPORADIC
|
|
|
|
|
param.sched_ss_low_priority = attr->low_priority;
|
|
|
|
|
param.sched_ss_max_repl = attr->max_repl;
|
|
|
|
|
param.sched_ss_repl_period.tv_sec = attr->repl_period.tv_sec;
|
|
|
|
|
param.sched_ss_repl_period.tv_nsec = attr->repl_period.tv_nsec;
|
|
|
|
|
param.sched_ss_init_budget.tv_sec = attr->budget.tv_sec;
|
|
|
|
|
param.sched_ss_init_budget.tv_nsec = attr->budget.tv_nsec;
|
2007-02-17 23:21:28 +00:00
|
|
|
#endif
|
|
|
|
|
}
|
2015-07-25 12:50:53 -06:00
|
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_SPORADIC
|
|
|
|
|
if (policy == SCHED_SPORADIC)
|
2007-02-17 23:21:28 +00:00
|
|
|
{
|
2015-07-25 12:50:53 -06:00
|
|
|
FAR struct sporadic_s *sporadic;
|
!sys/types.h: change time_t and clock_t to int64_t to align with other OSes
POSIX leaves the signedness of time_t and clock_t unspecified, but
mainstream implementations (Linux glibc/musl, the BSDs, macOS, RTEMS,
Zephyr's POSIX layer, Windows _time64) expose time_t as signed 64-bit.
NuttX has historically used uint64_t only because it was tied to the
CONFIG_SYSTEM_TIME64 knob; with that gone, switch:
time_t : uint64_t -> int64_t
clock_t : uint64_t -> int64_t
CLOCK_MAX: UINT64_MAX -> INT64_MAX
This lets (time_t)-1 sentinels, negative tick deltas, and host-side
headers behave as on every other POSIX system without source churn.
Headers updated:
- include/sys/types.h, include/limits.h, include/nuttx/clock.h
- include/nuttx/fs/hostfs.h (nuttx_time_t alias)
- include/nuttx/{mqueue.h,wdog.h,wqueue.h,timers/clkcnt.h}
Because clock_t is now signed 64-bit, the NuttX-internal sclock_t
alias becomes redundant: every sclock_t/SCLOCK_MAX use is folded
back to clock_t/CLOCK_MAX (notably in sched/wdog, sched/mqueue,
sched/sched, sched/clock, sched/timer, libs/libc/time, fs/vfs and
the drivers/arch consumers below).
Tick/period constants (NSEC_PER_SEC, USEC_PER_SEC, MSEC_PER_SEC,
SEC_PER_MIN, ...) in include/nuttx/clock.h are retyped from "long"
literals to INT64_C(...) so that 64-bit arithmetic no longer
depends on the host's long width.
Strip now-redundant (time_t)/(clock_t)/(unsigned long) casts and
unsigned-only branches across the tree:
- arch RTC / oneshot / tickless lowerhalfs:
arm: cxd56xx, efm32, imxrt, lc823450, max326xx, sam34, sama5,
samd5e5, samv7, stm32, stm32f7, stm32h7, stm32l4, stm32wb,
xmc4
mips: pic32mz sparc: bm3803 x86_64: intel64
risc-v/xtensa: espressif (esp_i2c[_slave], esp_rtc,
esp32c3{_i2c,_rtc,_wifi_adapter}, esp32{,s2,s3}_*),
mpfs_perf
- drivers: audio/tone, input/aw86225, power/pm/{activity,
stability}_governor, rpmsg/rpmsg_ping,
timers/{ds3231,mcp794xx,pcf85263,rx8010},
wireless/ieee80211/bcm43xxx, wireless/spirit/spirit_spi
- core: fs/vfs/{fs_poll,fs_timerfd}, mm/iob/iob_alloc,
libs/libc/{netdb/lib_dnscache,time/{lib_calendar2utc,
lib_time}}, net/icmp/icmp_pmtu, net/icmpv6/icmpv6_pmtu,
net/ipfrag, net/tcp/{tcp.h,tcp_timer},
net/utils/net_snoop, net/mld/mld_query (drop the now-dead
mld_mrc2mrd helper since signed math handles it directly),
sched/clock/{clock,clock_initialize},
sched/sched/{sched_profil,sched_setparam,sched_setscheduler},
sched/pthread/pthread_create,
sched/wdog/{wd_gettime,wd_start,wdog.h},
sched/timer/timer_gettime, sched/mqueue/*
Flip the few in-tree printf format strings that assumed an
unsigned 64-bit tv_sec:
* drivers/rpmsg/rpmsg_ping.c PRIu64 -> PRId64
* arch/xtensa/src/esp32{,s2,s3}/esp32*_oneshot_lowerhalf.c
PRIu32 (already wrong) -> PRId64
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2026-05-03 16:48:08 +08:00
|
|
|
clock_t repl_ticks;
|
|
|
|
|
clock_t budget_ticks;
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2015-07-25 12:50:53 -06:00
|
|
|
/* Convert timespec values to system clock ticks */
|
2015-07-23 13:16:32 -06:00
|
|
|
|
2024-05-24 21:55:42 +08:00
|
|
|
repl_ticks = clock_time2ticks(¶m.sched_ss_repl_period);
|
|
|
|
|
budget_ticks = clock_time2ticks(¶m.sched_ss_init_budget);
|
2015-07-23 13:16:32 -06:00
|
|
|
|
2015-07-25 12:50:53 -06:00
|
|
|
/* The replenishment period must be greater than or equal to the
|
|
|
|
|
* budget period.
|
|
|
|
|
*/
|
2015-07-23 13:16:32 -06:00
|
|
|
|
2015-07-25 12:50:53 -06:00
|
|
|
if (repl_ticks < budget_ticks)
|
|
|
|
|
{
|
|
|
|
|
errcode = EINVAL;
|
2022-12-16 15:42:18 +08:00
|
|
|
goto errout_with_tcb;
|
2015-07-25 12:50:53 -06:00
|
|
|
}
|
2015-07-23 13:16:32 -06:00
|
|
|
|
2015-07-25 12:50:53 -06:00
|
|
|
/* Initialize the sporadic policy */
|
2015-07-23 15:08:41 -06:00
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
ret = nxsched_initialize_sporadic(ptcb);
|
2015-07-25 12:50:53 -06:00
|
|
|
if (ret >= 0)
|
|
|
|
|
{
|
2025-02-18 15:19:02 +08:00
|
|
|
sporadic = ptcb->sporadic;
|
2015-07-25 12:50:53 -06:00
|
|
|
DEBUGASSERT(sporadic != NULL);
|
2015-07-23 15:08:41 -06:00
|
|
|
|
|
|
|
|
/* Save the sporadic scheduling parameters */
|
|
|
|
|
|
2015-07-25 12:50:53 -06:00
|
|
|
sporadic->hi_priority = param.sched_priority;
|
|
|
|
|
sporadic->low_priority = param.sched_ss_low_priority;
|
|
|
|
|
sporadic->max_repl = param.sched_ss_max_repl;
|
|
|
|
|
sporadic->repl_period = repl_ticks;
|
|
|
|
|
sporadic->budget = budget_ticks;
|
2015-07-23 15:08:41 -06:00
|
|
|
|
2015-07-25 12:50:53 -06:00
|
|
|
/* And start the first replenishment interval */
|
2015-07-24 09:03:21 -06:00
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
ret = nxsched_start_sporadic(ptcb);
|
2015-07-25 12:50:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Handle any failures */
|
|
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
errcode = -ret;
|
2022-12-16 15:42:18 +08:00
|
|
|
goto errout_with_tcb;
|
2015-07-23 15:08:41 -06:00
|
|
|
}
|
2007-02-17 23:21:28 +00:00
|
|
|
}
|
2015-07-25 12:50:53 -06:00
|
|
|
#endif
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2007-03-11 22:19:01 +00:00
|
|
|
/* Initialize the task control block */
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2020-05-16 09:06:29 -06:00
|
|
|
ret = pthread_setup_scheduler(ptcb, param.sched_priority, pthread_start,
|
2020-06-29 08:26:29 -06:00
|
|
|
entry);
|
2012-12-11 21:42:15 +00:00
|
|
|
if (ret != OK)
|
2007-02-17 23:21:28 +00:00
|
|
|
{
|
2013-01-25 17:23:38 +00:00
|
|
|
errcode = EBUSY;
|
2022-12-16 15:42:18 +08:00
|
|
|
goto errout_with_tcb;
|
2007-02-17 23:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
2025-04-04 13:50:56 +03:00
|
|
|
/* Initialize thread local storage */
|
|
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
ret = tls_init_info(ptcb);
|
2025-04-04 13:50:56 +03:00
|
|
|
if (ret != OK)
|
|
|
|
|
{
|
|
|
|
|
errcode = -ret;
|
|
|
|
|
goto errout_with_tcb;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-19 17:33:35 -06:00
|
|
|
#ifdef CONFIG_SMP
|
2020-05-16 09:06:29 -06:00
|
|
|
/* pthread_setup_scheduler() will set the affinity mask by inheriting the
|
2016-02-19 17:33:35 -06:00
|
|
|
* setting from the parent task. We need to override this setting
|
|
|
|
|
* with the value from the pthread attributes unless that value is
|
|
|
|
|
* zero: Zero is the default value and simply means to inherit the
|
|
|
|
|
* parent thread's affinity mask.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-08-14 17:19:27 -06:00
|
|
|
if (attr->affinity != 0)
|
|
|
|
|
{
|
2025-02-18 15:19:02 +08:00
|
|
|
ptcb->affinity = attr->affinity;
|
2017-08-14 17:19:27 -06:00
|
|
|
}
|
2016-02-19 17:33:35 -06:00
|
|
|
#endif
|
|
|
|
|
|
2007-03-11 22:19:01 +00:00
|
|
|
/* Configure the TCB for a pthread receiving on parameter
|
|
|
|
|
* passed by value
|
|
|
|
|
*/
|
|
|
|
|
|
2023-01-29 16:27:04 +08:00
|
|
|
pthread_tcb_setup(ptcb, parent, trampoline, arg);
|
2007-03-11 22:19:01 +00:00
|
|
|
|
2013-01-25 19:15:05 +00:00
|
|
|
/* Join the parent's task group */
|
|
|
|
|
|
2024-06-06 15:03:40 +08:00
|
|
|
group_join(ptcb);
|
2013-01-25 19:15:05 +00:00
|
|
|
|
2015-07-23 09:58:41 -06:00
|
|
|
/* Set the appropriate scheduling policy in the TCB */
|
2007-02-17 23:21:28 +00:00
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
ptcb->flags &= ~TCB_FLAG_POLICY_MASK;
|
2015-07-23 09:58:41 -06:00
|
|
|
switch (policy)
|
2007-02-17 23:21:28 +00:00
|
|
|
{
|
2015-07-23 09:58:41 -06:00
|
|
|
default:
|
|
|
|
|
case SCHED_FIFO:
|
2025-02-18 15:19:02 +08:00
|
|
|
ptcb->flags |= TCB_FLAG_SCHED_FIFO;
|
2015-07-23 09:58:41 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
#if CONFIG_RR_INTERVAL > 0
|
2023-01-29 12:07:20 +08:00
|
|
|
case SCHED_OTHER:
|
2015-07-23 09:58:41 -06:00
|
|
|
case SCHED_RR:
|
2025-02-18 15:19:02 +08:00
|
|
|
ptcb->flags |= TCB_FLAG_SCHED_RR;
|
|
|
|
|
ptcb->timeslice = MSEC2TICK(CONFIG_RR_INTERVAL);
|
2015-07-23 09:58:41 -06:00
|
|
|
break;
|
2007-02-17 23:21:28 +00:00
|
|
|
#endif
|
|
|
|
|
|
2015-07-23 09:58:41 -06:00
|
|
|
#ifdef CONFIG_SCHED_SPORADIC
|
|
|
|
|
case SCHED_SPORADIC:
|
2025-02-18 15:19:02 +08:00
|
|
|
ptcb->flags |= TCB_FLAG_SCHED_SPORADIC;
|
2015-07-23 09:58:41 -06:00
|
|
|
break;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-06 15:03:40 +08:00
|
|
|
/* Return the thread information to the caller */
|
2012-12-11 21:42:15 +00:00
|
|
|
|
2024-06-06 15:03:40 +08:00
|
|
|
if (thread != NULL)
|
2007-02-17 23:21:28 +00:00
|
|
|
{
|
2025-02-18 15:19:02 +08:00
|
|
|
*thread = (pthread_t)ptcb->pid;
|
2007-02-17 23:21:28 +00:00
|
|
|
}
|
2012-07-14 19:30:31 +00:00
|
|
|
|
2024-10-10 13:38:58 +08:00
|
|
|
/* Then activate the task */
|
|
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
nxtask_activate(ptcb);
|
2024-10-10 13:38:58 +08:00
|
|
|
|
2024-06-06 15:03:40 +08:00
|
|
|
return OK;
|
2013-01-25 17:23:38 +00:00
|
|
|
|
|
|
|
|
errout_with_tcb:
|
2019-10-24 10:49:28 -06:00
|
|
|
|
2024-06-06 15:03:40 +08:00
|
|
|
/* Since we do not join the group, assign group to NULL to clear binding */
|
2015-07-01 06:24:34 -06:00
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
ptcb->group = NULL;
|
2015-07-01 06:24:34 -06:00
|
|
|
|
2025-02-18 15:19:02 +08:00
|
|
|
nxsched_release_tcb(ptcb, TCB_FLAG_TTYPE_PTHREAD);
|
2013-01-25 17:23:38 +00:00
|
|
|
return errcode;
|
2007-02-17 23:21:28 +00:00
|
|
|
}
|