comp.lang.ada
 help / color / mirror / Atom feed
From: "Nick Roberts" <nickroberts@adaos.worldonline.co.uk>
Subject: Re: How to avoid unreferenced objects (mutexes etc)
Date: Sat, 12 Jan 2002 01:11:43 -0000
Date: 2002-01-12T01:11:43+00:00	[thread overview]
Message-ID: <a1o2h1$sfe3f$5@ID-25716.news.dfncis.de> (raw)
In-Reply-To: 3c3ee8c8.105408250@News.CIS.DFN.DE

"Dmitry A. Kazakov" <dmitry@elros.cbb-automation.de> wrote in message
news:3c3ee8c8.105408250@News.CIS.DFN.DE...

> protected type Mutex is
>    entry Seize;
>    procedure Release;
> private
>    Owned : Boolean := False;
> end Mutex;
>
> type Lock (Resource : access Mutex) is new
>    Ada.Finalization.Limited_Controlled with null record;

> procedure Initialize (Object : in out Lock) is
> begin
>    Object.Resource.Seize;
> end Initialize;

> procedure Finalize (Object : in out Lock) is
> begin
>    Object.Resource.Release;
> end Finalize;
>
> The idea is to write critical sections as follows:
>
>    Temp : Lock (Mutex_of_a_resource'Access);
> begin
>    ...  -- Safe access to the resource
> end; -- Mutex is released even if an exception propagates


This may well be simply my own taste, but I would much prefer the critical
section to be written thus:


  type Indirect_Mutex is access all Mutex;

  The_Lock: aliased Mutex;

  ...

    Lock: [constant] Indirect_Mutex := The_Lock'Access;
  begin
    -- here we can do any non-critical pre-processing
    Lock.Seize;
    begin
      -- critical code goes here
    exception
      when others =>
        -- reset resource state to something stable
    end;
    Lock.Release;
    -- here we can do any non-critical post-processing
  end;


This way, no mucking about with finalisation or unreferenced objects is
required, and you have natural places to put pre- and post-processing code.
More importantly, this scheme ensures that if some critical processing
(presumably messing about with the state of the resource) does go wrong, the
resource is 'cleaned up' before any other innocent task can try using it.

However, really, a much better design is like this:


  protected type My_Resource_Type is
    procedure Do_Something_Critical;
  private
    ... -- resource state
  end;

  ...

  protected body My_Resource_Type is

    procedure Do_Something_Critical is
    begin
      -- critical code here
    exception
      when others =>
        -- reset resource state to something stable
    end;

  end My_Resource_Type;

  ...

  The_Resource: My_Resource_Type;

  ...

  begin
    -- pre-processing
    The_Resource.Do_Something_Critical;
    -- post-processing
  end;


The general idea is that a mutex is a low-level synchronisation construct,
and Ada provides a higher-level construct (protected objects) that takes
away much of the pain and danger of using mutexes directly.

--
Best wishes,
Nick Roberts






  parent reply	other threads:[~2002-01-12  1:11 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-01-11 13:48 How to avoid unreferenced objects (mutexes etc) Dmitry A. Kazakov
2002-01-11 13:52 ` Lutz Donnerhacke
2002-01-11 14:47 ` Robert A Duff
2002-01-11 18:02 ` Jeffrey Carter
2002-01-11 19:40 ` Robert Dewar
2002-01-12 10:18   ` Martin Dowie
2002-01-14  8:54   ` Dmitry A. Kazakov
2002-01-12  1:11 ` Nick Roberts [this message]
2002-01-12 22:04   ` Matthew Heaney
2002-01-13  5:45     ` Nick Roberts
2002-01-13  8:21       ` tmoran
2002-01-13 16:12         ` Nick Roberts
2002-01-13 15:08       ` Simon Wright
2002-01-15 17:53         ` Nick Roberts
2002-01-13 16:51       ` Jeffrey Carter
2002-01-14 23:32       ` Matthew Heaney
2002-01-15  8:53         ` Dmitry A. Kazakov
2002-01-14  8:31     ` Jean-Pierre Rosen
2002-01-14  9:42   ` Dmitry A. Kazakov
2002-01-15 15:41     ` Matthew Heaney
2002-01-15 16:18       ` Hyman Rosen
2002-01-15 16:57       ` Darren New
2002-01-15 18:57         ` Matthew Heaney
2002-01-16  0:57           ` Darren New
2002-01-16 16:35             ` Stephen Leake
2002-01-16 18:07               ` Darren New
2002-01-16 23:18                 ` Matthew Heaney
2002-01-16 23:04             ` Matthew Heaney
2002-01-17  0:21               ` Darren New
2002-01-16 15:18       ` Dmitry A. Kazakov
2002-01-15 18:59     ` Nick Roberts
2002-01-16 15:05       ` Dmitry A. Kazakov
2002-01-16 18:30         ` Matthew Heaney
2002-01-17  8:58           ` Dmitry A. Kazakov
2002-01-17  9:19             ` Lutz Donnerhacke
2002-01-17 10:42               ` Dmitry A. Kazakov
2002-01-17 10:55                 ` Lutz Donnerhacke
2002-01-17 15:30                   ` Dmitry A. Kazakov
2002-01-17 16:29                     ` Lutz Donnerhacke
2002-01-16 20:28         ` Robert A Duff
2002-01-17 19:05         ` Nick Roberts
replies disabled

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