File : s-bbprot.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . B B . P R O T E C T I O N --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1999-2002 Universidad Politecnica de Madrid --
10 -- Copyright (C) 2003-2005 The European Space Agency --
11 -- Copyright (C) 2003-2016, AdaCore --
12 -- --
13 -- GNARL is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 3, or (at your option) any later ver- --
16 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. --
19 -- --
20 -- --
21 -- --
22 -- --
23 -- --
24 -- You should have received a copy of the GNU General Public License and --
25 -- a copy of the GCC Runtime Library Exception along with this program; --
26 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
27 -- <http://www.gnu.org/licenses/>. --
28 -- --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 -- --
32 -- The port of GNARL to bare board targets was initially developed by the --
33 -- Real-Time Systems Group at the Technical University of Madrid. --
34 -- --
35 ------------------------------------------------------------------------------
36
37 pragma Restrictions (No_Elaboration_Code);
38
39 with System.BB.CPU_Primitives;
40 with System.BB.Parameters;
41 with System.BB.Board_Support;
42 with System.BB.Threads;
43 with System.BB.Time;
44
45 with System.BB.Threads.Queues;
46
47 -- The following pragma Elaborate is anomalous. We generally do not like
48 -- to use pragma Elaborate, since it disconnects the static elaboration
49 -- model checking (and generates a warning when using this model). So
50 -- either replace with Elaborate_All, or document why we need this and
51 -- why it is safe ???
52
53 pragma Warnings (Off);
54 pragma Elaborate (System.BB.Threads.Queues);
55 pragma Warnings (On);
56
57 package body System.BB.Protection is
58
59 ------------------
60 -- Enter_Kernel --
61 ------------------
62
63 procedure Enter_Kernel is
64 begin
65 -- Interrupts are disabled to avoid concurrency problems when modifying
66 -- kernel data. This way, external interrupts cannot be raised.
67
68 CPU_Primitives.Disable_Interrupts;
69 end Enter_Kernel;
70
71 ------------------
72 -- Leave_Kernel --
73 ------------------
74
75 procedure Leave_Kernel is
76 use System.BB.Time;
77 use type System.BB.Threads.Thread_Id;
78 use type System.BB.Threads.Thread_States;
79
80 begin
81 -- Interrupts are always disabled when entering here
82
83 -- Wake up served entry calls
84
85 if Parameters.Multiprocessor
86 and then Wakeup_Served_Entry_Callback /= null
87 then
88 Wakeup_Served_Entry_Callback.all;
89 end if;
90
91 -- The idle task is always runnable, so there is always a task to be
92 -- run.
93
94 -- We need to check whether a context switch is needed
95
96 if Threads.Queues.Context_Switch_Needed then
97
98 -- Perform a context switch because the currently executing thread
99 -- is blocked or it is no longer the one with the highest priority.
100
101 -- Update execution time before context switch
102
103 if Scheduling_Event_Hook /= null then
104 Scheduling_Event_Hook.all;
105 end if;
106
107 CPU_Primitives.Context_Switch;
108 end if;
109
110 -- There is always a running thread (at worst the idle thread)
111
112 pragma Assert (Threads.Queues.Running_Thread.State = Threads.Runnable);
113
114 -- Now we need to set the hardware interrupt masking level equal to the
115 -- software priority of the task that is executing.
116
117 CPU_Primitives.Enable_Interrupts
118 (Threads.Queues.Running_Thread.Active_Priority);
119 end Leave_Kernel;
120
121 end System.BB.Protection;