OSDN Git Service

PR bootstrap/11932
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-taskin.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS               --
4 --                                                                          --
5 --                        S Y S T E M . T A S K I N G                       --
6 --                                                                          --
7 --                                  B o d y                                 --
8 --                                                                          --
9 --          Copyright (C) 1992-2003, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNARL is free software; you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNARL; see file COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
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 ------------------------------------------------------------------------------
33
34 pragma Polling (Off);
35 --  Turn off polling, we do not want ATC polling to take place during
36 --  tasking operations. It causes infinite loops and other problems.
37
38 with System.Task_Primitives.Operations;
39 --  used for Self
40
41 with Unchecked_Deallocation;
42 --  To recover from failure of ATCB initialization.
43
44 with System.Storage_Elements;
45 --  Needed for initializing Stack_Info.Size
46
47 with System.Parameters;
48 --  Used for Adjust_Storage_Size
49
50 package body System.Tasking is
51
52    package STPO renames System.Task_Primitives.Operations;
53
54    procedure Free is new
55      Unchecked_Deallocation (Ada_Task_Control_Block, Task_ID);
56
57    ----------
58    -- Self --
59    ----------
60
61    function Self return Task_ID renames STPO.Self;
62
63    ---------------------
64    -- Initialize_ATCB --
65    ---------------------
66
67    procedure Initialize_ATCB
68      (Self_ID          : Task_ID;
69       Task_Entry_Point : Task_Procedure_Access;
70       Task_Arg         : System.Address;
71       Parent           : Task_ID;
72       Elaborated       : Access_Boolean;
73       Base_Priority    : System.Any_Priority;
74       Task_Info        : System.Task_Info.Task_Info_Type;
75       Stack_Size       : System.Parameters.Size_Type;
76       T                : in out Task_ID;
77       Success          : out Boolean) is
78    begin
79       T.Common.State := Unactivated;
80
81       --  Initialize T.Common.LL
82
83       STPO.Initialize_TCB (T, Success);
84
85       if not Success then
86          Free (T);
87          return;
88       end if;
89
90       T.Common.Parent := Parent;
91       T.Common.Base_Priority := Base_Priority;
92       T.Common.Current_Priority := 0;
93       T.Common.Call := null;
94       T.Common.Task_Arg := Task_Arg;
95       T.Common.Task_Entry_Point := Task_Entry_Point;
96       T.Common.Activator := Self_ID;
97       T.Common.Wait_Count := 0;
98       T.Common.Elaborated := Elaborated;
99       T.Common.Activation_Failed := False;
100       T.Common.Task_Info := Task_Info;
101
102       if T.Common.Parent = null then
103          --  For the environment task, the adjusted stack size is
104          --  meaningless. For example, an unspecified Stack_Size means
105          --  that the stack size is determined by the environment, or
106          --  can grow dynamically. The Stack_Checking algorithm
107          --  therefore needs to use the requested size, or 0 in
108          --  case of an unknown size.
109
110          T.Common.Compiler_Data.Pri_Stack_Info.Size :=
111             Storage_Elements.Storage_Offset (Stack_Size);
112
113       else
114          T.Common.Compiler_Data.Pri_Stack_Info.Size :=
115            Storage_Elements.Storage_Offset
116              (Parameters.Adjust_Storage_Size (Stack_Size));
117       end if;
118
119       --  Link the task into the list of all tasks.
120
121       T.Common.All_Tasks_Link := All_Tasks_List;
122       All_Tasks_List := T;
123    end Initialize_ATCB;
124
125    Main_Task_Image : constant String := "main_task";
126    --  Image of environment task.
127
128    Main_Priority : Integer;
129    pragma Import (C, Main_Priority, "__gl_main_priority");
130    --  Priority for main task. Note that this is of type Integer, not
131    --  Priority, because we use the value -1 to indicate the default
132    --  main priority, and that is of course not in Priority'range.
133
134    ----------------------------
135    -- Tasking Initialization --
136    ----------------------------
137
138    --  This block constitutes the first part of the initialization of the
139    --  GNARL. This includes creating data structures to make the initial thread
140    --  into the environment task. The last part of the initialization is done
141    --  in System.Tasking.Initialization or System.Tasking.Restricted.Stages.
142    --  All the initializations used to be in Tasking.Initialization, but this
143    --  is no longer possible with the run time simplification (including
144    --  optimized PO and the restricted run time) since one cannot rely on
145    --  System.Tasking.Initialization being present, as was done before.
146
147 begin
148    declare
149       T             : Task_ID;
150       Success       : Boolean;
151       Base_Priority : Any_Priority;
152
153    begin
154       --  Initialize Environment Task
155
156       if Main_Priority = Unspecified_Priority then
157          Base_Priority := Default_Priority;
158       else
159          Base_Priority := Priority (Main_Priority);
160       end if;
161
162       Success := True;
163       T := STPO.New_ATCB (0);
164       Initialize_ATCB
165         (null, null, Null_Address, Null_Task, null, Base_Priority,
166          Task_Info.Unspecified_Task_Info, 0, T, Success);
167       pragma Assert (Success);
168
169       STPO.Initialize (T);
170       STPO.Set_Priority (T, T.Common.Base_Priority);
171       T.Common.State := Runnable;
172       T.Common.Task_Image_Len := Main_Task_Image'Length;
173       T.Common.Task_Image (Main_Task_Image'Range) := Main_Task_Image;
174
175       --  Only initialize the first element since others are not relevant
176       --  in ravenscar mode. Rest of the initialization is done in Init_RTS.
177
178       T.Entry_Calls (1).Self := T;
179    end;
180 end System.Tasking;