source: rtems/cpukit/score/src/threadinitialize.c @ 9bfad8c

5
Last change on this file since 9bfad8c was 9bfad8c, checked in by Sebastian Huber <sebastian.huber@…>, on 06/08/16 at 20:22:46

score: Add thread priority to scheduler nodes

The thread priority is manifest in two independent areas. One area is
the user visible thread priority along with a potential thread queue.
The other is the scheduler. Currently, a thread priority update via
_Thread_Change_priority() first updates the user visble thread priority
and the thread queue, then the scheduler is notified if necessary. The
priority is passed to the scheduler via a local variable. A generation
counter ensures that the scheduler discards out-of-date priorities.

This use of a local variable ties the update in these two areas close
together. For later enhancements and the OMIP locking protocol
implementation we need more flexibility. Add a thread priority
information block to Scheduler_Node and synchronize priority value
updates via a sequence lock on SMP configurations.

Update #2556.

  • Property mode set to 100644
File size: 7.6 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief Initialize Thread
5 *
6 *  @ingroup ScoreThread
7 */
8/*
9 *  COPYRIGHT (c) 1989-2014.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <rtems/score/threadimpl.h>
22#include <rtems/score/resourceimpl.h>
23#include <rtems/score/schedulerimpl.h>
24#include <rtems/score/stackimpl.h>
25#include <rtems/score/tls.h>
26#include <rtems/score/userextimpl.h>
27#include <rtems/score/watchdogimpl.h>
28#include <rtems/score/wkspace.h>
29#include <rtems/score/cpusetimpl.h>
30#include <rtems/config.h>
31
32bool _Thread_Initialize(
33  Thread_Information                   *information,
34  Thread_Control                       *the_thread,
35  const Scheduler_Control              *scheduler,
36  void                                 *stack_area,
37  size_t                                stack_size,
38  bool                                  is_fp,
39  Priority_Control                      priority,
40  bool                                  is_preemptible,
41  Thread_CPU_budget_algorithms          budget_algorithm,
42  Thread_CPU_budget_algorithm_callout   budget_callout,
43  uint32_t                              isr_level,
44  Objects_Name                          name
45)
46{
47  uintptr_t                tls_size = _TLS_Get_size();
48  size_t                   actual_stack_size = 0;
49  void                    *stack = NULL;
50  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
51    void                  *fp_area = NULL;
52  #endif
53  bool                     extension_status;
54  size_t                   i;
55  bool                     scheduler_node_initialized = false;
56  Per_CPU_Control         *cpu = _Per_CPU_Get_by_index( 0 );
57
58#if defined( RTEMS_SMP )
59  if ( rtems_configuration_is_smp_enabled() && !is_preemptible ) {
60    return false;
61  }
62#endif
63
64  memset(
65    &the_thread->current_state,
66    0,
67    information->Objects.size - offsetof( Thread_Control, current_state )
68  );
69
70  for ( i = 0 ; i < _Thread_Control_add_on_count ; ++i ) {
71    const Thread_Control_add_on *add_on = &_Thread_Control_add_ons[ i ];
72
73    *(void **) ( (char *) the_thread + add_on->destination_offset ) =
74      (char *) the_thread + add_on->source_offset;
75  }
76
77  /*
78   *  Allocate and Initialize the stack for this thread.
79   */
80  #if !defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
81    actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
82    if ( !actual_stack_size || actual_stack_size < stack_size )
83      return false;                     /* stack allocation failed */
84
85    stack = the_thread->Start.stack;
86  #else
87    if ( !stack_area ) {
88      actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
89      if ( !actual_stack_size || actual_stack_size < stack_size )
90        return false;                     /* stack allocation failed */
91
92      stack = the_thread->Start.stack;
93      the_thread->Start.core_allocated_stack = true;
94    } else {
95      stack = stack_area;
96      actual_stack_size = stack_size;
97      the_thread->Start.core_allocated_stack = false;
98    }
99  #endif
100
101  _Stack_Initialize(
102     &the_thread->Start.Initial_stack,
103     stack,
104     actual_stack_size
105  );
106
107  /* Thread-local storage (TLS) area allocation */
108  if ( tls_size > 0 ) {
109    uintptr_t tls_align = _TLS_Heap_align_up( (uintptr_t) _TLS_Alignment );
110    uintptr_t tls_alloc = _TLS_Get_allocation_size( tls_size, tls_align );
111
112    the_thread->Start.tls_area =
113      _Workspace_Allocate_aligned( tls_alloc, tls_align );
114
115    if ( the_thread->Start.tls_area == NULL ) {
116      goto failed;
117    }
118  }
119
120  /*
121   *  Allocate the floating point area for this thread
122   */
123  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
124    if ( is_fp ) {
125      fp_area = _Workspace_Allocate( CONTEXT_FP_SIZE );
126      if ( !fp_area )
127        goto failed;
128      fp_area = _Context_Fp_start( fp_area, 0 );
129    }
130    the_thread->fp_context       = fp_area;
131    the_thread->Start.fp_context = fp_area;
132  #endif
133
134  /*
135   *  Get thread queue heads
136   */
137  the_thread->Wait.spare_heads = _Freechain_Get(
138    &information->Free_thread_queue_heads,
139    _Workspace_Allocate,
140    _Objects_Extend_size( &information->Objects ),
141    THREAD_QUEUE_HEADS_SIZE( _Scheduler_Count )
142  );
143  if ( the_thread->Wait.spare_heads == NULL ) {
144    goto failed;
145  }
146  _Thread_queue_Heads_initialize( the_thread->Wait.spare_heads );
147
148  /*
149   *  General initialization
150   */
151
152  the_thread->is_fp                  = is_fp;
153  the_thread->Start.isr_level        = isr_level;
154  the_thread->Start.is_preemptible   = is_preemptible;
155  the_thread->Start.budget_algorithm = budget_algorithm;
156  the_thread->Start.budget_callout   = budget_callout;
157
158  _Thread_Timer_initialize( &the_thread->Timer, cpu );
159
160  switch ( budget_algorithm ) {
161    case THREAD_CPU_BUDGET_ALGORITHM_NONE:
162    case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE:
163      break;
164    #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE)
165      case THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE:
166        the_thread->cpu_time_budget =
167          rtems_configuration_get_ticks_per_timeslice();
168        break;
169    #endif
170    #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT)
171      case THREAD_CPU_BUDGET_ALGORITHM_CALLOUT:
172        break;
173    #endif
174  }
175
176#if defined(RTEMS_SMP)
177  RTEMS_STATIC_ASSERT( THREAD_SCHEDULER_BLOCKED == 0, Scheduler_state );
178  the_thread->Scheduler.own_control = scheduler;
179  the_thread->Scheduler.control = scheduler;
180  the_thread->Scheduler.own_node = the_thread->Scheduler.node;
181  _Resource_Node_initialize( &the_thread->Resource_node );
182  the_thread->Lock.current = &the_thread->Lock.Default;
183  _SMP_ticket_lock_Initialize( &the_thread->Lock.Default );
184  _SMP_lock_Stats_initialize( &the_thread->Lock.Stats, "Thread Lock" );
185  _SMP_lock_Stats_initialize( &the_thread->Potpourri_stats, "Thread Potpourri" );
186#endif
187
188  _Thread_Debug_set_real_processor( the_thread, cpu );
189
190  /* Initialize the CPU for the non-SMP schedulers */
191  _Thread_Set_CPU( the_thread, cpu );
192
193  _Thread_queue_Initialize( &the_thread->Join_queue );
194
195  the_thread->current_state           = STATES_DORMANT;
196  the_thread->Wait.operations         = &_Thread_queue_Operations_default;
197  the_thread->current_priority        = priority;
198  the_thread->real_priority           = priority;
199  the_thread->Start.initial_priority  = priority;
200
201  RTEMS_STATIC_ASSERT( THREAD_WAIT_FLAGS_INITIAL == 0, Wait_flags );
202
203  _Scheduler_Node_initialize( scheduler, the_thread, priority );
204  scheduler_node_initialized = true;
205
206  /* POSIX Keys */
207  _RBTree_Initialize_empty( &the_thread->Keys.Key_value_pairs );
208  _ISR_lock_Initialize( &the_thread->Keys.Lock, "POSIX Key Value Pairs" );
209
210  _Thread_Action_control_initialize( &the_thread->Post_switch_actions );
211
212  /*
213   *  Open the object
214   */
215  _Objects_Open( &information->Objects, &the_thread->Object, name );
216
217  /*
218   *  We assume the Allocator Mutex is locked and dispatching is
219   *  enabled when we get here.  We want to be able to run the
220   *  user extensions with dispatching enabled.  The Allocator
221   *  Mutex provides sufficient protection to let the user extensions
222   *  run safely.
223   */
224  extension_status = _User_extensions_Thread_create( the_thread );
225  if ( extension_status )
226    return true;
227
228failed:
229
230  if ( scheduler_node_initialized ) {
231    _Scheduler_Node_destroy( scheduler, the_thread );
232  }
233
234  _Workspace_Free( the_thread->Start.tls_area );
235
236  _Freechain_Put(
237    &information->Free_thread_queue_heads,
238    the_thread->Wait.spare_heads
239  );
240
241  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
242    _Workspace_Free( fp_area );
243  #endif
244
245   _Thread_Stack_Free( the_thread );
246  return false;
247}
Note: See TracBrowser for help on using the repository browser.