Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / gcc / ada / s-tporft.adb
blobbcd7f97fd96320c783620bba1e3e6957e600daf5
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- SYSTEM.TASK_PRIMITIVES.OPERATIONS.REGISTER_FOREIGN_THREAD --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2002-2005, 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 ------------------------------------------------------------------------------
34 with System.Task_Info;
35 -- Use for Unspecified_Task_Info
37 with System.Soft_Links;
38 -- used to initialize TSD for a C thread, in function Self
40 separate (System.Task_Primitives.Operations)
41 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id is
42 Local_ATCB : aliased Ada_Task_Control_Block (0);
43 Self_Id : Task_Id;
44 Succeeded : Boolean;
46 use type Interfaces.C.unsigned;
48 begin
49 -- This section is tricky. We must not call anything that might require
50 -- an ATCB, until the new ATCB is in place. In order to get an ATCB
51 -- immediately, we fake one, so that it is then possible to e.g allocate
52 -- memory (which might require accessing self).
54 -- Record this as the Task_Id for the thread
56 Local_ATCB.Common.LL.Thread := Thread;
57 Local_ATCB.Common.Current_Priority := System.Priority'First;
58 Specific.Set (Local_ATCB'Unchecked_Access);
60 -- It is now safe to use an allocator
62 Self_Id := new Ada_Task_Control_Block (0);
64 -- Finish initialization
66 Lock_RTS;
67 System.Tasking.Initialize_ATCB
68 (Self_Id, null, Null_Address, Null_Task,
69 Foreign_Task_Elaborated'Access,
70 System.Priority'First, Task_Info.Unspecified_Task_Info, 0, Self_Id,
71 Succeeded);
72 Unlock_RTS;
73 pragma Assert (Succeeded);
75 Self_Id.Master_of_Task := 0;
76 Self_Id.Master_Within := Self_Id.Master_of_Task + 1;
78 for L in Self_Id.Entry_Calls'Range loop
79 Self_Id.Entry_Calls (L).Self := Self_Id;
80 Self_Id.Entry_Calls (L).Level := L;
81 end loop;
83 Self_Id.Common.State := Runnable;
84 Self_Id.Awake_Count := 1;
86 Self_Id.Common.Task_Image (1 .. 14) := "foreign thread";
87 Self_Id.Common.Task_Image_Len := 14;
89 -- Since this is not an ordinary Ada task, we will start out undeferred
91 Self_Id.Deferral_Level := 0;
93 System.Soft_Links.Create_TSD (Self_Id.Common.Compiler_Data);
95 -- ???
96 -- The following call is commented out to avoid dependence on
97 -- the System.Tasking.Initialization package.
98 -- It seems that if we want Ada.Task_Attributes to work correctly
99 -- for C threads we will need to raise the visibility of this soft
100 -- link to System.Soft_Links.
101 -- We are putting that off until this new functionality is otherwise
102 -- stable.
104 -- System.Tasking.Initialization.Initialize_Attributes_Link.all (T);
106 Enter_Task (Self_Id);
108 return Self_Id;
109 end Register_Foreign_Thread;