OSDN Git Service

* targhooks.c (default_stack_protect_guard): Avoid sharing RTL
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-tposen.adb
index b262add..10cfca2 100644 (file)
@@ -1,77 +1,68 @@
 ------------------------------------------------------------------------------
 --                                                                          --
---               GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS                --
+--                GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS                  --
 --                                                                          --
---              SYSTEM.TASKING.PROTECTED_OBJECTS.SINGLE_ENTRY               --
+--             SYSTEM.TASKING.PROTECTED_OBJECTS.SINGLE_ENTRY                --
 --                                                                          --
---                                  B o d y                                 --
+--                                B o d y                                   --
 --                                                                          --
---                                                                          --
---         Copyright (C) 1998-2002, Free Software Foundation, Inc.          --
+--         Copyright (C) 1998-2009, Free Software Foundation, Inc.          --
 --                                                                          --
 -- GNARL is free software; you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
--- ware  Foundation;  either version 2,  or (at your option) any later ver- --
--- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
+-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
+-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
--- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
--- for  more details.  You should have  received  a copy of the GNU General --
--- Public License  distributed with GNARL; see file COPYING.  If not, write --
--- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
--- MA 02111-1307, USA.                                                      --
+-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
+--                                                                          --
+-- As a special exception under Section 7 of GPL version 3, you are granted --
+-- additional permissions described in the GCC Runtime Library Exception,   --
+-- version 3.1, as published by the Free Software Foundation.               --
 --                                                                          --
--- As a special exception,  if other files  instantiate  generics from this --
--- unit, or you link  this unit with other files  to produce an executable, --
--- this  unit  does not  by itself cause  the resulting  executable  to  be --
--- covered  by the  GNU  General  Public  License.  This exception does not --
--- however invalidate  any other reasons why  the executable file  might be --
--- covered by the  GNU Public License.                                      --
+-- You should have received a copy of the GNU General Public License and    --
+-- a copy of the GCC Runtime Library Exception along with this program;     --
+-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
+-- <http://www.gnu.org/licenses/>.                                          --
 --                                                                          --
--- GNARL was developed by the GNARL team at Florida State University. It is --
--- now maintained by Ada Core Technologies, Inc. (http://www.gnat.com).     --
+-- GNARL was developed by the GNARL team at Florida State University.       --
+-- Extensive contributions were provided by Ada Core Technologies, Inc.     --
 --                                                                          --
 ------------------------------------------------------------------------------
 
 pragma Style_Checks (All_Checks);
---  Turn off subprogram ordering check, since restricted GNARLI
---  subprograms are gathered together at end.
+--  Turn off subprogram ordering check, since restricted GNARLI subprograms are
+--  gathered together at end.
 
 --  This package provides an optimized version of Protected_Objects.Operations
 --  and Protected_Objects.Entries making the following assumptions:
---
---  PO have only one entry
---  There is only one caller at a time (No_Entry_Queue)
---  There is no dynamic priority support (No_Dynamic_Priorities)
---  No Abort Statements
---    (No_Abort_Statements, Max_Asynchronous_Select_Nesting => 0)
---  PO are at library level
---  No Requeue
---  None of the tasks will terminate (no need for finalization)
---
+
+--    PO has only one entry
+--    There is only one caller at a time (No_Entry_Queue)
+--    There is no dynamic priority support (No_Dynamic_Priorities)
+--    No Abort Statements
+--     (No_Abort_Statements, Max_Asynchronous_Select_Nesting => 0)
+--    PO are at library level
+--    No Requeue
+--    None of the tasks will terminate (no need for finalization)
+
 --  This interface is intended to be used in the ravenscar and restricted
 --  profiles, the compiler is responsible for ensuring that the conditions
 --  mentioned above are respected, except for the No_Entry_Queue restriction
 --  that is checked dynamically in this package, since the check cannot be
 --  performed at compile time, and is relatively cheap (see PO_Do_Or_Queue,
---  PO_Service_Entry).
+--  Service_Entry).
 
 pragma Polling (Off);
 --  Turn off polling, we do not want polling to take place during tasking
 --  operations. It can cause  infinite loops and other problems.
 
 pragma Suppress (All_Checks);
