You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ag...@apache.org on 2020/08/20 09:46:22 UTC

[incubator-nuttx] branch master updated: sched/pthread: Implement pthread_attr_[get|set]detachstate

This is an automated email from the ASF dual-hosted git repository.

aguettouche pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new beb745e  sched/pthread: Implement pthread_attr_[get|set]detachstate
beb745e is described below

commit beb745ef92959db500c8f79ca3518cbf1d67b9b2
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Wed Aug 19 23:32:48 2020 +0800

    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 <xi...@xiaomi.com>
    Change-Id: I84ec2d14f14cb7118aac3f7f356f83a5f9af4e18
---
 include/nuttx/pthread.h                         |  4 ++
 include/pthread.h                               | 11 ++++-
 libs/libc/pthread/Make.defs                     |  1 +
 libs/libc/pthread/pthread_attr_getdetachstate.c | 59 +++++++++++++++++++++++++
 libs/libc/pthread/pthread_attr_setdetachstate.c | 59 +++++++++++++++++++++++++
 sched/pthread/pthread_create.c                  |  5 +++
 6 files changed, 138 insertions(+), 1 deletion(-)

diff --git a/include/nuttx/pthread.h b/include/nuttx/pthread.h
index ce1202e..07712b1 100644
--- a/include/nuttx/pthread.h
+++ b/include/nuttx/pthread.h
@@ -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 */ \
   }
diff --git a/include/pthread.h b/include/pthread.h
index 4495c77..2f91463 100644
--- a/include/pthread.h
+++ b/include/pthread.h
@@ -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 */
diff --git a/libs/libc/pthread/Make.defs b/libs/libc/pthread/Make.defs
index 53afa0a..3f62e4e 100644
--- a/libs/libc/pthread/Make.defs
+++ b/libs/libc/pthread/Make.defs
@@ -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
diff --git a/libs/libc/pthread/pthread_attr_getdetachstate.c b/libs/libc/pthread/pthread_attr_getdetachstate.c
new file mode 100644
index 0000000..d27d55d
--- /dev/null
+++ b/libs/libc/pthread/pthread_attr_getdetachstate.c
@@ -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;
+}
diff --git a/libs/libc/pthread/pthread_attr_setdetachstate.c b/libs/libc/pthread/pthread_attr_setdetachstate.c
new file mode 100644
index 0000000..1669fcc
--- /dev/null
+++ b/libs/libc/pthread/pthread_attr_setdetachstate.c
@@ -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;
+}
diff --git a/sched/pthread/pthread_create.c b/sched/pthread/pthread_create.c
index daef024..1b836f5 100644
--- a/sched/pthread/pthread_create.c
+++ b/sched/pthread/pthread_create.c
@@ -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 */