Skip to content

Commit f04cbf7

Browse files
committed
* common/gcc12/environment_task.adb: if the link includes a symbol
_environment_task_secondary_stack_size, use this as the stack size: otherwise, use the default (see System.Parameters). If it is specified, allocate as much space as required (including that for the header info in an SS_Stack) using System.Memory. The usual case for setting the size may well be when a secondary stack isn't required; you would have thought we could create a single instance and use that, but this package can't use elaboration code (it actually performs elaboration! when Environment_Task calls main). * common/gnat-ce-2020/environment_task.adb: likewise. * common/gcc12/s-tarest.adb (Create_Restricted_Task): new block Calculate_Secondary_Stack_Size. Checks configuration issues: o if the address is null, the secondary stack is to be carved out of the primary stack; check it's less (this can happen if the Default_Secondary_Stack_Size is specified, see System.Parameters). o If the address isn't null, the size must be specified. * common/gnat-ce-2020/s-tarest.adb: likewise.
1 parent 818a47a commit f04cbf7

File tree

4 files changed

+182
-18
lines changed

4 files changed

+182
-18
lines changed

common/gcc12/environment_task.adb

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- Copyright (C) 2016-2018 Free Software Foundation, Inc.
1+
-- Copyright (C) 2016-2022 Free Software Foundation, Inc.
22
--
33
-- This file is part of the Cortex GNAT RTS project. This file is
44
-- free software; you can redistribute it and/or modify it under
@@ -18,7 +18,10 @@
1818
-- program; see the files COPYING3 and COPYING.RUNTIME respectively.
1919
-- If not, see <http://www.gnu.org/licenses/>.
2020

21+
with System.Address_To_Access_Conversions;
22+
with System.Memory;
2123
with System.Parameters;
24+
with System.Secondary_Stack;
2225
with System.Tasking.Restricted.Stages;
2326
with System.Task_Info;
2427

@@ -54,22 +57,70 @@ package body Environment_Task is
5457
External_Name => "_environment_task_storage_size";
5558
pragma Weak_External (Environment_Task_Storage_Size);
5659

