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-2005, 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);
47 use type Interfaces
.C
.unsigned
;
50 -- This section is tricky. We must not call anything that might require
51 -- an ATCB, until the new ATCB is in place. In order to get an ATCB
52 -- immediately, we fake one, so that it is then possible to e.g allocate
53 -- memory (which might require accessing self).
55 -- Record this as the Task_Id for the thread
57 Local_ATCB
.Common
.LL
.Thread
:= Thread
;
58 Local_ATCB
.Common
.Current_Priority
:= System
.Priority
'First;
59 Specific
.Set
(Local_ATCB
'Unchecked_Access);
61 -- It is now safe to use an allocator
63 Self_Id
:= new Ada_Task_Control_Block
(0);
65 -- Finish initialization
68 System
.Tasking
.Initialize_ATCB
69 (Self_Id
, null, Null_Address
, Null_Task
,
70 Foreign_Task_Elaborated
'Access,
71 System
.Priority
'First, Task_Info
.Unspecified_Task_Info
, 0, Self_Id
,
74 pragma Assert
(Succeeded
);
76 Self_Id
.Master_of_Task
:= 0;
77 Self_Id
.Master_Within
:= Self_Id
.Master_of_Task
+ 1;
79 for L
in Self_Id
.Entry_Calls
'Range loop
80 Self_Id
.Entry_Calls
(L
).Self
:= Self_Id
;
81 Self_Id
.Entry_Calls
(L
).Level
:= L
;
84 Self_Id
.Common
.State
:= Runnable
;
85 Self_Id
.Awake_Count
:= 1;
87 Self_Id
.Common
.Task_Image
(1 .. 14) := "foreign thread";
88 Self_Id
.Common
.Task_Image_Len
:= 14;
90 -- Since this is not an ordinary Ada task, we will start out undeferred
92 Self_Id
.Deferral_Level
:= 0;
94 System
.Soft_Links
.Create_TSD
(Self_Id
.Common
.Compiler_Data
);
97 -- The following call is commented out to avoid dependence on
98 -- the System.Tasking.Initialization package.
99 -- It seems that if we want Ada.Task_Attributes to work correctly
100 -- for C threads we will need to raise the visibility of this soft
101 -- link to System.Soft_Links.
102 -- We are putting that off until this new functionality is otherwise
105 -- System.Tasking.Initialization.Initialize_Attributes_Link.all (T);
107 Enter_Task
(Self_Id
);
110 end Register_Foreign_Thread
;