Attached are files for posix support of the affinity
attribute in pthread.  The affinity methods and tests
will follow.


Jennifer Averett
On-Line Applications Research
From d4214167cc08bfe6464a719468b0f7513a90ca42 Mon Sep 17 00:00:00 2001
From: Jennifer Averett <jennifer.aver...@oarcorp.com>
Date: Fri, 31 Jan 2014 08:54:45 -0600
Subject: [PATCH 07/15] posix: Add support for new lib affinity elements.

---
 cpukit/posix/include/rtems/posix/pthreadimpl.h | 65 +++++++++++++++++++++++++-
 cpukit/posix/src/pthread.c                     | 52 ++++++++++++++++-----
 cpukit/posix/src/pthreadattrinit.c             |  5 +-
 cpukit/posix/src/pthreadcreate.c               | 19 +++++++-
 4 files changed, 125 insertions(+), 16 deletions(-)

diff --git a/cpukit/posix/include/rtems/posix/pthreadimpl.h b/cpukit/posix/include/rtems/posix/pthreadimpl.h
index a65f849..933aeac 100644
--- a/cpukit/posix/include/rtems/posix/pthreadimpl.h
+++ b/cpukit/posix/include/rtems/posix/pthreadimpl.h
@@ -48,7 +48,7 @@ POSIX_EXTERN Objects_Information  _POSIX_Threads_Information;
 /**
  * This variable contains the default POSIX Thread attributes.
  */
-extern const pthread_attr_t _POSIX_Threads_Default_attributes;
+extern pthread_attr_t _POSIX_Threads_Default_attributes;
 
 /**
  * When the user configures a set of POSIX API initialization threads,
@@ -77,6 +77,23 @@ void _POSIX_Threads_Manager_initialization(void);
 RTEMS_INLINE_ROUTINE Thread_Control *_POSIX_Threads_Allocate( void );
 
 /**
+ * @brief Copy POSIX Thread attribute structure
+ *
+ * This routine copies the attr2 thread attribute structure
+ * to the attr1 Thread Attribute structure.
+ *
+ * @param[out] attr1 is a pointer to the thread attribute
+ * structure to copy into.
+ *
+ * @param[out] attr2 is a pointer to the thread attribute
+ * structure to copy from.
+ */
+RTEMS_INLINE_ROUTINE void _POSIX_Threads_Copy_attributes( 
+  pthread_attr_t  *attr1,
+  const pthread_attr_t  *attr2
+);
+
+/**
  * @brief Free POSIX control block.
  *
  * This routine frees a pthread control block to the
@@ -110,6 +127,15 @@ RTEMS_INLINE_ROUTINE Thread_Control *_POSIX_Threads_Get(
 );
 
 /**
+ * @brief POSIX threads initialize user threads body.
+ *
+ * This routine initializes the thread attributes structure.
+ */
+RTEMS_INLINE_ROUTINE void _POSIX_Threads_Initialize_attributes( 
+  pthread_attr_t  *attr 
+);
+
+/**
  * @brief Check if a POSIX thread control block is NULL.
  *
  * This function returns @c TRUE if the_pthread is @c NULL and @c FALSE
@@ -177,6 +203,14 @@ int _POSIX_Thread_Translate_sched_param(
 );
 
 /*
+ * rtems_pthread_attribute_compare
+ */
+int rtems_pthread_attribute_compare(
+  const pthread_attr_t *attr1,
+  const pthread_attr_t *attr2
+);
+ 
+/*
  *  _POSIX_Threads_Allocate
  */
 
@@ -186,6 +220,21 @@ RTEMS_INLINE_ROUTINE Thread_Control *_POSIX_Threads_Allocate( void )
 }
 
 /*
+ * _POSIX_Threads_Copy_attributes
+ */
+
+RTEMS_INLINE_ROUTINE void _POSIX_Threads_Copy_attributes( 
+  pthread_attr_t        *attr1,
+  const pthread_attr_t  *attr2
+)
+{
+  *attr1 = *attr2;
+#if HAVE_SYS_CPUSET_H
+  attr1->affinityset = &attr1->affinitysetpreallocated;
+#endif
+} 
+
+/*
  *  _POSIX_Threads_Free
  */
 
