OSDN Git Service

New Language: Ada
[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 --                             $Revision: 1.38 $
10 --                                                                          --
11 --             Copyright (C) 1991-2001 Florida State University             --
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 2,  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.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNARL; see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- As a special exception,  if other files  instantiate  generics from this --
25 -- unit, or you link  this unit with other files  to produce an executable, --
26 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
27 -- covered  by the  GNU  General  Public  License.  This exception does not --
28 -- however invalidate  any other reasons why  the executable file  might be --
29 -- covered by the  GNU Public License.                                      --
30 --                                                                          --
31 -- GNARL was developed by the GNARL team at Florida State University. It is --
32 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
33 -- State University (http://www.gnat.com).                                  --
34 --                                                                          --
35 ------------------------------------------------------------------------------
36
37 pragma Polling (Off);
38 --  Turn off polling, we do not want ATC polling to take place during
39 --  tasking operations. It causes infinite loops and other problems.
40
41 with System.Task_Primitives.Operations;
42 --  used for Self
43
44 with Unchecked_Deallocation;
45 --  To recover from failure of ATCB initialization.
46
47 with System.Storage_Elements;
48 --  Needed for initializing Stack_Info.Size
49
50 with System.Parameters;
51 --  Used for Adjust_Storage_Size
52
53 package body System.Tasking is
54
55    package STPO renames System.Task_Primitives.Operations;
56
57    procedure Free is new
58      Unchecked_Deallocation (Ada_Task_Control_Block, Task_ID);
59
60    ----------
61    -- Self --
62    ----------
63
64    function Self return Task_ID renames STPO.Self;
65
66    ---------------------
67    -- Initialize_ATCB --
68    ---------------------
69
70    --  Call this only with abort deferred and holding All_Tasks_L.
71
72    procedure Initialize_ATCB
73      (Self_ID          : Task_ID;
74       Task_Entry_Point : Task_Procedure_Access;
75       Task_Arg         : System.Address;
76       Parent           : Task_ID;
77       Elaborated       : Access_Boolean;
78       Base_Priority    : System.Any_Priority;
79       Task_Info        : System.Task_Info.Task_Info_Type;
80       Stack_Size       : System.Parameters.Size_Type;
81       T                : in out Task_ID;
82       Success          : out Boolean) is
83    begin
84       T.Common.State := Unactivated;
85
86       --  Initialize T.Common.LL
87
88       STPO.Initialize_TCB (T, Success);
89
90       if not Success then
91          Free (T);
92          return;
93       end if;
94
95       T.Common.Parent := Parent;
96       T.Common.Base_Priority := Base_Priority;
97       T.Common.Current_Priority := 0;
98       T.Common.Call := null;
99       T.Common.Task_Arg := Task_Arg;
100       T.Common.Task_Entry_Point := Task_Entry_Point;
101       T.Common.Activator := Self_ID;
102       T.Common.Wait_Count := 0;
103       T.Common.Elaborated := Elaborated;
104       T.Common.Activation_Failed := False;
105       T.Common.Task_Info := Task_Info;
106
107       if T.Common.Parent = null then
108          --  For the environment task, the adjusted stack size is
109          --  meaningless. For example, an unspecified Stack_Size means
110          --  that the stack size is determined by the environment, or
111          --  can grow dynamically. The Stack_Checking algorithm
112          --  therefore needs to use the requested size, or 0 in
113          --  case of an unknown size.
114
115          T.Common.Compiler_Data.Pri_Stack_Info.Size :=
116             Storage_Elements.Storage_Offset (Stack_Size);
117
118       else
119          T.Common.Compiler_Data.Pri_Stack_Info.Size :=
120            Storage_Elements.Storage_Offset
121              (Parameters.Adjust_Storage_Size (Stack_Size));
122       end if;
123
124       --  Link the task into the list of all tasks.
125
126       T.Common.All_Tasks_Link := All_Tasks_List;
127       All_Tasks_List := T;
128    end Initialize_ATCB;
129
130    Main_Task_Image : aliased String := "main_task";
131    --  Declare a global variable to avoid allocating dynamic memory.
132
133    Main_Priority : Priority;
134    pragma Import (C, Main_Priority, "__gl_main_priority");
135
136    ----------------------------
137    -- Tasking Initialization --
138    ----------------------------
139
140    --  This block constitutes the first part of the initialization of the
141    --  GNARL. This includes creating data structures to make the initial thread
142    --  into the environment task. The last part of the initialization is done
143    --  in System.Tasking.Initialization or System.Tasking.Restricted.Stages.
144    --  All the initializations used to be in Tasking.Initialization, but this
145    --  is no longer possible with the run time simplification (including
146    --  optimized PO and the restricted run time) since one cannot rely on
147    --  System.Tasking.Initialization being present, as was done before.
148
149 begin
150    declare
151       T             : Task_ID;
152       Success       : Boolean;
153       Base_Priority : Any_Priority;
154
155    begin
156       --  Initialize Environment Task
157
158       if Main_Priority = Unspecified_Priority then
159          Base_Priority := Default_Priority;
160       else
161          Base_Priority := Main_Priority;
162       end if;
163
164       Success := True;
165       T := STPO.New_ATCB (0);
166       Initialize_ATCB
167         (null, null, Null_Address, Null_Task, null, Base_Priority,
168          Task_Info.Unspecified_Task_Info, 0, T, Success);
169       pragma Assert (Success);
170
171       STPO.Initialize (T);
172       STPO.Set_Priority (T, T.Common.Base_Priority);
173       T.Common.State := Runnable;
174       T.Common.Task_Image := Main_Task_Image'Unrestricted_Access;
175
176       --  Only initialize the first element since others are not relevant
177       --  in ravenscar mode. Rest of the initialization is done in Init_RTS.
178
179       T.Entry_Calls (1).Self := T;
180    end;
181 end System.Tasking;