[PATCH 7/9] score: Avoid direct usage of _Thread_Executing

Sebastian Huber sebastian.huber at embedded-brains.de
Wed Jul 17 14:18:58 UTC 2013


Pass the executing thread as a function parameter.  Obtain the executing
thread inside a thread dispatch critical section to avoid problems on
SMP.
---
 cpukit/libnetworking/rtems/rtems_glue.c          |    7 +++-
 cpukit/posix/src/mutexinit.c                     |    1 +
 cpukit/posix/src/mutexlocksupp.c                 |    5 ++-
 cpukit/rtems/src/semcreate.c                     |    1 +
 cpukit/rtems/src/semobtain.c                     |    7 +++-
 cpukit/score/include/rtems/score/coremuteximpl.h |   35 +++++++++++++---------
 cpukit/score/src/apimutexallocate.c              |    2 +-
 cpukit/score/src/apimutexlock.c                  |    1 +
 cpukit/score/src/coremutex.c                     |   15 +++++----
 cpukit/score/src/coremutexseize.c                |   13 ++++++--
 cpukit/score/src/coremutexseizeintr.c            |    7 ++++-
 11 files changed, 63 insertions(+), 31 deletions(-)

diff --git a/cpukit/libnetworking/rtems/rtems_glue.c b/cpukit/libnetworking/rtems/rtems_glue.c
index 3412051..05221bf 100644
--- a/cpukit/libnetworking/rtems/rtems_glue.c
+++ b/cpukit/libnetworking/rtems/rtems_glue.c
@@ -366,17 +366,20 @@ rtems_bsdnet_semaphore_obtain (void)
 {
 #ifdef RTEMS_FAST_MUTEX
 	ISR_Level level;
+	Thread_Control *executing;
 	_ISR_Disable (level);
+	executing = _Thread_Executing;
 	_CORE_mutex_Seize (
 		&the_networkSemaphore->Core_control.mutex,
+		executing,
 		networkSemaphore,
 		1,		/* wait */
 		0,		/* forever */
 		level
 		);
-	if (_Thread_Executing->Wait.return_code)
+	if (executing->Wait.return_code)
 		rtems_panic ("rtems-net: can't obtain network sema: %d\n",
-                 _Thread_Executing->Wait.return_code);
+                 executing->Wait.return_code);
 #else
 	rtems_status_code sc;
 