60+
-- If the link includes a symbol _environment_task_secondary_stack_size,
61+
-- use this as the storage size: otherwise, use the default (10%
62+
-- of the task storage size).
63+
Environment_Task_Secondary_Stack_Size : constant System.Parameters.Size_Type
64+
with
65+
Import,
66+
Convention => Ada,
67+
External_Name => "_environment_task_secondary_stack_size";
68+
pragma Weak_External (Environment_Task_Secondary_Stack_Size);
69+
5770
procedure Create is
5871
-- Will be overwritten by binder-generated code if the main
5972
-- program has pragma Priority.
6073
Main_Priority : Integer;
6174
pragma Import (C, Main_Priority, "__gl_main_priority");
75+
76+
-- Handling the secondary stack size & allocation; these are
77+
-- the default settings, the secondary stack is allocated from
78+
-- the task stack.
79+
Sec_Stack_Address : System.Address := System.Null_Address;
80+
Sec_Stack_Size : System.Parameters.Size_Type
81+
:= System.Parameters.Unspecified_Size;
82+
83+
-- A secondary stack of size s consists of management data
84+
-- followed by s bytes of actual stack.
85+
subtype Null_Secondary_Stack is System.Secondary_Stack.SS_Stack (0);
86+
Basic_Secondary_Stack_Size : constant System.Parameters.Size_Type
87+
:= Null_Secondary_Stack'Max_Size_In_Storage_Elements;
88+
89+
package Stack_Address_Conversions
90+
is new System.Address_To_Access_Conversions
91+
(Object => System.Secondary_Stack.SS_Stack);
92+
6293
use type System.Address;
94+
use type System.Parameters.Size_Type;
6395
begin
96+
-- If the secondary stack size is specified, allocate it (in
97+
-- heap; the compiler would have allocated it in BSS for a
98+
-- source-language task).
99+
if Environment_Task_Secondary_Stack_Size'Address /=
100+
System.Null_Address
101+
then
102+
Sec_Stack_Size := Environment_Task_Secondary_Stack_Size;
103+
Sec_Stack_Address
104+
:= System.Memory.Alloc
105+
(System.Memory.size_t (Sec_Stack_Size
106+
+ Basic_Secondary_Stack_Size));
107+
if Sec_Stack_Address = System.Null_Address then
108+
raise Storage_Error with
109+
"couldn't allocate secondary stack for environment task";
110+
end if;
111+
end if;
112+
64113
System.Tasking.Restricted.Stages.Create_Restricted_Task
65114
(Priority => Main_Priority,
66115
Stack_Address => System.Null_Address,
67116
Size =>
68117
(if Environment_Task_Storage_Size'Address = System.Null_Address
69118
then 1536
70119
else Environment_Task_Storage_Size),
71-
Sec_Stack_Address => null,
72-
Secondary_Stack_Size => System.Parameters.Unspecified_Size,
120+
Sec_Stack_Address =>
121+
Stack_Address_Conversions.To_Pointer (Sec_Stack_Address)
122+
.all'Unchecked_Access,
123+
Secondary_Stack_Size => Sec_Stack_Size,
73124
Task_Info => System.Task_Info.Unspecified_Task_Info,
74125
CPU => System.Tasking.Unspecified_CPU,
75126
State => Environment_Task'Access,

common/gcc12/s-tarest.adb

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
-- --
77
-- B o d y --
88
-- --
9-
-- Copyright (C) 2016-2018, 2020 Free Software Foundation, Inc. --
9+
-- Copyright (C) 2016-2022 Free Software Foundation, Inc. --
1010
-- --
1111
-- GNARL is free software; you can redistribute it and/or modify it under --
1212
-- terms of the GNU General Public License as published by the Free Soft- --
@@ -138,16 +138,50 @@ package body System.Tasking.Restricted.Stages is
138138

139139
Actual_Stack_Size : constant System.Parameters.Size_Type :=
140140
System.Parameters.Adjust_Storage_Size (Size);
141+
Actual_Secondary_Stack_Size : System.Parameters.Size_Type;
141142

142143
Wrapper_Parameter_Address : constant System.Address :=
143144
Memory.Alloc (Parameters'Max_Size_In_Storage_Elements);
144145
Wrapper_Parameter_Access :
145146
constant Parameters_Conversion.Object_Pointer :=
146147
Parameters_Conversion.To_Pointer (Wrapper_Parameter_Address);
147148

148-
use type System.Parameters.Size_Type;
149149
use type FreeRTOS.Tasks.Task_Handle;
150150
begin
151+
152+
Calculate_Secondary_Stack_Size :
153+
declare
154+
use type System.Secondary_Stack.SS_Stack_Ptr;
155+
use type System.Parameters.Size_Type;
156+
begin
157+
if Sec_Stack_Address = null then
158+
if Secondary_Stack_Size = System.Parameters.Unspecified_Size
159+
then
160+
-- The Default_Secondary_Stack_Size if non-zero, or
161+
-- 10% of the task's stack size
162+
Actual_Secondary_Stack_Size :=
163+
System.Parameters.Secondary_Stack_Size (Actual_Stack_Size);
164+
if Actual_Secondary_Stack_Size > Actual_Stack_Size then
165+
raise Program_Error with
166+
"secondary stack larger than primary stack";
167+
end if;
168+
else
169+
raise Program_Error with
170+
"compiler should have allocated secondary stack";
171+
end if;
172+
else
173+
-- The programmer has specified the secondary stack size,
174+
-- and the compiler has allocated space for it in BSS
175+
if Secondary_Stack_Size = System.Parameters.Unspecified_Size
176+
then
177+
raise Program_Error with
178+
"compiler should have specified secondary stack size";
179+
else
180+
Actual_Secondary_Stack_Size := Secondary_Stack_Size;
181+
end if;
182+
end if;
183+
end Calculate_Secondary_Stack_Size;
184+
151185
if Wrapper_Parameter_Address = System.Null_Address then
152186
raise Storage_Error with "couldn't allocate task wrapper";
153187
end if;
@@ -156,10 +190,7 @@ package body System.Tasking.Restricted.Stages is
156190
Task_Proc => State,
157191
Discriminants => Discriminants,
158192
SStack_Addr => Sec_Stack_Address,
159-
SStack_Size =>
160-
(if Secondary_Stack_Size = System.Parameters.Unspecified_Size
161-
then System.Parameters.Secondary_Stack_Size (Actual_Stack_Size)
162-
else Secondary_Stack_Size)); -- don't think this will happen?
193+
SStack_Size => Actual_Secondary_Stack_Size);
163194

