sched/pthread: Implement pthread_attr_[get|set]detachstate

specified here:
https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_attr_getdetachstate.html

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Change-Id: I84ec2d14f14cb7118aac3f7f356f83a5f9af4e18
This commit is contained in:
Xiang Xiao 2020-08-19 23:32:48 +08:00 committed by Abdelatif Guettouche
parent f9359d3fe8
commit beb745ef92
6 changed files with 138 additions and 1 deletions

View File

@ -68,6 +68,7 @@
PTHREAD_DEFAULT_PRIORITY, /* priority */ \
PTHREAD_DEFAULT_POLICY, /* policy */ \
PTHREAD_EXPLICIT_SCHED, /* inheritsched */ \
PTHREAD_CREATE_JOINABLE, /* detachstate */ \
0, /* low_priority */ \
0, /* max_repl */ \
0, /* affinity */ \
@ -82,6 +83,7 @@
PTHREAD_DEFAULT_PRIORITY, /* priority */ \
PTHREAD_DEFAULT_POLICY, /* policy */ \
PTHREAD_EXPLICIT_SCHED, /* inheritsched */ \
PTHREAD_CREATE_JOINABLE, /* detachstate */ \
0, /* low_priority */ \
0, /* max_repl */ \
NULL, /* stackaddr */ \
@ -95,6 +97,7 @@
PTHREAD_DEFAULT_PRIORITY, /* priority */ \
PTHREAD_DEFAULT_POLICY, /* policy */ \
PTHREAD_EXPLICIT_SCHED, /* inheritsched */ \
PTHREAD_CREATE_JOINABLE, /* detachstate */ \
0, /* affinity */ \
NULL, /* stackaddr */ \
PTHREAD_STACK_DEFAULT, /* stacksize */ \
@ -105,6 +108,7 @@
PTHREAD_DEFAULT_PRIORITY, /* priority */ \
PTHREAD_DEFAULT_POLICY, /* policy */ \
PTHREAD_EXPLICIT_SCHED, /* inheritsched */ \
PTHREAD_CREATE_JOINABLE, /* detachstate */ \
NULL, /* stackaddr */ \
PTHREAD_STACK_DEFAULT, /* stacksize */ \
}

View File

@ -129,6 +129,11 @@
#define PTHREAD_INHERIT_SCHED 0
#define PTHREAD_EXPLICIT_SCHED 1
/* Detach state */
#define PTHREAD_CREATE_JOINABLE 0
#define PTHREAD_CREATE_DETACHED 1
/* Default priority */
#define PTHREAD_DEFAULT_PRIORITY 100
@ -241,7 +246,7 @@ struct pthread_attr_s
uint8_t priority; /* Priority of the pthread */
uint8_t policy; /* Pthread scheduler policy */
uint8_t inheritsched; /* Inherit parent priority/policy? */
uint8_t detachstate; /* Initialize to the detach state */
#ifdef CONFIG_SCHED_SPORADIC
uint8_t low_priority; /* Low scheduling priority */
uint8_t max_repl; /* Maximum pending replenishments */
@ -465,6 +470,10 @@ int pthread_attr_setinheritsched(FAR pthread_attr_t *attr,
int inheritsched);
int pthread_attr_getinheritsched(FAR const pthread_attr_t *attr,
FAR int *inheritsched);
int pthread_attr_getdetachstate(FAR const pthread_attr_t *attr,
FAR int *detachstate);
int pthread_attr_setdetachstate(FAR pthread_attr_t *attr,
int detachstate);
#ifdef CONFIG_SMP
/* Set or obtain thread affinity attributes */

View File

@ -30,6 +30,7 @@ ifneq ($(CONFIG_DISABLE_PTHREAD),y)
CSRCS += pthread_attr_init.c pthread_attr_destroy.c
CSRCS += pthread_attr_setschedpolicy.c pthread_attr_getschedpolicy.c
CSRCS += pthread_attr_setinheritsched.c pthread_attr_getinheritsched.c
CSRCS += pthread_attr_setdetachstate.c pthread_attr_getdetachstate.c
CSRCS += pthread_attr_setstacksize.c pthread_attr_getstacksize.c
CSRCS += pthread_attr_setstack.c pthread_attr_getstack.c
CSRCS += pthread_attr_setschedparam.c pthread_attr_getschedparam.c

View File

@ -0,0 +1,59 @@
/****************************************************************************
* libs/libc/pthread/pthread_attr_getdetachstate.c
*
* 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 <pthread.h>
#include <errno.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: pthread_attr_getdetachstate
*
* Description:
* Get the detachstate attribute
*
* Returned Value:
* Upon successful completion, pthread_attr_getdetachstate() shall return
* a value of 0; otherwise, an error number shall be returned to indicate
* the error. The pthread_attr_getdetachstate() function stores the value
* of the detachstate attribute in detachstate if successful.
*
****************************************************************************/
int pthread_attr_getdetachstate(FAR const pthread_attr_t *attr,
FAR int *detachstate)
{
if (!attr || !detachstate)
{
return EINVAL;
}
*detachstate = attr->detachstate;
return OK;
}

View File

@ -0,0 +1,59 @@
/****************************************************************************
* libs/libc/pthread/pthread_attr_setdetachstate.c
*
* 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 <pthread.h>
#include <errno.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: pthread_attr_setdetachstate
*
* Description:
* Set the detachstate attribute
*
* Returned Value:
* Upon successful completion, pthread_attr_setdetachstate() shall return
* a value of 0; otherwise, an error number shall be returned to indicate
* the error.
*
****************************************************************************/
int pthread_attr_setdetachstate(FAR pthread_attr_t *attr,
int detachstate)
{
if (!attr || (detachstate != PTHREAD_CREATE_DETACHED &&
detachstate != PTHREAD_CREATE_JOINABLE))
{
return EINVAL;
}
attr->detachstate = detachstate;
return OK;
}

View File

@ -284,6 +284,11 @@ int pthread_create(FAR pthread_t *thread, FAR const pthread_attr_t *attr,
goto errout_with_tcb;
}
if (attr->detachstate == PTHREAD_CREATE_DETACHED)
{
pjoin->detached = true;
}
if (attr->stackaddr)
{
/* Use pre-allocated stack */