annotate gcc/ada/libgnarl/g-thread.ads @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT RUN-TIME COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- G N A T . T H R E A D S --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
9 -- Copyright (C) 1998-2018, AdaCore --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- This package provides facilities for creation or registration of foreign
kono
parents:
diff changeset
33 -- threads for use as Ada tasks. In order to execute general Ada code, the
kono
parents:
diff changeset
34 -- run-time system must know about all tasks. This package allows foreign
kono
parents:
diff changeset
35 -- code, e.g. a C program, to create a thread that the Ada run-time knows
kono
parents:
diff changeset
36 -- about, or to register the current thread.
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 -- For some implementations of GNAT Pro, the registration of foreign threads
kono
parents:
diff changeset
39 -- is automatic. However, in such implementations, if the Ada program has no
kono
parents:
diff changeset
40 -- tasks at all and no tasking constructs other than delay, then by default
kono
parents:
diff changeset
41 -- the non-tasking version of the Ada run-time will be loaded. If foreign
kono
parents:
diff changeset
42 -- threads are present, it is important to ensure that the tasking version
kono
parents:
diff changeset
43 -- of the Ada run time is loaded. This may be achieved by adding "with
kono
parents:
diff changeset
44 -- GNAT.Threads" to any unit in the partition.
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 with System;
kono
parents:
diff changeset
47 with Ada.Task_Identification;
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 package GNAT.Threads is
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 type Void_Ptr is access all Integer;
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 function Create_Thread
kono
parents:
diff changeset
54 (Code : System.Address; -- pointer
kono
parents:
diff changeset
55 Parm : Void_Ptr; -- pointer
kono
parents:
diff changeset
56 Size : Natural; -- int
kono
parents:
diff changeset
57 Prio : Integer) -- int
kono
parents:
diff changeset
58 return System.Address;
kono
parents:
diff changeset
59 pragma Export (C, Create_Thread, "__gnat_create_thread");
kono
parents:
diff changeset
60 -- Creates a thread with the given (Size) stack size in bytes, and
kono
parents:
diff changeset
61 -- the given (Prio) priority. The task will execute a call to the
kono
parents:
diff changeset
62 -- procedure whose address is given by Code. This procedure has
kono
parents:
diff changeset
63 -- the prototype
kono
parents:
diff changeset
64 --
kono
parents:
diff changeset
65 -- void thread_code (void *id, void *parm);
kono
parents:
diff changeset
66 --
kono
parents:
diff changeset
67 -- where id is the id of the created task, and parm is the parameter
kono
parents:
diff changeset
68 -- passed to Create_Thread. The called procedure is the body of the
kono
parents:
diff changeset
69 -- code for the task, the task will be automatically terminated when
kono
parents:
diff changeset
70 -- the procedure returns.
kono
parents:
diff changeset
71 --
kono
parents:
diff changeset
72 -- This function returns the Ada Id of the created task that can then be
kono
parents:
diff changeset
73 -- used as a parameter to the procedures below.
kono
parents:
diff changeset
74 --
kono
parents:
diff changeset
75 -- C declaration:
kono
parents:
diff changeset
76 --
kono
parents:
diff changeset
77 -- extern void *__gnat_create_thread
kono
parents:
diff changeset
78 -- (void (*code)(void *, void *), void *parm, int size, int prio);
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 function Register_Thread return System.Address;
kono
parents:
diff changeset
81 pragma Export (C, Register_Thread, "__gnat_register_thread");
kono
parents:
diff changeset
82 -- Create an Ada task Id for the current thread if needed.
kono
parents:
diff changeset
83 -- If the thread could not be registered, System.Null_Address is returned.
kono
parents:
diff changeset
84 --
kono
parents:
diff changeset
85 -- This function returns the Ada Id of the current task that can then be
kono
parents:
diff changeset
86 -- used as a parameter to the procedures below.
kono
parents:
diff changeset
87 --
kono
parents:
diff changeset
88 -- C declaration:
kono
parents:
diff changeset
89 --
kono
parents:
diff changeset
90 -- extern void *__gnat_register_thread ();
kono
parents:
diff changeset
91 --
kono
parents:
diff changeset
92 -- Here is a typical usage of the Register/Unregister_Thread procedures:
kono
parents:
diff changeset
93 --
kono
parents:
diff changeset
94 -- void thread_body ()
kono
parents:
diff changeset
95 -- {
kono
parents:
diff changeset
96 -- void *task_id = __gnat_register_thread ();
kono
parents:
diff changeset
97 -- ... thread body ...
kono
parents:
diff changeset
98 -- __gnat_unregister_thread ();
kono
parents:
diff changeset
99 -- }
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 procedure Unregister_Thread;
kono
parents:
diff changeset
102 pragma Export (C, Unregister_Thread, "__gnat_unregister_thread");
kono
parents:
diff changeset
103 -- Unregister the current task from the GNAT run time and destroy the
kono
parents:
diff changeset
104 -- memory allocated for its task id.
kono
parents:
diff changeset
105 --
kono
parents:
diff changeset
106 -- C declaration:
kono
parents:
diff changeset
107 --
kono
parents:
diff changeset
108 -- extern void __gnat_unregister_thread ();
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 procedure Unregister_Thread_Id (Thread : System.Address);
kono
parents:
diff changeset
111 pragma Export (C, Unregister_Thread_Id, "__gnat_unregister_thread_id");
kono
parents:
diff changeset
112 -- Unregister the task associated with Thread from the GNAT run time and
kono
parents:
diff changeset
113 -- destroy the memory allocated for its task id.
kono
parents:
diff changeset
114 -- If no task id is associated with Thread, do nothing.
kono
parents:
diff changeset
115 --
kono
parents:
diff changeset
116 -- C declaration:
kono
parents:
diff changeset
117 --
kono
parents:
diff changeset
118 -- extern void __gnat_unregister_thread_id (pthread_t *thread);
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 procedure Destroy_Thread (Id : System.Address);
kono
parents:
diff changeset
121 pragma Export (C, Destroy_Thread, "__gnat_destroy_thread");
kono
parents:
diff changeset
122 -- This procedure may be used to prematurely abort the created thread.
kono
parents:
diff changeset
123 -- The value Id is the value that was passed to the thread code procedure
kono
parents:
diff changeset
124 -- at activation time.
kono
parents:
diff changeset
125 --
kono
parents:
diff changeset
126 -- C declaration:
kono
parents:
diff changeset
127 --
kono
parents:
diff changeset
128 -- extern void __gnat_destroy_thread (void *id);
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 procedure Get_Thread (Id : System.Address; Thread : System.Address);
kono
parents:
diff changeset
131 pragma Export (C, Get_Thread, "__gnat_get_thread");
kono
parents:
diff changeset
132 -- This procedure is used to retrieve the thread id of a given task.
kono
parents:
diff changeset
133 -- The value Id is the value that was passed to the thread code procedure
kono
parents:
diff changeset
134 -- at activation time.
kono
parents:
diff changeset
135 -- Thread is a pointer to a thread id that will be updated by this
kono
parents:
diff changeset
136 -- procedure.
kono
parents:
diff changeset
137 --
kono
parents:
diff changeset
138 -- C declaration:
kono
parents:
diff changeset
139 --
kono
parents:
diff changeset
140 -- extern void __gnat_get_thread (void *id, pthread_t *thread);
kono
parents:
diff changeset
141
kono
parents:
diff changeset
142 function To_Task_Id
kono
parents:
diff changeset
143 (Id : System.Address)
kono
parents:
diff changeset
144 return Ada.Task_Identification.Task_Id;
kono
parents:
diff changeset
145 -- Ada interface only.
kono
parents:
diff changeset
146 -- Given a low level Id, as returned by Create_Thread, return a Task_Id,
kono
parents:
diff changeset
147 -- so that operations in Ada.Task_Identification can be used.
kono
parents:
diff changeset
148
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
149 function Make_Independent return Boolean;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
150 -- If a procedure loads a shared library containing tasks, and that
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
151 -- procedure is considered to be a master by the compiler (because it
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
152 -- contains tasks or class-wide objects that might contain tasks),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
153 -- then the tasks in the shared library need to call Make_Independent
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
154 -- because otherwise they will depend on the procedure that loaded the
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
155 -- shared library.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
156 --
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
157 -- See System.Tasking.Utilities.Make_Independent in s-tasuti.ads for
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
158 -- further documentation.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
159
111
kono
parents:
diff changeset
160 end GNAT.Threads;