source: rtems/cpukit/score/include/rtems/score/threadimpl.h @ 8d6e6eeb

4.115
Last change on this file since 8d6e6eeb was 8d6e6eeb, checked in by Sebastian Huber <sebastian.huber@…>, on 02/17/15 at 09:00:43

score: Fix FP context restore via _Thread_Handler

After a context switch we end up in the second part of
_Thread_Dispatch() or in _Thread_Handler() in case of new threads. Use
the same function _Thread_Restore_fp() to restore the floating-point
context. It makes no sense to do this in _Thread_Start_multitasking().
This fixes also a race condition in SMP configurations.

Update #2268.

  • Property mode set to 100644
File size: 25.8 KB
Line 
1/**
2 * @file
3 *
4 * @brief Inlined Routines from the Thread Handler
5 *
6 * This file contains the macro implementation of the inlined
7 * routines from the Thread handler.
8 */
9
10/*
11 *  COPYRIGHT (c) 1989-2008.
12 *  On-Line Applications Research Corporation (OAR).
13 *
14 *  Copyright (c) 2014 embedded brains GmbH.
15 *
16 *  The license and distribution terms for this file may be
17 *  found in the file LICENSE in this distribution or at
18 *  http://www.rtems.org/license/LICENSE.
19 */
20
21#ifndef _RTEMS_SCORE_THREADIMPL_H
22#define _RTEMS_SCORE_THREADIMPL_H
23
24#include <rtems/score/thread.h>
25#include <rtems/score/chainimpl.h>
26#include <rtems/score/interr.h>
27#include <rtems/score/isr.h>
28#include <rtems/score/objectimpl.h>
29#include <rtems/score/resourceimpl.h>
30#include <rtems/score/statesimpl.h>
31#include <rtems/score/sysstate.h>
32#include <rtems/score/todimpl.h>
33#include <rtems/config.h>
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39/**
40 * @addtogroup ScoreThread
41 */
42/**@{**/
43
44/**
45 *  The following structure contains the information necessary to manage
46 *  a thread which it is  waiting for a resource.
47 */
48#define THREAD_STATUS_PROXY_BLOCKING 0x1111111
49
50/**
51 *  Self for the GNU Ada Run-Time
52 */
53SCORE_EXTERN void *rtems_ada_self;
54
55/**
56 *  The following defines the information control block used to
57 *  manage this class of objects.
58 */
59SCORE_EXTERN Objects_Information _Thread_Internal_information;
60
61/**
62 *  The following points to the thread whose floating point
63 *  context is currently loaded.
64 */
65#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
66SCORE_EXTERN Thread_Control *_Thread_Allocated_fp;
67#endif
68
69#if !defined(__DYNAMIC_REENT__)
70/**
71 * The C library re-enter-rant global pointer. Some C library implementations
72 * such as newlib have a single global pointer that changed during a context
73 * switch. The pointer points to that global pointer. The Thread control block
74 * holds a pointer to the task specific data.
75 */
76SCORE_EXTERN struct _reent **_Thread_libc_reent;
77#endif
78
79#define THREAD_RBTREE_NODE_TO_THREAD( node ) \
80  RTEMS_CONTAINER_OF( node, Thread_Control, RBNode )
81
82#if defined(RTEMS_SMP)
83#define THREAD_RESOURCE_NODE_TO_THREAD( node ) \
84  RTEMS_CONTAINER_OF( node, Thread_Control, Resource_node )
85#endif
86
87/**
88 *  @brief Initialize thread handler.
89 *
90 *  This routine performs the initialization necessary for this handler.
91 */
92void _Thread_Handler_initialization(void);
93
94/**
95 *  @brief Create idle thread.
96 *
97 *  This routine creates the idle thread.
98 *
99 *  @warning No thread should be created before this one.
100 */
101void _Thread_Create_idle(void);
102
103/**
104 *  @brief Start thread multitasking.
105 *
106 *  This routine initiates multitasking.  It is invoked only as
107 *  part of initialization and its invocation is the last act of
108 *  the non-multitasking part of the system initialization.
109 */
110void _Thread_Start_multitasking( void ) RTEMS_COMPILER_NO_RETURN_ATTRIBUTE;
111
112/**
113 *  @brief Allocate the requested stack space for the thread.
114 *
115 *  Allocate the requested stack space for the thread.
116 *  Set the Start.stack field to the address of the stack.
117 *
118 *  @param[in] the_thread is the thread where the stack space is requested
119 *
120 *  @retval actual size allocated after any adjustment
121 *  @retval zero if the allocation failed
122 */
123size_t _Thread_Stack_Allocate(
124  Thread_Control *the_thread,
125  size_t          stack_size
126);
127
128/**
129 *  @brief Deallocate thread stack.
130 *
131 *  Deallocate the Thread's stack.
132 */
133void _Thread_Stack_Free(
134  Thread_Control *the_thread
135);
136
137/**
138 *  @brief Initialize thread.
139 *
140 *  This routine initializes the specified the thread.  It allocates
141 *  all memory associated with this thread.  It completes by adding
142 *  the thread to the local object table so operations on this
143 *  thread id are allowed.
144 *
145 *  @note If stack_area is NULL, it is allocated from the workspace.
146 *
147 *  @note If the stack is allocated from the workspace, then it is
148 *        guaranteed to be of at least minimum size.
149 */
150bool _Thread_Initialize(
151  Objects_Information                  *information,
152  Thread_Control                       *the_thread,
153  const struct Scheduler_Control       *scheduler,
154  void                                 *stack_area,
155  size_t                                stack_size,
156  bool                                  is_fp,
157  Priority_Control                      priority,
158  bool                                  is_preemptible,
159  Thread_CPU_budget_algorithms          budget_algorithm,
160  Thread_CPU_budget_algorithm_callout   budget_callout,
161  uint32_t                              isr_level,
162  Objects_Name                          name
163);
164
165/**
166 *  @brief Initializes thread and executes it.
167 *
168 *  This routine initializes the executable information for a thread
169 *  and makes it ready to execute.  After this routine executes, the
170 *  thread competes with all other threads for CPU time.
171 *
172 *  @param the_thread is the thread to be initialized
173 *  @param the_prototype
174 *  @param entry_point
175 *  @param pointer_argument
176 *  @param numeric_argument
177 *  @param[in,out] cpu The processor if used to start an idle thread
178 *  during system initialization.  Must be set to @c NULL to start a normal
179 *  thread.
180 */
181bool _Thread_Start(
182  Thread_Control            *the_thread,
183  Thread_Start_types         the_prototype,
184  void                      *entry_point,
185  void                      *pointer_argument,
186  Thread_Entry_numeric_type  numeric_argument,
187  Per_CPU_Control           *cpu
188);
189
190bool _Thread_Restart(
191  Thread_Control            *the_thread,
192  Thread_Control            *executing,
193  void                      *pointer_argument,
194  Thread_Entry_numeric_type  numeric_argument
195);
196
197void _Thread_Yield( Thread_Control *executing );
198
199bool _Thread_Set_life_protection( bool protect );
200
201void _Thread_Life_action_handler(
202  Thread_Control  *executing,
203  Thread_Action   *action,
204  Per_CPU_Control *cpu,
205  ISR_Level        level
206);
207
208/**
209 * @brief Kills all zombie threads in the system.
210 *
211 * Threads change into the zombie state as the last step in the thread
212 * termination sequence right before a context switch to the heir thread is
213 * initiated.  Since the thread stack is still in use during this phase we have
214 * to postpone the thread stack reclamation until this point.  On SMP
215 * configurations we may have to busy wait for context switch completion here.
216 */
217void _Thread_Kill_zombies( void );
218
219/**
220 * @brief Closes the thread.
221 *
222 * Closes the thread object and starts the thread termination sequence.  In
223 * case the executing thread is not terminated, then this function waits until
224 * the terminating thread reached the zombie state.
225 */
226void _Thread_Close( Thread_Control *the_thread, Thread_Control *executing );
227
228/**
229 *  @brief Removes any set states for @a the_thread.
230 *
231 *  This routine removes any set states for @a the_thread.  It performs
232 *  any necessary scheduling operations including the selection of
233 *  a new heir thread.
234 *
235 *  - INTERRUPT LATENCY:
236 *    + ready chain
237 *    + select heir
238 */
239void _Thread_Ready(
240  Thread_Control *the_thread
241);
242
243/**
244 *  @brief Clears the indicated STATES for @a the_thread.
245 *
246 *  This routine clears the indicated STATES for @a the_thread.  It performs
247 *  any necessary scheduling operations including the selection of
248 *  a new heir thread.
249 *
250 *  - INTERRUPT LATENCY:
251 *    + priority map
252 *    + select heir
253 */
254void _Thread_Clear_state(
255  Thread_Control *the_thread,
256  States_Control  state
257);
258
259/**
260 *  @brief Sets the indicated @a state for @a the_thread.
261 *
262 *  This routine sets the indicated @a state for @a the_thread.  It performs
263 *  any necessary scheduling operations including the selection of
264 *  a new heir thread.
265 *
266 *  @param[in] the_thread is the thread to set the state for.
267 *  @param[in] state is the state to set the_thread to.
268 *
269 *  - INTERRUPT LATENCY:
270 *   + ready chain
271 *   + select map
272 */
273void _Thread_Set_state(
274  Thread_Control *the_thread,
275  States_Control  state
276);
277
278/**
279 *  @brief Initializes enviroment for a thread.
280 *
281 *  This routine initializes the context of @a the_thread to its
282 *  appropriate starting state.
283 *
284 *  @param[in] the_thread is the pointer to the thread control block.
285 */
286void _Thread_Load_environment(
287  Thread_Control *the_thread
288);
289
290/**
291 *  @brief Wrapper function for all threads.
292 *
293 *  This routine is the wrapper function for all threads.  It is
294 *  the starting point for all threads.  The user provided thread
295 *  entry point is invoked by this routine.  Operations
296 *  which must be performed immediately before and after the user's
297 *  thread executes are found here.
298 *
299 *  @note On entry, it is assumed all interrupts are blocked and that this
300 *  routine needs to set the initial isr level.  This may or may not
301 *  actually be needed by the context switch routine and as a result
302 *  interrupts may already be at there proper level.  Either way,
303 *  setting the initial isr level properly here is safe.
304 */
305void _Thread_Handler( void );
306
307/**
308 * @brief Executes the global constructors and then restarts itself as the
309 * first initialization thread.
310 *
311 * The first initialization thread is the first RTEMS initialization task or
312 * the first POSIX initialization thread in case no RTEMS initialization tasks
313 * are present.
314 */
315void *_Thread_Global_construction( void );
316
317/**
318 *  @brief Ended the delay of a thread.
319 *
320 *  This routine is invoked when a thread must be unblocked at the
321 *  end of a time based delay (i.e. wake after or wake when).
322 *  It is called by the watchdog handler.
323 *
324 *  @param[in] id is the thread id
325 */
326void _Thread_Delay_ended(
327  Objects_Id  id,
328  void       *ignored
329);
330
331/**
332 *  @brief Change the priority of a thread.
333 *
334 *  This routine changes the current priority of @a the_thread to
335 *  @a new_priority.  It performs any necessary scheduling operations
336 *  including the selection of a new heir thread.
337 *
338 *  @param[in] the_thread is the thread to change
339 *  @param[in] new_priority is the priority to set @a the_thread to
340 *  @param[in] prepend_it is a switch to prepend the thread
341 */
342void _Thread_Change_priority (
343  Thread_Control   *the_thread,
344  Priority_Control  new_priority,
345  bool              prepend_it
346);
347
348/**
349 *  @brief Set thread priority.
350 *
351 *  This routine updates the priority related fields in the_thread
352 *  control block to indicate the current priority is now new_priority.
353 */
354void _Thread_Set_priority(
355  Thread_Control   *the_thread,
356  Priority_Control  new_priority
357);
358
359/**
360 *  This routine updates the related suspend fields in the_thread
361 *  control block to indicate the current nested level.
362 */
363#define _Thread_Suspend( _the_thread ) \
364        _Thread_Set_state( _the_thread, STATES_SUSPENDED )
365
366/**
367 *  This routine updates the related suspend fields in the_thread
368 *  control block to indicate the current nested level.  A force
369 *  parameter of true will force a resume and clear the suspend count.
370 */
371#define _Thread_Resume( _the_thread ) \
372        _Thread_Clear_state( _the_thread, STATES_SUSPENDED )
373
374/**
375 *  @brief Maps thread Id to a TCB pointer.
376 *
377 *  This function maps thread IDs to thread control
378 *  blocks.  If ID corresponds to a local thread, then it
379 *  returns the_thread control pointer which maps to ID
380 *  and @a location is set to OBJECTS_LOCAL.  If the thread ID is
381 *  global and resides on a remote node, then location is set
382 *  to OBJECTS_REMOTE, and the_thread is undefined.
383 *  Otherwise, location is set to OBJECTS_ERROR and
384 *  the_thread is undefined.
385 *
386 *  @param[in] id is the id of the thread.
387 *  @param[in] location is the location of the block.
388 *
389 *  @note  The performance of many RTEMS services depends upon
390 *         the quick execution of the "good object" path in this
391 *         routine.  If there is a possibility of saving a few
392 *         cycles off the execution time, this routine is worth
393 *         further optimization attention.
394 */
395Thread_Control *_Thread_Get (
396  Objects_Id         id,
397  Objects_Locations *location
398);
399
400/**
401 *  @brief Cancel a blocking operation due to ISR.
402 *
403 *  This method is used to cancel a blocking operation that was
404 *  satisfied from an ISR while the thread executing was in the
405 *  process of blocking.
406 *
407 *  This method will restore the previous ISR disable level during the cancel
408 *  operation.  Thus it is an implicit _ISR_Enable().
409 *
410 *  @param[in] sync_state is the synchronization state
411 *  @param[in] the_thread is the thread whose blocking is canceled
412 *  @param[in] level is the previous ISR disable level
413 *
414 *  @note This is a rare routine in RTEMS.  It is called with
415 *        interrupts disabled and only when an ISR completed
416 *        a blocking condition in process.
417 */
418void _Thread_blocking_operation_Cancel(
419  Thread_blocking_operation_States  sync_state,
420  Thread_Control                   *the_thread,
421  ISR_Level                         level
422);
423
424/**
425 *  @brief Finalize a blocking operation.
426 *
427 *  This method is used to finalize a blocking operation that was
428 *  satisfied. It may be used with thread queues or any other synchronization
429 *  object that uses the blocking states and watchdog times for timeout.
430 *
431 *  This method will restore the previous ISR disable level during the cancel
432 *  operation.  Thus it is an implicit _ISR_Enable().
433 *
434 *  @param[in] the_thread is the thread whose blocking is canceled
435 *  @param[in] level is the previous ISR disable level
436 */
437void _Thread_blocking_operation_Finalize(
438  Thread_Control                   *the_thread,
439  ISR_Level                         level
440);
441
442RTEMS_INLINE_ROUTINE Per_CPU_Control *_Thread_Get_CPU(
443  const Thread_Control *thread
444)
445{
446#if defined(RTEMS_SMP)
447  return thread->Scheduler.cpu;
448#else
449  (void) thread;
450
451  return _Per_CPU_Get();
452#endif
453}
454
455RTEMS_INLINE_ROUTINE void _Thread_Set_CPU(
456  Thread_Control *thread,
457  Per_CPU_Control *cpu
458)
459{
460#if defined(RTEMS_SMP)
461  thread->Scheduler.cpu = cpu;
462#else
463  (void) thread;
464  (void) cpu;
465#endif
466}
467
468/**
469 * This function returns true if the_thread is the currently executing
470 * thread, and false otherwise.
471 */
472
473RTEMS_INLINE_ROUTINE bool _Thread_Is_executing (
474  const Thread_Control *the_thread
475)
476{
477  return ( the_thread == _Thread_Executing );
478}
479
480#if defined(RTEMS_SMP)
481/**
482 * @brief Returns @true in case the thread executes currently on some processor
483 * in the system, otherwise @a false.
484 *
485 * Do not confuse this with _Thread_Is_executing() which checks only the
486 * current processor.
487 */
488RTEMS_INLINE_ROUTINE bool _Thread_Is_executing_on_a_processor(
489  const Thread_Control *the_thread
490)
491{
492  return _CPU_Context_Get_is_executing( &the_thread->Registers );
493}
494#endif
495
496/**
497 * @brief Returns @true and sets time_of_context_switch to the the
498 * time of the last context switch when the thread is currently executing
499 * in the system, otherwise @a false.
500 */
501RTEMS_INLINE_ROUTINE bool _Thread_Get_time_of_last_context_switch(
502  Thread_Control    *the_thread,
503  Timestamp_Control *time_of_context_switch
504)
505{
506  bool retval = false;
507
508  _Thread_Disable_dispatch();
509  #ifndef RTEMS_SMP
510    if ( _Thread_Executing->Object.id == the_thread->Object.id ) {
511      *time_of_context_switch = _Thread_Time_of_last_context_switch;
512      retval = true;
513    }
514  #else
515    if ( _Thread_Is_executing_on_a_processor( the_thread ) ) {
516      *time_of_context_switch =
517        _Thread_Get_CPU( the_thread )->time_of_last_context_switch;
518      retval = true;
519    }
520  #endif
521  _Thread_Enable_dispatch();
522  return retval;
523}
524
525
526/**
527 * This function returns true if the_thread is the heir
528 * thread, and false otherwise.
529 */
530
531RTEMS_INLINE_ROUTINE bool _Thread_Is_heir (
532  const Thread_Control *the_thread
533)
534{
535  return ( the_thread == _Thread_Heir );
536}
537
538/**
539 * This routine clears any blocking state for the_thread.  It performs
540 * any necessary scheduling operations including the selection of
541 * a new heir thread.
542 */
543
544RTEMS_INLINE_ROUTINE void _Thread_Unblock (
545  Thread_Control *the_thread
546)
547{
548  _Thread_Clear_state( the_thread, STATES_BLOCKED );
549}
550
551/**
552 * This routine resets the current context of the calling thread
553 * to that of its initial state.
554 */
555
556RTEMS_INLINE_ROUTINE void _Thread_Restart_self( Thread_Control *executing )
557{
558#if defined(RTEMS_SMP)
559  ISR_Level level;
560
561  _Giant_Release();
562
563  _ISR_Disable_without_giant( level );
564  ( void ) level;
565#endif
566
567#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
568  if ( executing->fp_context != NULL )
569    _Context_Restore_fp( &executing->fp_context );
570#endif
571
572  _CPU_Context_Restart_self( &executing->Registers );
573}
574
575/**
576 * This function returns true if the floating point context of
577 * the_thread is currently loaded in the floating point unit, and
578 * false otherwise.
579 */
580
581#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
582RTEMS_INLINE_ROUTINE bool _Thread_Is_allocated_fp (
583  const Thread_Control *the_thread
584)
585{
586  return ( the_thread == _Thread_Allocated_fp );
587}
588#endif
589
590/*
591 *  If the CPU has hardware floating point, then we must address saving
592 *  and restoring it as part of the context switch.
593 *
594 *  The second conditional compilation section selects the algorithm used
595 *  to context switch between floating point tasks.  The deferred algorithm
596 *  can be significantly better in a system with few floating point tasks
597 *  because it reduces the total number of save and restore FP context
598 *  operations.  However, this algorithm can not be used on all CPUs due
599 *  to unpredictable use of FP registers by some compilers for integer
600 *  operations.
601 */
602
603RTEMS_INLINE_ROUTINE void _Thread_Save_fp( Thread_Control *executing )
604{
605#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
606#if ( CPU_USE_DEFERRED_FP_SWITCH != TRUE )
607  if ( executing->fp_context != NULL )
608    _Context_Save_fp( &executing->fp_context );
609#endif
610#endif
611}
612
613RTEMS_INLINE_ROUTINE void _Thread_Restore_fp( Thread_Control *executing )
614{
615#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
616#if ( CPU_USE_DEFERRED_FP_SWITCH == TRUE )
617  if ( (executing->fp_context != NULL) &&
618       !_Thread_Is_allocated_fp( executing ) ) {
619    if ( _Thread_Allocated_fp != NULL )
620      _Context_Save_fp( &_Thread_Allocated_fp->fp_context );
621    _Context_Restore_fp( &executing->fp_context );
622    _Thread_Allocated_fp = executing;
623  }
624#else
625  if ( executing->fp_context != NULL )
626    _Context_Restore_fp( &executing->fp_context );
627#endif
628#endif
629}
630
631/**
632 * This routine is invoked when the currently loaded floating
633 * point context is now longer associated with an active thread.
634 */
635
636#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
637RTEMS_INLINE_ROUTINE void _Thread_Deallocate_fp( void )
638{
639  _Thread_Allocated_fp = NULL;
640}
641#endif
642
643/**
644 * This function returns true if dispatching is disabled, and false
645 * otherwise.
646 */
647
648RTEMS_INLINE_ROUTINE bool _Thread_Is_context_switch_necessary( void )
649{
650  return ( _Thread_Dispatch_necessary );
651}
652
653/**
654 * This function returns true if the_thread is NULL and false otherwise.
655 */
656
657RTEMS_INLINE_ROUTINE bool _Thread_Is_null (
658  const Thread_Control *the_thread
659)
660{
661  return ( the_thread == NULL );
662}
663
664/**
665 * @brief Is proxy blocking.
666 *
667 * status which indicates that a proxy is blocking, and false otherwise.
668 */
669RTEMS_INLINE_ROUTINE bool _Thread_Is_proxy_blocking (
670  uint32_t   code
671)
672{
673  return (code == THREAD_STATUS_PROXY_BLOCKING);
674}
675
676RTEMS_INLINE_ROUTINE uint32_t _Thread_Get_maximum_internal_threads(void)
677{
678  /* Idle threads */
679  uint32_t maximum_internal_threads =
680    rtems_configuration_get_maximum_processors();
681
682  /* MPCI thread */
683#if defined(RTEMS_MULTIPROCESSING)
684  if ( _System_state_Is_multiprocessing ) {
685    ++maximum_internal_threads;
686  }
687#endif
688
689  return maximum_internal_threads;
690}
691
692RTEMS_INLINE_ROUTINE Thread_Control *_Thread_Internal_allocate( void )
693{
694  return (Thread_Control *)
695    _Objects_Allocate_unprotected( &_Thread_Internal_information );
696}
697
698RTEMS_INLINE_ROUTINE void _Thread_Request_dispatch_if_executing(
699  Thread_Control *thread
700)
701{
702#if defined(RTEMS_SMP)
703  if ( _Thread_Is_executing_on_a_processor( thread ) ) {
704    const Per_CPU_Control *cpu_of_executing = _Per_CPU_Get();
705    Per_CPU_Control *cpu_of_thread = _Thread_Get_CPU( thread );
706
707    cpu_of_thread->dispatch_necessary = true;
708
709    if ( cpu_of_executing != cpu_of_thread ) {
710      _Per_CPU_Send_interrupt( cpu_of_thread );
711    }
712  }
713#else
714  (void) thread;
715#endif
716}
717
718RTEMS_INLINE_ROUTINE void _Thread_Signal_notification( Thread_Control *thread )
719{
720  if ( _ISR_Is_in_progress() && _Thread_Is_executing( thread ) ) {
721    _Thread_Dispatch_necessary = true;
722  } else {
723#if defined(RTEMS_SMP)
724    if ( _Thread_Is_executing_on_a_processor( thread ) ) {
725      const Per_CPU_Control *cpu_of_executing = _Per_CPU_Get();
726      Per_CPU_Control *cpu_of_thread = _Thread_Get_CPU( thread );
727
728      if ( cpu_of_executing != cpu_of_thread ) {
729        cpu_of_thread->dispatch_necessary = true;
730        _Per_CPU_Send_interrupt( cpu_of_thread );
731      }
732    }
733#endif
734  }
735}
736
737/**
738 * @brief Gets the heir of the processor and makes it executing.
739 *
740 * The thread dispatch necessary indicator is cleared as a side-effect.
741 *
742 * @return The heir thread.
743 *
744 * @see _Thread_Dispatch(), _Thread_Start_multitasking() and
745 * _Thread_Dispatch_update_heir().
746 */
747RTEMS_INLINE_ROUTINE Thread_Control *_Thread_Get_heir_and_make_it_executing(
748  Per_CPU_Control *cpu_self
749)
750{
751  Thread_Control *heir;
752
753  cpu_self->dispatch_necessary = false;
754
755#if defined( RTEMS_SMP )
756  /*
757   * It is critical that we first update the dispatch necessary and then the
758   * read the heir so that we don't miss an update by
759   * _Thread_Dispatch_update_heir().
760   */
761  _Atomic_Fence( ATOMIC_ORDER_SEQ_CST );
762#endif
763
764  heir = cpu_self->heir;
765  cpu_self->executing = heir;
766
767  return heir;
768}
769
770#if defined( RTEMS_SMP )
771RTEMS_INLINE_ROUTINE void _Thread_Dispatch_update_heir(
772  Per_CPU_Control *cpu_self,
773  Per_CPU_Control *cpu_for_heir,
774  Thread_Control  *heir
775)
776{
777  cpu_for_heir->heir = heir;
778
779  /*
780   * It is critical that we first update the heir and then the dispatch
781   * necessary so that _Thread_Get_heir_and_make_it_executing() cannot miss an
782   * update.
783   */
784  _Atomic_Fence( ATOMIC_ORDER_SEQ_CST );
785
786  /*
787   * Only update the dispatch necessary indicator if not already set to
788   * avoid superfluous inter-processor interrupts.
789   */
790  if ( !cpu_for_heir->dispatch_necessary ) {
791    cpu_for_heir->dispatch_necessary = true;
792
793    if ( cpu_for_heir != cpu_self ) {
794      _Per_CPU_Send_interrupt( cpu_for_heir );
795    }
796  }
797}
798#endif
799
800RTEMS_INLINE_ROUTINE void _Thread_Update_cpu_time_used(
801  Thread_Control *executing,
802  Timestamp_Control *time_of_last_context_switch
803)
804{
805  Timestamp_Control uptime;
806  Timestamp_Control ran;
807
808  _TOD_Get_uptime( &uptime );
809  _Timestamp_Subtract(
810    time_of_last_context_switch,
811    &uptime,
812    &ran
813  );
814  *time_of_last_context_switch = uptime;
815  _Timestamp_Add_to( &executing->cpu_time_used, &ran );
816}
817
818RTEMS_INLINE_ROUTINE void _Thread_Action_control_initialize(
819  Thread_Action_control *action_control
820)
821{
822  _Chain_Initialize_empty( &action_control->Chain );
823}
824
825RTEMS_INLINE_ROUTINE void _Thread_Action_initialize(
826  Thread_Action         *action,
827  Thread_Action_handler  handler
828)
829{
830  action->handler = handler;
831  _Chain_Set_off_chain( &action->Node );
832}
833
834RTEMS_INLINE_ROUTINE Per_CPU_Control *
835  _Thread_Action_ISR_disable_and_acquire_for_executing( ISR_Level *level )
836{
837  Per_CPU_Control *cpu;
838
839  _ISR_Disable_without_giant( *level );
840  cpu = _Per_CPU_Get();
841  _Per_CPU_Acquire( cpu );
842
843  return cpu;
844}
845
846RTEMS_INLINE_ROUTINE Per_CPU_Control *_Thread_Action_ISR_disable_and_acquire(
847  Thread_Control *thread,
848  ISR_Level      *level
849)
850{
851  Per_CPU_Control *cpu;
852
853  _ISR_Disable_without_giant( *level );
854  cpu = _Thread_Get_CPU( thread );
855  _Per_CPU_Acquire( cpu );
856
857  return cpu;
858}
859
860RTEMS_INLINE_ROUTINE void _Thread_Action_release_and_ISR_enable(
861  Per_CPU_Control *cpu,
862  ISR_Level level
863)
864{
865  _Per_CPU_Release_and_ISR_enable( cpu, level );
866}
867
868RTEMS_INLINE_ROUTINE void _Thread_Add_post_switch_action(
869  Thread_Control *thread,
870  Thread_Action  *action
871)
872{
873  Per_CPU_Control *cpu;
874  ISR_Level        level;
875
876  cpu = _Thread_Action_ISR_disable_and_acquire( thread, &level );
877  _Chain_Append_if_is_off_chain_unprotected(
878    &thread->Post_switch_actions.Chain,
879    &action->Node
880  );
881  _Thread_Action_release_and_ISR_enable( cpu, level );
882}
883
884RTEMS_INLINE_ROUTINE bool _Thread_Is_life_restarting(
885  Thread_Life_state life_state
886)
887{
888  return ( life_state & THREAD_LIFE_RESTARTING ) != 0;
889}
890
891RTEMS_INLINE_ROUTINE bool _Thread_Is_life_terminating(
892  Thread_Life_state life_state
893)
894{
895  return ( life_state & THREAD_LIFE_TERMINATING ) != 0;
896}
897
898RTEMS_INLINE_ROUTINE bool _Thread_Is_life_protected(
899  Thread_Life_state life_state
900)
901{
902  return ( life_state & THREAD_LIFE_PROTECTED ) != 0;
903}
904
905RTEMS_INLINE_ROUTINE bool _Thread_Is_life_changing(
906  Thread_Life_state life_state
907)
908{
909  return ( life_state & THREAD_LIFE_RESTARTING_TERMINATING ) != 0;
910}
911
912/**
913 * @brief Returns true if the thread owns resources, and false otherwise.
914 *
915 * Resources are accounted with the Thread_Control::resource_count resource
916 * counter.  This counter is used by semaphore objects for example.
917 *
918 * In addition to the resource counter there is a resource dependency tree
919 * available on SMP configurations.  In case this tree is non-empty, then the
920 * thread owns resources.
921 *
922 * @param[in] the_thread The thread.
923 */
924RTEMS_INLINE_ROUTINE bool _Thread_Owns_resources(
925  const Thread_Control *the_thread
926)
927{
928  bool owns_resources = the_thread->resource_count != 0;
929
930#if defined(RTEMS_SMP)
931  owns_resources = owns_resources
932    || _Resource_Node_owns_resources( &the_thread->Resource_node );
933#endif
934
935  return owns_resources;
936}
937
938RTEMS_INLINE_ROUTINE void _Thread_Debug_set_real_processor(
939  Thread_Control  *the_thread,
940  Per_CPU_Control *cpu
941)
942{
943#if defined(RTEMS_SMP) && defined(RTEMS_DEBUG)
944  the_thread->Scheduler.debug_real_cpu = cpu;
945#else
946  (void) the_thread;
947  (void) cpu;
948#endif
949}
950
951#if !defined(__DYNAMIC_REENT__)
952/**
953 * This routine returns the C library re-enterant pointer.
954 */
955
956RTEMS_INLINE_ROUTINE struct _reent **_Thread_Get_libc_reent( void )
957{
958  return _Thread_libc_reent;
959}
960
961/**
962 * This routine set the C library re-enterant pointer.
963 */
964
965RTEMS_INLINE_ROUTINE void _Thread_Set_libc_reent (
966  struct _reent **libc_reent
967)
968{
969  _Thread_libc_reent = libc_reent;
970}
971#endif
972
973/** @}*/
974
975#ifdef __cplusplus
976}
977#endif
978
979#if defined(RTEMS_MULTIPROCESSING)
980#include <rtems/score/threadmp.h>
981#endif
982
983#endif
984/* end of include file */
Note: See TracBrowser for help on using the repository browser.