1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
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 --
10 -- Copyright (C) 2002-2007, Free Software Foundation, Inc. --
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. --
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. --
30 -- GNARL was developed by the GNARL team at Florida State University. --
31 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
33 ------------------------------------------------------------------------------
35 with System
.Task_Info
;
36 -- Use for Unspecified_Task_Info
38 with System
.Soft_Links
;
39 -- used to initialize TSD for a C thread, in function Self
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);
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).
53 -- Record this as the Task_Id for the thread
55 Local_ATCB
.Common
.LL
.Thread
:= Thread
;
56 Local_ATCB
.Common
.Current_Priority
:= System
.Priority
'First;
57 Specific
.Set
(Local_ATCB
'Unchecked_Access);
59 -- It is now safe to use an allocator
61 Self_Id
:= new Ada_Task_Control_Block
(0);
63 -- Finish initialization
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
,
72 pragma Assert
(Succeeded
);
74 Self_Id
.Master_of_Task
:= 0;
75 Self_Id
.Master_Within
:= Self_Id
.Master_of_Task
+ 1;
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
;
82 Self_Id
.Common
.State
:= Runnable
;
83 Self_Id
.Awake_Count
:= 1;
85 Self_Id
.Common
.Task_Image
(1 .. 14) := "foreign thread";
86 Self_Id
.Common
.Task_Image_Len
:= 14;
88 -- Since this is not an ordinary Ada task, we will start out undeferred
90 Self_Id
.Deferral_Level
:= 0;
92 System
.Soft_Links
.Create_TSD
(Self_Id
.Common
.Compiler_Data
);
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
103 -- System.Tasking.Initialization.Initialize_Attributes_Link.all (T);
105 Enter_Task
(Self_Id
);
108 end Register_Foreign_Thread
;