2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* sched/group/group_create.c
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +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
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +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.
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2013-01-25 18:23:38 +01:00
|
|
|
* Included Files
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-01-25 18:23:38 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sched.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
#include <nuttx/irq.h>
|
2022-02-28 08:34:03 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2013-01-25 18:23:38 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2016-11-03 19:42:02 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2019-08-02 18:01:30 +02:00
|
|
|
#include <nuttx/sched.h>
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2019-08-06 22:13:43 +02:00
|
|
|
#include "sched/sched.h"
|
2014-09-03 22:58:24 +02:00
|
|
|
#include "group/group.h"
|
2022-05-27 22:58:16 +02:00
|
|
|
#include "tls/tls.h"
|
2013-01-26 00:21:27 +01:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2013-01-26 18:28:20 +01:00
|
|
|
* Public Data
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2014-08-26 22:54:39 +02:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2013-01-25 18:23:38 +01:00
|
|
|
* Private Functions
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2019-08-06 22:13:43 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: group_inherit_identity
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* All inherit the user identity from the parent task group.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* group - The new task group.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The parent of the new task is the task at the head of the assigned task
|
|
|
|
* list for the current CPU.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_USER_IDENTITY
|
|
|
|
static inline void group_inherit_identity(FAR struct task_group_s *group)
|
|
|
|
{
|
|
|
|
FAR struct tcb_s *rtcb = this_task();
|
|
|
|
FAR struct task_group_s *rgroup = rtcb->group;
|
|
|
|
|
|
|
|
/* Inherit the user identity from the parent task group. */
|
|
|
|
|
|
|
|
DEBUGASSERT(group != NULL);
|
|
|
|
group->tg_uid = rgroup->tg_uid;
|
|
|
|
group->tg_gid = rgroup->tg_gid;
|
2023-04-15 08:47:19 +02:00
|
|
|
group->tg_euid = rgroup->tg_euid;
|
|
|
|
group->tg_egid = rgroup->tg_egid;
|
2019-08-06 22:13:43 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define group_inherit_identity(group)
|
|
|
|
#endif
|
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2013-01-25 18:23:38 +01:00
|
|
|
* Public Functions
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2024-03-04 02:19:27 +01:00
|
|
|
* Name: group_initialize
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2013-01-25 20:15:05 +01:00
|
|
|
* Create and a new task group structure for the specified TCB. This
|
|
|
|
* function is called as part of the task creation sequence. The structure
|
2014-09-03 21:49:35 +02:00
|
|
|
* allocated and zeroed, but otherwise uninitialized. The full creation
|
2013-01-25 20:15:05 +01:00
|
|
|
* of the group of a two step process: (1) First, this function allocates
|
2020-04-30 20:43:08 +02:00
|
|
|
* group structure early in the task creation sequence in order to provide
|
2024-03-04 02:19:27 +01:00
|
|
|
* a group container, then (2) group_postinitialize() is called to set up
|
|
|
|
* the group membership.
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2014-09-03 22:58:24 +02:00
|
|
|
* tcb - The tcb in need of the task group.
|
|
|
|
* ttype - Type of the thread that is the parent of the group
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2013-01-25 18:23:38 +01:00
|
|
|
* 0 (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called during task creation in a safe context. No special precautions
|
|
|
|
* are required here.
|
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2024-03-04 02:19:27 +01:00
|
|
|
int group_initialize(FAR struct task_tcb_s *tcb, uint8_t ttype)
|
2013-01-25 18:23:38 +01:00
|
|
|
{
|
2013-02-03 17:43:58 +01:00
|
|
|
FAR struct task_group_s *group;
|
2023-10-24 16:27:33 +02:00
|
|
|
int ret;
|
2013-01-26 00:21:27 +01:00
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
DEBUGASSERT(tcb && !tcb->cmn.group);
|
2013-01-25 20:15:05 +01:00
|
|
|
|
|
|
|
/* Allocate the group structure and assign it to the TCB */
|
|
|
|
|
2024-03-04 02:19:27 +01:00
|
|
|
group = &tcb->group;
|
2013-01-26 00:21:27 +01:00
|
|
|
|
2021-06-29 12:35:42 +02:00
|
|
|
#if defined(CONFIG_MM_KERNEL_HEAP)
|
2020-04-30 20:43:08 +02:00
|
|
|
/* If this group is being created for a privileged thread, then all
|
|
|
|
* elements of the group must be created for privileged access.
|
2014-09-03 21:49:35 +02:00
|
|
|
*/
|
|
|
|
|
2014-09-03 22:58:24 +02:00
|
|
|
if ((ttype & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_KERNEL)
|
2014-09-03 21:49:35 +02:00
|
|
|
{
|
|
|
|
group->tg_flags |= GROUP_FLAG_PRIVILEGED;
|
|
|
|
}
|
2021-06-29 12:35:42 +02:00
|
|
|
#endif /* defined(CONFIG_MM_KERNEL_HEAP) */
|
|
|
|
|
2022-03-01 19:24:05 +01:00
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
2024-03-06 03:13:47 +01:00
|
|
|
/* Initialize member list of the group */
|
2022-03-01 19:24:05 +01:00
|
|
|
|
2024-03-06 03:13:47 +01:00
|
|
|
sq_init(&group->tg_members);
|
2022-03-01 19:24:05 +01:00
|
|
|
#endif
|
|
|
|
|
2013-02-03 17:43:58 +01:00
|
|
|
/* Attach the group to the TCB */
|
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
tcb->cmn.group = group;
|
2013-02-03 17:43:58 +01:00
|
|
|
|
2019-08-06 22:13:43 +02:00
|
|
|
/* Inherit the user identity from the parent task group */
|
|
|
|
|
|
|
|
group_inherit_identity(group);
|
|
|
|
|
2022-02-28 08:34:03 +01:00
|
|
|
/* Initialize file descriptors for the TCB */
|
|
|
|
|
|
|
|
files_initlist(&group->tg_filelist);
|
|
|
|
|
2023-01-10 10:54:49 +01:00
|
|
|
/* Alloc task info for group */
|
2022-02-28 08:34:03 +01:00
|
|
|
|
2023-01-10 10:54:49 +01:00
|
|
|
ret = task_init_info(group);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2024-03-04 02:19:27 +01:00
|
|
|
return ret;
|
2023-01-10 10:54:49 +01:00
|
|
|
}
|
2022-02-28 08:34:03 +01:00
|
|
|
|
2016-08-10 15:37:25 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
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
|
|
|
/* Initialize the task group join */
|
2013-02-03 17:43:58 +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
|
|
|
nxrmutex_init(&group->tg_joinlock);
|
|
|
|
sq_init(&group->tg_joinqueue);
|
2013-02-03 17:43:58 +01:00
|
|
|
#endif
|
|
|
|
|
2016-08-10 15:37:25 +02:00
|
|
|
#if defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_SCHED_HAVE_PARENT)
|
2022-10-18 11:09:22 +02:00
|
|
|
/* Initialize the exit/wait semaphores */
|
2016-08-10 15:37:25 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_init(&group->tg_exitsem, 0, 0);
|
2016-08-10 15:37:25 +02:00
|
|
|
#endif
|
|
|
|
|
2013-01-26 00:21:27 +01:00
|
|
|
return OK;
|
2013-01-25 20:15:05 +01:00
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2024-03-04 02:19:27 +01:00
|
|
|
* Name: group_postinitialize
|
2013-01-25 20:15:05 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Add the task as the initial member of the group. The full creation of
|
|
|
|
* the group of a two step process: (1) First, this group structure is
|
2024-03-04 02:19:27 +01:00
|
|
|
* allocated by group_initialize() early in the task creation sequence,
|
|
|
|
* then (2) this function is called to set up the initial group
|
|
|
|
* membership.
|
2013-01-25 20:15:05 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2013-01-25 20:15:05 +01:00
|
|
|
* tcb - The tcb in need of the task group.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2022-03-01 19:24:05 +01:00
|
|
|
* None.
|
2013-01-25 20:15:05 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called during task creation in a safe context. No special precautions
|
|
|
|
* are required here.
|
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2013-01-25 20:15:05 +01:00
|
|
|
|
2024-03-04 02:19:27 +01:00
|
|
|
void group_postinitialize(FAR struct task_tcb_s *tcb)
|
2013-01-25 20:15:05 +01:00
|
|
|
{
|
2013-01-25 18:23:38 +01:00
|
|
|
FAR struct task_group_s *group;
|
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
DEBUGASSERT(tcb && tcb->cmn.group);
|
|
|
|
group = tcb->cmn.group;
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2022-04-07 14:17:21 +02:00
|
|
|
/* Allocate mm_map list if required */
|
|
|
|
|
2023-01-11 09:23:08 +01:00
|
|
|
mm_map_initialize(&group->tg_mm_map,
|
|
|
|
(tcb->cmn.flags & TCB_FLAG_TTYPE_KERNEL) != 0);
|
2022-04-07 14:17:21 +02:00
|
|
|
|
2013-01-26 00:21:27 +01:00
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
2013-02-04 23:38:59 +01:00
|
|
|
/* Assign the PID of this new task as a member of the group. */
|
2013-01-25 20:15:05 +01:00
|
|
|
|
2024-03-06 03:13:47 +01:00
|
|
|
sq_addlast(&tcb->cmn.member, &group->tg_members);
|
2013-01-25 20:15:05 +01:00
|
|
|
#endif
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
/* Save the ID of the main task within the group of threads. This needed
|
2020-10-29 01:12:53 +01:00
|
|
|
* for things like SIGCHLD. It ID is also saved in the TCB of the main
|
2013-02-04 23:38:59 +01:00
|
|
|
* task but is also retained in the group which may persist after the main
|
|
|
|
* task has exited.
|
|
|
|
*/
|
|
|
|
|
2021-03-03 18:11:40 +01:00
|
|
|
group->tg_pid = tcb->cmn.pid;
|
2013-01-25 18:23:38 +01:00
|
|
|
}
|