164195
Created_Task.Common.Base_Priority := (if Priority = Unspecified_Priority
165196
then System.Default_Priority

common/gnat-ce-2020/environment_task.adb

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- Copyright (C) 2016-2018 Free Software Foundation, Inc.
1+
-- Copyright (C) 2016-2022 Free Software Foundation, Inc.
22
--
33
-- This file is part of the Cortex GNAT RTS project. This file is
44
-- free software; you can redistribute it and/or modify it under
@@ -18,7 +18,10 @@
1818
-- program; see the files COPYING3 and COPYING.RUNTIME respectively.
1919
-- If not, see <http://www.gnu.org/licenses/>.
2020

21+
with System.Address_To_Access_Conversions;
22+
with System.Memory;
2123
with System.Parameters;
24+
with System.Secondary_Stack;
2225
with System.Tasking.Restricted.Stages;
2326
with System.Task_Info;
2427

@@ -54,22 +57,70 @@ package body Environment_Task is
5457
External_Name => "_environment_task_storage_size";
5558
pragma Weak_External (Environment_Task_Storage_Size);
5659

60+
-- If the link includes a symbol _environment_task_secondary_stack_size,
61+
-- use this as the storage size: otherwise, use the default (10%
62+
-- of the task storage size).
63+
Environment_Task_Secondary_Stack_Size : constant System.Parameters.Size_Type
64+
with
65+
Import,
66+
Convention => Ada,
67+
External_Name => "_environment_task_secondary_stack_size";
68+
pragma Weak_External (Environment_Task_Secondary_Stack_Size);
69+
5770
procedure Create is
5871
-- Will be overwritten by binder-generated code if the main
5972
-- program has pragma Priority.
6073
Main_Priority : Integer;
6174
pragma Import (C, Main_Priority, "__gl_main_priority");
75+
76+
-- Handling the secondary stack size & allocation; these are
77+
-- the default settings, the secondary stack is allocated from
78+
-- the task stack.
79+
Sec_Stack_Address : System.Address := System.Null_Address;
80+
Sec_Stack_Size : System.Parameters.Size_Type
81+
:= System.Parameters.Unspecified_Size;
82+
83+
-- A secondary stack of size s consists of management data
84+
-- followed by s bytes of actual stack.
85+
subtype Null_Secondary_Stack is System.Secondary_Stack.SS_Stack (0);
86+
Basic_Secondary_Stack_Size : constant System.Parameters.Size_Type
87+
:= Null_Secondary_Stack'Max_Size_In_Storage_Elements;
88+
89+
package Stack_Address_Conversions
90+
is new System.Address_To_Access_Conversions
91+
(Object => System.Secondary_Stack.SS_Stack);
92+
6293
use type System.Address;
94+
use type System.Parameters.Size_Type;
6395
begin
96+
-- If the secondary stack size is specified, allocate it (in
97+
-- heap; the compiler would have allocated it in BSS for a
98+
-- source-language task).
99+
if Environment_Task_Secondary_Stack_Size'Address /=
100+
System.Null_Address
101+
then
102+
Sec_Stack_Size := Environment_Task_Secondary_Stack_Size;
103+
Sec_Stack_Address
104+
:= System.Memory.Alloc
105+
(System.Memory.size_t (Sec_Stack_Size
106+
+ Basic_Secondary_Stack_Size));
107+
if Sec_Stack_Address = System.Null_Address then
108+
raise Storage_Error with
109+
"couldn't allocate secondary stack for environment task";
110+
end if;
111+
end if;
112+
64113
System.Tasking.Restricted.Stages.Create_Restricted_Task
65114
(Priority => Main_Priority,
66115
Stack_Address => System.Null_Address,
67116
Size =>
68117
(if Environment_Task_Storage_Size'Address = System.Null_Address
69118
then 1536
70119
else Environment_Task_Storage_Size),
71-
Sec_Stack_Address => null,
72-
Secondary_Stack_Size => System.Parameters.Unspecified_Size,
120+
Sec_Stack_Address =>
121+
Stack_Address_Conversions.To_Pointer (Sec_Stack_Address)
122+
.all'Unchecked_Access,
123+
Secondary_Stack_Size => Sec_Stack_Size,
73124
Task_Info => System.Task_Info.Unspecified_Task_Info,
74125
CPU => System.Tasking.Unspecified_CPU,
75126
State => Environment_Task'Access,

common/gnat-ce-2020/s-tarest.adb

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
-- --
77
-- B o d y --
88
-- --
9-
-- Copyright (C) 2016-2018, 2020 Free Software Foundation, Inc. --
9+
-- Copyright (C) 2016-2022 Free Software Foundation, Inc. --
1010
-- --
1111
-- GNARL is free software; you can redistribute it and/or modify it under --
1212
-- terms of the GNU General Public License as published by the Free Soft- --
@@ -138,16 +138,50 @@ package body System.Tasking.Restricted.Stages is
138138

139139
Actual_Stack_Size : constant System.Parameters.Size_Type :=
140140
System.Parameters.Adjust_Storage_Size (Size);
141+
Actual_Secondary_Stack_Size : System.Parameters.Size_Type;
141142

142143
Wrapper_Parameter_Address : constant System.Address :=
143144
Memory.Alloc (Parameters'Max_Size_In_Storage_Elements);
144145
Wrapper_Parameter_Access :
145146
constant Parameters_Conversion.Object_Pointer :=
146147
Parameters_Conversion.To_Pointer (Wrapper_Parameter_Address);
147148

148-
use type System.Parameters.Size_Type;
149149
use type FreeRTOS.Tasks.Task_Handle;
150150
begin
151+
152+
Calculate_Secondary_Stack_Size :
153+
declare
154+
use type System.Secondary_Stack.SS_Stack_Ptr;
155+
use type System.Parameters.Size_Type;
156+
begin
157+
if Sec_Stack_Address = null then
158+
if Secondary_Stack_Size = System.Parameters.Unspecified_Size
159+
then
160+
-- The Default_Secondary_Stack_Size if non-zero, or
161+
-- 10% of the task's stack size
162+
Actual_Secondary_Stack_Size :=
163+
System.Parameters.Secondary_Stack_Size (Actual_Stack_Size);
164+
if Actual_Secondary_Stack_Size > Actual_Stack_Size then
165+
raise Program_Error with
166+
"secondary stack larger than primary stack";
167+
end if;
168+
else
169+
raise Program_Error with
170+
"compiler should have allocated secondary stack";
171+
end if;
172+
else
173+
-- The programmer has specified the secondary stack size,
174+
-- and the compiler has allocated space for it in BSS
175+
if Secondary_Stack_Size = System.Parameters.Unspecified_Size
176+
then
177+
raise Program_Error with
178+
"compiler should have specified secondary stack size";
179+
else
180+
Actual_Secondary_Stack_Size := Secondary_Stack_Size;
181+
end if;
182+
end if;
183+
end Calculate_Secondary_Stack_Size;
184+
151185
if Wrapper_Parameter_Address = System.Null_Address then
152186
raise Storage_Error with "couldn't allocate task wrapper";
153187
end if;
@@ -156,10 +190,7 @@ package body System.Tasking.Restricted.Stages is
156190
Task_Proc => State,
157191
Discriminants => Discriminants,
158192
SStack_Addr => Sec_Stack_Address,
159-
SStack_Size =>
160-
(if Secondary_Stack_Size = System.Parameters.Unspecified_Size
161-
then System.Parameters.Secondary_Stack_Size (Actual_Stack_Size)
162-
else Secondary_Stack_Size)); -- don't think this will happen?
193+
SStack_Size => Actual_Secondary_Stack_Size);
163194

164195
Created_Task.Common.Base_Priority := (if Priority = Unspecified_Priority
165196
then System.Default_Priority

0 commit comments

Comments
 (0)