@@ -210,6 +259,20 @@ RTEMS_INLINE_ROUTINE Thread_Control *_POSIX_Threads_Get (
 }
 
 /*
+ * _POSIX_Threads_Initialize_attributes
+ */
+
+RTEMS_INLINE_ROUTINE void _POSIX_Threads_Initialize_attributes( 
+  pthread_attr_t  *attr 
+)
+{
+  _POSIX_Threads_Copy_attributes(
+    attr,
+    &_POSIX_Threads_Default_attributes
+  );
+} 
+
+/*
  *  _POSIX_Threads_Is_null
  */
 
diff --git a/cpukit/posix/src/pthread.c b/cpukit/posix/src/pthread.c
index 5988a5e..b087026 100644
--- a/cpukit/posix/src/pthread.c
+++ b/cpukit/posix/src/pthread.c
@@ -17,10 +17,12 @@
 #if HAVE_CONFIG_H
 #include "config.h"
 #endif
+#include <stdio.h>
 
 #include <errno.h>
 #include <pthread.h>
 #include <limits.h>
+#include <assert.h>
 
 #include <rtems/system.h>
 #include <rtems/config.h>
@@ -46,13 +48,14 @@
  *  NOTE: Be careful .. if the default attribute set changes,
  *        _POSIX_Threads_Initialize_user_threads will need to be examined.
  */
