mirror of
https://github.com/apache/nuttx.git
synced 2026-08-01 20:28:58 +00:00
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>
469 lines
13 KiB
C
469 lines
13 KiB
C
/****************************************************************************
|
|
* sched/pthread/pthread_create.c
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*
|
|
* 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
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* 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.
|
|
*
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Included Files
|
|
****************************************************************************/
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <sys/types.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
#include <pthread.h>
|
|
#include <sched.h>
|
|
#include <nuttx/debug.h>
|
|
#include <assert.h>
|
|
#include <errno.h>
|
|
|
|
#include <nuttx/queue.h>
|
|
#include <nuttx/sched.h>
|
|
#include <nuttx/arch.h>
|
|
#include <nuttx/semaphore.h>
|
|
#include <nuttx/kmalloc.h>
|
|
#include <nuttx/pthread.h>
|
|
|
|
#include "task/task.h"
|
|
#include "sched/sched.h"
|
|
#include "group/group.h"
|
|
#include "clock/clock.h"
|
|
#include "pthread/pthread.h"
|
|
#include "tls/tls.h"
|
|
|
|
/****************************************************************************
|
|
* Public Data
|
|
****************************************************************************/
|
|
|
|
/* 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.
|
|
*/
|
|
|
|
const pthread_attr_t g_default_pthread_attr = PTHREAD_ATTR_INITIALIZER;
|
|
|
|
/****************************************************************************
|
|
* Private Functions
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Name: pthread_tcb_setup
|
|
*
|
|
* Description:
|
|
* This function sets up parameters in the Task Control Block (TCB) in
|
|
* preparation for starting a new thread.
|
|
*
|
|
* pthread_tcb_setup() is called from nxtask_init() and nxtask_start() to
|
|
* 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).
|
|
*
|
|
* Input Parameters:
|
|
* tcb - Address of the new task's TCB
|
|
* trampoline - User space pthread startup function
|
|
* arg - The argument to provide to the pthread on startup.
|
|
*
|
|
* Returned Value:
|
|
* None
|
|
*
|
|
****************************************************************************/
|
|
|
|
static inline void pthread_tcb_setup(FAR struct tcb_s *ptcb,
|
|
FAR struct tcb_s *parent,
|
|
pthread_trampoline_t trampoline,
|
|
pthread_addr_t arg)
|
|
{
|
|
FAR struct pthread_entry_s *entry;
|
|
#if CONFIG_TASK_NAME_SIZE > 0
|
|
/* Copy the pthread name into the TCB */
|
|
|
|
strlcpy(ptcb->name, parent->name, CONFIG_TASK_NAME_SIZE);
|
|
#endif /* CONFIG_TASK_NAME_SIZE */
|
|
|
|
/* For pthreads, args are strictly pass-by-value; that actual
|
|
* type wrapped by pthread_addr_t is unknown.
|
|
*/
|
|
|
|
entry = (FAR struct pthread_entry_s *)(ptcb + 1);
|
|
entry->trampoline = trampoline;
|
|
entry->arg = arg;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Name: pthread_start
|
|
*
|
|
* Description:
|
|
* This function is the low level entry point into the pthread
|
|
*
|
|
* Input Parameters:
|
|
* None
|
|
*
|
|
****************************************************************************/
|
|
|
|
static void pthread_start(void)
|
|
{
|
|
FAR struct tcb_s *ptcb = this_task();
|
|
FAR struct pthread_entry_s *entry =
|
|
(FAR struct pthread_entry_s *)(ptcb + 1);
|
|
|
|
/* 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.
|
|
*/
|
|
|
|
if (ptcb->sched_priority > ptcb->init_priority)
|
|
{
|
|
DEBUGVERIFY(nxsched_set_priority(ptcb, ptcb->init_priority));
|
|
}
|
|
|
|
/* 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.
|
|
*/
|
|
|
|
DEBUGASSERT(entry->trampoline != NULL && ptcb->entry.pthread != NULL);
|
|
|
|
#ifdef CONFIG_BUILD_FLAT
|
|
entry->trampoline(ptcb->entry.pthread, entry->arg);
|
|
#else
|
|
up_pthread_start(entry->trampoline, ptcb->entry.pthread, entry->arg);
|
|
#endif
|
|
|
|
/* The thread has returned (should never happen) */
|
|
|
|
DEBUGPANIC();
|
|
pthread_exit(NULL);
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Public Functions
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Name: nx_pthread_create
|
|
*
|
|
* Description:
|
|
* This function creates and activates a new thread with specified
|
|
* attributes.
|
|
*
|
|
* Input Parameters:
|
|
* 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
|
|
*
|
|
* Returned Value:
|
|
* OK (0) on success; a (non-negated) errno value on failure. The errno
|
|
* variable is not set.
|
|
*
|
|
****************************************************************************/
|
|
|
|
int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
|
|
FAR const pthread_attr_t *attr,
|
|
pthread_startroutine_t entry, pthread_addr_t arg)
|
|
{
|
|
pthread_attr_t default_attr = g_default_pthread_attr;
|
|
FAR struct tcb_s *ptcb;
|
|
struct sched_param param;
|
|
FAR struct tcb_s *parent;
|
|
int policy;
|
|
int errcode;
|
|
int ret;
|
|
|
|
DEBUGASSERT(trampoline != NULL);
|
|
|
|
parent = this_task();
|
|
DEBUGASSERT(parent != NULL);
|
|
|
|
/* If attributes were not supplied, use the default attributes */
|
|
|
|
if (!attr)
|
|
{
|
|
/* Inherit parent priority by default. except idle */
|
|
|
|
if (!is_idle_task(parent))
|
|
{
|
|
default_attr.priority = parent->sched_priority;
|
|
}
|
|
|
|
attr = &default_attr;
|
|
}
|
|
|
|
/* Allocate a TCB for the new task. */
|
|
|
|
ptcb = kmm_zalloc(sizeof(struct tcb_s) + sizeof(struct pthread_entry_s));
|
|
if (!ptcb)
|
|
{
|
|
serr("ERROR: Failed to allocate TCB\n");
|
|
return ENOMEM;
|
|
}
|
|
|
|
ptcb->flags |= TCB_FLAG_FREE_TCB;
|
|
|
|
/* Initialize the task join */
|
|
|
|
nxtask_joininit(ptcb);
|
|
|
|
/* Bind the parent's group to the new TCB (we have not yet joined the
|
|
* group).
|
|
*/
|
|
|
|
group_bind(ptcb);
|
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
/* Share the address environment of the parent task group. */
|
|
|
|
ret = addrenv_join(this_task(), ptcb);
|
|
if (ret < 0)
|
|
{
|
|
errcode = -ret;
|
|
goto errout_with_tcb;
|
|
}
|
|
#endif
|
|
|
|
if (attr->detachstate == PTHREAD_CREATE_DETACHED)
|
|
{
|
|
ptcb->flags |= TCB_FLAG_DETACHED;
|
|
}
|
|
|
|
if (attr->stackaddr)
|
|
{
|
|
/* Use pre-allocated stack */
|
|
|
|
ret = up_use_stack(ptcb, attr->stackaddr, attr->stacksize);
|
|
}
|
|
else
|
|
{
|
|
/* Allocate the stack for the TCB */
|
|
|
|
ret = up_create_stack(ptcb,
|
|
attr->stacksize + attr->guardsize,
|
|
TCB_FLAG_TTYPE_PTHREAD);
|
|
}
|
|
|
|
if (ret != OK)
|
|
{
|
|
errcode = ENOMEM;
|
|
goto errout_with_tcb;
|
|
}
|
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_ARCH_KERNEL_STACK)
|
|
/* Allocate the kernel stack */
|
|
|
|
ret = up_addrenv_kstackalloc(ptcb);
|
|
if (ret < 0)
|
|
{
|
|
errcode = ENOMEM;
|
|
goto errout_with_tcb;
|
|
}
|
|
#endif
|
|
|
|
/* Should we use the priority and scheduler specified in the pthread
|
|
* attributes? Or should we use the current thread's priority and
|
|
* scheduler?
|
|
*/
|
|
|
|
if (attr->inheritsched == PTHREAD_INHERIT_SCHED)
|
|
{
|
|
/* Get the priority (and any other scheduling parameters) for this
|
|
* thread.
|
|
*/
|
|
|
|
ret = nxsched_get_param(0, ¶m);
|
|
if (ret < 0)
|
|
{
|
|
errcode = -ret;
|
|
goto errout_with_tcb;
|
|
}
|
|
|
|
/* Get the scheduler policy for this thread */
|
|
|
|
policy = nxsched_get_scheduler(0);
|
|
if (policy < 0)
|
|
{
|
|
errcode = -policy;
|
|
goto errout_with_tcb;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/* Use the scheduler policy and policy the attributes */
|
|
|
|
policy = attr->policy;
|
|
param.sched_priority = attr->priority;
|
|
|
|
#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;
|
|
#endif
|
|
}
|
|
|
|
#ifdef CONFIG_SCHED_SPORADIC
|
|
if (policy == SCHED_SPORADIC)
|
|
{
|
|
FAR struct sporadic_s *sporadic;
|
|
clock_t repl_ticks;
|
|
clock_t budget_ticks;
|
|
|
|
/* Convert timespec values to system clock ticks */
|
|
|
|
repl_ticks = clock_time2ticks(¶m.sched_ss_repl_period);
|
|
budget_ticks = clock_time2ticks(¶m.sched_ss_init_budget);
|
|
|
|
/* The replenishment period must be greater than or equal to the
|
|
* budget period.
|
|
*/
|
|
|
|
if (repl_ticks < budget_ticks)
|
|
{
|
|
errcode = EINVAL;
|
|
goto errout_with_tcb;
|
|
}
|
|
|
|
/* Initialize the sporadic policy */
|
|
|
|
ret = nxsched_initialize_sporadic(ptcb);
|
|
if (ret >= 0)
|
|
{
|
|
sporadic = ptcb->sporadic;
|
|
DEBUGASSERT(sporadic != NULL);
|
|
|
|
/* Save the sporadic scheduling parameters */
|
|
|
|
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;
|
|
|
|
/* And start the first replenishment interval */
|
|
|
|
ret = nxsched_start_sporadic(ptcb);
|
|
}
|
|
|
|
/* Handle any failures */
|
|
|
|
if (ret < 0)
|
|
{
|
|
errcode = -ret;
|
|
goto errout_with_tcb;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
/* Initialize the task control block */
|
|
|
|
ret = pthread_setup_scheduler(ptcb, param.sched_priority, pthread_start,
|
|
entry);
|
|
if (ret != OK)
|
|
{
|
|
errcode = EBUSY;
|
|
goto errout_with_tcb;
|
|
}
|
|
|
|
/* Initialize thread local storage */
|
|
|
|
ret = tls_init_info(ptcb);
|
|
if (ret != OK)
|
|
{
|
|
errcode = -ret;
|
|
goto errout_with_tcb;
|
|
}
|
|
|
|
#ifdef CONFIG_SMP
|
|
/* pthread_setup_scheduler() will set the affinity mask by inheriting the
|
|
* 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.
|
|
*/
|
|
|
|
if (attr->affinity != 0)
|
|
{
|
|
ptcb->affinity = attr->affinity;
|
|
}
|
|
#endif
|
|
|
|
/* Configure the TCB for a pthread receiving on parameter
|
|
* passed by value
|
|
*/
|
|
|
|
pthread_tcb_setup(ptcb, parent, trampoline, arg);
|
|
|
|
/* Join the parent's task group */
|
|
|
|
group_join(ptcb);
|
|
|
|
/* Set the appropriate scheduling policy in the TCB */
|
|
|
|
ptcb->flags &= ~TCB_FLAG_POLICY_MASK;
|
|
switch (policy)
|
|
{
|
|
default:
|
|
case SCHED_FIFO:
|
|
ptcb->flags |= TCB_FLAG_SCHED_FIFO;
|
|
break;
|
|
|
|
#if CONFIG_RR_INTERVAL > 0
|
|
case SCHED_OTHER:
|
|
case SCHED_RR:
|
|
ptcb->flags |= TCB_FLAG_SCHED_RR;
|
|
ptcb->timeslice = MSEC2TICK(CONFIG_RR_INTERVAL);
|
|
break;
|
|
#endif
|
|
|
|
#ifdef CONFIG_SCHED_SPORADIC
|
|
case SCHED_SPORADIC:
|
|
ptcb->flags |= TCB_FLAG_SCHED_SPORADIC;
|
|
break;
|
|
#endif
|
|
}
|
|
|
|
/* Return the thread information to the caller */
|
|
|
|
if (thread != NULL)
|
|
{
|
|
*thread = (pthread_t)ptcb->pid;
|
|
}
|
|
|
|
/* Then activate the task */
|
|
|
|
nxtask_activate(ptcb);
|
|
|
|
return OK;
|
|
|
|
errout_with_tcb:
|
|
|
|
/* Since we do not join the group, assign group to NULL to clear binding */
|
|
|
|
ptcb->group = NULL;
|
|
|
|
nxsched_release_tcb(ptcb, TCB_FLAG_TTYPE_PTHREAD);
|
|
return errcode;
|
|
}
|