comp.lang.ada
 help / color / mirror / Atom feed
From: Jano <nono@celes.unizar.es>
Subject: Suspicious reentrant semaphore
Date: Wed, 30 Apr 2003 00:04:23 +0200
Date: 2003-04-30T00:04:23+02:00	[thread overview]
Message-ID: <MPG.19191554aff631919896ed@News.CIS.DFN.DE> (raw)

Hello everybody,

I'm developing a multitasking program that runs OK for several hours. At 
some indeterminate moment, a bunch of its tasks cease to respond, and I 
have a strong suspicion that the culprit is a semaphore package I've 
done.

The purpose is to have a semaphore which can be called more than a time 
_by the same task_, and that must be released the same amount of times. 
Nested calls, simply.

Leaving aside why I have done that (yes, poor design, I'm removing it 
anyway ;-), I would like to know if my package is right or really is 
there some race condition. Please comment on it. I know that 
Current_Task shouldn't be called inside entries (in fact Gnat doesn't 
allow it, I tried :-P) but doesn't complain about it being used in 
barriers. Anyways, I have a (maybe wrong) feel about that being related 
to the deadlock.

Here are the spec and body:

P.s: I can guarantee that never use it without the controlled object, so 
no calls to V should occur without a previous P.

P.s: If it turns out that this isn't the culprit, I will amuse you ;-) 
with more samples of my code ^_^

Thanks!

------------SPEC---------------------------------------------------

package Adagio.Monitor is

   use Ada.Task_identification;
   
   protected type Semaphore is
      entry P (Owner : Task_id);
      entry V;
   private
      Caller : Task_id := Null_task_id;           -- Requester
      In_use : Natural := 0;                      -- Times requested
   end Semaphore;

   type Semaphore_access is access all Semaphore;

   -- Use: 
   -- S : aliased Semaphore;
   -- M : Object (S'access);
   type Object (S : access Semaphore) is new 
      Finalization.Limited_Controlled with null record;

   procedure Initialize (this : in out Object);
   procedure Finalize   (this : in out Object);

end Adagio.Monitor;

---------------------BODY----------------------------------

package body Adagio.Monitor is

   protected body Semaphore is

      entry P (Owner: Task_id) 
         when In_use = 0 or else Current_task = Caller is
      begin
         Caller := Owner;
         In_use := In_use + 1;
      end P;

      entry V when In_use > 0 and Current_task = Caller is
      begin
         In_use := In_use - 1;
      end V;

   end Semaphore;

   -- Get
   procedure Initialize (this : in out Object) is
   begin
      this.S.P (Current_task);
   end Initialize;

   -- Release
   procedure Finalize (this : in out Object) is
   begin
      this.S.V;
   end Finalize;

end Adagio.Monitor;

-- 
-------------------------
Jano
402450.at.cepsz.unizar.es
-------------------------



             reply	other threads:[~2003-04-29 22:04 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-29 22:04 Jano [this message]
2003-04-30  8:06 ` Suspicious reentrant semaphore Dmitry A. Kazakov
2003-05-01 12:33   ` Jano
2003-05-02  7:28     ` Dmitry A. Kazakov
2003-05-02 11:24       ` Jano
2003-05-02 20:29         ` tmoran
2003-05-05 11:24       ` Michal Morawski
2003-05-05 17:42         ` Simon Wright
2003-05-05 18:03         ` Jano
2003-05-05 20:18           ` Micha� Morawski
2003-04-30 13:07 ` Ian Broster
2003-05-01 12:33   ` Jano
2003-04-30 17:39 ` Randy Brukardt
2003-04-30 23:21   ` Peter Richtmyer
2003-05-01  4:59     ` Simon Wright
2003-05-01 12:46   ` Jano
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox