source: rtems/cpukit/rtems/src/scheduleraddprocessor.c @ 6378978

5
Last change on this file since 6378978 was 7097962, checked in by Sebastian Huber <sebastian.huber@…>, on 08/29/18 at 07:43:44

score: Add thread pin/unpin support

Add support to temporarily pin a thread to its current processor. This
may be used to access per-processor data structures in critical sections
with enabled thread dispatching, e.g. a pinned thread is allowed to
block.

Update #3508.

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * Copyright (c) 2016 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#if HAVE_CONFIG_H
16#include "config.h"
17#endif
18
19#include <rtems/rtems/tasks.h>
20#include <rtems/score/assert.h>
21#include <rtems/score/schedulerimpl.h>
22#include <rtems/config.h>
23
24rtems_status_code rtems_scheduler_add_processor(
25  rtems_id scheduler_id,
26  uint32_t cpu_index
27)
28{
29  uint32_t                 scheduler_index;
30#if defined(RTEMS_SMP)
31  Per_CPU_Control         *cpu;
32  rtems_status_code        status;
33#endif
34
35  scheduler_index = _Scheduler_Get_index_by_id( scheduler_id );
36
37  if ( scheduler_index >= _Scheduler_Count ) {
38    return RTEMS_INVALID_ID;
39  }
40
41  if ( cpu_index >= rtems_configuration_get_maximum_processors() ) {
42    return RTEMS_NOT_CONFIGURED;
43  }
44
45#if defined(RTEMS_SMP)
46  cpu = _Per_CPU_Get_by_index( cpu_index );
47
48  if ( _Scheduler_Initial_assignments[ cpu_index ].scheduler == NULL ) {
49    return RTEMS_NOT_CONFIGURED;
50  }
51
52  if ( !_Per_CPU_Is_processor_online( cpu ) ) {
53    return RTEMS_INCORRECT_STATE;
54  }
55
56  _Objects_Allocator_lock();
57
58  if ( cpu->Scheduler.control == NULL ) {
59    const Scheduler_Control *scheduler;
60    Scheduler_Context       *scheduler_context;
61    Priority_Control         idle_priority;
62    Thread_Control          *idle;
63    Scheduler_Node          *scheduler_node;
64    ISR_lock_Context         lock_context;
65    Per_CPU_Control         *cpu_self;
66
67    scheduler = &_Scheduler_Table[ scheduler_index ];
68    scheduler_context = _Scheduler_Get_context( scheduler );
69    idle_priority =
70      _Scheduler_Map_priority( scheduler, scheduler->maximum_priority );
71
72    idle = cpu->Scheduler.idle_if_online_and_unused;
73    _Assert( idle != NULL );
74    cpu->Scheduler.idle_if_online_and_unused = NULL;
75
76    idle->Scheduler.home_scheduler = scheduler;
77    idle->Start.initial_priority = idle_priority;
78    scheduler_node =
79      _Thread_Scheduler_get_node_by_index( idle, scheduler_index );
80    _Priority_Node_set_priority( &idle->Real_priority, idle_priority );
81    _Priority_Initialize_one(
82      &scheduler_node->Wait.Priority,
83      &idle->Real_priority
84    );
85    _Assert( _Chain_Is_empty( &idle->Scheduler.Wait_nodes ) );
86    _Chain_Initialize_one(
87      &idle->Scheduler.Wait_nodes,
88      &scheduler_node->Thread.Wait_node
89    );
90    _Assert( _Chain_Is_empty( &idle->Scheduler.Scheduler_nodes ) );
91    _Chain_Initialize_one(
92      &idle->Scheduler.Scheduler_nodes,
93      &scheduler_node->Thread.Scheduler_node.Chain
94    );
95
96    _ISR_lock_ISR_disable( &lock_context );
97    _Scheduler_Acquire_critical( scheduler, &lock_context );
98    _Processor_mask_Set( &scheduler_context->Processors, cpu_index );
99    cpu->Scheduler.control = scheduler;
100    cpu->Scheduler.context = scheduler_context;
101    ( *scheduler->Operations.add_processor )( scheduler, idle );
102    cpu_self = _Thread_Dispatch_disable_critical( &lock_context );
103    _Scheduler_Release_critical( scheduler, &lock_context );
104    _ISR_lock_ISR_enable( &lock_context );
105    _Thread_Dispatch_direct( cpu_self );
106    status = RTEMS_SUCCESSFUL;
107  } else {
108    status = RTEMS_RESOURCE_IN_USE;
109  }
110
111  _Objects_Allocator_unlock();
112  return status;
113#else
114  return RTEMS_RESOURCE_IN_USE;
115#endif
116}
Note: See TracBrowser for help on using the repository browser.