diff mbox

[Ada] mutex attributes not initialized in the linux run-time library

Message ID 20110902064902.GA17598@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet Sept. 2, 2011, 6:49 a.m. UTC
Before this patch, the mutex attribute was used (by Initialize_TCB from
the environment task) before it was initialized later by Initialize. This
patch creates and initializes mutex and condition variable attributes locally
when needed to avoid this problem.

The following test case should compile and execute without producing any
output when recompiling the run-time library.

gnatmake -f -q -gnato -a -gnatws initialization
---
pragma Normalize_Scalars;

procedure Initialization is
   task T;

   task body T is
   begin
      null;
   end T;
begin
   null;
end Initialization;

Tested on x86_64-pc-linux-gnu, committed on trunk

2011-09-02  Jose Ruiz  <ruiz@adacore.com>

	* s-taprop-linux.adb (Initialize_Lock, Initialize_TCB,
	Initialize): Define and initialize the
	mutex attributes and condition variable attributes locally.
diff mbox

Patch

Index: s-taprop-linux.adb
===================================================================
--- s-taprop-linux.adb	(revision 178418)
+++ s-taprop-linux.adb	(working copy)
@@ -97,12 +97,6 @@ 
    Dispatching_Policy : Character;
    pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
 
-   --  The following are effectively constants, but they need to be initialized
-   --  by calling a pthread_ function.
-
-   Mutex_Attr   : aliased pthread_mutexattr_t;
-   Cond_Attr    : aliased pthread_condattr_t;
-
    Foreign_Task_Elaborated : aliased Boolean := True;
    --  Used to identified fake tasks (i.e., non-Ada Threads)
 
@@ -261,9 +255,13 @@ 
    is
       pragma Unreferenced (Prio);
 
-      Result : Interfaces.C.int;
+      Mutex_Attr : aliased pthread_mutexattr_t;
+      Result     : Interfaces.C.int;
 
    begin
+      Result := pthread_mutexattr_init (Mutex_Attr'Access);
+      pragma Assert (Result = 0);
+
       Result := pthread_mutex_init (L, Mutex_Attr'Access);
 
       pragma Assert (Result = 0 or else Result = ENOMEM);
@@ -279,9 +277,13 @@ 
    is
       pragma Unreferenced (Level);
 
-      Result : Interfaces.C.int;
+      Mutex_Attr : aliased pthread_mutexattr_t;
+      Result     : Interfaces.C.int;
 
    begin
+      Result := pthread_mutexattr_init (Mutex_Attr'Access);
+      pragma Assert (Result = 0);
+
       Result := pthread_mutex_init (L, Mutex_Attr'Access);
 
       pragma Assert (Result = 0 or else Result = ENOMEM);
@@ -762,7 +764,9 @@ 
    --------------------
 
    procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
-      Result : Interfaces.C.int;
+      Mutex_Attr : aliased pthread_mutexattr_t;
+      Cond_Attr  : aliased pthread_condattr_t;
+      Result     : Interfaces.C.int;
 
    begin
       --  Give the task a unique serial number
@@ -774,6 +778,9 @@ 
       Self_ID.Common.LL.Thread := Null_Thread_Id;
 
       if not Single_Lock then
+         Result := pthread_mutexattr_init (Mutex_Attr'Access);
+         pragma Assert (Result = 0);
+
          Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
            Mutex_Attr'Access);
          pragma Assert (Result = 0 or else Result = ENOMEM);
@@ -784,6 +791,9 @@ 
          end if;
       end if;
 
+      Result := pthread_condattr_init (Cond_Attr'Access);
+      pragma Assert (Result = 0);
+
       Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
         Cond_Attr'Access);
       pragma Assert (Result = 0 or else Result = ENOMEM);
@@ -1027,7 +1037,9 @@ 
    ----------------
 
    procedure Initialize (S : in out Suspension_Object) is
-      Result : Interfaces.C.int;
+      Mutex_Attr : aliased pthread_mutexattr_t;
+      Cond_Attr  : aliased pthread_condattr_t;
+      Result     : Interfaces.C.int;
 
    begin
       --  Initialize internal state (always to False (RM D.10(6)))
@@ -1037,6 +1049,9 @@ 
 
       --  Initialize internal mutex
 
+      Result := pthread_mutexattr_init (Mutex_Attr'Access);
+      pragma Assert (Result = 0);
+
       Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
 
       pragma Assert (Result = 0 or else Result = ENOMEM);
@@ -1047,6 +1062,9 @@ 
 
       --  Initialize internal condition variable
 
+      Result := pthread_condattr_init (Cond_Attr'Access);
+      pragma Assert (Result = 0);
+
       Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
 
       pragma Assert (Result = 0 or else Result = ENOMEM);
@@ -1340,12 +1358,6 @@ 
          end if;
       end loop;
 
-      Result := pthread_mutexattr_init (Mutex_Attr'Access);
-      pragma Assert (Result = 0);
-
-      Result := pthread_condattr_init (Cond_Attr'Access);
-      pragma Assert (Result = 0);
-
       Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
 
       --  Initialize the global RTS lock