source: rtems/cpukit/score/src/threadinitialize.c @ 1fcac5ad

5
Last change on this file since 1fcac5ad was 1fcac5ad, checked in by Sebastian Huber <sebastian.huber@…>, on 07/25/16 at 14:35:37

score: Turn thread lock into thread wait lock

The _Thread_Lock_acquire() function had a potentially infinite run-time
due to the lack of fairness at atomic operations level.

Update #2412.
Update #2556.
Update #2765.

  • 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/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  Scheduler_Node          *scheduler_node;
56  bool                     scheduler_node_initialized = false;
57  Per_CPU_Control         *cpu = _Per_CPU_Get_by_index( 0 );
58
59#if defined( RTEMS_SMP )
60  if ( rtems_configuration_is_smp_enabled() && !is_preemptible ) {
61    return false;
62  }
63#endif
64
65  memset(
66    &the_thread->current_state,
67    0,
68    information->Objects.size - offsetof( Thread_Control, current_state )
69  );
70
71  for ( i = 0 ; i < _Thread_Control_add_on_count ; ++i ) {
72    const Thread_Control_add_on *add_on = &_Thread_Control_add_ons[ i ];
73
74    *(void **) ( (char *) the_thread + add_on->destination_offset ) =
75      (char *) the_thread + add_on->source_offset;
76  }
77
78  /*
79   *  Allocate and Initialize the stack for this thread.
80   */
81  #if !defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
82    actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
83    if ( !actual_stack_size || actual_stack_size < stack_size )
84      return false;                     /* stack allocation failed */
85
86    stack = the_thread->Start.stack;
87  #else
88    if ( !stack_area ) {
89      actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
90      if ( !actual_stack_size || actual_stack_size < stack_size )
91        return false;                     /* stack allocation failed */
92
93      stack = the_thread->Start.stack;
94      the_thread->Start.core_allocated_stack = true;
95    } else {
96      stack = stack_area;
97      actual_stack_size = stack_size;
98      the_thread->Start.core_allocated_stack = false;
99    }
100  #endif
101
102  _Stack_Initialize(
103     &the_thread->Start.Initial_stack,
104     stack,
105     actual_stack_size
106  );
107
108  /* Thread-local storage (TLS) area allocation */
109  if ( tls_size > 0 ) {
110    uintptr_t tls_align = _TLS_Heap_align_up( (uintptr_t) _TLS_Alignment );
111    uintptr_t tls_alloc = _TLS_Get_allocation_size( tls_size, tls_align );
112
113    the_thread->Start.tls_area =
114      _Workspace_Allocate_aligned( tls_alloc, tls_align );
115
116    if ( the_thread->Start.tls_area == NULL ) {
117      goto failed;
118    }
119  }
120
121  /*
122   *  Allocate the floating point area for this thread
123   */
124  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
125    if ( is_fp ) {
126      fp_area = _Workspace_Allocate( CONTEXT_FP_SIZE );
127      if ( !fp_area )
128        goto failed;
129      fp_area = _Context_Fp_start( fp_area, 0 );
130    }
131    the_thread->fp_context       = fp_area;
132    the_thread->Start.fp_context = fp_area;
133  #endif
134
135  /*
136   *  Get thread queue heads
137   */
138  the_thread->Wait.spare_heads = _Freechain_Get(
139    &information->Free_thread_queue_heads,
140    _Workspace_Allocate,
141    _Objects_Extend_size( &information->Objects ),
142    THREAD_QUEUE_HEADS_SIZE( _Scheduler_Count )
143  );
144  if ( the_thread->Wait.spare_heads == NULL ) {
145    goto failed;
146  }
147  _Thread_queue_Heads_initialize( the_thread->Wait.spare_heads );
148
149  /*
150   *  General initialization
151   */
152
153  the_thread->is_fp                  = is_fp;
154  the_thread->Start.isr_level        = isr_level;
155  the_thread->Start.is_preemptible   = is_preemptible;
156  the_thread->Start.budget_algorithm = budget_algorithm;
157  the_thread->Start.budget_callout   = budget_callout;
158
159  _Thread_Timer_initialize( &the_thread->Timer, cpu );
160
161  switch ( budget_algorithm ) {
162    case THREAD_CPU_BUDGET_ALGORITHM_NONE:
163    case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE:
164      break;
165    #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE)
166      case THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE:
167        the_thread->cpu_time_budget =
168          rtems_configuration_get_ticks_per_timeslice();
169        break;
170    #endif
171    #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT)
172      case THREAD_CPU_BUDGET_ALGORITHM_CALLOUT:
173        break;
174    #endif
175  }
176
177  scheduler_node = the_thread->Scheduler.node;
178
179#if defined(RTEMS_SMP)
180  RTEMS_STATIC_ASSERT( THREAD_SCHEDULER_BLOCKED == 0, Scheduler_state );
181  the_thread->Scheduler.own_control = scheduler;
182  the_thread->Scheduler.control = scheduler;
183  the_thread->Scheduler.own_node = scheduler_node;
184  _Resource_Node_initialize( &the_thread->Resource_node );
185  _ISR_lock_Initialize(
186    &the_thread->Wait.Lock.Default,
187    "Thread Wait Default Lock"
188  );
189  _Chain_Initialize_empty( &the_thread->Wait.Lock.Pending_requests );
190  _SMP_lock_Stats_initialize( &the_thread->Potpourri_stats, "Thread Potpourri" );
191#endif
192
193  _Thread_Debug_set_real_processor( the_thread, cpu );
194
195  /* Initialize the CPU for the non-SMP schedulers */
196  _Thread_Set_CPU( the_thread, cpu );
197
198  _Thread_queue_Initialize( &the_thread->Join_queue );
199
200  the_thread->current_state           = STATES_DORMANT;
201  the_thread->Wait.operations         = &_Thread_queue_Operations_default;
202  the_thread->current_priority        = priority;
203  the_thread->real_priority           = priority;
204  the_thread->Start.initial_priority  = priority;
205
206  RTEMS_STATIC_ASSERT( THREAD_WAIT_FLAGS_INITIAL == 0, Wait_flags );
207
208  _Scheduler_Node_initialize( scheduler, scheduler_node, the_thread, priority );
209  scheduler_node_initialized = true;
210
211  /* POSIX Keys */
212  _RBTree_Initialize_empty( &the_thread->Keys.Key_value_pairs );
213  _ISR_lock_Initialize( &the_thread->Keys.Lock, "POSIX Key Value Pairs" );
214
215  _Thread_Action_control_initialize( &the_thread->Post_switch_actions );
216
217  /*
218   *  Open the object
219   */
220  _Objects_Open( &information->Objects, &the_thread->Object, name );
221
222  /*
223   *  We assume the Allocator Mutex is locked and dispatching is
224   *  enabled when we get here.  We want to be able to run the
225   *  user extensions with dispatching enabled.  The Allocator
226   *  Mutex provides sufficient protection to let the user extensions
227   *  run safely.
228   */
229  extension_status = _User_extensions_Thread_create( the_thread );
230  if ( extension_status )
231    return true;
232
233failed:
234
235  if ( scheduler_node_initialized ) {
236    _Scheduler_Node_destroy( scheduler, scheduler_node );
237  }
238
239  _Workspace_Free( the_thread->Start.tls_area );
240
241  _Freechain_Put(
242    &information->Free_thread_queue_heads,
243    the_thread->Wait.spare_heads
244  );
245
246  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
247    _Workspace_Free( fp_area );
248  #endif
249
250   _Thread_Stack_Free( the_thread );
251  return false;
252}
Note: See TracBrowser for help on using the repository browser.