OSDN Git Service

2008-05-26 Hristian Kirtchev <kirtchev@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-taskin.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                 GNAT 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-2008, 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,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, 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 tasking
36 --  operations. It causes infinite loops and other problems.
37
38 with Ada.Unchecked_Deallocation;
39
40 with System.Task_Primitives.Operations;
41 with System.Storage_Elements;
42
43 package body System.Tasking is
44
45    package STPO renames System.Task_Primitives.Operations;
46
47    ----------------------------
48    -- Free_Entry_Names_Array --
49    ----------------------------
50
51    procedure Free_Entry_Names_Array (Obj : in out Entry_Names_Array) is
52       procedure Free_String is new
53         Ada.Unchecked_Deallocation (String, String_Access);
54    begin
55       for Index in Obj'Range loop
56          Free_String (Obj (Index));
57       end loop;
58    end Free_Entry_Names_Array;
59
60    ---------------------
61    -- Detect_Blocking --
62    ---------------------
63
64    function Detect_Blocking return Boolean is
65       GL_Detect_Blocking : Integer;
66       pragma Import (C, GL_Detect_Blocking, "__gl_detect_blocking");
67       --  Global variable exported by the binder generated file.
68       --  A value equal to 1 indicates that pragma Detect_Blocking is active,
69       --  while 0 is used for the pragma not being present.
70
71    begin
72       return GL_Detect_Blocking = 1;
73    end Detect_Blocking;
74
75    ----------
76    -- Self --
77    ----------
78
79    function Self return Task_Id renames STPO.Self;
80
81    ------------------
82    -- Storage_Size --
83    ------------------
84
85    function Storage_Size (T : Task_Id) return System.Parameters.Size_Type is
86    begin
87       return
88          System.Parameters.Size_Type
89            (T.Common.Compiler_Data.Pri_Stack_Info.Size);
90    end Storage_Size;
91
92    ---------------------
93    -- Initialize_ATCB --
94    ---------------------
95
96    procedure Initialize_ATCB
97      (Self_ID          : Task_Id;
98       Task_Entry_Point : Task_Procedure_Access;
99       Task_Arg         : System.Address;
100       Parent           : Task_Id;
101       Elaborated       : Access_Boolean;
102       Base_Priority    : System.Any_Priority;
103       Task_Info        : System.Task_Info.Task_Info_Type;
104       Stack_Size       : System.Parameters.Size_Type;
105       T                : Task_Id;
106       Success          : out Boolean) is
107    begin
108       T.Common.State := Unactivated;
109
110       --  Initialize T.Common.LL
111
112       STPO.Initialize_TCB (T, Success);
113
114       if not Success then
115          return;
116       end if;
117
118       T.Common.Parent := Parent;
119       T.Common.Base_Priority := Base_Priority;
120       T.Common.Current_Priority := 0;
121       T.Common.Protected_Action_Nesting := 0;
122       T.Common.Call := null;
123       T.Common.Task_Arg := Task_Arg;
124       T.Common.Task_Entry_Point := Task_Entry_Point;
125       T.Common.Activator := Self_ID;
126       T.Common.Wait_Count := 0;
127       T.Common.Elaborated := Elaborated;
128       T.Common.Activation_Failed := False;
129       T.Common.Task_Info := Task_Info;
130       T.Common.Global_Task_Lock_Nesting := 0;
131       T.Common.Fall_Back_Handler := null;
132       T.Common.Specific_Handler  := null;
133
134       if T.Common.Parent = null then
135          --  For the environment task, the adjusted stack size is
136          --  meaningless. For example, an unspecified Stack_Size means
137          --  that the stack size is determined by the environment, or
138          --  can grow dynamically. The Stack_Checking algorithm
139          --  therefore needs to use the requested size, or 0 in
140          --  case of an unknown size.
141
142          T.Common.Compiler_Data.Pri_Stack_Info.Size :=
143             Storage_Elements.Storage_Offset (Stack_Size);
144
145       else
146          T.Common.Compiler_Data.Pri_Stack_Info.Size :=
147            Storage_Elements.Storage_Offset
148              (Parameters.Adjust_Storage_Size (Stack_Size));
149       end if;
150
151       --  Link the task into the list of all tasks
152
153       T.Common.All_Tasks_Link := All_Tasks_List;
154       All_Tasks_List := T;
155    end Initialize_ATCB;
156
157    ----------------
158    -- Initialize --
159    ----------------
160
161    Main_Task_Image : constant String := "main_task";
162    --  Image of environment task
163
164    Main_Priority : Integer;
165    pragma Import (C, Main_Priority, "__gl_main_priority");
166    --  Priority for main task. Note that this is of type Integer, not
167    --  Priority, because we use the value -1 to indicate the default
168    --  main priority, and that is of course not in Priority'range.
169
170    Initialized : Boolean := False;
171    --  Used to prevent multiple calls to Initialize
172
173    procedure Initialize is
174       T             : Task_Id;
175       Base_Priority : Any_Priority;
176
177       Success : Boolean;
178       pragma Warnings (Off, Success);
179
180    begin
181       if Initialized then
182          return;
183       end if;
184
185       Initialized := True;
186
187       --  Initialize Environment Task
188
189       if Main_Priority = Unspecified_Priority then
190          Base_Priority := Default_Priority;
191       else
192          Base_Priority := Priority (Main_Priority);
193       end if;
194
195       Success := True;
196       T := STPO.New_ATCB (0);
197       Initialize_ATCB
198         (null, null, Null_Address, Null_Task, null, Base_Priority,
199          Task_Info.Unspecified_Task_Info, 0, T, Success);
200       pragma Assert (Success);
201
202       STPO.Initialize (T);
203       STPO.Set_Priority (T, T.Common.Base_Priority);
204       T.Common.State := Runnable;
205       T.Common.Task_Image_Len := Main_Task_Image'Length;
206       T.Common.Task_Image (Main_Task_Image'Range) := Main_Task_Image;
207
208       --  Only initialize the first element since others are not relevant
209       --  in ravenscar mode. Rest of the initialization is done in Init_RTS.
210
211       T.Entry_Calls (1).Self := T;
212    end Initialize;
213
214 end System.Tasking;