OSDN Git Service

New Language: Ada
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-tposen.adb
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 --                          S I N G L E _ E N T R Y                         --
7 --                                                                          --
8 --                                  B o d y                                 --
9 --                                                                          --
10 --                             $Revision: 1.14 $
11 --                                                                          --
12 --              Copyright (C) 1998-2001 Ada Core Technologies               --
13 --                                                                          --
14 -- GNARL is free software; you can  redistribute it  and/or modify it under --
15 -- terms of the  GNU General Public License as published  by the Free Soft- --
16 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
17 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
18 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
19 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
20 -- for  more details.  You should have  received  a copy of the GNU General --
21 -- Public License  distributed with GNARL; see file COPYING.  If not, write --
22 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
23 -- MA 02111-1307, USA.                                                      --
24 --                                                                          --
25 -- As a special exception,  if other files  instantiate  generics from this --
26 -- unit, or you link  this unit with other files  to produce an executable, --
27 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
28 -- covered  by the  GNU  General  Public  License.  This exception does not --
29 -- however invalidate  any other reasons why  the executable file  might be --
30 -- covered by the  GNU Public License.                                      --
31 --                                                                          --
32 -- GNARL was developed by the GNARL team at Florida State University. It is --
33 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
34 -- State University (http://www.gnat.com).                                  --
35 --                                                                          --
36 ------------------------------------------------------------------------------
37
38 pragma Style_Checks (All_Checks);
39 --  Turn off subprogram ordering check, since restricted GNARLI
40 --  subprograms are gathered together at end.
41
42 --  This package provides an optimized version of Protected_Objects.Operations
43 --  and Protected_Objects.Entries making the following assumptions:
44 --
45 --  PO have only one entry
46 --  There is only one caller at a time (No_Entry_Queue)
47 --  There is no dynamic priority support (No_Dynamic_Priorities)
48 --  No Abort Statements
49 --    (No_Abort_Statements, Max_Asynchronous_Select_Nesting => 0)
50 --  PO are at library level
51 --  No Requeue
52 --  None of the tasks will terminate (no need for finalization)
53 --
54 --  This interface is intended to be used in the ravenscar and restricted
55 --  profiles, the compiler is responsible for ensuring that the conditions
56 --  mentioned above are respected, except for the No_Entry_Queue restriction
57 --  that is checked dynamically in this package, since the check cannot be
58 --  performed at compile time, and is relatively cheap (see PO_Do_Or_Queue,
59 --  PO_Service_Entry).
60
61 pragma Polling (Off);
62 --  Turn off polling, we do not want polling to take place during tasking
63 --  operations. It can cause  infinite loops and other problems.
64
65 pragma Suppress (All_Checks);
66
67 with System.Task_Primitives.Operations;
68 --  used for Self
69 --           Finalize_Lock
70 --           Write_Lock
71 --           Unlock
72
73 with Ada.Exceptions;
74 --  used for Exception_Id;
75
76 with Unchecked_Conversion;
77
78 package body System.Tasking.Protected_Objects.Single_Entry is
79
80    package STPO renames System.Task_Primitives.Operations;
81
82    function To_Address is new
83      Unchecked_Conversion (Protection_Entry_Access, System.Address);
84
85    -----------------------
86    -- Local Subprograms --
87    -----------------------
88
89    procedure Send_Program_Error
90      (Self_Id    : Task_ID;
91       Entry_Call : Entry_Call_Link);
92    pragma Inline (Send_Program_Error);
93    --  Raise Program_Error in the caller of the specified entry call
94
95    --------------------------
96    -- Entry Calls Handling --
97    --------------------------
98
99    procedure Wakeup_Entry_Caller
100      (Self_ID    : Task_ID;
101       Entry_Call : Entry_Call_Link;
102       New_State  : Entry_Call_State);
103    pragma Inline (Wakeup_Entry_Caller);
104    --  This is called at the end of service of an entry call,
105    --  to abort the caller if he is in an abortable part, and
106    --  to wake up the caller if he is on Entry_Caller_Sleep.
107    --  Call it holding the lock of Entry_Call.Self.
108    --
109    --  Timed_Call or Simple_Call:
110    --    The caller is waiting on Entry_Caller_Sleep, in
111    --    Wait_For_Completion, or Wait_For_Completion_With_Timeout.
112
113    procedure Wait_For_Completion
114      (Self_ID    : Task_ID;
115       Entry_Call : Entry_Call_Link);
116    pragma Inline (Wait_For_Completion);
117    --  This procedure suspends the calling task until the specified entry call
118    --  has either been completed or cancelled. On exit, the call will not be
119    --  queued. This waits for calls on protected entries.
120    --  Call this only when holding Self_ID locked.
121
122    procedure Wait_For_Completion_With_Timeout
123      (Self_ID     : Task_ID;
124       Entry_Call  : Entry_Call_Link;
125       Wakeup_Time : Duration;
126       Mode        : Delay_Modes);
127    --  Same as Wait_For_Completion but it waits for a timeout with the value
128    --  specified in Wakeup_Time as well.
129    --  Self_ID will be locked by this procedure.
130
131    procedure Check_Exception
132      (Self_ID : Task_ID;
133       Entry_Call : Entry_Call_Link);
134    pragma Inline (Check_Exception);
135    --  Raise any pending exception from the Entry_Call.
136    --  This should be called at the end of every compiler interface procedure
137    --  that implements an entry call.
138    --  The caller should not be holding any locks, or there will be deadlock.
139
140    procedure PO_Do_Or_Queue
141      (Self_Id    : Task_ID;
142       Object     : Protection_Entry_Access;
143       Entry_Call : Entry_Call_Link);
144
145    ---------------------
146    -- Check_Exception --
147    ---------------------
148
149    procedure Check_Exception
150      (Self_ID    : Task_ID;
151       Entry_Call : Entry_Call_Link)
152    is
153       use type Ada.Exceptions.Exception_Id;
154
155       procedure Internal_Raise (X : Ada.Exceptions.Exception_Id);
156       pragma Import (C, Internal_Raise, "__gnat_raise_with_msg");
157
158       E : constant Ada.Exceptions.Exception_Id :=
159         Entry_Call.Exception_To_Raise;
160
161    begin
162       if E /= Ada.Exceptions.Null_Id then
163          Internal_Raise (E);
164       end if;
165    end Check_Exception;
166
167    ------------------------
168    -- Send_Program_Error --
169    ------------------------
170
171    procedure Send_Program_Error
172      (Self_Id    : Task_ID;
173       Entry_Call : Entry_Call_Link)
174    is
175       Caller : constant Task_ID := Entry_Call.Self;
176    begin
177       Entry_Call.Exception_To_Raise := Program_Error'Identity;
178       STPO.Write_Lock (Caller);
179       Wakeup_Entry_Caller (Self_Id, Entry_Call, Done);
180       STPO.Unlock (Caller);
181    end Send_Program_Error;
182
183    -------------------------
184    -- Wait_For_Completion --
185    -------------------------
186
187    --  Call this only when holding Self_ID locked
188
189    procedure Wait_For_Completion
190      (Self_ID    : Task_ID;
191       Entry_Call : Entry_Call_Link)
192    is
193    begin
194       pragma Assert (Self_ID = Entry_Call.Self);
195       Self_ID.Common.State := Entry_Caller_Sleep;
196
197       STPO.Sleep (Self_ID, Entry_Caller_Sleep);
198
199       Self_ID.Common.State := Runnable;
200    end Wait_For_Completion;
201
202    --------------------------------------
203    -- Wait_For_Completion_With_Timeout --
204    --------------------------------------
205
206    --  This routine will lock Self_ID.
207
208    --  This procedure waits for the entry call to
209    --  be served, with a timeout.  It tries to cancel the
210    --  call if the timeout expires before the call is served.
211
212    --  If we wake up from the timed sleep operation here,
213    --  it may be for the following possible reasons:
214
215    --  1) The entry call is done being served.
216    --  2) The timeout has expired (Timedout = True)
217
218    --  Once the timeout has expired we may need to continue to wait if
219    --  the call is already being serviced. In that case, we want to go
220    --  back to sleep, but without any timeout. The variable Timedout is
221    --  used to control this. If the Timedout flag is set, we do not need
222    --  to Sleep with a timeout. We just sleep until we get a wakeup for
223    --  some status change.
224
225    procedure Wait_For_Completion_With_Timeout
226      (Self_ID     : Task_ID;
227       Entry_Call  : Entry_Call_Link;
228       Wakeup_Time : Duration;
229       Mode        : Delay_Modes)
230    is
231       Timedout : Boolean;
232       Yielded  : Boolean;
233
234       use type Ada.Exceptions.Exception_Id;
235
236    begin
237       STPO.Write_Lock (Self_ID);
238
239       pragma Assert (Entry_Call.Self = Self_ID);
240       pragma Assert (Entry_Call.Mode = Timed_Call);
241       Self_ID.Common.State := Entry_Caller_Sleep;
242
243       STPO.Timed_Sleep
244         (Self_ID, Wakeup_Time, Mode, Entry_Caller_Sleep, Timedout, Yielded);
245
246       if Timedout then
247          Entry_Call.State := Cancelled;
248       else
249          Entry_Call.State := Done;
250       end if;
251
252       Self_ID.Common.State := Runnable;
253       STPO.Unlock (Self_ID);
254    end Wait_For_Completion_With_Timeout;
255
256    -------------------------
257    -- Wakeup_Entry_Caller --
258    -------------------------
259
260    --  This is called at the end of service of an entry call, to abort the
261    --  caller if he is in an abortable part, and to wake up the caller if it
262    --  is on Entry_Caller_Sleep. It assumes that the call is already off-queue.
263
264    --  (This enforces the rule that a task must be off-queue if its state is
265    --  Done or Cancelled.) Call it holding the lock of Entry_Call.Self.
266
267    --  Timed_Call or Simple_Call:
268    --    The caller is waiting on Entry_Caller_Sleep, in
269    --    Wait_For_Completion, or Wait_For_Completion_With_Timeout.
270
271    --  Conditional_Call:
272    --    The caller might be in Wait_For_Completion,
273    --    waiting for a rendezvous (possibly requeued without abort)
274    --    to complete.
275
276    procedure Wakeup_Entry_Caller
277      (Self_ID    : Task_ID;
278       Entry_Call : Entry_Call_Link;
279       New_State  : Entry_Call_State)
280    is
281       Caller : constant Task_ID := Entry_Call.Self;
282    begin
283       pragma Assert (New_State = Done or else New_State = Cancelled);
284       pragma Assert
285         (Caller.Common.State /= Terminated and then
286          Caller.Common.State /= Unactivated);
287
288       Entry_Call.State := New_State;
289       STPO.Wakeup (Caller, Entry_Caller_Sleep);
290    end Wakeup_Entry_Caller;
291
292    -----------------------
293    -- Restricted GNARLI --
294    -----------------------
295
296    --------------------------------
297    -- Complete_Single_Entry_Body --
298    --------------------------------
299
300    procedure Complete_Single_Entry_Body (Object : Protection_Entry_Access) is
301    begin
302       --  Nothing needs to be done since
303       --  Object.Call_In_Progress.Exception_To_Raise has already been set to
304       --  Null_Id
305       null;
306    end Complete_Single_Entry_Body;
307
308    --------------------------------------------
309    -- Exceptional_Complete_Single_Entry_Body --
310    --------------------------------------------
311
312    procedure Exceptional_Complete_Single_Entry_Body
313      (Object : Protection_Entry_Access;
314       Ex     : Ada.Exceptions.Exception_Id) is
315    begin
316       Object.Call_In_Progress.Exception_To_Raise := Ex;
317    end Exceptional_Complete_Single_Entry_Body;
318
319    ---------------------------------
320    -- Initialize_Protection_Entry --
321    ---------------------------------
322
323    procedure Initialize_Protection_Entry
324      (Object            : Protection_Entry_Access;
325       Ceiling_Priority  : Integer;
326       Compiler_Info     : System.Address;
327       Entry_Body        : Entry_Body_Access)
328    is
329       Init_Priority  : Integer := Ceiling_Priority;
330
331    begin
332       if Init_Priority = Unspecified_Priority then
333          Init_Priority := System.Priority'Last;
334       end if;
335
336       STPO.Initialize_Lock (Init_Priority, Object.L'Access);
337       Object.Ceiling := System.Any_Priority (Init_Priority);
338       Object.Compiler_Info := Compiler_Info;
339       Object.Call_In_Progress := null;
340       Object.Entry_Body := Entry_Body;
341       Object.Entry_Queue := null;
342    end Initialize_Protection_Entry;
343
344    ----------------
345    -- Lock_Entry --
346    ----------------
347
348    --  Compiler interface only.
349    --  Do not call this procedure from within the run-time system.
350
351    procedure Lock_Entry (Object : Protection_Entry_Access) is
352       Ceiling_Violation : Boolean;
353    begin
354       STPO.Write_Lock (Object.L'Access, Ceiling_Violation);
355
356       if Ceiling_Violation then
357          raise Program_Error;
358       end if;
359    end Lock_Entry;
360
361    --------------------------
362    -- Lock_Read_Only_Entry --
363    --------------------------
364
365    --  Compiler interface only.
366    --  Do not call this procedure from within the runtime system.
367
368    procedure Lock_Read_Only_Entry (Object : Protection_Entry_Access) is
369       Ceiling_Violation : Boolean;
370    begin
371       STPO.Read_Lock (Object.L'Access, Ceiling_Violation);
372
373       if Ceiling_Violation then
374          raise Program_Error;
375       end if;
376    end Lock_Read_Only_Entry;
377
378    --------------------
379    -- PO_Do_Or_Queue --
380    --------------------
381
382    procedure PO_Do_Or_Queue
383      (Self_Id    : Task_ID;
384       Object     : Protection_Entry_Access;
385       Entry_Call : Entry_Call_Link)
386    is
387       Barrier_Value : Boolean;
388    begin
389       --  When the Action procedure for an entry body returns, it must be
390       --  completed (having called [Exceptional_]Complete_Entry_Body).
391
392       Barrier_Value := Object.Entry_Body.Barrier (Object.Compiler_Info, 1);
393
394       if Barrier_Value then
395          if Object.Call_In_Progress /= null then
396             --  This violates the No_Entry_Queue restriction, send
397             --  Program_Error to the caller.
398
399             Send_Program_Error (Self_Id, Entry_Call);
400             return;
401          end if;
402
403          Object.Call_In_Progress := Entry_Call;
404          Object.Entry_Body.Action
405            (Object.Compiler_Info, Entry_Call.Uninterpreted_Data, 1);
406          Object.Call_In_Progress := null;
407          Wakeup_Entry_Caller (Self_Id, Entry_Call, Done);
408
409       elsif Entry_Call.Mode /= Conditional_Call then
410          Object.Entry_Queue := Entry_Call;
411       else
412          --  Conditional_Call
413
414          STPO.Write_Lock (Entry_Call.Self);
415          Wakeup_Entry_Caller (Self_Id, Entry_Call, Cancelled);
416          STPO.Unlock (Entry_Call.Self);
417       end if;
418
419    exception                       --  not needed in no exc mode
420       when others =>               --  not needed in no exc mode
421          Send_Program_Error        --  not needed in no exc mode
422            (Self_Id, Entry_Call);  --  not needed in no exc mode
423    end PO_Do_Or_Queue;
424
425    ----------------------------
426    -- Protected_Single_Count --
427    ----------------------------
428
429    function Protected_Count_Entry
430      (Object : Protection_Entry) return Natural is
431    begin
432       if Object.Call_In_Progress /= null then
433          return 1;
434       else
435          return 0;
436       end if;
437    end Protected_Count_Entry;
438
439    ---------------------------------
440    -- Protected_Single_Entry_Call --
441    ---------------------------------
442
443    procedure Protected_Single_Entry_Call
444      (Object             : Protection_Entry_Access;
445       Uninterpreted_Data : System.Address;
446       Mode               : Call_Modes)
447    is
448       Self_Id           : constant Task_ID := STPO.Self;
449       Entry_Call        : Entry_Call_Record renames Self_Id.Entry_Calls (1);
450       Ceiling_Violation : Boolean;
451
452    begin
453       STPO.Write_Lock (Object.L'Access, Ceiling_Violation);
454
455       if Ceiling_Violation then
456          raise Program_Error;
457       end if;
458
459       Entry_Call.Mode := Mode;
460       Entry_Call.State := Now_Abortable;
461       Entry_Call.Uninterpreted_Data := Uninterpreted_Data;
462       Entry_Call.Exception_To_Raise := Ada.Exceptions.Null_Id;
463
464       PO_Do_Or_Queue (Self_Id, Object, Entry_Call'Access);
465       Unlock_Entry (Object);
466
467       --  The call is either `Done' or not. It cannot be cancelled since there
468       --  is no ATC construct.
469
470       pragma Assert (Entry_Call.State /= Cancelled);
471
472       if Entry_Call.State = Done then
473          Check_Exception (Self_Id, Entry_Call'Access);
474          return;
475       end if;
476
477       STPO.Write_Lock (Self_Id);
478       Wait_For_Completion (Self_Id, Entry_Call'Access);
479       STPO.Unlock (Self_Id);
480       Check_Exception (Self_Id, Entry_Call'Access);
481    end Protected_Single_Entry_Call;
482
483    -----------------------------------
484    -- Protected_Single_Entry_Caller --
485    -----------------------------------
486
487    function Protected_Single_Entry_Caller
488      (Object : Protection_Entry) return Task_ID is
489    begin
490       return Object.Call_In_Progress.Self;
491    end Protected_Single_Entry_Caller;
492
493    -------------------
494    -- Service_Entry --
495    -------------------
496
497    procedure Service_Entry (Object : Protection_Entry_Access) is
498       Self_Id       : constant Task_ID := STPO.Self;
499       Entry_Call    : Entry_Call_Link;
500       Caller        : Task_ID;
501       Barrier_Value : Boolean;
502
503    begin
504       Entry_Call := Object.Entry_Queue;
505
506       if Entry_Call /= null then
507          Barrier_Value :=
508            Object.Entry_Body.Barrier (Object.Compiler_Info, 1);
509
510          if Barrier_Value then
511             if Object.Call_In_Progress /= null then
512
513                --  This violates the No_Entry_Queue restriction, send
514                --  Program_Error to the caller.
515
516                Send_Program_Error (Self_Id, Entry_Call);
517                return;
518             end if;
519
520             Object.Call_In_Progress := Entry_Call;
521             Object.Entry_Body.Action
522               (Object.Compiler_Info, Entry_Call.Uninterpreted_Data, 1);
523             Object.Call_In_Progress := null;
524             Caller := Entry_Call.Self;
525             STPO.Write_Lock (Caller);
526             Wakeup_Entry_Caller (Self_Id, Entry_Call, Done);
527             STPO.Unlock (Caller);
528          end if;
529       end if;
530
531    exception                       --  not needed in no exc mode
532       when others =>               --  not needed in no exc mode
533          Send_Program_Error        --  not needed in no exc mode
534            (Self_Id, Entry_Call);  --  not needed in no exc mode
535    end Service_Entry;
536
537    ---------------------------------------
538    -- Timed_Protected_Single_Entry_Call --
539    ---------------------------------------
540
541    --  Compiler interface only. Do not call from within the RTS.
542
543    procedure Timed_Protected_Single_Entry_Call
544      (Object                : Protection_Entry_Access;
545       Uninterpreted_Data    : System.Address;
546       Timeout               : Duration;
547       Mode                  : Delay_Modes;
548       Entry_Call_Successful : out Boolean)
549    is
550       Self_Id           : constant Task_ID  := STPO.Self;
551       Entry_Call        : Entry_Call_Record renames Self_Id.Entry_Calls (1);
552       Ceiling_Violation : Boolean;
553
554    begin
555       STPO.Write_Lock (Object.L'Access, Ceiling_Violation);
556
557       if Ceiling_Violation then
558          raise Program_Error;
559       end if;
560
561       Entry_Call.Mode := Timed_Call;
562       Entry_Call.State := Now_Abortable;
563       Entry_Call.Uninterpreted_Data := Uninterpreted_Data;
564       Entry_Call.Exception_To_Raise := Ada.Exceptions.Null_Id;
565
566       PO_Do_Or_Queue (Self_Id, Object, Entry_Call'Access);
567       Unlock_Entry (Object);
568
569       --  Try to avoid waiting for completed calls.
570       --  The call is either `Done' or not. It cannot be cancelled since there
571       --  is no ATC construct and the timed wait has not started yet.
572
573       pragma Assert (Entry_Call.State /= Cancelled);
574
575       if Entry_Call.State = Done then
576          Check_Exception (Self_Id, Entry_Call'Access);
577          Entry_Call_Successful := True;
578          return;
579       end if;
580
581       Wait_For_Completion_With_Timeout
582         (Self_Id, Entry_Call'Access, Timeout, Mode);
583
584       pragma Assert (Entry_Call.State >= Done);
585
586       Check_Exception (Self_Id, Entry_Call'Access);
587       Entry_Call_Successful := Entry_Call.State = Done;
588    end Timed_Protected_Single_Entry_Call;
589
590    ------------------
591    -- Unlock_Entry --
592    ------------------
593
594    procedure Unlock_Entry (Object : Protection_Entry_Access) is
595    begin
596       STPO.Unlock (Object.L'Access);
597    end Unlock_Entry;
598
599 end System.Tasking.Protected_Objects.Single_Entry;