OSDN Git Service

New Language: Ada
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-taprob.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS               --
4 --                                                                          --
5 --      S Y S T E M . T A S K I N G . P R O T E C T E D _ O B J E C T S     --
6 --                                                                          --
7 --                                  S p e c                                 --
8 --                                                                          --
9 --                             $Revision: 1.35 $
10 --                                                                          --
11 --          Copyright (C) 1992-2001, Free Software Foundation, Inc.         --
12 --                                                                          --
13 -- GNARL is free software; you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNARL; see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- As a special exception,  if other files  instantiate  generics from this --
25 -- unit, or you link  this unit with other files  to produce an executable, --
26 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
27 -- covered  by the  GNU  General  Public  License.  This exception does not --
28 -- however invalidate  any other reasons why  the executable file  might be --
29 -- covered by the  GNU Public License.                                      --
30 --                                                                          --
31 -- GNARL was developed by the GNARL team at Florida State University. It is --
32 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
33 -- State University (http://www.gnat.com).                                  --
34 --                                                                          --
35 ------------------------------------------------------------------------------
36
37 --  This package provides necessary definitions to handle simple (i.e without
38 --  entries) protected objects.
39 --
40 --  All the routines that handle protected objects with entries have been moved
41 --  to two children: Entries and Operations. Note that Entries only contains
42 --  the type declaration and the OO primitives. This is needed to avoid
43 --  circular dependency.
44
45 --  This package is part of the high level tasking interface used by the
46 --  compiler to expand Ada 95 tasking constructs into simpler run time calls
47 --  (aka GNARLI, GNU Ada Run-time Library Interface)
48
49 --  Note: the compiler generates direct calls to this interface, via Rtsfind.
50 --  Any changes to this interface may require corresponding compiler changes
51 --  in exp_ch9.adb and possibly exp_ch7.adb
52
53 package System.Tasking.Protected_Objects is
54    pragma Elaborate_Body;
55
56    ---------------------------------
57    -- Compiler Interface (GNARLI) --
58    ---------------------------------
59
60    --  The compiler will expand in the GNAT tree the following construct:
61    --
62    --  protected PO is
63    --     procedure P;
64    --  private
65    --     open : boolean := false;
66    --  end PO;
67    --
68    --  protected body PO is
69    --     procedure P is
70    --        ...variable declarations...
71    --     begin
72    --        ...B...
73    --     end P;
74    --  end PO;
75    --
76    --  as follows:
77    --
78    --  protected type poT is
79    --     procedure p;
80    --  private
81    --     open : boolean := false;
82    --  end poT;
83    --  type poTV is limited record
84    --     open : boolean := false;
85    --     _object : aliased protection;
86    --  end record;
87    --  procedure poPT__pN (_object : in out poTV);
88    --  procedure poPT__pP (_object : in out poTV);
89    --  freeze poTV [
90    --     procedure _init_proc (_init : in out poTV) is
91    --     begin
92    --        _init.open := false;
93    --        _init_proc (_init._object);
94    --        initialize_protection (_init._object'unchecked_access,
95    --          unspecified_priority);
96    --        return;
97    --     end _init_proc;
98    --  ]
99    --  po : poT;
100    --  _init_proc (poTV!(po));
101    --
102    --  procedure poPT__pN (_object : in out poTV) is
103    --     poR : protection renames _object._object;
104    --     openP : boolean renames _object.open;
105    --     ...variable declarations...
106    --  begin
107    --     ...B...
108    --     return;
109    --  end poPT__pN;
110    --
111    --  procedure poPT__pP (_object : in out poTV) is
112    --     procedure _clean is
113    --     begin
114    --        unlock (_object._object'unchecked_access);
115    --        return;
116    --     end _clean;
117    --  begin
118    --     lock (_object._object'unchecked_access);
119    --     B2b : begin
120    --        poPT__pN (_object);
121    --     at end
122    --        _clean;
123    --     end B2b;
124    --     return;
125    --  end poPT__pP;
126
127    Null_Protected_Entry : constant := Null_Entry;
128
129    Max_Protected_Entry : constant := Max_Entry;
130
131    type Protected_Entry_Index is new Entry_Index
132      range Null_Protected_Entry .. Max_Protected_Entry;
133
134    type Barrier_Function_Pointer is access
135      function
136        (O    : System.Address;
137         E    : Protected_Entry_Index)
138         return Boolean;
139    --  Pointer to a function which evaluates the barrier of a protected
140    --  entry body. O is a pointer to the compiler-generated record
141    --  representing the protected object, and E is the index of the
142    --  entry serviced by the body.
143
144    type Entry_Action_Pointer is access
145      procedure
146        (O : System.Address;
147         P : System.Address;
148         E : Protected_Entry_Index);
149    --  Pointer to a procedure which executes the sequence of statements
150    --  of a protected entry body. O is a pointer to the compiler-generated
151    --  record representing the protected object, P is a pointer to the
152    --  record of entry parameters, and E is the index of the
153    --  entry serviced by the body.
154
155    type Entry_Body is record
156       Barrier : Barrier_Function_Pointer;
157       Action  : Entry_Action_Pointer;
158    end record;
159    --  The compiler-generated code passes objects of this type to the GNARL
160    --  to allow it to access the executable code of an entry body.
161
162    type Entry_Body_Access is access all Entry_Body;
163
164    type Protection is limited private;
165    --  This type contains the GNARL state of a protected object. The
166    --  application-defined portion of the state (i.e. private objects)
167    --  is maintained by the compiler-generated code.
168    --  Note that there are now 2 Protection types. One for the simple
169    --  case (no entries) and one for the general case that needs the whole
170    --  Finalization mechanism.
171    --  This split helps in the case of restricted run time where we want to
172    --  minimize the size of the code.
173
174    type Protection_Access is access all Protection;
175
176    Null_PO : constant Protection_Access := null;
177
178    procedure Initialize_Protection
179      (Object           : Protection_Access;
180       Ceiling_Priority : Integer);
181    --  Initialize the Object parameter so that it can be used by the runtime
182    --  to keep track of the runtime state of a protected object.
183
184    procedure Lock (Object : Protection_Access);
185    --  Lock a protected object for write access. Upon return, the caller
186    --  owns the lock to this object, and no other call to Lock or
187    --  Lock_Read_Only with the same argument will return until the
188    --  corresponding call to Unlock has been made by the caller.
189
190    procedure Lock_Read_Only (Object : Protection_Access);
191    --  Lock a protected object for read access. Upon return, the caller
192    --  owns the lock for read access, and no other calls to Lock with the
193    --  same argument will return until the corresponding call to Unlock
194    --  has been made by the caller. Other calls to Lock_Read_Only may (but
195    --  need not) return before the call to Unlock, and the corresponding
196    --  callers will also own the lock for read access.
197    --
198    --  Note: we are not currently using this interface, it is provided
199    --  for possible future use. At the current time, everyone uses Lock
200    --  for both read and write locks.
201
202    procedure Unlock (Object : Protection_Access);
203    --  Relinquish ownership of the lock for the object represented by
204    --  the Object parameter. If this ownership was for write access, or
205    --  if it was for read access where there are no other read access
206    --  locks outstanding, one (or more, in the case of Lock_Read_Only)
207    --  of the tasks waiting on this lock (if any) will be given the
208    --  lock and allowed to return from the Lock or Lock_Read_Only call.
209
210 private
211    type Protection is record
212       L       : aliased Task_Primitives.Lock;
213       Ceiling : System.Any_Priority;
214    end record;
215    pragma Volatile (Protection);
216    for Protection'Alignment use Standard'Maximum_Alignment;
217    --  Needed so that we can uncheck convert a Protection_Access to a
218    --  Protection_Entries_Access.
219
220    procedure Finalize_Protection (Object : in out Protection);
221    --  Clean up a Protection object; in particular, finalize the associated
222    --  Lock object. The compiler generates automatically calls to this
223    --  procedure
224
225 end System.Tasking.Protected_Objects;