1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- G N A T . T H R E A D S --
9 -- Copyright (C) 1998-2010, AdaCore --
11 -- GNAT 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 3, or (at your option) any later ver- --
14 -- sion. GNAT 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Ada
.Task_Identification
; use Ada
.Task_Identification
;
33 with System
.Task_Primitives
.Operations
;
35 with System
.Tasking
.Stages
; use System
.Tasking
.Stages
;
36 with System
.OS_Interface
; use System
.OS_Interface
;
37 with System
.Soft_Links
; use System
.Soft_Links
;
38 with Ada
.Unchecked_Conversion
;
40 package body GNAT
.Threads
is
44 package STPO
renames System
.Task_Primitives
.Operations
;
46 type Thread_Id_Ptr
is access all Thread_Id
;
48 pragma Warnings
(Off
);
49 -- The following unchecked conversions are aliasing safe, since they
50 -- are never used to create pointers to improperly aliased data.
52 function To_Addr
is new Ada
.Unchecked_Conversion
(Task_Id
, Address
);
53 function To_Id
is new Ada
.Unchecked_Conversion
(Address
, Task_Id
);
54 function To_Id
is new Ada
.Unchecked_Conversion
(Address
, Tasking
.Task_Id
);
55 function To_Tid
is new Ada
.Unchecked_Conversion
56 (Address
, Ada
.Task_Identification
.Task_Id
);
57 function To_Thread
is new Ada
.Unchecked_Conversion
(Address
, Thread_Id_Ptr
);
61 type Code_Proc
is access procedure (Id
: Address
; Parm
: Void_Ptr
);
69 pragma Priority
(Prio
);
70 pragma Storage_Size
(Stsz
);
75 Code
.all (To_Addr
(Current_Task
), Parm
);
78 type Tptr
is access Thread
;
84 function Create_Thread
88 Prio
: Integer) return System
.Address
92 function To_CP
is new Ada
.Unchecked_Conversion
(Address
, Code_Proc
);
95 TP
:= new Thread
(Size
, Prio
, Parm
, To_CP
(Code
));
96 return To_Addr
(TP
'Identity);
100 -- Register_Thread --
101 ---------------------
103 function Register_Thread
return System
.Address
is
105 return Task_Primitives
.Operations
.Register_Foreign_Thread
.all'Address;
108 -----------------------
109 -- Unregister_Thread --
110 -----------------------
112 procedure Unregister_Thread
is
113 Self_Id
: constant Tasking
.Task_Id
:= Task_Primitives
.Operations
.Self
;
115 Self_Id
.Common
.State
:= Tasking
.Terminated
;
116 Destroy_TSD
(Self_Id
.Common
.Compiler_Data
);
118 end Unregister_Thread
;
120 --------------------------
121 -- Unregister_Thread_Id --
122 --------------------------
124 procedure Unregister_Thread_Id
(Thread
: System
.Address
) is
125 Thr
: constant Thread_Id
:= To_Thread
(Thread
).all;
128 use type Tasking
.Task_Id
;
129 -- This use clause should be removed once a visibility problem
130 -- with the MaRTE run time has been fixed. ???
132 pragma Warnings
(Off
);
133 use type System
.OS_Interface
.Thread_Id
;
134 pragma Warnings
(On
);
139 T
:= Tasking
.All_Tasks_List
;
141 exit when T
= null or else STPO
.Get_Thread_Id
(T
) = Thr
;
143 T
:= T
.Common
.All_Tasks_Link
;
149 T
.Common
.State
:= Tasking
.Terminated
;
150 Destroy_TSD
(T
.Common
.Compiler_Data
);
153 end Unregister_Thread_Id
;
159 procedure Destroy_Thread
(Id
: Address
) is
160 Tid
: constant Task_Id
:= To_Id
(Id
);
169 procedure Get_Thread
(Id
: Address
; Thread
: Address
) is
170 Thr
: constant Thread_Id_Ptr
:= To_Thread
(Thread
);
172 Thr
.all := Task_Primitives
.Operations
.Get_Thread_Id
(To_Id
(Id
));
180 (Id
: System
.Address
) return Ada
.Task_Identification
.Task_Id