From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,28c043d47104f5d9 X-Google-Attributes: gid103376,public From: "Matthew Heaney" Subject: Re: Controlled types for resource locking Date: 1999/09/08 Message-ID: <37d6dde9@news1.prserv.net>#1/1 X-Deja-AN: 522696296 Content-transfer-encoding: 7bit References: <936824686.950.0.nnrp-07.c2de848f@news.demon.co.uk> Content-Type: text/plain; charset="US-ASCII" X-Complaints-To: abuse@prserv.net X-Trace: 8 Sep 1999 22:06:33 GMT, 32.101.8.55 Organization: Global Network Services - Remote Access Mail & News Services Mime-version: 1.0 Newsgroups: comp.lang.ada Date: 1999-09-08T00:00:00+00:00 List-Id: In article <936824686.950.0.nnrp-07.c2de848f@news.demon.co.uk> , "Steve Folly" 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: 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 end Op; ... end P; You can even do it in an ordinary declare-block: declare Control : Semaphore_Control (Sema'Access); begin 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.