1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- G N A T . T H R E A D S --
9 -- Copyright (C) 1998-2005 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 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. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 -- This package provides facilities for creation of foreign threads for
35 -- use as Ada tasks. In order to execute general Ada code, the run-time
36 -- system must know about all tasks. This package allows foreign code,
37 -- e.g. a C program, to create a thread that the Ada run-time knows about.
40 with Ada
.Task_Identification
;
42 package GNAT
.Threads
is
44 type Void_Ptr
is access all Integer;
46 function Create_Thread
47 (Code
: System
.Address
; -- pointer
48 Parm
: Void_Ptr
; -- pointer
49 Size
: Natural; -- int
50 Prio
: Integer) -- int
51 return System
.Address
;
52 pragma Export
(C
, Create_Thread
, "__gnat_create_thread");
53 -- Creates a thread with the given (Size) stack size in bytes, and
54 -- the given (Prio) priority. The task will execute a call to the
55 -- procedure whose address is given by Code. This procedure has
58 -- void thread_code (void *id, void *parm);
60 -- where id is the id of the created task, and parm is the parameter
61 -- passed to Create_Thread. The called procedure is the body of the
62 -- code for the task, the task will be automatically terminated when
63 -- the procedure returns.
65 -- This function returns the Ada Id of the created task that can then be
66 -- used as a parameter to the procedures below.
70 -- extern void *__gnat_create_thread
71 -- (void (*code)(void *, void *), void *parm, int size, int prio);
73 function Register_Thread
return System
.Address
;
74 pragma Export
(C
, Register_Thread
, "__gnat_register_thread");
75 -- Create an Ada task Id for the current thread if needed.
76 -- If the thread could not be registered, System.Null_Address is returned.
78 -- This function returns the Ada Id of the current task that can then be
79 -- used as a parameter to the procedures below.
83 -- extern void *__gnat_register_thread ();
85 -- Here is a typical usage of the Register/Unregister_Thread procedures:
87 -- void thread_body ()
89 -- void *task_id = __gnat_register_thread ();
90 -- ... thread body ...
91 -- __gnat_unregister_thread ();
94 procedure Unregister_Thread
;
95 pragma Export
(C
, Unregister_Thread
, "__gnat_unregister_thread");
96 -- Unregister the current task from the GNAT run time and destroy the
97 -- memory allocated for its task id.
101 -- extern void __gnat_unregister_thread ();
103 procedure Unregister_Thread_Id
(Thread
: System
.Address
);
104 pragma Export
(C
, Unregister_Thread_Id
, "__gnat_unregister_thread_id");
105 -- Unregister the task associated with Thread from the GNAT run time and
106 -- destroy the memory allocated for its task id.
107 -- If no task id is associated with Thread, do nothing.
111 -- extern void __gnat_unregister_thread_id (pthread_t *thread);
113 procedure Destroy_Thread
(Id
: System
.Address
);
114 pragma Export
(C
, Destroy_Thread
, "__gnat_destroy_thread");
115 -- This procedure may be used to prematurely abort the created thread.
116 -- The value Id is the value that was passed to the thread code procedure
117 -- at activation time.
121 -- extern void __gnat_destroy_thread (void *id);
123 procedure Get_Thread
(Id
: System
.Address
; Thread
: System
.Address
);
124 pragma Export
(C
, Get_Thread
, "__gnat_get_thread");
125 -- This procedure is used to retrieve the thread id of a given task.
126 -- The value Id is the value that was passed to the thread code procedure
127 -- at activation time.
128 -- Thread is a pointer to a thread id that will be updated by this
133 -- extern void __gnat_get_thread (void *id, pthread_t *thread);
136 (Id
: System
.Address
)
137 return Ada
.Task_Identification
.Task_Id
;
138 -- Ada interface only.
139 -- Given a low level Id, as returned by Create_Thread, return a Task_Id,
140 -- so that operations in Ada.Task_Identification can be used.