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=-0.5 required=5.0 tests=BAYES_00,INVALID_MSGID, PP_MIME_FAKE_ASCII_TEXT autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII X-Google-Thread: ffc1e,fb45e48e8dddeabd X-Google-Attributes: gidffc1e,public X-Google-Thread: 103376,fb45e48e8dddeabd X-Google-Attributes: gid103376,public From: ka@hiway1.exit109.com (Kenneth Almquist) Subject: Re: Ada Protected Object Tutorial #1 Date: 1999/12/24 Message-ID: <83v08h$f2o$1@news1.exit109.com>#1/1 X-Deja-AN: 564448045 References: <839toq$pu$1@bgtnsc03.worldnet.att.net> <3858E5B3.9AB004E9@bton.ac.uk> <385984BC.1FB1@hello.nl> X-Trace: news1.exit109.com 946013265 15448 208.225.64.3 (24 Dec 1999 05:27:45 GMT) Organization: None NNTP-Posting-Date: 24 Dec 1999 05:27:45 GMT Newsgroups: comp.programming.threads,comp.lang.ada Originator: ka@exit109.com (Ken Lamquist) Date: 1999-12-24T05:27:45+00:00 List-Id: Karel Th�nissen wrote: > I am wondering whether there is a clean solution to the problem that Mr > Kylheku presented: how can we combine several protected type services > into one transaction-like construct without requiring clairvoyance or > retrofitting. One approach is to have the client perform the locking, and to specify an interface to the object which forces the client to lock the object before performing any other operations on it. E.g.: package Object_Package is type Object is limited private; type Object_Ptr is access all Obj; -- Object is the type of the objects we want to protect. type Object_Handle is limited private; -- An Object_Handle is used to refer to an Object. Only one -- Object_Handle is allowed to refer to a given object at -- any one time, so if you have an Object_Handle referring -- to an Object, you can safely operate on that Object -- without worrying about another task modifying the -- Object. procedure Set_Handle(Handle : Object_Handle, Obj : Object_Ptr); -- If Obj is not null, then this routine will make Handle -- refer to the specified Object. If another Object_Handle -- current refers to the Object, then this routine will -- block until that is no longer the case. -- -- Since only one Object_Handle can refer to an Object at -- any one time, when you are done using an Object, you -- should make the Object_Handle stop referring to it, -- either by calling Set_Handle with the Obj parameter -- set to null, or by destroying the Object_Handle. procecure Op_1(Handle : Object_Handle); procecure Op_2(Handle : Object_Handle); -- These are operations which apply to Objects. To call them, -- you pass an Object_Handle rather than an Object. This -- ensures that two tasks cannot perform an operation on the -- same object at the same time. procedure Op_1(Obj : Object_Ptr); -- This is a convenience function, intended to be used when -- you only want to perform a single Op_1 on an object. It -- is equivalent to: -- -- procedure Op_1(Obj : Object_Ptr) is -- Handle : Object_Handle; -- begin -- Set_Handle(Handle, Obj); -- Op_1(Handle); -- end Op_1; end Object_Package; Object is implemented as a protected type, and includes Lock and Unlock operations. An Object_Handle is a record containing an Object_Ptr. Set_Handle unlocks the Object currently pointed to by the Object_Handle (unless the handle is null), and locks the new Object (unless the new Object_Ptr is null). Object_Handle is implemented as a controlled type, so that the Object it refers to can be freed when the handle is destroyed. The second version of Op_1 can be implemented as shown in the specification, but it may be more efficient to have it call a protected entry whose barrier condition specifies that the semaphore is unlocked. Kenneth Almquist