1 |
281 |
jeremybenn |
------------------------------------------------------------------------------
|
2 |
|
|
-- --
|
3 |
|
|
-- GNAT RUN-TIME COMPONENTS --
|
4 |
|
|
-- --
|
5 |
|
|
-- G N A T . T H R E A D S --
|
6 |
|
|
-- --
|
7 |
|
|
-- S p e c --
|
8 |
|
|
-- --
|
9 |
|
|
-- Copyright (C) 1998-2007, AdaCore --
|
10 |
|
|
-- --
|
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 2, 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. 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 GNAT; see file COPYING. If not, write --
|
19 |
|
|
-- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
|
20 |
|
|
-- Boston, MA 02110-1301, 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 |
|
|
-- GNAT was originally developed by the GNAT team at New York University. --
|
30 |
|
|
-- Extensive contributions were provided by Ada Core Technologies Inc. --
|
31 |
|
|
-- --
|
32 |
|
|
------------------------------------------------------------------------------
|
33 |
|
|
|
34 |
|
|
-- This package provides facilities for creation or registration of foreign
|
35 |
|
|
-- threads for use as Ada tasks. In order to execute general Ada code, the
|
36 |
|
|
-- run-time system must know about all tasks. This package allows foreign
|
37 |
|
|
-- code, e.g. a C program, to create a thread that the Ada run-time knows
|
38 |
|
|
-- about, or to register the current thread.
|
39 |
|
|
|
40 |
|
|
-- For some implementations of GNAT Pro, the registration of foreign threads
|
41 |
|
|
-- is automatic. However, in such implementations, if the Ada program has no
|
42 |
|
|
-- tasks at all and no tasking constructs other than delay, then by default
|
43 |
|
|
-- the non-tasking version of the Ada run-time will be loaded. If foreign
|
44 |
|
|
-- threads are present, it is important to ensure that the tasking version
|
45 |
|
|
-- of the Ada run time is loaded. This may be achieved by adding "with
|
46 |
|
|
-- GNAT.Threads" to any unit in the partition.
|
47 |
|
|
|
48 |
|
|
with System;
|
49 |
|
|
with Ada.Task_Identification;
|
50 |
|
|
|
51 |
|
|
package GNAT.Threads is
|
52 |
|
|
|
53 |
|
|
type Void_Ptr is access all Integer;
|
54 |
|
|
|
55 |
|
|
function Create_Thread
|
56 |
|
|
(Code : System.Address; -- pointer
|
57 |
|
|
Parm : Void_Ptr; -- pointer
|
58 |
|
|
Size : Natural; -- int
|
59 |
|
|
Prio : Integer) -- int
|
60 |
|
|
return System.Address;
|
61 |
|
|
pragma Export (C, Create_Thread, "__gnat_create_thread");
|
62 |
|
|
-- Creates a thread with the given (Size) stack size in bytes, and
|
63 |
|
|
-- the given (Prio) priority. The task will execute a call to the
|
64 |
|
|
-- procedure whose address is given by Code. This procedure has
|
65 |
|
|
-- the prototype
|
66 |
|
|
--
|
67 |
|
|
-- void thread_code (void *id, void *parm);
|
68 |
|
|
--
|
69 |
|
|
-- where id is the id of the created task, and parm is the parameter
|
70 |
|
|
-- passed to Create_Thread. The called procedure is the body of the
|
71 |
|
|
-- code for the task, the task will be automatically terminated when
|
72 |
|
|
-- the procedure returns.
|
73 |
|
|
--
|
74 |
|
|
-- This function returns the Ada Id of the created task that can then be
|
75 |
|
|
-- used as a parameter to the procedures below.
|
76 |
|
|
--
|
77 |
|
|
-- C declaration:
|
78 |
|
|
--
|
79 |
|
|
-- extern void *__gnat_create_thread
|
80 |
|
|
-- (void (*code)(void *, void *), void *parm, int size, int prio);
|
81 |
|
|
|
82 |
|
|
function Register_Thread return System.Address;
|
83 |
|
|
pragma Export (C, Register_Thread, "__gnat_register_thread");
|
84 |
|
|
-- Create an Ada task Id for the current thread if needed.
|
85 |
|
|
-- If the thread could not be registered, System.Null_Address is returned.
|
86 |
|
|
--
|
87 |
|
|
-- This function returns the Ada Id of the current task that can then be
|
88 |
|
|
-- used as a parameter to the procedures below.
|
89 |
|
|
--
|
90 |
|
|
-- C declaration:
|
91 |
|
|
--
|
92 |
|
|
-- extern void *__gnat_register_thread ();
|
93 |
|
|
--
|
94 |
|
|
-- Here is a typical usage of the Register/Unregister_Thread procedures:
|
95 |
|
|
--
|
96 |
|
|
-- void thread_body ()
|
97 |
|
|
-- {
|
98 |
|
|
-- void *task_id = __gnat_register_thread ();
|
99 |
|
|
-- ... thread body ...
|
100 |
|
|
-- __gnat_unregister_thread ();
|
101 |
|
|
-- }
|
102 |
|
|
|
103 |
|
|
procedure Unregister_Thread;
|
104 |
|
|
pragma Export (C, Unregister_Thread, "__gnat_unregister_thread");
|
105 |
|
|
-- Unregister the current task from the GNAT run time and destroy the
|
106 |
|
|
-- memory allocated for its task id.
|
107 |
|
|
--
|
108 |
|
|
-- C declaration:
|
109 |
|
|
--
|
110 |
|
|
-- extern void __gnat_unregister_thread ();
|
111 |
|
|
|
112 |
|
|
procedure Unregister_Thread_Id (Thread : System.Address);
|
113 |
|
|
pragma Export (C, Unregister_Thread_Id, "__gnat_unregister_thread_id");
|
114 |
|
|
-- Unregister the task associated with Thread from the GNAT run time and
|
115 |
|
|
-- destroy the memory allocated for its task id.
|
116 |
|
|
-- If no task id is associated with Thread, do nothing.
|
117 |
|
|
--
|
118 |
|
|
-- C declaration:
|
119 |
|
|
--
|
120 |
|
|
-- extern void __gnat_unregister_thread_id (pthread_t *thread);
|
121 |
|
|
|
122 |
|
|
procedure Destroy_Thread (Id : System.Address);
|
123 |
|
|
pragma Export (C, Destroy_Thread, "__gnat_destroy_thread");
|
124 |
|
|
-- This procedure may be used to prematurely abort the created thread.
|
125 |
|
|
-- The value Id is the value that was passed to the thread code procedure
|
126 |
|
|
-- at activation time.
|
127 |
|
|
--
|
128 |
|
|
-- C declaration:
|
129 |
|
|
--
|
130 |
|
|
-- extern void __gnat_destroy_thread (void *id);
|
131 |
|
|
|
132 |
|
|
procedure Get_Thread (Id : System.Address; Thread : System.Address);
|
133 |
|
|
pragma Export (C, Get_Thread, "__gnat_get_thread");
|
134 |
|
|
-- This procedure is used to retrieve the thread id of a given task.
|
135 |
|
|
-- The value Id is the value that was passed to the thread code procedure
|
136 |
|
|
-- at activation time.
|
137 |
|
|
-- Thread is a pointer to a thread id that will be updated by this
|
138 |
|
|
-- procedure.
|
139 |
|
|
--
|
140 |
|
|
-- C declaration:
|
141 |
|
|
--
|
142 |
|
|
-- extern void __gnat_get_thread (void *id, pthread_t *thread);
|
143 |
|
|
|
144 |
|
|
function To_Task_Id
|
145 |
|
|
(Id : System.Address)
|
146 |
|
|
return Ada.Task_Identification.Task_Id;
|
147 |
|
|
-- Ada interface only.
|
148 |
|
|
-- Given a low level Id, as returned by Create_Thread, return a Task_Id,
|
149 |
|
|
-- so that operations in Ada.Task_Identification can be used.
|
150 |
|
|
|
151 |
|
|
end GNAT.Threads;
|