source: rtems/cpukit/score/src/threadinitialize.c @ c5831a3f

4.115
Last change on this file since c5831a3f was c5831a3f, checked in by Sebastian Huber <sebastian.huber@…>, on 04/09/14 at 13:07:54

score: Add clustered/partitioned scheduling

Clustered/partitioned scheduling helps to control the worst-case
latencies in the system. The goal is to reduce the amount of shared
state in the system and thus prevention of lock contention. Modern
multi-processor systems tend to have several layers of data and
instruction caches. With clustered/partitioned scheduling it is
possible to honour the cache topology of a system and thus avoid
expensive cache synchronization traffic.

We have clustered scheduling in case the set of processors of a system
is partitioned into non-empty pairwise-disjoint subsets. These subsets
are called clusters. Clusters with a cardinality of one are partitions.
Each cluster is owned by exactly one scheduler instance.

  • Property mode set to 100644
File size: 7.7 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/schedulerimpl.h>
23#include <rtems/score/stackimpl.h>
24#include <rtems/score/tls.h>
25#include <rtems/score/userextimpl.h>
26#include <rtems/score/watchdogimpl.h>
27#include <rtems/score/wkspace.h>
28#include <rtems/score/cpusetimpl.h>
29#include <rtems/config.h>
30
31bool _Thread_Initialize(
32  Objects_Information                  *information,
33  Thread_Control                       *the_thread,
34  const Scheduler_Control              *scheduler,
35  void                                 *stack_area,
36  size_t                                stack_size,
37  bool                                  is_fp,
38  Priority_Control                      priority,
39  bool                                  is_preemptible,
40  Thread_CPU_budget_algorithms          budget_algorithm,
41  Thread_CPU_budget_algorithm_callout   budget_callout,
42  uint32_t                              isr_level,
43  Objects_Name                          name
44)
45{
46  size_t                   actual_stack_size = 0;
47  void                    *stack = NULL;
48  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
49    void                  *fp_area = NULL;
50  #endif
51  bool                     extension_status;
52  size_t                   i;
53  bool                     scheduler_allocated = false;
54
55  /*
56   * Do not use _TLS_Size here since this will lead GCC to assume that this
57   * symbol is not 0 and the later > 0 test will be optimized away.
58   */
59  uintptr_t  tls_size = (uintptr_t) _TLS_BSS_end - (uintptr_t) _TLS_Data_begin;
60
61#if defined( RTEMS_SMP )
62  if ( rtems_configuration_is_smp_enabled() && !is_preemptible ) {
63    return false;
64  }
65#endif
66
67  for ( i = 0 ; i < _Thread_Control_add_on_count ; ++i ) {
68    const Thread_Control_add_on *add_on = &_Thread_Control_add_ons[ i ];
69
70    *(void **) ( (char *) the_thread + add_on->destination_offset ) =
71      (char *) the_thread + add_on->source_offset;
72  }
73
74  /*
75   *  Initialize the Ada self pointer
76   */
77  #if __RTEMS_ADA__
78    the_thread->rtems_ada_self = NULL;
79  #endif
80
81  the_thread->Start.tls_area = NULL;
82
83  /*
84   *  Allocate and Initialize the stack for this thread.
85   */
86  #if !defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
87    actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
88    if ( !actual_stack_size || actual_stack_size < stack_size )
89      return false;                     /* stack allocation failed */
90
91    stack = the_thread->Start.stack;
92  #else
93    if ( !stack_area ) {
94      actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
95      if ( !actual_stack_size || actual_stack_size < stack_size )
96        return false;                     /* stack allocation failed */
97
98      stack = the_thread->Start.stack;
99      the_thread->Start.core_allocated_stack = true;
100    } else {
101      stack = stack_area;
102      actual_stack_size = stack_size;
103      the_thread->Start.core_allocated_stack = false;
104    }
105  #endif
106
107  _Stack_Initialize(
108     &the_thread->Start.Initial_stack,
109     stack,
110     actual_stack_size
111  );
112
113  /* Thread-local storage (TLS) area allocation */
114  if ( tls_size > 0 ) {
115    uintptr_t tls_align = _TLS_Heap_align_up( (uintptr_t) _TLS_Alignment );
116    uintptr_t tls_alloc = _TLS_Get_allocation_size( tls_size, tls_align );
117
118    the_thread->Start.tls_area =
119      _Workspace_Allocate_aligned( tls_alloc, tls_align );
120
121    if ( the_thread->Start.tls_area == NULL ) {
122      goto failed;
123    }
124  }
125
126  /*
127   *  Allocate the floating point area for this thread
128   */
129  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
130    if ( is_fp ) {
131      fp_area = _Workspace_Allocate( CONTEXT_FP_SIZE );
132      if ( !fp_area )
133        goto failed;
134      fp_area = _Context_Fp_start( fp_area, 0 );
135    }
136    the_thread->fp_context       = fp_area;
137    the_thread->Start.fp_context = fp_area;
138  #endif
139
140  /*
141   *  Initialize the thread timer
142   */
143  _Watchdog_Initialize( &the_thread->Timer, NULL, 0, NULL );
144
145  #ifdef __RTEMS_STRICT_ORDER_MUTEX__
146    /* Initialize the head of chain of held mutexes */
147    _Chain_Initialize_empty(&the_thread->lock_mutex);
148  #endif
149
150  /*
151   * Clear the extensions area so extension users can determine
152   * if they are linked to the thread. An extension user may
153   * create the extension long after tasks have been created
154   * so they cannot rely on the thread create user extension
155   * call.  The object index starts with one, so the first extension context is
156   * unused.
157   */
158  for ( i = 1 ; i <= rtems_configuration_get_maximum_extensions() ; ++i )
159    the_thread->extensions[ i ] = NULL;
160
161  /*
162   *  General initialization
163   */
164
165  the_thread->Start.is_preemptible   = is_preemptible;
166  the_thread->Start.budget_algorithm = budget_algorithm;
167  the_thread->Start.budget_callout   = budget_callout;
168
169  switch ( budget_algorithm ) {
170    case THREAD_CPU_BUDGET_ALGORITHM_NONE:
171    case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE:
172      break;
173    #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE)
174      case THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE:
175        the_thread->cpu_time_budget =
176          rtems_configuration_get_ticks_per_timeslice();
177        break;
178    #endif
179    #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT)
180      case THREAD_CPU_BUDGET_ALGORITHM_CALLOUT:
181        break;
182    #endif
183  }
184
185  the_thread->Start.isr_level         = isr_level;
186
187#if defined(RTEMS_SMP)
188  the_thread->is_scheduled            = false;
189  the_thread->is_in_the_air           = false;
190  the_thread->is_executing            = false;
191  the_thread->scheduler               = scheduler;
192#endif
193
194  /* Initialize the CPU for the non-SMP schedulers */
195  _Thread_Set_CPU( the_thread, _Per_CPU_Get_by_index( 0 ) );
196
197  the_thread->current_state           = STATES_DORMANT;
198  the_thread->Wait.queue              = NULL;
199  the_thread->resource_count          = 0;
200  the_thread->real_priority           = priority;
201  the_thread->Start.initial_priority  = priority;
202
203  scheduler_allocated = _Scheduler_Allocate( scheduler, the_thread );
204  if ( !scheduler_allocated ) {
205    goto failed;
206  }
207
208  _Thread_Set_priority( the_thread, priority );
209
210  /*
211   *  Initialize the CPU usage statistics
212   */
213  #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
214    _Timestamp_Set_to_zero( &the_thread->cpu_time_used );
215  #else
216    the_thread->cpu_time_used = 0;
217  #endif
218
219  /*
220   * initialize thread's key vaule node chain
221   */
222  _Chain_Initialize_empty( &the_thread->Key_Chain );
223
224  _Thread_Action_control_initialize( &the_thread->Post_switch_actions );
225
226  _Thread_Action_initialize(
227    &the_thread->Life.Action,
228    _Thread_Life_action_handler
229  );
230  the_thread->Life.state = THREAD_LIFE_NORMAL;
231  the_thread->Life.terminator = NULL;
232
233  /*
234   *  Open the object
235   */
236  _Objects_Open( information, &the_thread->Object, name );
237
238  /*
239   *  We assume the Allocator Mutex is locked and dispatching is
240   *  enabled when we get here.  We want to be able to run the
241   *  user extensions with dispatching enabled.  The Allocator
242   *  Mutex provides sufficient protection to let the user extensions
243   *  run safely.
244   */
245  extension_status = _User_extensions_Thread_create( the_thread );
246  if ( extension_status )
247    return true;
248
249failed:
250
251  if ( scheduler_allocated ) {
252    _Scheduler_Free( scheduler, the_thread );
253  }
254
255  _Workspace_Free( the_thread->Start.tls_area );
256
257  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
258    _Workspace_Free( fp_area );
259  #endif
260
261   _Thread_Stack_Free( the_thread );
262  return false;
263}
Note: See TracBrowser for help on using the repository browser.