OSDN Git Service

2005-03-08 Ed Schonberg <schonberg@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-thread.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUNTIME 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-2003 Ada Core Technologies, Inc.            --
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,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, 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 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.
38
39 with System;
40 with Ada.Task_Identification;
41
42 package GNAT.Threads is
43
44    type Void_Ptr is access all Integer;
45
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
56    --  the prototype
57    --
58    --    void thread_code (void *id, void *parm);
59    --
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.
64    --
65    --  This function returns the Ada Id of the created task that can then be
66    --  used as a parameter to the procedures below.
67    --
68    --  C declaration:
69    --
70    --  extern void *__gnat_create_thread
71    --    (void (*code)(void *, void *), void *parm, int size, int prio);
72
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.
77    --
78    --  This function returns the Ada Id of the current task that can then be
79    --  used as a parameter to the procedures below.
80    --
81    --  C declaration:
82    --
83    --  extern void *__gnat_register_thread ();
84    --
85    --  Here is a typical usage of the Register/Unregister_Thread procedures:
86    --
87    --  void thread_body ()
88    --  {
89    --    void *task_id = __gnat_register_thread ();
90    --    ... thread body ...
91    --    __gnat_unregister_thread ();
92    --  }
93
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.
98    --
99    --  C declaration:
100    --
101    --  extern void __gnat_unregister_thread ();
102
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.
108    --
109    --  C declaration:
110    --
111    --  extern void __gnat_unregister_thread_id (pthread_t *thread);
112
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.
118    --
119    --  C declaration:
120    --
121    --  extern void __gnat_destroy_thread (void *id);
122
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
129    --  procedure.
130    --
131    --  C declaration:
132    --
133    --  extern void __gnat_get_thread (void *id, pthread_t *thread);
134
135    function To_Task_Id
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.
141
142 end GNAT.Threads;