1 | /* |
---|
2 | * Copyright (c) 2014 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/score/schedulerimpl.h> |
---|
20 | |
---|
21 | typedef struct { |
---|
22 | Thread_Control *root; |
---|
23 | Thread_Control *needs_help; |
---|
24 | } Scheduler_Set_root_context; |
---|
25 | |
---|
26 | RTEMS_INLINE_ROUTINE bool _Scheduler_Set_root_visitor( |
---|
27 | Resource_Node *resource_node, |
---|
28 | void *arg |
---|
29 | ) |
---|
30 | { |
---|
31 | Scheduler_Set_root_context *ctx = arg; |
---|
32 | Thread_Control *root = ctx->root; |
---|
33 | Thread_Control *needs_help = root; |
---|
34 | Thread_Control *offers_help = |
---|
35 | THREAD_RESOURCE_NODE_TO_THREAD( resource_node ); |
---|
36 | const Scheduler_Control *scheduler = _Scheduler_Get_own( offers_help ); |
---|
37 | Thread_Control *needs_help_too; |
---|
38 | |
---|
39 | _Resource_Node_set_root( resource_node, &root->Resource_node ); |
---|
40 | |
---|
41 | needs_help_too = ( *scheduler->Operations.ask_for_help )( |
---|
42 | scheduler, |
---|
43 | offers_help, |
---|
44 | needs_help |
---|
45 | ); |
---|
46 | |
---|
47 | if ( needs_help_too != needs_help && needs_help_too != NULL ) { |
---|
48 | _Assert( ctx->needs_help == NULL ); |
---|
49 | ctx->needs_help = needs_help_too; |
---|
50 | } |
---|
51 | |
---|
52 | return false; |
---|
53 | } |
---|
54 | |
---|
55 | void _Scheduler_Thread_change_resource_root( |
---|
56 | Thread_Control *top, |
---|
57 | Thread_Control *root |
---|
58 | ) |
---|
59 | { |
---|
60 | Scheduler_Set_root_context ctx = { root, NULL }; |
---|
61 | Thread_Control *offers_help = top; |
---|
62 | Scheduler_Node *offers_help_node; |
---|
63 | Thread_Control *offers_help_too; |
---|
64 | ISR_Level level; |
---|
65 | |
---|
66 | _ISR_Disable( level ); |
---|
67 | |
---|
68 | offers_help_node = _Scheduler_Thread_get_node( offers_help ); |
---|
69 | offers_help_too = _Scheduler_Node_get_owner( offers_help_node ); |
---|
70 | |
---|
71 | if ( offers_help != offers_help_too ) { |
---|
72 | _Scheduler_Set_root_visitor( &offers_help_too->Resource_node, &ctx ); |
---|
73 | _Assert( ctx.needs_help == offers_help ); |
---|
74 | ctx.needs_help = NULL; |
---|
75 | } |
---|
76 | |
---|
77 | _Scheduler_Set_root_visitor( &top->Resource_node, &ctx ); |
---|
78 | _Resource_Iterate( &top->Resource_node, _Scheduler_Set_root_visitor, &ctx ); |
---|
79 | |
---|
80 | if ( ctx.needs_help != NULL ) { |
---|
81 | _Scheduler_Ask_for_help( ctx.needs_help ); |
---|
82 | } |
---|
83 | |
---|
84 | _ISR_Enable( level ); |
---|
85 | } |
---|