comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <matthew_heaney@acm.org>
Subject: Re: Controlled types for resource locking
Date: 1999/09/08
Date: 1999-09-08T00:00:00+00:00	[thread overview]
Message-ID: <37d6dde9@news1.prserv.net> (raw)
In-Reply-To: 936824686.950.0.nnrp-07.c2de848f@news.demon.co.uk

In article <936824686.950.0.nnrp-07.c2de848f@news.demon.co.uk> , "Steve 
Folly" <steve@follysplace.demon.co.uk> wrote:


> A couple of months ago (maybe more?) I remember somebody posted some code,
> or a link to some code to do with controlled types and how they can be
> useful for resource locking.
>
> If anyone remembers this, or indeed if you were the person who posted it, I
> would be grateful for any link or reference to it.

I did a bunch of examples of this for the Ada95 design patterns list.  All
of my posts were archived:

<http://www.acm.org/archives/patterns.html>

The idiom you're looking for was done this year (1999), around April.  There
were several implementations of the Dining Philosophers problem, each
featuring different resource control techniques.

I'll be covering this stuff in my Design Patterns tutorial at this year's
SIGAda conference.


> I have some code in which I'm protecting a hardware resource using a
> protected type. It's getting quite a mess at the moment with lock/unlocks
> all over the place - especially where exception handlers get involved. I'm
> sure this code could come in handy.

Yep.  The basic idea goes something like this:

package Semaphores.Binary is

  protected type Semaphore_Type is

    entry Seize;
    procedure Release;

  end Semaphore_Type;

end Semaphores.Binary;


package Semaphores.Binary.Controls is

  type Semaphore_Control
    (Semaphore : access Semaphore_Type) is limited private;

private

  type Semaphore_Control (...) is
    new Limited_Controlled with null record;

  procedure Initialize (...);

  procedure Finalize (...);

end Semaphores.Binary.Controls;


package body Semaphores.Binary.Controls is

  procedure Initialize (Control : in out Sema_Cont) is
  begin
    Control.Semaphore.Seize;
  end;

  procedure Finalize (Control : in out Sema_Cont) is
  begin
    Control.Semaphore.Release;
  end;

end Semaphores.Binary.Controls;



You use it like this:

package body P is

  Sema : aliased Semaphore_Type;

  procedure Op is
    Control : Semaphore_Control (Sema'Access);
  begin
    <do whatever, even if it raises an exception>
  end Op;

...
end P;

You can even do it in an ordinary declare-block:

  declare
    Control : Semaphore_Control (Sema'Access);
  begin
    <do your little algorithm here>
  end;


Of course, this isn't the quite the same as using a protected object
directly, to manage access to the resource.  With a protected operation, you
can't make blocking calls, which often makes it unusable as a resource
control mechanism.  The approach I've shown above does not suffer from this
limitation, although it's likely to be slightly less efficient.  It's a
trade-off.

Matt

P.S. BTW, this limitation of protected objects is something the language
maintainers need to address.




  parent reply	other threads:[~1999-09-08  0:00 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-09-08  0:00 Controlled types for resource locking Steve Folly
1999-09-08  0:00 ` David C. Hoos, Sr.
1999-09-08  0:00 ` Matthew Heaney [this message]
1999-09-08  0:00 ` tmoran
1999-09-09  0:00   ` Pat Rogers
1999-09-09  0:00     ` Matthew Heaney
1999-09-09  0:00       ` Pat Rogers
1999-09-09  0:00         ` Matthew Heaney
1999-09-09  0:00           ` Pat Rogers
1999-09-09  0:00   ` Ted Dennison
1999-09-09  0:00     ` Tucker Taft
replies disabled

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