diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2018-08-29 09:43:44 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2018-09-10 10:38:45 +0200 |
commit | 709796209c88e6749320b3096df57f369c2d62be (patch) | |
tree | 072e7cd5cef37aad7404a02344724a4348602f35 /cpukit | |
parent | score: Modify _Scheduler_Unblock() (diff) | |
download | rtems-709796209c88e6749320b3096df57f369c2d62be.tar.bz2 |
score: Add thread pin/unpin support
Add support to temporarily pin a thread to its current processor. This
may be used to access per-processor data structures in critical sections
with enabled thread dispatching, e.g. a pinned thread is allowed to
block.
Update #3508.
Diffstat (limited to 'cpukit')
-rw-r--r-- | cpukit/include/rtems/score/scheduler.h | 48 | ||||
-rw-r--r-- | cpukit/include/rtems/score/scheduleredfsmp.h | 33 | ||||
-rw-r--r-- | cpukit/include/rtems/score/schedulerimpl.h | 20 | ||||
-rw-r--r-- | cpukit/include/rtems/score/schedulerpriorityaffinitysmp.h | 2 | ||||
-rw-r--r-- | cpukit/include/rtems/score/schedulerprioritysmp.h | 4 | ||||
-rw-r--r-- | cpukit/include/rtems/score/schedulersimplesmp.h | 4 | ||||
-rw-r--r-- | cpukit/include/rtems/score/schedulersmpimpl.h | 9 | ||||
-rw-r--r-- | cpukit/include/rtems/score/schedulerstrongapa.h | 4 | ||||
-rw-r--r-- | cpukit/include/rtems/score/smpimpl.h | 3 | ||||
-rw-r--r-- | cpukit/include/rtems/score/thread.h | 43 | ||||
-rw-r--r-- | cpukit/include/rtems/score/threadimpl.h | 52 | ||||
-rw-r--r-- | cpukit/rtems/src/scheduleraddprocessor.c | 2 | ||||
-rw-r--r-- | cpukit/score/Makefile.am | 2 | ||||
-rw-r--r-- | cpukit/score/src/schedulerdefaultpinunpin.c | 35 | ||||
-rw-r--r-- | cpukit/score/src/scheduleredfsmp.c | 96 | ||||
-rw-r--r-- | cpukit/score/src/threaddispatch.c | 88 | ||||
-rw-r--r-- | cpukit/score/src/threadinitialize.c | 2 | ||||
-rw-r--r-- | cpukit/score/src/threadunpin.c | 72 |
18 files changed, 466 insertions, 53 deletions
diff --git a/cpukit/include/rtems/score/scheduler.h b/cpukit/include/rtems/score/scheduler.h index a6066c8e4a..fe6315daef 100644 --- a/cpukit/include/rtems/score/scheduler.h +++ b/cpukit/include/rtems/score/scheduler.h @@ -137,6 +137,36 @@ typedef struct { ); /** + * @brief Pin thread operation. + * + * @param[in] scheduler The scheduler instance of the specified processor. + * @param[in] the_thread The thread to pin. + * @param[in] node The scheduler node of the thread. + * @param[in] cpu The processor to pin the thread. + */ + void ( *pin )( + const Scheduler_Control *scheduler, + Thread_Control *the_thread, + Scheduler_Node *node, + struct Per_CPU_Control *cpu + ); + + /** + * @brief Unpin thread operation. + * + * @param[in] scheduler The scheduler instance of the specified processor. + * @param[in] the_thread The thread to unpin. + * @param[in] node The scheduler node of the thread. + * @param[in] cpu The processor to unpin the thread. + */ + void ( *unpin )( + const Scheduler_Control *scheduler, + Thread_Control *the_thread, + Scheduler_Node *node, + struct Per_CPU_Control *cpu + ); + + /** * @brief Add processor operation. * * @param[in] scheduler The scheduler instance to add the processor. @@ -405,10 +435,28 @@ Priority_Control _Scheduler_default_Unmap_priority( Thread_Scheduler_state next_state ); + /** + * @brief Does nothing in a single processor system, otherwise a fatal error + * is issued. + * + * @param[in] scheduler Unused. + * @param[in] the_thread Unused. + * @param[in] node Unused. + * @param[in] cpu Unused. + */ + void _Scheduler_default_Pin_or_unpin( + const Scheduler_Control *scheduler, + Thread_Control *the_thread, + Scheduler_Node *node, + struct Per_CPU_Control *cpu + ); + #define SCHEDULER_OPERATION_DEFAULT_ASK_FOR_HELP \ _Scheduler_default_Ask_for_help, \ _Scheduler_default_Reconsider_help_request, \ _Scheduler_default_Withdraw_node, \ + _Scheduler_default_Pin_or_unpin, \ + _Scheduler_default_Pin_or_unpin, \ NULL, \ NULL, #else diff --git a/cpukit/include/rtems/score/scheduleredfsmp.h b/cpukit/include/rtems/score/scheduleredfsmp.h index 018568190e..72c906eefe 100644 --- a/cpukit/include/rtems/score/scheduleredfsmp.h +++ b/cpukit/include/rtems/score/scheduleredfsmp.h @@ -7,7 +7,7 @@ */ /* - * Copyright (c) 2017 embedded brains GmbH. + * Copyright (c) 2017, 2018 embedded brains GmbH. * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at @@ -43,13 +43,24 @@ typedef struct { int64_t generation; /** - * @brief The ready queue index depending on the processor affinity of the thread. + * @brief The ready queue index depending on the processor affinity and + * pinning of the thread. * * The ready queue index zero is used for threads with a one-to-all thread * processor affinity. Threads with a one-to-one processor affinity use the * processor index plus one as the ready queue index. */ - uint32_t ready_queue_index; + uint8_t ready_queue_index; + + /** + * @brief Ready queue index according to thread affinity. + */ + uint8_t affinity_ready_queue_index; + + /** + * @brief Ready queue index according to thread pinning. + */ + uint8_t pinning_ready_queue_index; } Scheduler_EDF_SMP_Node; typedef struct { @@ -105,6 +116,8 @@ typedef struct { _Scheduler_EDF_SMP_Ask_for_help, \ _Scheduler_EDF_SMP_Reconsider_help_request, \ _Scheduler_EDF_SMP_Withdraw_node, \ + _Scheduler_EDF_SMP_Pin, \ + _Scheduler_EDF_SMP_Unpin, \ _Scheduler_EDF_SMP_Add_processor, \ _Scheduler_EDF_SMP_Remove_processor, \ _Scheduler_EDF_SMP_Node_initialize, \ @@ -162,6 +175,20 @@ void _Scheduler_EDF_SMP_Withdraw_node( Thread_Scheduler_state next_state ); +void _Scheduler_EDF_SMP_Pin( + const Scheduler_Control *scheduler, + Thread_Control *the_thread, + Scheduler_Node *node, + struct Per_CPU_Control *cpu +); + +void _Scheduler_EDF_SMP_Unpin( + const Scheduler_Control *scheduler, + Thread_Control *the_thread, + Scheduler_Node *node, + struct Per_CPU_Control *cpu +); + void _Scheduler_EDF_SMP_Add_processor( const Scheduler_Control *scheduler, Thread_Control *idle diff --git a/cpukit/include/rtems/score/schedulerimpl.h b/cpukit/include/rtems/score/schedulerimpl.h index 2fe30b3d6b..fda043ea5a 100644 --- a/cpukit/include/rtems/score/schedulerimpl.h +++ b/cpukit/include/rtems/score/schedulerimpl.h @@ -1095,17 +1095,13 @@ RTEMS_INLINE_ROUTINE Status_Control _Scheduler_Set( &the_thread->Real_priority ); - if ( !_Priority_Is_empty( &old_scheduler_node->Wait.Priority ) ) { - _Priority_Plain_insert( - &old_scheduler_node->Wait.Priority, - &the_thread->Real_priority, - the_thread->Real_priority.priority - ); - return STATUS_RESOURCE_IN_USE; - } - + if ( + !_Priority_Is_empty( &old_scheduler_node->Wait.Priority ) #if defined(RTEMS_SMP) - if ( !_Chain_Has_only_one_node( &the_thread->Scheduler.Wait_nodes ) ) { + || !_Chain_Has_only_one_node( &the_thread->Scheduler.Wait_nodes ) + || the_thread->Scheduler.pin_level != 0 +#endif + ) { _Priority_Plain_insert( &old_scheduler_node->Wait.Priority, &the_thread->Real_priority, @@ -1114,6 +1110,7 @@ RTEMS_INLINE_ROUTINE Status_Control _Scheduler_Set( return STATUS_RESOURCE_IN_USE; } +#if defined(RTEMS_SMP) old_scheduler = _Thread_Scheduler_get_home( the_thread ); new_scheduler_node = _Thread_Scheduler_get_node_by_index( the_thread, @@ -1140,7 +1137,8 @@ RTEMS_INLINE_ROUTINE Status_Control _Scheduler_Set( return STATUS_UNSATISFIED; } - the_thread->Scheduler.home = new_scheduler; + _Assert( the_thread->Scheduler.pinned_scheduler == NULL ); + the_thread->Scheduler.home_scheduler = new_scheduler; _Scheduler_Release_critical( new_scheduler, &lock_context ); diff --git a/cpukit/include/rtems/score/schedulerpriorityaffinitysmp.h b/cpukit/include/rtems/score/schedulerpriorityaffinitysmp.h index d988d5752a..e8ee528c90 100644 --- a/cpukit/include/rtems/score/schedulerpriorityaffinitysmp.h +++ b/cpukit/include/rtems/score/schedulerpriorityaffinitysmp.h @@ -61,6 +61,8 @@ extern "C" { _Scheduler_priority_affinity_SMP_Ask_for_help, \ _Scheduler_priority_affinity_SMP_Reconsider_help_request, \ _Scheduler_priority_affinity_SMP_Withdraw_node, \ + _Scheduler_default_Pin_or_unpin, \ + _Scheduler_default_Pin_or_unpin, \ _Scheduler_priority_affinity_SMP_Add_processor, \ _Scheduler_priority_affinity_SMP_Remove_processor, \ _Scheduler_priority_affinity_SMP_Node_initialize, \ diff --git a/cpukit/include/rtems/score/schedulerprioritysmp.h b/cpukit/include/rtems/score/schedulerprioritysmp.h index 6671da5b7a..7d579f1846 100644 --- a/cpukit/include/rtems/score/schedulerprioritysmp.h +++ b/cpukit/include/rtems/score/schedulerprioritysmp.h @@ -7,7 +7,7 @@ */ /* - * Copyright (c) 2013, 2016 embedded brains GmbH. All rights reserved. + * Copyright (c) 2013, 2018 embedded brains GmbH. All rights reserved. * * embedded brains GmbH * Dornierstr. 4 @@ -89,6 +89,8 @@ typedef struct { _Scheduler_priority_SMP_Ask_for_help, \ _Scheduler_priority_SMP_Reconsider_help_request, \ _Scheduler_priority_SMP_Withdraw_node, \ + _Scheduler_default_Pin_or_unpin, \ + _Scheduler_default_Pin_or_unpin, \ _Scheduler_priority_SMP_Add_processor, \ _Scheduler_priority_SMP_Remove_processor, \ _Scheduler_priority_SMP_Node_initialize, \ diff --git a/cpukit/include/rtems/score/schedulersimplesmp.h b/cpukit/include/rtems/score/schedulersimplesmp.h index bc75b205d5..d9b4c1d46c 100644 --- a/cpukit/include/rtems/score/schedulersimplesmp.h +++ b/cpukit/include/rtems/score/schedulersimplesmp.h @@ -9,7 +9,7 @@ /* * Copyright (C) 2011 On-Line Applications Research Corporation (OAR). * - * Copyright (c) 2013, 2016 embedded brains GmbH. + * Copyright (c) 2013, 2018 embedded brains GmbH. * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at @@ -72,6 +72,8 @@ typedef struct { _Scheduler_simple_SMP_Ask_for_help, \ _Scheduler_simple_SMP_Reconsider_help_request, \ _Scheduler_simple_SMP_Withdraw_node, \ + _Scheduler_default_Pin_or_unpin, \ + _Scheduler_default_Pin_or_unpin, \ _Scheduler_simple_SMP_Add_processor, \ _Scheduler_simple_SMP_Remove_processor, \ _Scheduler_simple_SMP_Node_initialize, \ diff --git a/cpukit/include/rtems/score/schedulersmpimpl.h b/cpukit/include/rtems/score/schedulersmpimpl.h index 09c95662b6..0abe82b1f0 100644 --- a/cpukit/include/rtems/score/schedulersmpimpl.h +++ b/cpukit/include/rtems/score/schedulersmpimpl.h @@ -1185,6 +1185,14 @@ static inline bool _Scheduler_SMP_Ask_for_help( ISR_lock_Context lock_context; bool success; + if ( thread->Scheduler.pinned_scheduler != NULL ) { + /* + * Pinned threads are not allowed to ask for help. Return success to break + * the loop in _Thread_Ask_for_help() early. + */ + return true; + } + lowest_scheduled = ( *get_lowest_scheduled )( context, node ); _Thread_Scheduler_acquire_critical( thread, &lock_context ); @@ -1474,6 +1482,7 @@ static inline void _Scheduler_SMP_Set_affinity( ( *set_affinity )( context, node, arg ); ( *enqueue )( context, node, insert_priority ); } else { + _Assert( node_state == SCHEDULER_SMP_NODE_BLOCKED ); ( *set_affinity )( context, node, arg ); } } diff --git a/cpukit/include/rtems/score/schedulerstrongapa.h b/cpukit/include/rtems/score/schedulerstrongapa.h index d961f20c68..bc113197e7 100644 --- a/cpukit/include/rtems/score/schedulerstrongapa.h +++ b/cpukit/include/rtems/score/schedulerstrongapa.h @@ -7,7 +7,7 @@ */ /* - * Copyright (c) 2013, 2016 embedded brains GmbH. All rights reserved. + * Copyright (c) 2013, 2018 embedded brains GmbH. All rights reserved. * * embedded brains GmbH * Dornierstr. 4 @@ -89,6 +89,8 @@ typedef struct { _Scheduler_strong_APA_Ask_for_help, \ _Scheduler_strong_APA_Reconsider_help_request, \ _Scheduler_strong_APA_Withdraw_node, \ + _Scheduler_default_Pin_or_unpin, \ + _Scheduler_default_Pin_or_unpin, \ _Scheduler_strong_APA_Add_processor, \ _Scheduler_strong_APA_Remove_processor, \ _Scheduler_strong_APA_Node_initialize, \ diff --git a/cpukit/include/rtems/score/smpimpl.h b/cpukit/include/rtems/score/smpimpl.h index 75d36ac865..532e5a6b4e 100644 --- a/cpukit/include/rtems/score/smpimpl.h +++ b/cpukit/include/rtems/score/smpimpl.h @@ -79,7 +79,8 @@ typedef enum { SMP_FATAL_MULTITASKING_START_ON_UNASSIGNED_PROCESSOR, SMP_FATAL_SHUTDOWN, SMP_FATAL_SHUTDOWN_RESPONSE, - SMP_FATAL_START_OF_MANDATORY_PROCESSOR_FAILED + SMP_FATAL_START_OF_MANDATORY_PROCESSOR_FAILED, + SMP_FATAL_SCHEDULER_PIN_OR_UNPIN_NOT_SUPPORTED } SMP_Fatal_code; static inline void _SMP_Fatal( SMP_Fatal_code code ) diff --git a/cpukit/include/rtems/score/thread.h b/cpukit/include/rtems/score/thread.h index 7e0e2722dd..4dd1023045 100644 --- a/cpukit/include/rtems/score/thread.h +++ b/cpukit/include/rtems/score/thread.h @@ -259,9 +259,14 @@ typedef struct { Thread_Scheduler_state state; /** - * @brief The home scheduler control of this thread. + * @brief The home scheduler of this thread. */ - const struct _Scheduler_Control *home; + const struct _Scheduler_Control *home_scheduler; + + /** + * @brief The pinned scheduler of this thread. + */ + const struct _Scheduler_Control *pinned_scheduler; /** * @brief The processor assigned by the current scheduler. @@ -270,12 +275,12 @@ typedef struct { /** * @brief Scheduler nodes immediately available to the thread by its home - * scheduler instance and due to thread queue ownerships. + * scheduler and due to thread queue ownerships. * * This chain is protected by the thread wait lock. * * This chain is never empty. The first scheduler node on the chain is the - * scheduler node of the home scheduler instance. + * scheduler node of the home scheduler. */ Chain_Control Wait_nodes; @@ -285,8 +290,12 @@ typedef struct { * * This chain is protected by the thread state lock. * - * This chain is never empty. The first scheduler node on the chain is the - * scheduler node of the home scheduler instance. + * This chain is never empty for normal threads (the only exception are idle + * threads associated with an online processor which is not used by a + * scheduler). In case a pinned scheduler is set for this thread, then the + * first scheduler node of this chain belongs to the pinned scheduler, + * otherwise the first scheduler node of this chain belongs to the home + * scheduler. */ Chain_Control Scheduler_nodes; @@ -313,6 +322,28 @@ typedef struct { Scheduler_Node *requests; /** + * @brief The thread pinning to current processor level. + * + * Must be touched only by the executing thread with thread dispatching + * disabled. If non-zero, then the thread is pinned to its current + * processor. The pin level is incremented and decremented by two. The + * least-significant bit indicates that the thread was pre-empted and must + * undo the pinning with respect to the scheduler once the level changes from + * three to one. + * + * The thread pinning may be used to access per-processor data structures in + * critical sections with enabled thread dispatching, e.g. a pinned thread is + * allowed to block. + * + * Thread pinning should be used only for short critical sections and not all + * the time. Thread pinning is a very low overhead operation in case the + * thread is not preempted during the pinning. + * + * @see _Thread_Pin() and _Thread_Unpin(). + */ + int pin_level; + + /** * @brief The thread processor affinity set. */ Processor_mask Affinity; diff --git a/cpukit/include/rtems/score/threadimpl.h b/cpukit/include/rtems/score/threadimpl.h index 4ab855d3ff..530035ff67 100644 --- a/cpukit/include/rtems/score/threadimpl.h +++ b/cpukit/include/rtems/score/threadimpl.h @@ -1025,7 +1025,7 @@ RTEMS_INLINE_ROUTINE const Scheduler_Control *_Thread_Scheduler_get_home( ) { #if defined(RTEMS_SMP) - return the_thread->Scheduler.home; + return the_thread->Scheduler.home_scheduler; #else (void) the_thread; return &_Scheduler_Table[ 0 ]; @@ -1953,6 +1953,56 @@ size_t _Thread_Get_name( size_t buffer_size ); +#if defined(RTEMS_SMP) +#define THREAD_PIN_STEP 2 + +#define THREAD_PIN_PREEMPTION 1 + +void _Thread_Do_unpin( + Thread_Control *executing, + Per_CPU_Control *cpu_self +); +#endif + +RTEMS_INLINE_ROUTINE void _Thread_Pin( Thread_Control *executing ) +{ +#if defined(RTEMS_SMP) + _Assert( executing == _Thread_Executing ); + + executing->Scheduler.pin_level += THREAD_PIN_STEP; +#else + (void) executing; +#endif +} + +RTEMS_INLINE_ROUTINE void _Thread_Unpin( + Thread_Control *executing, + Per_CPU_Control *cpu_self +) +{ +#if defined(RTEMS_SMP) + unsigned int pin_level; + + _Assert( executing == _Thread_Executing ); + + pin_level = executing->Scheduler.pin_level; + _Assert( pin_level > 0 ); + + if ( + RTEMS_PREDICT_TRUE( + pin_level != ( THREAD_PIN_STEP | THREAD_PIN_PREEMPTION ) + ) + ) { + executing->Scheduler.pin_level = pin_level - THREAD_PIN_STEP; + } else { + _Thread_Do_unpin( executing, cpu_self ); + } +#else + (void) executing; + (void) cpu_self; +#endif +} + /** @}*/ #ifdef __cplusplus diff --git a/cpukit/rtems/src/scheduleraddprocessor.c b/cpukit/rtems/src/scheduleraddprocessor.c index 56d6811afd..243fb6c246 100644 --- a/cpukit/rtems/src/scheduleraddprocessor.c +++ b/cpukit/rtems/src/scheduleraddprocessor.c @@ -73,7 +73,7 @@ rtems_status_code rtems_scheduler_add_processor( _Assert( idle != NULL ); cpu->Scheduler.idle_if_online_and_unused = NULL; - idle->Scheduler.home = scheduler; + idle->Scheduler.home_scheduler = scheduler; idle->Start.initial_priority = idle_priority; scheduler_node = _Thread_Scheduler_get_node_by_index( idle, scheduler_index ); diff --git a/cpukit/score/Makefile.am b/cpukit/score/Makefile.am index e345f77daa..cdd9740d41 100644 --- a/cpukit/score/Makefile.am +++ b/cpukit/score/Makefile.am @@ -26,6 +26,7 @@ endif if HAS_SMP libscore_a_SOURCES += src/percpustatewait.c libscore_a_SOURCES += src/profilingsmplock.c +libscore_a_SOURCES += src/schedulerdefaultpinunpin.c libscore_a_SOURCES += src/scheduleredfsmp.c libscore_a_SOURCES += src/schedulerpriorityaffinitysmp.c libscore_a_SOURCES += src/schedulerprioritysmp.c @@ -38,6 +39,7 @@ libscore_a_SOURCES += src/schedulerdefaultaskforhelp.c libscore_a_SOURCES += src/schedulerdefaultsetaffinity.c libscore_a_SOURCES += src/schedulersmp.c libscore_a_SOURCES += src/schedulersmpstartidle.c +libscore_a_SOURCES += src/threadunpin.c endif ## CORE_APIMUTEX_C_FILES diff --git a/cpukit/score/src/schedulerdefaultpinunpin.c b/cpukit/score/src/schedulerdefaultpinunpin.c new file mode 100644 index 0000000000..5fc9cca026 --- /dev/null +++ b/cpukit/score/src/schedulerdefaultpinunpin.c @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2018 embedded brains GmbH + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.org/license/LICENSE. + */ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include <rtems/score/scheduler.h> +#include <rtems/score/interr.h> +#include <rtems/score/smpimpl.h> + +void _Scheduler_default_Pin_or_unpin( + const Scheduler_Control *scheduler, + Thread_Control *the_thread, + Scheduler_Node *node, + struct Per_CPU_Control *cpu +) +{ + (void) scheduler; + (void) the_thread; + (void) node; + (void) cpu; + + if ( _SMP_Get_processor_count() > 1 ) { + _Terminate( + RTEMS_FATAL_SOURCE_SMP, + SMP_FATAL_SCHEDULER_PIN_OR_UNPIN_NOT_SUPPORTED + ); + } +} diff --git a/cpukit/score/src/scheduleredfsmp.c b/cpukit/score/src/scheduleredfsmp.c index 874ec3e58b..62ddbdafa0 100644 --- a/cpukit/score/src/scheduleredfsmp.c +++ b/cpukit/score/src/scheduleredfsmp.c @@ -143,7 +143,7 @@ static inline Scheduler_Node *_Scheduler_EDF_SMP_Get_highest_ready( Scheduler_EDF_SMP_Context *self; Scheduler_EDF_SMP_Node *highest_ready; Scheduler_EDF_SMP_Node *node; - uint32_t rqi; + uint8_t rqi; const Chain_Node *tail; Chain_Node *next; @@ -199,7 +199,7 @@ static inline void _Scheduler_EDF_SMP_Set_scheduled( static inline Scheduler_EDF_SMP_Node *_Scheduler_EDF_SMP_Get_scheduled( const Scheduler_EDF_SMP_Context *self, - uint32_t rqi + uint8_t rqi ) { return self->Ready[ rqi ].scheduled; @@ -211,7 +211,7 @@ static inline Scheduler_Node *_Scheduler_EDF_SMP_Get_lowest_scheduled( ) { Scheduler_EDF_SMP_Node *filter; - uint32_t rqi; + uint8_t rqi; filter = _Scheduler_EDF_SMP_Node_downcast( filter_base ); rqi = filter->ready_queue_index; @@ -240,7 +240,7 @@ static inline void _Scheduler_EDF_SMP_Insert_ready( { Scheduler_EDF_SMP_Context *self; Scheduler_EDF_SMP_Node *node; - uint32_t rqi; + uint8_t rqi; Scheduler_EDF_SMP_Ready_queue *ready_queue; int generation_index; int increment; @@ -306,7 +306,7 @@ static inline void _Scheduler_EDF_SMP_Extract_from_ready( { Scheduler_EDF_SMP_Context *self; Scheduler_EDF_SMP_Node *node; - uint32_t rqi; + uint8_t rqi; Scheduler_EDF_SMP_Ready_queue *ready_queue; self = _Scheduler_EDF_SMP_Get_self( context ); @@ -369,7 +369,7 @@ static inline void _Scheduler_EDF_SMP_Allocate_processor( { Scheduler_EDF_SMP_Context *self; Scheduler_EDF_SMP_Node *scheduled; - uint32_t rqi; + uint8_t rqi; (void) victim_base; self = _Scheduler_EDF_SMP_Get_self( context ); @@ -647,7 +647,7 @@ static inline void _Scheduler_EDF_SMP_Do_set_affinity( ) { Scheduler_EDF_SMP_Node *node; - const uint32_t *rqi; + const uint8_t *rqi; node = _Scheduler_EDF_SMP_Node_downcast( node_base ); rqi = arg; @@ -672,16 +672,61 @@ void _Scheduler_EDF_SMP_Start_idle( ); } +void _Scheduler_EDF_SMP_Pin( + const Scheduler_Control *scheduler, + Thread_Control *thread, + Scheduler_Node *node_base, + struct Per_CPU_Control *cpu +) +{ + Scheduler_EDF_SMP_Node *node; + uint8_t rqi; + + (void) scheduler; + node = _Scheduler_EDF_SMP_Node_downcast( node_base ); + rqi = (uint8_t) _Per_CPU_Get_index( cpu ) + 1; + + _Assert( + _Scheduler_SMP_Node_state( &node->Base.Base ) == SCHEDULER_SMP_NODE_BLOCKED + ); + + node = _Scheduler_EDF_SMP_Node_downcast( node_base ); + node->ready_queue_index = rqi; + node->pinning_ready_queue_index = rqi; +} + +void _Scheduler_EDF_SMP_Unpin( + const Scheduler_Control *scheduler, + Thread_Control *thread, + Scheduler_Node *node_base, + struct Per_CPU_Control *cpu +) +{ + Scheduler_EDF_SMP_Node *node; + + (void) scheduler; + (void) cpu; + node = _Scheduler_EDF_SMP_Node_downcast( node_base ); + + _Assert( + _Scheduler_SMP_Node_state( &node->Base.Base ) == SCHEDULER_SMP_NODE_BLOCKED + ); + + node->ready_queue_index = node->affinity_ready_queue_index; + node->pinning_ready_queue_index = 0; +} + bool _Scheduler_EDF_SMP_Set_affinity( const Scheduler_Control *scheduler, Thread_Control *thread, - Scheduler_Node *node, + Scheduler_Node *node_base, const Processor_mask *affinity ) { - Scheduler_Context *context; - Processor_mask local_affinity; - uint32_t rqi; + Scheduler_Context *context; + Scheduler_EDF_SMP_Node *node; + Processor_mask local_affinity; + uint8_t rqi; context = _Scheduler_Get_context( scheduler ); _Processor_mask_And( &local_affinity, &context->Processors, affinity ); @@ -696,18 +741,23 @@ bool _Scheduler_EDF_SMP_Set_affinity( rqi = _Processor_mask_Find_last_set( &local_affinity ); } - _Scheduler_SMP_Set_affinity( - context, - thread, - node, - &rqi, - _Scheduler_EDF_SMP_Do_set_affinity, - _Scheduler_EDF_SMP_Extract_from_ready, - _Scheduler_EDF_SMP_Get_highest_ready, - _Scheduler_EDF_SMP_Move_from_ready_to_scheduled, - _Scheduler_EDF_SMP_Enqueue, - _Scheduler_EDF_SMP_Allocate_processor - ); + node = _Scheduler_EDF_SMP_Node_downcast( node_base ); + node->affinity_ready_queue_index = rqi; + + if ( node->pinning_ready_queue_index == 0 ) { + _Scheduler_SMP_Set_affinity( + context, + thread, + node_base, + &rqi, + _Scheduler_EDF_SMP_Do_set_affinity, + _Scheduler_EDF_SMP_Extract_from_ready, + _Scheduler_EDF_SMP_Get_highest_ready, + _Scheduler_EDF_SMP_Move_from_ready_to_scheduled, + _Scheduler_EDF_SMP_Enqueue, + _Scheduler_EDF_SMP_Allocate_processor + ); + } return true; } diff --git a/cpukit/score/src/threaddispatch.c b/cpukit/score/src/threaddispatch.c index d6207bc898..b82fe539c1 100644 --- a/cpukit/score/src/threaddispatch.c +++ b/cpukit/score/src/threaddispatch.c @@ -9,7 +9,7 @@ * COPYRIGHT (c) 1989-2009. * On-Line Applications Research Corporation (OAR). * - * Copyright (c) 2014, 2016 embedded brains GmbH. + * Copyright (c) 2014, 2018 embedded brains GmbH. * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at @@ -37,6 +37,80 @@ Thread_Control *_Thread_Allocated_fp; CHAIN_DEFINE_EMPTY( _User_extensions_Switches_list ); #if defined(RTEMS_SMP) +static ISR_Level _Thread_Check_pinning( + Thread_Control *executing, + Per_CPU_Control *cpu_self, + ISR_Level level +) +{ + unsigned int pin_level; + + pin_level = executing->Scheduler.pin_level; + + if ( + RTEMS_PREDICT_FALSE( pin_level != 0 ) + && ( pin_level & THREAD_PIN_PREEMPTION ) == 0 + ) { + ISR_lock_Context state_lock_context; + ISR_lock_Context scheduler_lock_context; + const Scheduler_Control *pinned_scheduler; + Scheduler_Node *pinned_node; + const Scheduler_Control *home_scheduler; + + _ISR_Local_enable( level ); + + executing->Scheduler.pin_level = pin_level | THREAD_PIN_PREEMPTION; + + _Thread_State_acquire( executing, &state_lock_context ); + + pinned_scheduler = _Scheduler_Get_by_CPU( cpu_self ); + pinned_node = _Thread_Scheduler_get_node_by_index( + executing, + _Scheduler_Get_index( pinned_scheduler ) + ); + + if ( _Thread_Is_ready( executing ) ) { + _Scheduler_Block( executing); + } + + home_scheduler = _Thread_Scheduler_get_home( executing ); + executing->Scheduler.pinned_scheduler = pinned_scheduler; + + if ( home_scheduler != pinned_scheduler ) { + _Chain_Extract_unprotected( &pinned_node->Thread.Scheduler_node.Chain ); + _Chain_Prepend_unprotected( + &executing->Scheduler.Scheduler_nodes, + &pinned_node->Thread.Scheduler_node.Chain + ); + } + + _Scheduler_Acquire_critical( pinned_scheduler, &scheduler_lock_context ); + + ( *pinned_scheduler->Operations.pin )( + pinned_scheduler, + executing, + pinned_node, + cpu_self + ); + + if ( _Thread_Is_ready( executing ) ) { + ( *pinned_scheduler->Operations.unblock )( + pinned_scheduler, + executing, + pinned_node + ); + } + + _Scheduler_Release_critical( pinned_scheduler, &scheduler_lock_context ); + + _Thread_State_release( executing, &state_lock_context ); + + _ISR_Local_disable( level ); + } + + return level; +} + static void _Thread_Ask_for_help( Thread_Control *the_thread ) { Chain_Node *node; @@ -77,9 +151,15 @@ static bool _Thread_Can_ask_for_help( const Thread_Control *executing ) } #endif -static void _Thread_Preemption_intervention( Per_CPU_Control *cpu_self ) +static ISR_Level _Thread_Preemption_intervention( + Thread_Control *executing, + Per_CPU_Control *cpu_self, + ISR_Level level +) { #if defined(RTEMS_SMP) + level = _Thread_Check_pinning( executing, cpu_self, level ); + _Per_CPU_Acquire( cpu_self ); while ( !_Chain_Is_empty( &cpu_self->Threads_in_need_for_help ) ) { @@ -102,6 +182,8 @@ static void _Thread_Preemption_intervention( Per_CPU_Control *cpu_self ) #else (void) cpu_self; #endif + + return level; } static void _Thread_Post_switch_cleanup( Thread_Control *executing ) @@ -192,7 +274,7 @@ void _Thread_Do_dispatch( Per_CPU_Control *cpu_self, ISR_Level level ) do { Thread_Control *heir; - _Thread_Preemption_intervention( cpu_self ); + level = _Thread_Preemption_intervention( executing, cpu_self, level ); heir = _Thread_Get_heir_and_make_it_executing( cpu_self ); /* diff --git a/cpukit/score/src/threadinitialize.c b/cpukit/score/src/threadinitialize.c index c2296fbf55..401973ec9b 100644 --- a/cpukit/score/src/threadinitialize.c +++ b/cpukit/score/src/threadinitialize.c @@ -245,7 +245,7 @@ bool _Thread_Initialize( #if defined(RTEMS_SMP) RTEMS_STATIC_ASSERT( THREAD_SCHEDULER_BLOCKED == 0, Scheduler_state ); - the_thread->Scheduler.home = scheduler; + the_thread->Scheduler.home_scheduler = scheduler; _ISR_lock_Initialize( &the_thread->Scheduler.Lock, "Thread Scheduler" ); _Processor_mask_Assign( &the_thread->Scheduler.Affinity, diff --git a/cpukit/score/src/threadunpin.c b/cpukit/score/src/threadunpin.c new file mode 100644 index 0000000000..bf05790694 --- /dev/null +++ b/cpukit/score/src/threadunpin.c @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2018 embedded brains GmbH + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.org/license/LICENSE. + */ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include <rtems/score/schedulerimpl.h> + +void _Thread_Do_unpin( Thread_Control *executing, Per_CPU_Control *cpu_self ) +{ + ISR_lock_Context state_lock_context; + ISR_lock_Context scheduler_lock_context; + Scheduler_Node *pinned_node; + const Scheduler_Control *pinned_scheduler; + Scheduler_Node *home_node; + const Scheduler_Control *home_scheduler; + const Scheduler_Control *scheduler; + + _Thread_State_acquire( executing, &state_lock_context ); + + executing->Scheduler.pin_level = 0; + + pinned_node = SCHEDULER_NODE_OF_THREAD_SCHEDULER_NODE( + _Chain_First( &executing->Scheduler.Scheduler_nodes ) + ); + pinned_scheduler = _Scheduler_Node_get_scheduler( pinned_node ); + home_node = _Thread_Scheduler_get_home_node( executing ); + home_scheduler = _Thread_Scheduler_get_home( executing ); + scheduler = pinned_scheduler; + + executing->Scheduler.pinned_scheduler = NULL; + + _Scheduler_Acquire_critical( scheduler, &scheduler_lock_context ); + + if ( _Thread_Is_ready( executing ) ) { + ( *scheduler->Operations.block )( scheduler, executing, pinned_node ); + } + + ( *scheduler->Operations.unpin )( + scheduler, + executing, + pinned_node, + cpu_self + ); + + if ( home_node != pinned_node ) { + _Scheduler_Release_critical( scheduler, &scheduler_lock_context ); + + _Chain_Extract_unprotected( &home_node->Thread.Scheduler_node.Chain ); + _Chain_Prepend_unprotected( + &executing->Scheduler.Scheduler_nodes, + &home_node->Thread.Scheduler_node.Chain + ); + scheduler = home_scheduler; + + _Scheduler_Acquire_critical( scheduler, &scheduler_lock_context ); + } + + if ( _Thread_Is_ready( executing ) ) { + ( *scheduler->Operations.unblock )( scheduler, executing, home_node ); + } + + _Scheduler_Release_critical( scheduler, &scheduler_lock_context ); + + _Thread_State_release( executing, &state_lock_context ); +} |