OSDN Git Service

2007-09-26 Hristian Kirtchev <kirtchev@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-tporft.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                 GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS                 --
4 --                                                                          --
5 --    S Y S T E M . T A S K _ P R I M I T I V E S . O P E R A T I O N S .   --
6 --               R E G I S T E R _ F O R E I G N _ T H R E A D              --
7 --                                                                          --
8 --                                B o d y                                   --
9 --                                                                          --
10 --          Copyright (C) 2002-2007, Free Software Foundation, Inc.         --
11 --                                                                          --
12 -- GNARL is free software; you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNARL; see file COPYING.  If not, write --
20 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
21 -- Boston, MA 02110-1301, USA.                                              --
22 --                                                                          --
23 -- As a special exception,  if other files  instantiate  generics from this --
24 -- unit, or you link  this unit with other files  to produce an executable, --
25 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
26 -- covered  by the  GNU  General  Public  License.  This exception does not --
27 -- however invalidate  any other reasons why  the executable file  might be --
28 -- covered by the  GNU Public License.                                      --
29 --                                                                          --
30 -- GNARL was developed by the GNARL team at Florida State University.       --
31 -- Extensive contributions were provided by Ada Core Technologies, Inc.     --
32 --                                                                          --
33 ------------------------------------------------------------------------------
34
35 with System.Task_Info;
36 --  Use for Unspecified_Task_Info
37
38 with System.Soft_Links;
39 --  used to initialize TSD for a C thread, in function Self
40
41 separate (System.Task_Primitives.Operations)
42 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id is
43    Local_ATCB : aliased Ada_Task_Control_Block (0);
44    Self_Id    : Task_Id;
45    Succeeded  : Boolean;
46
47 begin
48    --  This section is tricky. We must not call anything that might require
49    --  an ATCB, until the new ATCB is in place. In order to get an ATCB
50    --  immediately, we fake one, so that it is then possible to e.g allocate
51    --  memory (which might require accessing self).
52
53    --  Record this as the Task_Id for the thread
54
55    Local_ATCB.Common.LL.Thread := Thread;
56    Local_ATCB.Common.Current_Priority := System.Priority'First;
57    Specific.Set (Local_ATCB'Unchecked_Access);
58
59    --  It is now safe to use an allocator
60
61    Self_Id := new Ada_Task_Control_Block (0);
62
63    --  Finish initialization
64
65    Lock_RTS;
66    System.Tasking.Initialize_ATCB
67      (Self_Id, null, Null_Address, Null_Task,
68       Foreign_Task_Elaborated'Access,
69       System.Priority'First, Task_Info.Unspecified_Task_Info, 0, Self_Id,
70       Succeeded);
71    Unlock_RTS;
72    pragma Assert (Succeeded);
73
74    Self_Id.Master_of_Task := 0;
75    Self_Id.Master_Within := Self_Id.Master_of_Task + 1;
76
77    for L in Self_Id.Entry_Calls'Range loop
78       Self_Id.Entry_Calls (L).Self := Self_Id;
79       Self_Id.Entry_Calls (L).Level := L;
80    end loop;
81
82    Self_Id.Common.State := Runnable;
83    Self_Id.Awake_Count := 1;
84
85    Self_Id.Common.Task_Image (1 .. 14) := "foreign thread";
86    Self_Id.Common.Task_Image_Len := 14;
87
88    --  Since this is not an ordinary Ada task, we will start out undeferred
89
90    Self_Id.Deferral_Level := 0;
91
92    System.Soft_Links.Create_TSD (Self_Id.Common.Compiler_Data);
93
94    --  ???
95    --  The following call is commented out to avoid dependence on
96    --  the System.Tasking.Initialization package.
97    --  It seems that if we want Ada.Task_Attributes to work correctly
98    --  for C threads we will need to raise the visibility of this soft
99    --  link to System.Soft_Links.
100    --  We are putting that off until this new functionality is otherwise
101    --  stable.
102
103    --  System.Tasking.Initialization.Initialize_Attributes_Link.all (T);
104
105    Enter_Task (Self_Id);
106
107    return Self_Id;
108 end Register_Foreign_Thread;