diff --git a/cpukit/posix/src/mutexinit.c b/cpukit/posix/src/mutexinit.c
index d4f188c..36844c8 100644
--- a/cpukit/posix/src/mutexinit.c
+++ b/cpukit/posix/src/mutexinit.c
@@ -179,6 +179,7 @@ int pthread_mutex_init(
    */
   _CORE_mutex_Initialize(
     &the_mutex->Mutex,
+    NULL,
     the_mutex_attr,
     CORE_MUTEX_UNLOCKED
   );
diff --git a/cpukit/posix/src/mutexlocksupp.c b/cpukit/posix/src/mutexlocksupp.c
index 8b6274b..3bcf924 100644
--- a/cpukit/posix/src/mutexlocksupp.c
+++ b/cpukit/posix/src/mutexlocksupp.c
@@ -47,13 +47,16 @@ int _POSIX_Mutex_Lock_support(
   register POSIX_Mutex_Control *the_mutex;
   Objects_Locations             location;
   ISR_Level                     level;
+  Thread_Control               *executing;
 
   the_mutex = _POSIX_Mutex_Get_interrupt_disable( mutex, &location, &level );
   switch ( location ) {
 
     case OBJECTS_LOCAL:
+      executing = _Thread_Executing;
       _CORE_mutex_Seize(
         &the_mutex->Mutex,
+        executing,
         the_mutex->Object.id,
         blocking,
         timeout,
@@ -61,7 +64,7 @@ int _POSIX_Mutex_Lock_support(
       );
       _Objects_Put_for_get_isr_disable( &the_mutex->Object );
       return _POSIX_Mutex_Translate_core_mutex_return_code(
-        (CORE_mutex_Status) _Thread_Executing->Wait.return_code
+        (CORE_mutex_Status) executing->Wait.return_code
       );
 
 #if defined(RTEMS_MULTIPROCESSING)
diff --git a/cpukit/rtems/src/semcreate.c b/cpukit/rtems/src/semcreate.c
index 0f5bca4..01d2450 100644
--- a/cpukit/rtems/src/semcreate.c
+++ b/cpukit/rtems/src/semcreate.c
@@ -182,6 +182,7 @@ rtems_status_code rtems_semaphore_create(
 
     mutex_status = _CORE_mutex_Initialize(
       &the_semaphore->Core_control.mutex,
+      _Thread_Executing,
       &the_mutex_attr,
       (count == 1) ? CORE_MUTEX_UNLOCKED : CORE_MUTEX_LOCKED
     );
diff --git a/cpukit/rtems/src/semobtain.c b/cpukit/rtems/src/semobtain.c
index 7197496..08ccb80 100644
--- a/cpukit/rtems/src/semobtain.c
+++ b/cpukit/rtems/src/semobtain.c
@@ -47,14 +47,17 @@ rtems_status_code rtems_semaphore_obtain(
   register Semaphore_Control     *the_semaphore;
   Objects_Locations               location;
   ISR_Level                       level;
+  Thread_Control                 *executing;
 
   the_semaphore = _Semaphore_Get_interrupt_disable( id, &location, &level );
   switch ( location ) {
 
     case OBJECTS_LOCAL:
+      executing = _Thread_Executing;
       if ( !_Attributes_Is_counting_semaphore(the_semaphore->attribute_set) ) {
         _CORE_mutex_Seize(
           &the_semaphore->Core_control.mutex,
+          executing,
           id,
           ((_Options_Is_no_wait( option_set )) ? false : true),
           timeout,
@@ -62,7 +65,7 @@ rtems_status_code rtems_semaphore_obtain(
         );
         _Objects_Put_for_get_isr_disable( &the_semaphore->Object );
         return _Semaphore_Translate_core_mutex_return_code(
-                  _Thread_Executing->Wait.return_code );
+                  executing->Wait.return_code );
       }
 
       /* must be a counting semaphore */
@@ -75,7 +78,7 @@ rtems_status_code rtems_semaphore_obtain(
       );
       _Objects_Put_for_get_isr_disable( &the_semaphore->Object );
       return _Semaphore_Translate_core_semaphore_return_code(
-                  _Thread_Executing->Wait.return_code );
+                  executing->Wait.return_code );
 
 #if defined(RTEMS_MULTIPROCESSING)
     case OBJECTS_REMOTE:
diff --git a/cpukit/score/include/rtems/score/coremuteximpl.h b/cpukit/score/include/rtems/score/coremuteximpl.h
index c35c0f3..67e7c99 100644
--- a/cpukit/score/include/rtems/score/coremuteximpl.h
+++ b/cpukit/score/include/rtems/score/coremuteximpl.h
@@ -110,7 +110,8 @@ typedef enum {
  *
  *  This routine initializes the mutex based on the parameters passed.
  *
- *  @param[in] the_mutex is the mutex to initalize
+ *  @param[in,out] the_mutex is the mutex to initalize
+ *  @param[in,out] executing The currently executing thread.
  *  @param[in] the_mutex_attributes is the attributes associated with this
  *         mutex instance
  *  @param[in] initial_lock is the initial value of the mutex
@@ -119,7 +120,8 @@ typedef enum {
  */
 CORE_mutex_Status _CORE_mutex_Initialize(
   CORE_mutex_Control           *the_mutex,
-  CORE_mutex_Attributes        *the_mutex_attributes,
+  Thread_Control               *executing,
+  const CORE_mutex_Attributes  *the_mutex_attributes,
   uint32_t                      initial_lock
 );
 
@@ -131,7 +133,8 @@ CORE_mutex_Status _CORE_mutex_Initialize(
  *  returns.  Otherwise, the calling task is blocked until a unit becomes
  *  available.
  *
- *  @param[in] the_mutex is the mutex to attempt to lock
+ *  @param[in,out] executing The currently executing thread.
+ *  @param[in,out] the_mutex is the mutex to attempt to lock
  *  @param[in] level is the interrupt level
  *
  *  @retval This routine returns 0 if "trylock" can resolve whether or not
@@ -145,6 +148,7 @@ CORE_mutex_Status _CORE_mutex_Initialize(
 
 RTEMS_INLINE_ROUTINE int _CORE_mutex_Seize_interrupt_trylock_body(
   CORE_mutex_Control  *the_mutex,
+  Thread_Control      *executing,
   ISR_Level            level
 );
 
@@ -162,6 +166,7 @@ RTEMS_INLINE_ROUTINE int _CORE_mutex_Seize_interrupt_trylock_body(
    */
   int _CORE_mutex_Seize_interrupt_trylock(
     CORE_mutex_Control  *the_mutex,
+    Thread_Control      *executing,
     ISR_Level            level
   );
 #else
@@ -172,8 +177,8 @@ RTEMS_INLINE_ROUTINE int _CORE_mutex_Seize_interrupt_trylock_body(
    *  @param[in] _mutex will attempt to lock
    *  @param[in] _level is the interrupt level
    */
-  #define _CORE_mutex_Seize_interrupt_trylock( _mutex, _level ) \
-     _CORE_mutex_Seize_interrupt_trylock_body( _mutex, _level )
+  #define _CORE_mutex_Seize_interrupt_trylock( _mutex, _executing, _level ) \
+     _CORE_mutex_Seize_interrupt_trylock_body( _mutex, _executing, _level )
 #endif
 
 /**
@@ -183,11 +188,13 @@ RTEMS_INLINE_ROUTINE int _CORE_mutex_Seize_interrupt_trylock_body(
  *  It is an actual subroutine and is not implemented as something
  *  that may be inlined.
  *
- *  @param[in] the_mutex is the mutex to attempt to lock
+ *  @param[in,out] the_mutex is the mutex to attempt to lock
+ *  @param[in,out] executing The currently executing thread.
  *  @param[in] timeout is the maximum number of ticks to block
  */
 void _CORE_mutex_Seize_interrupt_blocking(
   CORE_mutex_Control  *the_mutex,
+  Thread_Control      *executing,
   Watchdog_Interval    timeout
 );
 /**
@@ -237,6 +244,7 @@ void _CORE_mutex_Seize_interrupt_blocking(
  */
 RTEMS_INLINE_ROUTINE void _CORE_mutex_Seize_body(
   CORE_mutex_Control  *the_mutex,
+  Thread_Control      *executing,
   Objects_Id           id,
   bool                 wait,
   Watchdog_Interval    timeout,
@@ -250,7 +258,7 @@ RTEMS_INLINE_ROUTINE void _CORE_mutex_Seize_body(
       INTERNAL_ERROR_MUTEX_OBTAIN_FROM_BAD_STATE
     );
   }
-  if ( _CORE_mutex_Seize_interrupt_trylock( the_mutex, level ) ) {
+  if ( _CORE_mutex_Seize_interrupt_trylock( the_mutex, executing, level ) ) {
     if ( !wait ) {
       _ISR_Enable( level );
       _Thread_Executing->Wait.return_code =
@@ -261,7 +269,7 @@ RTEMS_INLINE_ROUTINE void _CORE_mutex_Seize_body(
       _Thread_Executing->Wait.id = id;
       _Thread_Disable_dispatch();
       _ISR_Enable( level );
-      _CORE_mutex_Seize_interrupt_blocking( the_mutex, timeout );
+      _CORE_mutex_Seize_interrupt_blocking( the_mutex, executing, timeout );
     }
   }
 }
@@ -279,14 +287,15 @@ RTEMS_INLINE_ROUTINE void _CORE_mutex_Seize_body(
 #if defined(__RTEMS_DO_NOT_INLINE_CORE_MUTEX_SEIZE__)
   void _CORE_mutex_Seize(
     CORE_mutex_Control  *_the_mutex,
+    Thread_Control      *_executing,
     Objects_Id           _id,
     bool                 _wait,
     Watchdog_Interval    _timeout,
     ISR_Level            _level
   );
 #else
-  #define _CORE_mutex_Seize( _the_mutex, _id, _wait, _timeout, _level ) \
-     _CORE_mutex_Seize_body( _the_mutex, _id, _wait, _timeout, _level )
+  #define _CORE_mutex_Seize( _executing, _mtx, _id, _wait, _timeout, _level ) \
+     _CORE_mutex_Seize_body( _executing, _mtx, _id, _wait, _timeout, _level )
 #endif
 
 /**
@@ -357,7 +366,7 @@ RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_locked(
  * @retval false The mutex is not using FIFO blocking order.
  */
 RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_fifo(
-  CORE_mutex_Attributes *the_attribute
+  const CORE_mutex_Attributes *the_attribute
 )
 {
   return the_attribute->discipline == CORE_MUTEX_DISCIPLINES_FIFO;
@@ -429,14 +438,12 @@ RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_priority_ceiling(
 
 RTEMS_INLINE_ROUTINE int _CORE_mutex_Seize_interrupt_trylock_body(
   CORE_mutex_Control  *the_mutex,
+  Thread_Control      *executing,
   ISR_Level            level
 )
 {
-  Thread_Control   *executing;
-
   /* disabled when you get here */
 
-  executing = _Thread_Executing;
   executing->Wait.return_code = CORE_MUTEX_STATUS_SUCCESSFUL;
   if ( !_CORE_mutex_Is_locked( the_mutex ) ) {
     the_mutex->lock       = CORE_MUTEX_LOCKED;
diff --git a/cpukit/score/src/apimutexallocate.c b/cpukit/score/src/apimutexallocate.c
index a736e2b..c57cc66 100644
--- a/cpukit/score/src/apimutexallocate.c
+++ b/cpukit/score/src/apimutexallocate.c
@@ -37,7 +37,7 @@ void _API_Mutex_Allocate(
 
   mutex = (API_Mutex_Control *) _Objects_Allocate( &_API_Mutex_Information );
 
-  _CORE_mutex_Initialize( &mutex->Mutex, &attr, CORE_MUTEX_UNLOCKED );
+  _CORE_mutex_Initialize( &mutex->Mutex, NULL, &attr, CORE_MUTEX_UNLOCKED );
 
   _Objects_Open_u32( &_API_Mutex_Information, &mutex->Object, 1 );
 
diff --git a/cpukit/score/src/apimutexlock.c b/cpukit/score/src/apimutexlock.c
index c5a2e42..b6d957f 100644
--- a/cpukit/score/src/apimutexlock.c
+++ b/cpukit/score/src/apimutexlock.c
@@ -37,6 +37,7 @@ void _API_Mutex_Lock(
 
   _CORE_mutex_Seize(
     &the_mutex->Mutex,
+    _Thread_Executing,
     the_mutex->Object.id,
     true,
     0,
diff --git a/cpukit/score/src/coremutex.c b/cpukit/score/src/coremutex.c
index a39b4ef..8ea7d13 100644
--- a/cpukit/score/src/coremutex.c
+++ b/cpukit/score/src/coremutex.c
@@ -27,7 +27,8 @@
 
 CORE_mutex_Status _CORE_mutex_Initialize(
   CORE_mutex_Control           *the_mutex,
-  CORE_mutex_Attributes        *the_mutex_attributes,
+  Thread_Control               *executing,
+  const CORE_mutex_Attributes  *the_mutex_attributes,
   uint32_t                      initial_lock
 )
 {
@@ -43,21 +44,21 @@ CORE_mutex_Status _CORE_mutex_Initialize(
 
   if ( initial_lock == CORE_MUTEX_LOCKED ) {
     the_mutex->nest_count = 1;
-    the_mutex->holder     = _Thread_Executing;
-    the_mutex->holder_id  = _Thread_Executing->Object.id;
+    the_mutex->holder     = executing;
+    the_mutex->holder_id  = executing->Object.id;
     if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ||
          _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) {
 
-      if ( _Thread_Executing->current_priority <
+      if ( executing->current_priority <
              the_mutex->Attributes.priority_ceiling )
        return CORE_MUTEX_STATUS_CEILING_VIOLATED;
 #ifdef __RTEMS_STRICT_ORDER_MUTEX__
-       _Chain_Prepend_unprotected( &_Thread_Executing->lock_mutex,
+       _Chain_Prepend_unprotected( &executing->lock_mutex,
                                    &the_mutex->queue.lock_queue );
-       the_mutex->queue.priority_before = _Thread_Executing->current_priority;
+       the_mutex->queue.priority_before = executing->current_priority;
 #endif
 
-      _Thread_Executing->resource_count++;
+      executing->resource_count++;
     }
   } else {
     the_mutex->nest_count = 0;
diff --git a/cpukit/score/src/coremutexseize.c b/cpukit/score/src/coremutexseize.c
index 098d6f2..3da513a 100644
--- a/cpukit/score/src/coremutexseize.c
+++ b/cpukit/score/src/coremutexseize.c
@@ -28,24 +28,31 @@
 #if defined(__RTEMS_DO_NOT_INLINE_CORE_MUTEX_SEIZE__)
 void _CORE_mutex_Seize(
   CORE_mutex_Control  *_the_mutex,
+  Thread_Control      *_executing,
   Objects_Id           _id,
   bool                 _wait,
   Watchdog_Interval    _timeout,
   ISR_Level            _level
 )
 {
-  _CORE_mutex_Seize_body( _the_mutex, _id, _wait, _timeout, _level );
+  _CORE_mutex_Seize_body(
+    _the_mutex,
+    _executing,
+    _id,
+    _wait,
+    _timeout,
+    _level
+  );
 }
 #endif
 
 void _CORE_mutex_Seize_interrupt_blocking(
   CORE_mutex_Control  *the_mutex,
+  Thread_Control      *executing,
   Watchdog_Interval    timeout
 )
 {
-  Thread_Control   *executing;
 
-  executing = _Thread_Executing;
   if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ) {
     if ( _Scheduler_Is_priority_higher_than(
          executing->current_priority,
diff --git a/cpukit/score/src/coremutexseizeintr.c b/cpukit/score/src/coremutexseizeintr.c
index f31d2fb..cc726b6 100644
--- a/cpukit/score/src/coremutexseizeintr.c
+++ b/cpukit/score/src/coremutexseizeintr.c
@@ -28,9 +28,14 @@
 #if defined(__RTEMS_DO_NOT_INLINE_CORE_MUTEX_SEIZE__)
 int _CORE_mutex_Seize_interrupt_trylock(
   CORE_mutex_Control  *the_mutex,
+  Thread_Control      *executing,
   ISR_Level            level
 )
 {
-  return _CORE_mutex_Seize_interrupt_trylock_body( the_mutex, level );
+  return _CORE_mutex_Seize_interrupt_trylock_body(
+    the_mutex,
+    executing,
+    level
+  );
 }
 #endif
-- 
1.7.7




More information about the devel mailing list