-const pthread_attr_t _POSIX_Threads_Default_attributes = {
-  true,                       /* is_initialized */
-  NULL,                       /* stackaddr */
-  0,                          /* stacksize -- will be adjusted to minimum */
-  PTHREAD_SCOPE_PROCESS,      /* contentionscope */
-  PTHREAD_INHERIT_SCHED,      /* inheritsched */
-  SCHED_FIFO,                 /* schedpolicy */
+pthread_attr_t _POSIX_Threads_Default_attributes = {
+  .is_initialized  = true,                       /* is_initialized */
+  .stackaddr       = NULL,                       /* stackaddr */
+  .stacksize       = 0,                          /* stacksize -- will be adjusted to minimum */
+  .contentionscope = PTHREAD_SCOPE_PROCESS,      /* contentionscope */
+  .inheritsched    = PTHREAD_INHERIT_SCHED,      /* inheritsched */
+  .schedpolicy     = SCHED_FIFO,                 /* schedpolicy */
+  .schedparam      =
   {                           /* schedparam */
     2,                        /* sched_priority */
     #if defined(_POSIX_SPORADIC_SERVER) || \
@@ -63,13 +66,19 @@ const pthread_attr_t _POSIX_Threads_Default_attributes = {
       0                         /* sched_ss_max_repl */
     #endif
   },
+
   #if HAVE_DECL_PTHREAD_ATTR_SETGUARDSIZE
-    0,                        /* guardsize */
+    .guardsize = 0,                            /* guardsize */
   #endif
   #if defined(_POSIX_THREAD_CPUTIME)
-    1,                        /* cputime_clock_allowed */
+    .cputime_clock_allowed = 1,                        /* cputime_clock_allowed */
+  #endif
+  .detachstate             = PTHREAD_CREATE_JOINABLE,    /* detachstate */
+  #if HAVE_SYS_CPUSET_H
+    .affinitysetsize         = 0,
+    .affinityset             = NULL,
+    .affinitysetpreallocated = {{0x0}}
   #endif
-  PTHREAD_CREATE_JOINABLE,    /* detachstate */
 };
 
 /*
@@ -187,7 +196,7 @@ static bool _POSIX_Threads_Create_extension(
   created->API_Extensions[ THREAD_API_POSIX ] = api;
 
   /* XXX check all fields are touched */
-  api->Attributes  = _POSIX_Threads_Default_attributes;
+  _POSIX_Threads_Initialize_attributes( &api->Attributes );
   api->detachstate = _POSIX_Threads_Default_attributes.detachstate;
   api->schedpolicy = _POSIX_Threads_Default_attributes.schedpolicy;
   api->schedparam  = _POSIX_Threads_Default_attributes.schedparam;
@@ -346,6 +355,27 @@ User_extensions_Control _POSIX_Threads_User_extensions = {
  */
 void _POSIX_Threads_Manager_initialization(void)
 {
+  #if HAVE_SYS_CPUSET_H
+    pthread_attr_t *attr;
+    int i;
+    int max_cpus = 1;
+
+    /* Initialize default attribute. */
+    attr = &_POSIX_Threads_Default_attributes;
+
+    /* We do not support a cpu count over CPU_SETSIZE  */
+    max_cpus = _SMP_Get_processor_count();
+    assert( max_cpus <= CPU_SETSIZE );
+
+    /*  Initialize the affinity to be the set of all available CPU's   */
+    attr->affinityset             = &attr->affinitysetpreallocated;
+    attr->affinitysetsize         = sizeof( *attr->affinityset );
+    CPU_ZERO_S( attr->affinitysetsize, &attr->affinitysetpreallocated );
+
+    for (i=0; i<max_cpus; i++)  
+      CPU_SET_S(i, attr->affinitysetsize, attr->affinityset );
+  #endif
+
   _Objects_Initialize_information(
     &_POSIX_Threads_Information, /* object information table */
     OBJECTS_POSIX_API,           /* object API */
diff --git a/cpukit/posix/src/pthreadattrinit.c b/cpukit/posix/src/pthreadattrinit.c
index cf0ecab..3dc6644 100644
--- a/cpukit/posix/src/pthreadattrinit.c
+++ b/cpukit/posix/src/pthreadattrinit.c
@@ -34,6 +34,7 @@ int pthread_attr_init(
   if ( !attr )
     return EINVAL;
 
-  *attr = _POSIX_Threads_Default_attributes;
-   return 0;
+  _POSIX_Threads_Initialize_attributes( attr );
+
+  return 0;
 }
diff --git a/cpukit/posix/src/pthreadcreate.c b/cpukit/posix/src/pthreadcreate.c
index b780ecc..b148372 100644
--- a/cpukit/posix/src/pthreadcreate.c
+++ b/cpukit/posix/src/pthreadcreate.c
@@ -27,6 +27,7 @@
 #include <rtems/posix/priorityimpl.h>
 #include <rtems/posix/pthreadimpl.h>
 #include <rtems/posix/time.h>
+#include <rtems/score/cpusetimpl.h>
 #include <rtems/score/threadimpl.h>
 #include <rtems/score/apimutex.h>
 #include <rtems/score/stackimpl.h>
@@ -136,6 +137,14 @@ int pthread_create(
   if ( rc )
     return rc;
 
+#if defined(RTEMS_SMP)
+#if __RTEMS_HAVE_SYS_CPUSET_H__
+  rc = _CPU_set_Is_valid( attr->affinityset, attr->affinitysetsize );
+  if ( rc != 0 )
+    return EINVAL;
+#endif
+#endif
+
   /*
    *  Currently all POSIX threads are floating point if the hardware
    *  supports it.
@@ -179,19 +188,25 @@ int pthread_create(
     0,                    /* isr level */
     name                  /* posix threads don't have a name */
   );
-
   if ( !status ) {
     _POSIX_Threads_Free( the_thread );
     _RTEMS_Unlock_allocator();
     return EAGAIN;
   }
 
+#if defined(RTEMS_SMP)
+#if __RTEMS_HAVE_SYS_CPUSET_H__
+   the_thread->affinity.setsize   = attr->affinitysetsize;
+   *the_thread->affinity.set      = *attr->affinityset;
+#endif
+#endif
+
   /*
    *  finish initializing the per API structure
    */
   api = the_thread->API_Extensions[ THREAD_API_POSIX ];
 
-  api->Attributes  = *the_attr;
+  _POSIX_Threads_Copy_attributes( &api->Attributes, the_attr );
   api->detachstate = the_attr->detachstate;
   api->schedpolicy = schedpolicy;
   api->schedparam  = schedparam;
-- 
1.8.1.4

From 27809f5d7ba1aae02cd86619c03b9288174a2a80 Mon Sep 17 00:00:00 2001
From: Jennifer Averett <jennifer.aver...@oarcorp.com>
Date: Thu, 6 Feb 2014 12:56:34 -0600
Subject: [PATCH 08/15] posix: Add support method to compare two pthread
 attribute structures.

---
 cpukit/posix/Makefile.am              |  3 ++
 cpukit/posix/src/pthreadattrcompare.c | 92 +++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+)
 create mode 100644 cpukit/posix/src/pthreadattrcompare.c

diff --git a/cpukit/posix/Makefile.am b/cpukit/posix/Makefile.am
index 0e81abc..e3228b6 100644
--- a/cpukit/posix/Makefile.am
+++ b/cpukit/posix/Makefile.am
@@ -140,6 +140,9 @@ libposix_a_SOURCES += src/pthreadatfork.c src/pthreadattrdestroy.c \
     src/pthreadsetschedparam.c src/pthreadsigmask.c \
     src/psxpriorityisvalid.c src/psxtransschedparam.c
 
+## RTEMS specific support methods
+libposix_a_SOURCES += src/pthreadattrcompare.c 
+
 ## PSIGNAL_C_FILES
 libposix_a_SOURCES += src/psignal.c src/alarm.c src/kill.c src/killinfo.c \
     src/kill_r.c src/pause.c src/psignalclearprocesssignals.c \
diff --git a/cpukit/posix/src/pthreadattrcompare.c b/cpukit/posix/src/pthreadattrcompare.c
new file mode 100644
index 0000000..9bd1437
--- /dev/null
+++ b/cpukit/posix/src/pthreadattrcompare.c
@@ -0,0 +1,92 @@
+/**
+ * @file
+ *
+ * @brief RTEMS specific pthread attribute comparison
+ * @ingroup POSIX_PTHREADS Private Threads
+ */
+
+/*
+ *  COPYRIGHT (c) 1989-2014.
+ *  On-Line Applications Research Corporation (OAR).
+ *
+ *  The license and distribution terms for this file may be
+ *  found in the file LICENSE in this distribution or at
+ *  http://www.rtems.com/license/LICENSE.
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include <stdio.h>
+
+#include <errno.h>
+#include <pthread.h>
+#include <string.h>
+#include <rtems/posix/pthreadimpl.h>
+
+int rtems_pthread_attribute_compare(
+  const pthread_attr_t *attr1,
+  const pthread_attr_t *attr2
+)
+{
+  if ( attr1->is_initialized  !=  attr2->is_initialized )
+    return 1;
+
+  if ( attr1->stackaddr != attr2->stackaddr )
+    return 1;
+
+  if ( attr1->stacksize != attr2->stacksize )
+    return 1;
+
+  if ( attr1->contentionscope != attr2->contentionscope )
+    return 1;
+
+  if ( attr1->inheritsched != attr2->inheritsched )
+    return 1;
+
+  if ( attr1->schedpolicy != attr2->schedpolicy )
+    return 1;
+
+  if (memcmp(
+    &attr1->schedparam, 
+    &attr2->schedparam, 
+    sizeof(struct sched_param)
+  ))
+    return 1;
+
+  #if HAVE_DECL_PTHREAD_ATTR_SETGUARDSIZE
+    if ( attr1->guardsize != attr2->guardsize )
+      return 1;
+  #endif
+
+  #if defined(_POSIX_THREAD_CPUTIME)
+    if ( attr1->cputime_clock_allowed != attr2->cputime_clock_allowed )
+      return 1;
+  #endif
+
+  if ( attr1->detachstate != attr2->detachstate )
+    return 1;
+
+  #if HAVE_SYS_CPUSET_H
+    if ( attr1->affinitysetsize != attr2->affinitysetsize )
+      return 1;
+
+    if (!CPU_EQUAL_S( 
+      attr1->affinitysetsize, 
+      attr1->affinityset, 
+      attr2->affinityset
+    ))
+      return 1;
+
+    if (!CPU_EQUAL_S(
+      attr1->affinitysetsize, 
+      &attr1->affinitysetpreallocated, 
+      &attr2->affinitysetpreallocated
+    ))
+      return 1;
+  #endif
+
+  return 0;
+}
+
+
-- 
1.8.1.4

_______________________________________________
rtems-devel mailing list
rtems-devel@rtems.org
http://www.rtems.org/mailman/listinfo/rtems-devel

Reply via email to