OSDN Git Service

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