-
-with System.Task_Primitives.Operations;
---  used for Self
---           Finalize_Lock
---           Write_Lock
---           Unlock
+--  Why is this required ???
 
 with Ada.Exceptions;
---  used for Exception_Id;
 
+with System.Task_Primitives.Operations;
 with System.Parameters;
---  used for Single_Lock
 
 package body System.Tasking.Protected_Objects.Single_Entry is
 
@@ -84,7 +75,7 @@ package body System.Tasking.Protected_Objects.Single_Entry is
    -----------------------
 
    procedure Send_Program_Error
-     (Self_Id    : Task_ID;
+     (Self_Id    : Task_Id;
       Entry_Call : Entry_Call_Link);
    pragma Inline (Send_Program_Error);
    --  Raise Program_Error in the caller of the specified entry call
@@ -94,7 +85,7 @@ package body System.Tasking.Protected_Objects.Single_Entry is
    --------------------------
 
    procedure Wakeup_Entry_Caller
-     (Self_ID    : Task_ID;
+     (Self_ID    : Task_Id;
       Entry_Call : Entry_Call_Link;
       New_State  : Entry_Call_State);
    pragma Inline (Wakeup_Entry_Caller);
@@ -122,7 +113,7 @@ package body System.Tasking.Protected_Objects.Single_Entry is
    --  specified in Wakeup_Time as well.
 
    procedure Check_Exception
-     (Self_ID : Task_ID;
+     (Self_ID : Task_Id;
       Entry_Call : Entry_Call_Link);
    pragma Inline (Check_Exception);
    --  Raise any pending exception from the Entry_Call.
@@ -131,7 +122,7 @@ package body System.Tasking.Protected_Objects.Single_Entry is
    --  The caller should not be holding any locks, or there will be deadlock.
 
    procedure PO_Do_Or_Queue
-     (Self_Id    : Task_ID;
+     (Self_Id    : Task_Id;
       Object     : Protection_Entry_Access;
       Entry_Call : Entry_Call_Link);
    --  This procedure executes or queues an entry call, depending
@@ -143,7 +134,7 @@ package body System.Tasking.Protected_Objects.Single_Entry is
    ---------------------
 
    procedure Check_Exception
-     (Self_ID    : Task_ID;
+     (Self_ID    : Task_Id;
       Entry_Call : Entry_Call_Link)
    is
       pragma Warnings (Off, Self_ID);
@@ -154,7 +145,7 @@ package body System.Tasking.Protected_Objects.Single_Entry is
       use type Ada.Exceptions.Exception_Id;
 
       E : constant Ada.Exceptions.Exception_Id :=
-        Entry_Call.Exception_To_Raise;
+            Entry_Call.Exception_To_Raise;
 
    begin
       if E /= Ada.Exceptions.Null_Id then
@@ -167,10 +158,10 @@ package body System.Tasking.Protected_Objects.Single_Entry is
    ------------------------
 
    procedure Send_Program_Error
-     (Self_Id    : Task_ID;
+     (Self_Id    : Task_Id;
       Entry_Call : Entry_Call_Link)
    is
-      Caller : constant Task_ID := Entry_Call.Self;
+      Caller : constant Task_Id := Entry_Call.Self;
    begin
       Entry_Call.Exception_To_Raise := Program_Error'Identity;
 
@@ -192,7 +183,7 @@ package body System.Tasking.Protected_Objects.Single_Entry is
    -------------------------
 
    procedure Wait_For_Completion (Entry_Call : Entry_Call_Link) is
-      Self_Id : constant Task_ID := Entry_Call.Self;
+      Self_Id : constant Task_Id := Entry_Call.Self;
    begin
       Self_Id.Common.State := Entry_Caller_Sleep;
       STPO.Sleep (Self_Id, Entry_Caller_Sleep);
@@ -208,9 +199,11 @@ package body System.Tasking.Protected_Objects.Single_Entry is
       Wakeup_Time : Duration;
       Mode        : Delay_Modes)
    is
-      Self_Id  : constant Task_ID := Entry_Call.Self;
+      Self_Id  : constant Task_Id := Entry_Call.Self;
       Timedout : Boolean;
+
       Yielded  : Boolean;
+      pragma Unreferenced (Yielded);
 
       use type Ada.Exceptions.Exception_Id;
 
@@ -238,12 +231,7 @@ package body System.Tasking.Protected_Objects.Single_Entry is
       STPO.Timed_Sleep
         (Self_Id, Wakeup_Time, Mode, Entry_Caller_Sleep, Timedout, Yielded);
 
-      if Timedout then
-         Entry_Call.State := Cancelled;
-      else
-         Entry_Call.State := Done;
-      end if;
-
+      Entry_Call.State := (if Timedout then Cancelled else Done);
       Self_Id.Common.State := Runnable;
    end Wait_For_Completion_With_Timeout;
 
@@ -268,13 +256,13 @@ package body System.Tasking.Protected_Objects.Single_Entry is
    --    to complete.
 
    procedure Wakeup_Entry_Caller
-     (Self_ID    : Task_ID;
+     (Self_ID    : Task_Id;
       Entry_Call : Entry_Call_Link;
       New_State  : Entry_Call_State)
    is
       pragma Warnings (Off, Self_ID);
 
-      Caller : constant Task_ID := Entry_Call.Self;
+      Caller : constant Task_Id := Entry_Call.Self;
 
    begin
       pragma Assert (New_State = Done or else New_State = Cancelled);
@@ -325,14 +313,9 @@ package body System.Tasking.Protected_Objects.Single_Entry is
       Compiler_Info     : System.Address;
       Entry_Body        : Entry_Body_Access)
    is
-      Init_Priority : Integer := Ceiling_Priority;
    begin
-      if Init_Priority = Unspecified_Priority then
-         Init_Priority := System.Priority'Last;
-      end if;
+      Initialize_Protection (Object.Common'Access, Ceiling_Priority);
 
-      STPO.Initialize_Lock (Init_Priority, Object.L'Access);
-      Object.Ceiling := System.Any_Priority (Init_Priority);
       Object.Compiler_Info := Compiler_Info;
       Object.Call_In_Progress := null;
       Object.Entry_Body := Entry_Body;
@@ -347,30 +330,21 @@ package body System.Tasking.Protected_Objects.Single_Entry is
    --  Do not call this procedure from within the run-time system.
 
    procedure Lock_Entry (Object : Protection_Entry_Access) is
-      Ceiling_Violation : Boolean;
    begin
-      STPO.Write_Lock (Object.L'Access, Ceiling_Violation);
-
-      if Ceiling_Violation then
-         raise Program_Error;
-      end if;
+      Lock (Object.Common'Access);
    end Lock_Entry;
 
    --------------------------
    -- Lock_Read_Only_Entry --
    --------------------------
 
-   --  Compiler interface only.
-   --  Do not call this procedure from within the runtime system.
+   --  Compiler interface only
+
+   --  Do not call this procedure from within the runtime system
 
    procedure Lock_Read_Only_Entry (Object : Protection_Entry_Access) is
-      Ceiling_Violation : Boolean;
    begin
-      STPO.Read_Lock (Object.L'Access, Ceiling_Violation);
-
-      if Ceiling_Violation then
-         raise Program_Error;
-      end if;
+      Lock_Read_Only (Object.Common'Access);
    end Lock_Read_Only_Entry;
 
    --------------------
@@ -378,11 +352,12 @@ package body System.Tasking.Protected_Objects.Single_Entry is
    --------------------
 
    procedure PO_Do_Or_Queue
-     (Self_Id    : Task_ID;
+     (Self_Id    : Task_Id;
       Object     : Protection_Entry_Access;
       Entry_Call : Entry_Call_Link)
    is
       Barrier_Value : Boolean;
+
    begin
       --  When the Action procedure for an entry body returns, it must be
       --  completed (having called [Exceptional_]Complete_Entry_Body).
@@ -391,6 +366,7 @@ package body System.Tasking.Protected_Objects.Single_Entry is
 
       if Barrier_Value then
          if Object.Call_In_Progress /= null then
+
             --  This violates the No_Entry_Queue restriction, send
             --  Program_Error to the caller.
 
@@ -416,7 +392,17 @@ package body System.Tasking.Protected_Objects.Single_Entry is
          end if;
 
       elsif Entry_Call.Mode /= Conditional_Call then
-         Object.Entry_Queue := Entry_Call;
+         if Object.Entry_Queue /= null then
+
+            --  This violates the No_Entry_Queue restriction, send
+            --  Program_Error to the caller.
+
+            Send_Program_Error (Self_Id, Entry_Call);
+            return;
+         else
+            Object.Entry_Queue := Entry_Call;
+         end if;
+
       else
          --  Conditional_Call
 
@@ -445,7 +431,7 @@ package body System.Tasking.Protected_Objects.Single_Entry is
 
    function Protected_Count_Entry (Object : Protection_Entry) return Natural is
    begin
-      if Object.Call_In_Progress /= null then
+      if Object.Entry_Queue /= null then
          return 1;
       else
          return 0;
@@ -461,17 +447,21 @@ package body System.Tasking.Protected_Objects.Single_Entry is
       Uninterpreted_Data : System.Address;
       Mode               : Call_Modes)
    is
-      Self_Id           : constant Task_ID := STPO.Self;
-      Entry_Call        : Entry_Call_Record renames Self_Id.Entry_Calls (1);
-      Ceiling_Violation : Boolean;
-
+      Self_Id    : constant Task_Id := STPO.Self;
+      Entry_Call : Entry_Call_Record renames Self_Id.Entry_Calls (1);
    begin
-      STPO.Write_Lock (Object.L'Access, Ceiling_Violation);
-
-      if Ceiling_Violation then
-         raise Program_Error;
+      --  If pragma Detect_Blocking is active then Program_Error must be
+      --  raised if this potentially blocking operation is called from a
+      --  protected action.
+
+      if Detect_Blocking
+        and then Self_Id.Common.Protected_Action_Nesting > 0
+      then
+         raise Program_Error with "potentially blocking operation";
       end if;
 
+      Lock_Entry (Object);
+
       Entry_Call.Mode := Mode;
       Entry_Call.State := Now_Abortable;
       Entry_Call.Uninterpreted_Data := Uninterpreted_Data;
@@ -507,7 +497,7 @@ package body System.Tasking.Protected_Objects.Single_Entry is
    -----------------------------------
 
    function Protected_Single_Entry_Caller
-     (Object : Protection_Entry) return Task_ID is
+     (Object : Protection_Entry) return Task_Id is
    begin
       return Object.Call_In_Progress.Self;
    end Protected_Single_Entry_Caller;
@@ -517,54 +507,61 @@ package body System.Tasking.Protected_Objects.Single_Entry is
    -------------------
 
    procedure Service_Entry (Object : Protection_Entry_Access) is
-      Self_Id       : constant Task_ID := STPO.Self;
-      Entry_Call    : constant Entry_Call_Link := Object.Entry_Queue;
-      Caller        : Task_ID;
-      Barrier_Value : Boolean;
+      Self_Id    : constant Task_Id := STPO.Self;
+      Entry_Call : constant Entry_Call_Link := Object.Entry_Queue;
+      Caller     : Task_Id;
 
    begin
-      if Entry_Call /= null then
-         Barrier_Value := Object.Entry_Body.Barrier (Object.Compiler_Info, 1);
-
-         if Barrier_Value then
-            if Object.Call_In_Progress /= null then
-               --  This violates the No_Entry_Queue restriction, send
-               --  Program_Error to the caller.
-
-               Send_Program_Error (Self_Id, Entry_Call);
-               return;
-            end if;
-
-            Object.Call_In_Progress := Entry_Call;
-            Object.Entry_Body.Action
-              (Object.Compiler_Info, Entry_Call.Uninterpreted_Data, 1);
-            Object.Call_In_Progress := null;
-            Caller := Entry_Call.Self;
-
-            if Single_Lock then
-               STPO.Lock_RTS;
-            end if;
-
-            STPO.Write_Lock (Caller);
-            Wakeup_Entry_Caller (Self_Id, Entry_Call, Done);
-            STPO.Unlock (Caller);
-
-            if Single_Lock then
-               STPO.Unlock_RTS;
-            end if;
+      if Entry_Call /= null
+        and then Object.Entry_Body.Barrier (Object.Compiler_Info, 1)
+      then
+         Object.Entry_Queue := null;
+
+         if Object.Call_In_Progress /= null then
+
+            --  Violation of No_Entry_Queue restriction, raise exception
+
+            Send_Program_Error (Self_Id, Entry_Call);
+            Unlock_Entry (Object);
+            return;
          end if;
+
+         Object.Call_In_Progress := Entry_Call;
+         Object.Entry_Body.Action
+           (Object.Compiler_Info, Entry_Call.Uninterpreted_Data, 1);
+         Object.Call_In_Progress := null;
+         Caller := Entry_Call.Self;
+         Unlock_Entry (Object);
+
+         if Single_Lock then
+            STPO.Lock_RTS;
+         end if;
+
+         STPO.Write_Lock (Caller);
+         Wakeup_Entry_Caller (Self_Id, Entry_Call, Done);
+         STPO.Unlock (Caller);
+
+         if Single_Lock then
+            STPO.Unlock_RTS;
+         end if;
+
+      else
+         --  Just unlock the entry
+
+         Unlock_Entry (Object);
       end if;
 
    exception
       when others =>
          Send_Program_Error (Self_Id, Entry_Call);
+         Unlock_Entry (Object);
    end Service_Entry;
 
    ---------------------------------------
    -- Timed_Protected_Single_Entry_Call --
    ---------------------------------------
 
-   --  Compiler interface only. Do not call from within the RTS.
+   --  Compiler interface only (do not call from within the RTS)
 
    procedure Timed_Protected_Single_Entry_Call
      (Object                : Protection_Entry_Access;
@@ -573,17 +570,22 @@ package body System.Tasking.Protected_Objects.Single_Entry is
       Mode                  : Delay_Modes;
       Entry_Call_Successful : out Boolean)
    is
-      Self_Id           : constant Task_ID  := STPO.Self;
+      Self_Id           : constant Task_Id  := STPO.Self;
       Entry_Call        : Entry_Call_Record renames Self_Id.Entry_Calls (1);
-      Ceiling_Violation : Boolean;
 
    begin
-      STPO.Write_Lock (Object.L'Access, Ceiling_Violation);
-
-      if Ceiling_Violation then
-         raise Program_Error;
+      --  If pragma Detect_Blocking is active then Program_Error must be
+      --  raised if this potentially blocking operation is called from a
+      --  protected action.
+
+      if Detect_Blocking
+        and then Self_Id.Common.Protected_Action_Nesting > 0
+      then
+         raise Program_Error with "potentially blocking operation";
       end if;
 
+      Lock (Object.Common'Access);
+
       Entry_Call.Mode := Timed_Call;
       Entry_Call.State := Now_Abortable;
       Entry_Call.Uninterpreted_Data := Uninterpreted_Data;
@@ -630,7 +632,7 @@ package body System.Tasking.Protected_Objects.Single_Entry is
 
    procedure Unlock_Entry (Object : Protection_Entry_Access) is
    begin
-      STPO.Unlock (Object.L'Access);
+      Unlock (Object.Common'Access);
    end Unlock_Entry;
 
 end System.Tasking.Protected_Objects.Single_Entry;