2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2014-08-08 22:21:48 +02:00
|
|
|
* sched/pthread/pthread_create.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-05-16 18:05:46 +02: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-05-16 18:05:46 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-05-16 18:05:46 +02: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-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-14 22:15:18 +01:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <sys/types.h>
|
2009-12-14 22:15:18 +01:00
|
|
|
#include <stdbool.h>
|
2007-03-20 17:51:12 +01:00
|
|
|
#include <string.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <debug.h>
|
2015-07-24 17:03:21 +02:00
|
|
|
#include <assert.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <queue.h>
|
2011-03-31 03:42:50 +02:00
|
|
|
|
2018-01-30 18:07:36 +01:00
|
|
|
#include <nuttx/sched.h>
|
2016-11-03 19:42:02 +01:00
|
|
|
#include <nuttx/arch.h>
|
2016-11-03 19:58:02 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2011-03-31 03:42:50 +02:00
|
|
|
#include <nuttx/pthread.h>
|
2009-12-14 22:15:18 +01: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-08 22:43:02 +02:00
|
|
|
#include "clock/clock.h"
|
2014-08-08 20:55:02 +02:00
|
|
|
#include "pthread/pthread.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2015-07-23 22:36:49 +02:00
|
|
|
* Public Data
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-09-07 18:46:58 +02: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-18 00:21:28 +01:00
|
|
|
|
2014-09-07 18:46:58 +02:00
|
|
|
const pthread_attr_t g_default_pthread_attr = PTHREAD_ATTR_INITIALIZER;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2015-07-23 22:36:49 +02:00
|
|
|
* Private Data
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-02-27 01:15:16 +01:00
|
|
|
#if CONFIG_TASK_NAME_SIZE > 0
|
2007-03-14 00:03:12 +01:00
|
|
|
/* This is the name for name-less pthreads */
|
|
|
|
|
|
|
|
static const char g_pthreadname[] = "<pthread>";
|
2017-02-27 01:15:16 +01:00
|
|
|
#endif
|
2007-03-14 00:03:12 +01:00
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Private Functions
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2007-03-14 00:03:12 +01:00
|
|
|
* Name: pthread_argsetup
|
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 21:30:31 +02:00
|
|
|
* This functions sets up parameters in the Task Control Block (TCB) in
|
|
|
|
* preparation for starting a new thread.
|
2007-03-14 00:03:12 +01:00
|
|
|
*
|
2020-05-25 23:30:40 +02:00
|
|
|
* pthread_argsetup() is called from nxtask_init() and nxtask_start() to
|
2020-04-09 14:46:24 +02: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-14 00:03:12 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* tcb - Address of the new task's TCB
|
2013-02-04 22:24:00 +01:00
|
|
|
* arg - The argument to provide to the pthread on startup.
|
2007-03-14 00:03:12 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2009-05-19 21:30:57 +02:00
|
|
|
* None
|
2007-03-14 00:03:12 +01:00
|
|
|
*
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-03-14 00:03:12 +01:00
|
|
|
|
2020-01-12 17:07:54 +01:00
|
|
|
static inline void pthread_argsetup(FAR struct pthread_tcb_s *tcb,
|
|
|
|
pthread_addr_t arg)
|
2007-03-14 00:03:12 +01:00
|
|
|
{
|
|
|
|
#if CONFIG_TASK_NAME_SIZE > 0
|
|
|
|
/* Copy the pthread name into the TCB */
|
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
strncpy(tcb->cmn.name, g_pthreadname, CONFIG_TASK_NAME_SIZE);
|
2014-12-17 19:24:02 +01:00
|
|
|
tcb->cmn.name[CONFIG_TASK_NAME_SIZE] = '\0';
|
2007-03-14 00:03:12 +01:00
|
|
|
#endif /* CONFIG_TASK_NAME_SIZE */
|
|
|
|
|
|
|
|
/* For pthreads, args are strictly pass-by-value; that actual
|
|
|
|
* type wrapped by pthread_addr_t is unknown.
|
|
|
|
*/
|
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
tcb->arg = arg;
|
2007-03-14 00:03:12 +01:00
|
|
|
}
|
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: pthread_addjoininfo
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2013-02-03 17:43:58 +01:00
|
|
|
* Add a join structure to the local data set.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-02-18 00:21:28 +01:00
|
|
|
* pjoin
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2009-05-19 21:30:57 +02:00
|
|
|
* None
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The caller has provided protection from re-entrancy.
|
|
|
|
*
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-02-03 17:43:58 +01:00
|
|
|
static inline void pthread_addjoininfo(FAR struct task_group_s *group,
|
|
|
|
FAR struct join_s *pjoin)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
pjoin->next = NULL;
|
2013-02-03 17:43:58 +01:00
|
|
|
if (!group->tg_jointail)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2013-02-03 17:43:58 +01:00
|
|
|
group->tg_joinhead = pjoin;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-03 17:43:58 +01:00
|
|
|
group->tg_jointail->next = pjoin;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2013-02-03 17:43:58 +01:00
|
|
|
group->tg_jointail = pjoin;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Name: pthread_start
|
|
|
|
*
|
|
|
|
* Description:
|
2013-02-03 17:43:58 +01:00
|
|
|
* This function is the low level entry point into the pthread
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-02-18 00:21:28 +01:00
|
|
|
* None
|
|
|
|
*
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
static void pthread_start(void)
|
|
|
|
{
|
2016-02-07 00:44:41 +01:00
|
|
|
FAR struct pthread_tcb_s *ptcb = (FAR struct pthread_tcb_s *)this_task();
|
2013-02-04 22:24:00 +01:00
|
|
|
FAR struct task_group_s *group = ptcb->cmn.group;
|
2015-10-08 03:59:14 +02:00
|
|
|
FAR struct join_s *pjoin = (FAR struct join_s *)ptcb->joininfo;
|
2007-02-18 00:21:28 +01:00
|
|
|
pthread_addr_t exit_status;
|
|
|
|
|
2013-02-03 17:43:58 +01:00
|
|
|
DEBUGASSERT(group && pjoin);
|
|
|
|
|
2018-09-14 14:55:45 +02:00
|
|
|
/* Successfully spawned, add the pjoin to our data set. */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_sem_take(&group->tg_joinsem, NULL, false);
|
2013-02-03 17:43:58 +01:00
|
|
|
pthread_addjoininfo(group, pjoin);
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_sem_give(&group->tg_joinsem);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Report to the spawner that we successfully started. */
|
|
|
|
|
2009-12-14 22:15:18 +01:00
|
|
|
pjoin->started = true;
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_sem_give(&pjoin->data_sem);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-10-01 19:38:22 +02: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ptcb->cmn.sched_priority > ptcb->cmn.init_priority)
|
|
|
|
{
|
2020-05-09 20:40:14 +02:00
|
|
|
DEBUGVERIFY(nxsched_set_priority(&ptcb->cmn, ptcb->cmn.init_priority));
|
2016-10-01 19:38:22 +02:00
|
|
|
}
|
|
|
|
|
2013-03-16 18:37:40 +01: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-18 00:21:28 +01:00
|
|
|
|
2020-04-29 21:21:23 +02:00
|
|
|
#ifdef CONFIG_BUILD_FLAT
|
|
|
|
exit_status = (*ptcb->cmn.entry.pthread)(ptcb->arg);
|
|
|
|
#else
|
2013-03-16 18:37:40 +01:00
|
|
|
up_pthread_start(ptcb->cmn.entry.pthread, ptcb->arg);
|
|
|
|
exit_status = NULL;
|
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-03-16 18:37:40 +01:00
|
|
|
/* The thread has returned (should never happen in the kernel mode case) */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
pthread_exit(exit_status);
|
|
|
|
}
|
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Name: pthread_create
|
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 21:30:31 +02:00
|
|
|
* This function creates and activates a new thread with a specified
|
|
|
|
* attributes.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* thread
|
|
|
|
* attr
|
2007-03-11 23:19:01 +01:00
|
|
|
* start_routine
|
2007-02-18 00:21:28 +01:00
|
|
|
* arg
|
2009-05-19 21:30:57 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2009-05-19 21:30:57 +02:00
|
|
|
* OK (0) on success; a (non-negated) errno value on failure. The errno
|
|
|
|
* variable is not set.
|
|
|
|
*
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-09-08 14:21:48 +02:00
|
|
|
int pthread_create(FAR pthread_t *thread, FAR const pthread_attr_t *attr,
|
2008-01-30 22:59:12 +01:00
|
|
|
pthread_startroutine_t start_routine, pthread_addr_t arg)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2013-02-04 22:24:00 +01:00
|
|
|
FAR struct pthread_tcb_s *ptcb;
|
2013-02-03 17:43:58 +01:00
|
|
|
FAR struct join_s *pjoin;
|
2015-07-25 20:50:53 +02:00
|
|
|
struct sched_param param;
|
2007-02-18 00:21:28 +01:00
|
|
|
int policy;
|
2013-01-25 18:23:38 +01:00
|
|
|
int errcode;
|
2007-02-18 00:21:28 +01:00
|
|
|
pid_t pid;
|
2013-01-25 20:15:05 +01:00
|
|
|
int ret;
|
2015-07-01 14:24:34 +02:00
|
|
|
bool group_joined = false;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* If attributes were not supplied, use the default attributes */
|
|
|
|
|
|
|
|
if (!attr)
|
|
|
|
{
|
2014-09-08 14:21:48 +02:00
|
|
|
attr = &g_default_pthread_attr;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a TCB for the new task. */
|
|
|
|
|
2020-04-09 14:46:24 +02:00
|
|
|
ptcb = (FAR struct pthread_tcb_s *)
|
|
|
|
kmm_zalloc(sizeof(struct pthread_tcb_s));
|
2007-02-18 00:21:28 +01:00
|
|
|
if (!ptcb)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
serr("ERROR: Failed to allocate TCB\n");
|
2009-05-19 21:30:57 +02:00
|
|
|
return ENOMEM;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2013-01-25 20:15:05 +01:00
|
|
|
/* Bind the parent's group to the new TCB (we have not yet joined the
|
|
|
|
* group).
|
|
|
|
*/
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
ret = group_bind(ptcb);
|
2013-01-25 20:15:05 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = ENOMEM;
|
|
|
|
goto errout_with_tcb;
|
|
|
|
}
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2014-08-24 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2014-08-22 20:32:34 +02:00
|
|
|
/* Share the address environment of the parent task group. */
|
|
|
|
|
2016-02-07 00:44:41 +01:00
|
|
|
ret = up_addrenv_attach(ptcb->cmn.group, this_task());
|
2012-12-11 22:42:15 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2013-01-25 18:23:38 +01:00
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_tcb;
|
2012-12-11 22:42:15 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Allocate a detachable structure to support pthread_join logic */
|
|
|
|
|
2015-10-08 03:59:14 +02:00
|
|
|
pjoin = (FAR struct join_s *)kmm_zalloc(sizeof(struct join_s));
|
2007-02-18 00:21:28 +01:00
|
|
|
if (!pjoin)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
serr("ERROR: Failed to allocate join\n");
|
2013-01-25 18:23:38 +01:00
|
|
|
errcode = ENOMEM;
|
|
|
|
goto errout_with_tcb;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2020-08-19 17:32:48 +02:00
|
|
|
if (attr->detachstate == PTHREAD_CREATE_DETACHED)
|
|
|
|
{
|
|
|
|
pjoin->detached = true;
|
|
|
|
}
|
|
|
|
|
2018-08-23 17:48:53 +02:00
|
|
|
if (attr->stackaddr)
|
|
|
|
{
|
|
|
|
/* Use pre-allocated stack */
|
|
|
|
|
|
|
|
ret = up_use_stack((FAR struct tcb_s *)ptcb, attr->stackaddr,
|
|
|
|
attr->stacksize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Allocate the stack for the TCB */
|
|
|
|
|
|
|
|
ret = up_create_stack((FAR struct tcb_s *)ptcb, attr->stacksize,
|
|
|
|
TCB_FLAG_TTYPE_PTHREAD);
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-12-11 22:42:15 +01:00
|
|
|
if (ret != OK)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2013-01-25 18:23:38 +01:00
|
|
|
errcode = ENOMEM;
|
|
|
|
goto errout_with_join;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2015-07-25 20:50:53 +02: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-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (attr->inheritsched == PTHREAD_INHERIT_SCHED)
|
|
|
|
{
|
2015-07-23 21:16:32 +02:00
|
|
|
/* Get the priority (and any other scheduling parameters) for this
|
|
|
|
* thread.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-05-09 16:04:45 +02:00
|
|
|
ret = nxsched_get_param(0, ¶m);
|
2018-01-30 18:07:36 +01:00
|
|
|
if (ret < 0)
|
2007-02-27 22:17:21 +01:00
|
|
|
{
|
2018-01-30 18:07:36 +01:00
|
|
|
errcode = -ret;
|
2015-07-23 21:16:32 +02:00
|
|
|
goto errout_with_join;
|
2007-02-27 22:17:21 +01:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Get the scheduler policy for this thread */
|
|
|
|
|
2020-05-09 16:04:45 +02:00
|
|
|
policy = nxsched_get_scheduler(0);
|
2018-01-30 18:07:36 +01:00
|
|
|
if (policy < 0)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2018-01-30 18:07:36 +01:00
|
|
|
errcode = -policy;
|
2015-07-23 21:16:32 +02:00
|
|
|
goto errout_with_join;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2015-07-25 20:50:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Use the scheduler policy and policy the attributes */
|
2015-07-23 21:16:32 +02:00
|
|
|
|
2015-07-25 20:50:53 +02:00
|
|
|
policy = attr->policy;
|
|
|
|
param.sched_priority = attr->priority;
|
2015-07-23 21:16:32 +02:00
|
|
|
|
2015-07-25 20:50:53 +02: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-18 00:21:28 +01:00
|
|
|
#endif
|
|
|
|
}
|
2015-07-25 20:50:53 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_SPORADIC
|
|
|
|
if (policy == SCHED_SPORADIC)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2015-07-25 20:50:53 +02:00
|
|
|
FAR struct sporadic_s *sporadic;
|
2018-06-16 20:16:13 +02:00
|
|
|
sclock_t repl_ticks;
|
|
|
|
sclock_t budget_ticks;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-07-25 20:50:53 +02:00
|
|
|
/* Convert timespec values to system clock ticks */
|
2015-07-23 21:16:32 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
clock_time2ticks(¶m.sched_ss_repl_period, &repl_ticks);
|
|
|
|
clock_time2ticks(¶m.sched_ss_init_budget, &budget_ticks);
|
2015-07-23 21:16:32 +02:00
|
|
|
|
2015-07-25 20:50:53 +02:00
|
|
|
/* The replenishment period must be greater than or equal to the
|
|
|
|
* budget period.
|
|
|
|
*/
|
2015-07-23 21:16:32 +02:00
|
|
|
|
2015-07-25 20:50:53 +02:00
|
|
|
if (repl_ticks < budget_ticks)
|
|
|
|
{
|
|
|
|
errcode = EINVAL;
|
|
|
|
goto errout_with_join;
|
|
|
|
}
|
2015-07-23 21:16:32 +02:00
|
|
|
|
2015-07-25 20:50:53 +02:00
|
|
|
/* Initialize the sporadic policy */
|
2015-07-23 23:08:41 +02:00
|
|
|
|
2020-05-09 20:40:14 +02:00
|
|
|
ret = nxsched_initialize_sporadic(&ptcb->cmn);
|
2015-07-25 20:50:53 +02:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
sporadic = ptcb->cmn.sporadic;
|
|
|
|
DEBUGASSERT(sporadic != NULL);
|
2015-07-23 23:08:41 +02:00
|
|
|
|
|
|
|
/* Save the sporadic scheduling parameters */
|
|
|
|
|
2015-07-25 20:50:53 +02: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 23:08:41 +02:00
|
|
|
|
2015-07-25 20:50:53 +02:00
|
|
|
/* And start the first replenishment interval */
|
2015-07-24 17:03:21 +02:00
|
|
|
|
2020-05-09 20:40:14 +02:00
|
|
|
ret = nxsched_start_sporadic(&ptcb->cmn);
|
2015-07-25 20:50:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle any failures */
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_join;
|
2015-07-23 23:08:41 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2015-07-25 20:50:53 +02:00
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-11 23:19:01 +01:00
|
|
|
/* Initialize the task control block */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-05-16 17:06:29 +02:00
|
|
|
ret = pthread_setup_scheduler(ptcb, param.sched_priority, pthread_start,
|
2020-05-16 18:05:46 +02:00
|
|
|
start_routine);
|
2012-12-11 22:42:15 +01:00
|
|
|
if (ret != OK)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2013-01-25 18:23:38 +01:00
|
|
|
errcode = EBUSY;
|
|
|
|
goto errout_with_join;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2016-02-20 00:33:35 +01:00
|
|
|
#ifdef CONFIG_SMP
|
2020-05-16 17:06:29 +02:00
|
|
|
/* pthread_setup_scheduler() will set the affinity mask by inheriting the
|
2016-02-20 00:33:35 +01: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-15 01:19:27 +02:00
|
|
|
if (attr->affinity != 0)
|
|
|
|
{
|
|
|
|
ptcb->cmn.affinity = attr->affinity;
|
|
|
|
}
|
2016-02-20 00:33:35 +01:00
|
|
|
#endif
|
|
|
|
|
2007-03-11 23:19:01 +01:00
|
|
|
/* Configure the TCB for a pthread receiving on parameter
|
|
|
|
* passed by value
|
|
|
|
*/
|
|
|
|
|
2007-12-31 00:34:20 +01:00
|
|
|
pthread_argsetup(ptcb, arg);
|
2007-03-11 23:19:01 +01:00
|
|
|
|
2013-01-25 20:15:05 +01:00
|
|
|
/* Join the parent's task group */
|
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
ret = group_join(ptcb);
|
2013-01-25 20:15:05 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = ENOMEM;
|
|
|
|
goto errout_with_join;
|
|
|
|
}
|
2015-07-01 14:24:34 +02:00
|
|
|
|
|
|
|
group_joined = true;
|
2013-01-25 20:15:05 +01:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Attach the join info to the TCB. */
|
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
ptcb->joininfo = (FAR void *)pjoin;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-07-23 17:58:41 +02:00
|
|
|
/* Set the appropriate scheduling policy in the TCB */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-11-28 17:05:36 +01:00
|
|
|
ptcb->cmn.flags &= ~TCB_FLAG_POLICY_MASK;
|
2015-07-23 17:58:41 +02:00
|
|
|
switch (policy)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2015-07-23 17:58:41 +02:00
|
|
|
default:
|
|
|
|
DEBUGPANIC();
|
|
|
|
case SCHED_FIFO:
|
|
|
|
ptcb->cmn.flags |= TCB_FLAG_SCHED_FIFO;
|
|
|
|
break;
|
|
|
|
|
|
|
|
#if CONFIG_RR_INTERVAL > 0
|
|
|
|
case SCHED_RR:
|
|
|
|
ptcb->cmn.flags |= TCB_FLAG_SCHED_RR;
|
|
|
|
ptcb->cmn.timeslice = MSEC2TICK(CONFIG_RR_INTERVAL);
|
|
|
|
break;
|
2007-02-18 00:21:28 +01:00
|
|
|
#endif
|
|
|
|
|
2015-07-23 17:58:41 +02:00
|
|
|
#ifdef CONFIG_SCHED_SPORADIC
|
|
|
|
case SCHED_SPORADIC:
|
|
|
|
ptcb->cmn.flags |= TCB_FLAG_SCHED_SPORADIC;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0 /* Not supported */
|
|
|
|
case SCHED_OTHER:
|
|
|
|
ptcb->cmn.flags |= TCB_FLAG_SCHED_OTHER;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-12-09 14:23:00 +01:00
|
|
|
#ifdef CONFIG_CANCELLATION_POINTS
|
|
|
|
/* Set the deferred cancellation type */
|
|
|
|
|
|
|
|
ptcb->cmn.flags |= TCB_FLAG_CANCEL_DEFERRED;
|
|
|
|
#endif
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Get the assigned pid before we start the task (who knows what
|
|
|
|
* could happen to ptcb after this!). Copy this ID into the join structure
|
|
|
|
* as well.
|
|
|
|
*/
|
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
pid = (int)ptcb->cmn.pid;
|
2007-02-20 23:39:56 +01:00
|
|
|
pjoin->thread = (pthread_t)pid;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Initialize the semaphores in the join structure to zero. */
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
ret = nxsem_init(&pjoin->data_sem, 0, 0);
|
2012-12-11 22:42:15 +01:00
|
|
|
if (ret == OK)
|
2009-05-19 21:30:57 +02:00
|
|
|
{
|
2017-10-03 20:51:15 +02:00
|
|
|
ret = nxsem_init(&pjoin->exit_sem, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = -ret;
|
2009-05-19 21:30:57 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-11-03 19:42:02 +01:00
|
|
|
/* Thse semaphores are used for signaling and, hence, should not have
|
|
|
|
* priority inheritance enabled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
2020-05-17 15:56:21 +02:00
|
|
|
ret = nxsem_set_protocol(&pjoin->data_sem, SEM_PRIO_NONE);
|
2016-11-03 19:42:02 +01:00
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2020-05-17 15:56:21 +02:00
|
|
|
ret = nxsem_set_protocol(&pjoin->exit_sem, SEM_PRIO_NONE);
|
2017-10-03 20:51:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-01-30 23:16:41 +01:00
|
|
|
ret = -ret;
|
2017-10-03 20:51:15 +02:00
|
|
|
}
|
2016-11-03 19:42:02 +01:00
|
|
|
}
|
|
|
|
|
2016-10-01 19:38:22 +02:00
|
|
|
/* If the priority of the new pthread is lower than the priority of the
|
|
|
|
* parent thread, then starting the pthread could result in both the
|
|
|
|
* parent and the pthread to be blocked. This is a recipe for priority
|
|
|
|
* inversion issues.
|
|
|
|
*
|
|
|
|
* We avoid this here by boosting the priority of the (inactive) pthread
|
|
|
|
* so it has the same priority as the parent thread.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
FAR struct tcb_s *parent = this_task();
|
|
|
|
DEBUGASSERT(parent != NULL);
|
|
|
|
|
|
|
|
if (ptcb->cmn.sched_priority < parent->sched_priority)
|
|
|
|
{
|
2020-05-09 20:40:14 +02:00
|
|
|
ret = nxsched_set_priority(&ptcb->cmn, parent->sched_priority);
|
2017-10-03 20:51:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-01-30 18:07:36 +01:00
|
|
|
ret = -ret;
|
2017-10-03 20:51:15 +02:00
|
|
|
}
|
2016-10-01 19:38:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then activate the task */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
sched_lock();
|
2012-12-11 22:42:15 +01:00
|
|
|
if (ret == OK)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2020-06-04 14:19:40 +02:00
|
|
|
nxtask_activate((FAR struct tcb_s *)ptcb);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Wait for the task to actually get running and to register
|
2013-02-03 17:43:58 +01:00
|
|
|
* its join structure.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_sem_take(&pjoin->data_sem, NULL, false);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Return the thread information to the caller */
|
|
|
|
|
2012-12-11 22:42:15 +01:00
|
|
|
if (thread)
|
2017-08-15 01:19:27 +02:00
|
|
|
{
|
|
|
|
*thread = (pthread_t)pid;
|
|
|
|
}
|
2012-12-11 22:42:15 +01:00
|
|
|
|
|
|
|
if (!pjoin->started)
|
|
|
|
{
|
|
|
|
ret = EINVAL;
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
sched_unlock();
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_destroy(&pjoin->data_sem);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sched_unlock();
|
2015-10-08 03:59:14 +02:00
|
|
|
dq_rem((FAR dq_entry_t *)ptcb, (FAR dq_queue_t *)&g_inactivetasks);
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_destroy(&pjoin->data_sem);
|
|
|
|
nxsem_destroy(&pjoin->exit_sem);
|
2013-01-25 18:23:38 +01:00
|
|
|
|
|
|
|
errcode = EIO;
|
|
|
|
goto errout_with_join;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2012-12-11 22:42:15 +01:00
|
|
|
return ret;
|
2013-01-25 18:23:38 +01:00
|
|
|
|
|
|
|
errout_with_join:
|
2020-04-07 16:31:47 +02:00
|
|
|
kmm_free(pjoin);
|
2013-01-25 18:23:38 +01:00
|
|
|
ptcb->joininfo = NULL;
|
|
|
|
|
|
|
|
errout_with_tcb:
|
2019-10-24 18:49:28 +02:00
|
|
|
|
2015-07-01 14:24:34 +02:00
|
|
|
/* Clear group binding */
|
|
|
|
|
|
|
|
if (ptcb && !group_joined)
|
|
|
|
{
|
|
|
|
ptcb->cmn.group = NULL;
|
|
|
|
}
|
|
|
|
|
2020-05-09 16:04:45 +02:00
|
|
|
nxsched_release_tcb((FAR struct tcb_s *)ptcb, TCB_FLAG_TTYPE_PTHREAD);
|
2013-01-25 18:23:38 +01:00
|
|
|
return errcode;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|