score: PR2181: Add _Thread_Yield()

The _Scheduler_Yield() was called by the executing thread with thread
dispatching disabled and interrupts enabled.  The rtems_task_suspend()
is explicitly allowed in ISRs:

http://rtems.org/onlinedocs/doc-current/share/rtems/html/c_user/Interrupt-Manager-Directives-Allowed-from-an-ISR.html#Interrupt-Manager-Directives-Allowed-from-an-ISR

Unlike the other scheduler operations the locking was performed inside
the operation.  This lead to the following race condition.  Suppose a
ISR suspends the executing thread right before the yield scheduler
operation.  Now the executing thread is not longer in the set of ready
threads.  The typical scheduler operations did not check the thread
state and will now extract the thread again and enqueue it.  This
corrupted data structures.

Add _Thread_Yield() and do the scheduler yield operation with interrupts
disabled.  This has a negligible effect on the interrupt latency.
This commit is contained in:
Sebastian Huber
2014-06-12 16:13:26 +02:00
parent 970aa80fe1
commit 701dd96f59
13 changed files with 98 additions and 57 deletions
+1 -2
View File
@@ -22,7 +22,6 @@
#include <errno.h>
#include <rtems/seterr.h>
#include <rtems/score/schedulerimpl.h>
#include <rtems/score/threadimpl.h>
#include <rtems/score/timespec.h>
#include <rtems/score/watchdogimpl.h>
@@ -65,7 +64,7 @@ int nanosleep(
if ( !ticks ) {
_Thread_Disable_dispatch();
executing = _Thread_Executing;
_Scheduler_Yield( _Scheduler_Get( executing ), executing );
_Thread_Yield( executing );
_Thread_Enable_dispatch();
if ( rmtp ) {
rmtp->tv_sec = 0;
+2 -5
View File
@@ -21,16 +21,13 @@
#include <sched.h>
#include <rtems/score/percpu.h>
#include <rtems/score/schedulerimpl.h>
#include <rtems/score/threaddispatch.h>
#include <rtems/score/threadimpl.h>
int sched_yield( void )
{
Thread_Control *executing;
_Thread_Disable_dispatch();
executing = _Thread_Executing;
_Scheduler_Yield( _Scheduler_Get( executing ), executing );
_Thread_Yield( _Thread_Executing );
_Thread_Enable_dispatch();
return 0;
}
+1 -2
View File
@@ -20,7 +20,6 @@
#include <rtems/rtems/tasks.h>
#include <rtems/score/threadimpl.h>
#include <rtems/score/schedulerimpl.h>
#include <rtems/score/watchdogimpl.h>
rtems_status_code rtems_task_wake_after(
@@ -37,7 +36,7 @@ rtems_status_code rtems_task_wake_after(
executing = _Thread_Executing;
if ( ticks == 0 ) {
_Scheduler_Yield( _Scheduler_Get( executing ), executing );
_Thread_Yield( executing );
} else {
_Thread_Set_state( executing, STATES_DELAYING );
_Watchdog_Initialize(
+1
View File
@@ -287,6 +287,7 @@ libscore_a_SOURCES += src/thread.c src/threadchangepriority.c \
src/threadstackallocate.c src/threadstackfree.c src/threadstart.c \
src/threadstartmultitasking.c src/iterateoverthreads.c \
src/threadblockingoperationcancel.c
libscore_a_SOURCES += src/threadyield.c
if HAS_SMP
libscore_a_SOURCES += src/smpbarrierwait.c
@@ -672,6 +672,27 @@ static inline void _Scheduler_SMP_Change_priority(
}
}
static inline void _Scheduler_SMP_Yield(
Scheduler_Context *context,
Thread_Control *thread,
Scheduler_SMP_Extract extract_from_ready,
Scheduler_SMP_Enqueue enqueue_fifo,
Scheduler_SMP_Enqueue enqueue_scheduled_fifo
)
{
Scheduler_SMP_Node *node = _Scheduler_SMP_Node_get( thread );
if ( node->state == SCHEDULER_SMP_NODE_SCHEDULED ) {
_Scheduler_SMP_Extract_from_scheduled( thread );
( *enqueue_scheduled_fifo )( context, thread );
} else {
( *extract_from_ready )( context, thread );
( *enqueue_fifo )( context, thread );
}
}
static inline void _Scheduler_SMP_Insert_scheduled_lifo(
Scheduler_Context *context,
Thread_Control *thread
@@ -186,6 +186,8 @@ bool _Thread_Restart(
Thread_Entry_numeric_type numeric_argument
);
void _Thread_Yield( Thread_Control *executing );
bool _Thread_Set_life_protection( bool protect );
void _Thread_Life_action_handler(
+3 -1
View File
@@ -29,6 +29,8 @@ void _Scheduler_default_Tick(
Thread_Control *executing
)
{
(void) scheduler;
#ifdef __RTEMS_USE_TICKS_FOR_STATISTICS__
/*
* Increment the number of ticks this thread has been executing
@@ -69,7 +71,7 @@ void _Scheduler_default_Tick(
* currently executing thread is placed at the rear of the
* FIFO for this priority and a new heir is selected.
*/
_Scheduler_Yield( scheduler, executing );
_Thread_Yield( executing );
executing->cpu_time_budget =
rtems_configuration_get_ticks_per_timeslice();
}
-7
View File
@@ -29,9 +29,6 @@ void _Scheduler_EDF_Yield(
Scheduler_EDF_Context *context =
_Scheduler_EDF_Get_context( scheduler );
Scheduler_EDF_Node *node = _Scheduler_EDF_Node_get( the_thread );
ISR_Level level;
_ISR_Disable( level );
/*
* The RBTree has more than one node, enqueue behind the tasks
@@ -40,9 +37,5 @@ void _Scheduler_EDF_Yield(
_RBTree_Extract( &context->Ready, &node->Node );
_RBTree_Insert( &context->Ready, &node->Node );
_ISR_Flash( level );
_Scheduler_EDF_Schedule_body( scheduler, the_thread, false );
_ISR_Enable( level );
}
+7 -7
View File
@@ -237,12 +237,12 @@ void _Scheduler_priority_SMP_Yield(
)
{
Scheduler_Context *context = _Scheduler_Get_context( scheduler );
ISR_Level level;
_ISR_Disable( level );
_Scheduler_SMP_Extract_from_scheduled( thread );
_Scheduler_priority_SMP_Enqueue_scheduled_fifo( context, thread );
_ISR_Enable( level );
return _Scheduler_SMP_Yield(
context,
thread,
_Scheduler_priority_SMP_Extract_from_ready,
_Scheduler_priority_SMP_Enqueue_fifo,
_Scheduler_priority_SMP_Enqueue_scheduled_fifo
);
}
+9 -14
View File
@@ -19,7 +19,6 @@
#endif
#include <rtems/score/schedulerpriorityimpl.h>
#include <rtems/score/isr.h>
#include <rtems/score/threadimpl.h>
void _Scheduler_priority_Yield(
@@ -29,23 +28,19 @@ void _Scheduler_priority_Yield(
{
Scheduler_priority_Node *node = _Scheduler_priority_Node_get( the_thread );
Chain_Control *ready_chain = node->Ready_queue.ready_chain;
ISR_Level level;
(void) scheduler;
_ISR_Disable( level );
if ( !_Chain_Has_only_one_node( ready_chain ) ) {
_Chain_Extract_unprotected( &the_thread->Object.Node );
_Chain_Append_unprotected( ready_chain, &the_thread->Object.Node );
if ( !_Chain_Has_only_one_node( ready_chain ) ) {
_Chain_Extract_unprotected( &the_thread->Object.Node );
_Chain_Append_unprotected( ready_chain, &the_thread->Object.Node );
_ISR_Flash( level );
if ( _Thread_Is_heir( the_thread ) )
_Thread_Heir = (Thread_Control *) _Chain_First( ready_chain );
_Thread_Dispatch_necessary = true;
if ( _Thread_Is_heir( the_thread ) ) {
_Thread_Heir = (Thread_Control *) _Chain_First( ready_chain );
}
else if ( !_Thread_Is_heir( the_thread ) )
_Thread_Dispatch_necessary = true;
_ISR_Enable( level );
_Thread_Dispatch_necessary = true;
} else if ( !_Thread_Is_heir( the_thread ) ) {
_Thread_Dispatch_necessary = true;
}
}
+7 -7
View File
@@ -302,12 +302,12 @@ void _Scheduler_simple_SMP_Yield(
)
{
Scheduler_Context *context = _Scheduler_Get_context( scheduler );
ISR_Level level;
_ISR_Disable( level );
_Scheduler_SMP_Extract_from_scheduled( thread );
_Scheduler_simple_SMP_Enqueue_scheduled_fifo( context, thread );
_ISR_Enable( level );
return _Scheduler_SMP_Yield(
context,
thread,
_Scheduler_simple_SMP_Extract_from_ready,
_Scheduler_simple_SMP_Enqueue_fifo,
_Scheduler_simple_SMP_Enqueue_scheduled_fifo
);
}
+3 -12
View File
@@ -19,7 +19,6 @@
#endif
#include <rtems/score/schedulersimpleimpl.h>
#include <rtems/score/isr.h>
void _Scheduler_simple_Yield(
const Scheduler_Control *scheduler,
@@ -28,16 +27,8 @@ void _Scheduler_simple_Yield(
{
Scheduler_simple_Context *context =
_Scheduler_simple_Get_context( scheduler );
ISR_Level level;
_ISR_Disable( level );
_Chain_Extract_unprotected( &the_thread->Object.Node );
_Scheduler_simple_Insert_priority_fifo( &context->Ready, the_thread );
_ISR_Flash( level );
_Scheduler_simple_Schedule_body( scheduler, the_thread, false );
_ISR_Enable( level );
_Chain_Extract_unprotected( &the_thread->Object.Node );
_Scheduler_simple_Insert_priority_fifo( &context->Ready, the_thread );
_Scheduler_simple_Schedule_body( scheduler, the_thread, false );
}
+41
View File
@@ -0,0 +1,41 @@
/**
* @file
*
* @brief Thread Yield
*
* @ingroup ScoreThread
*/
/*
* Copyright (c) 2014 embedded brains GmbH. All rights reserved.
*
* embedded brains GmbH
* Dornierstr. 4
* 82178 Puchheim
* Germany
* <rtems@embedded-brains.de>
*
* 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/threadimpl.h>
#include <rtems/score/schedulerimpl.h>
void _Thread_Yield( Thread_Control *executing )
{
ISR_Level level;
_ISR_Disable( level );
if ( _States_Is_ready( executing->current_state ) ) {
_Scheduler_Yield( _Scheduler_Get( executing ), executing );
}
_ISR_Enable( level );
}