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

Last change on this file since b3b6d21e was b3b6d21e, checked in by Joel Sherrill <joel@…>, on 02/16/22 at 22:28:59

cpukit/rtems/src/[s-z]*.c: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/**
4 * @file
5 *
6 * @ingroup RTEMSImplClassicScheduler
7 *
8 * @brief This source file contains the implementation of
9 *   rtems_scheduler_add_processor().
10 */
11
12/*
13 * Copyright (c) 2016 embedded brains GmbH.  All rights reserved.
14 *
15 *  embedded brains GmbH
16 *  Dornierstr. 4
17 *  82178 Puchheim
18 *  Germany
19 *  <rtems@embedded-brains.de>
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 *    notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 *    notice, this list of conditions and the following disclaimer in the
28 *    documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
34 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
41 */
42
43#ifdef HAVE_CONFIG_H
44#include "config.h"
45#endif
46
47#include <rtems/rtems/scheduler.h>
48#include <rtems/score/assert.h>
49#include <rtems/score/schedulerimpl.h>
50#include <rtems/config.h>
51
52rtems_status_code rtems_scheduler_add_processor(
53  rtems_id scheduler_id,
54  uint32_t cpu_index
55)
56{
57  uint32_t                 scheduler_index;
58#if defined(RTEMS_SMP)
59  Per_CPU_Control         *cpu;
60  rtems_status_code        status;
61#endif
62
63  scheduler_index = _Scheduler_Get_index_by_id( scheduler_id );
64
65  if ( scheduler_index >= _Scheduler_Count ) {
66    return RTEMS_INVALID_ID;
67  }
68
69  if ( cpu_index >= rtems_configuration_get_maximum_processors() ) {
70    return RTEMS_NOT_CONFIGURED;
71  }
72
73#if defined(RTEMS_SMP)
74  cpu = _Per_CPU_Get_by_index( cpu_index );
75
76  if ( _Scheduler_Initial_assignments[ cpu_index ].scheduler == NULL ) {
77    return RTEMS_NOT_CONFIGURED;
78  }
79
80  if ( !_Per_CPU_Is_processor_online( cpu ) ) {
81    return RTEMS_INCORRECT_STATE;
82  }
83
84  _Objects_Allocator_lock();
85
86  if ( cpu->Scheduler.control == NULL ) {
87    const Scheduler_Control *scheduler;
88    Scheduler_Context       *scheduler_context;
89    Priority_Control         idle_priority;
90    Thread_Control          *idle;
91    Scheduler_Node          *scheduler_node;
92    ISR_lock_Context         lock_context;
93    Per_CPU_Control         *cpu_self;
94
95    scheduler = &_Scheduler_Table[ scheduler_index ];
96    scheduler_context = _Scheduler_Get_context( scheduler );
97    idle_priority =
98      _Scheduler_Map_priority( scheduler, scheduler->maximum_priority );
99
100    idle = cpu->Scheduler.idle_if_online_and_unused;
101    _Assert( idle != NULL );
102    cpu->Scheduler.idle_if_online_and_unused = NULL;
103
104    idle->Scheduler.home_scheduler = scheduler;
105    idle->Start.initial_priority = idle_priority;
106    scheduler_node =
107      _Thread_Scheduler_get_node_by_index( idle, scheduler_index );
108    _Priority_Node_set_priority( &idle->Real_priority, idle_priority );
109    _Priority_Initialize_one(
110      &scheduler_node->Wait.Priority,
111      &idle->Real_priority
112    );
113    _Assert( _Chain_Is_empty( &idle->Scheduler.Wait_nodes ) );
114    _Chain_Initialize_one(
115      &idle->Scheduler.Wait_nodes,
116      &scheduler_node->Thread.Wait_node
117    );
118    _Assert( _Chain_Is_empty( &idle->Scheduler.Scheduler_nodes ) );
119    _Chain_Initialize_one(
120      &idle->Scheduler.Scheduler_nodes,
121      &scheduler_node->Thread.Scheduler_node.Chain
122    );
123
124    _ISR_lock_ISR_disable( &lock_context );
125    _Scheduler_Acquire_critical( scheduler, &lock_context );
126    _Processor_mask_Set( &scheduler_context->Processors, cpu_index );
127    cpu->Scheduler.control = scheduler;
128    cpu->Scheduler.context = scheduler_context;
129    ( *scheduler->Operations.add_processor )( scheduler, idle );
130    cpu_self = _Thread_Dispatch_disable_critical( &lock_context );
131    _Scheduler_Release_critical( scheduler, &lock_context );
132    _ISR_lock_ISR_enable( &lock_context );
133    _Thread_Dispatch_direct( cpu_self );
134    status = RTEMS_SUCCESSFUL;
135  } else {
136    status = RTEMS_RESOURCE_IN_USE;
137  }
138
139  _Objects_Allocator_unlock();
140  return status;
141#else
142  return RTEMS_RESOURCE_IN_USE;
143#endif
144}
Note: See TracBrowser for help on using the repository browser.