[comp.sys.mac.programmer] HLock interdiction??

jtt@cunixd.cc.columbia.edu (James T. Tanis) (09/21/90)

while reading the September issue of MacTutor, in the Mousehole echo,
I saw a disturbing exchange...

the gist was, that HLock would _Unlock_ an object if it was locked to
begin with. Is this the case? Should I call HGetState first, to determine 
whether a handle is locked, and if so... with what mask???

For those interested, the exchange is on pg. 93 of the September issue.

thanks!

mxmora@unix.SRI.COM (Matt Mora) (09/21/90)

In article <1990Sep21.043054.24649@cunixf.cc.columbia.edu> jtt@cunixd.cc.columbia.edu (James T. Tanis) writes:
>while reading the September issue of MacTutor, in the Mousehole echo,
>I saw a disturbing exchange...
>
>the gist was, that HLock would _Unlock_ an object if it was locked to
>begin with. Is this the case? Should I call HGetState first, to determine 

This is not gist of it. You misunderstood what he meant. Hlock does not
toggle the lockedness (is that a word?) of a handle. What he meant was
that something else (maybe a rom routine) might have the handle locked for
a reason. The you come around lock the locked handle (nothing happens because
its already locked) do your thing then unlock it. Blamo, your program dies
a miserable death because the thing that locked it before, now receives an 
unlocked handle, problably moves some memory and then crashes.

You need to use Hgetstate/Hsetstate to keep the handle as it were. If you need
to lock a handle by all means do it. Just save the state of the handle before
hand and reset it back after you are done with it. Remeber that handles can 
have more states than just being locked. Also you probably don't need to do 
this with all handles, just the ones that you share with the ROM.




-- 
___________________________________________________________
Matthew Mora                |   my Mac  Matt_Mora@sri.com
SRI International           |  my unix  mxmora@unix.sri.com
___________________________________________________________

stevec@Apple.COM (Steve Christensen) (09/21/90)

jtt@cunixd.cc.columbia.edu (James T. Tanis) writes:
>while reading the September issue of MacTutor, in the Mousehole echo,
>I saw a disturbing exchange...
>
>the gist was, that HLock would _Unlock_ an object if it was locked to
>begin with. Is this the case? Should I call HGetState first, to determine 
>whether a handle is locked, and if so... with what mask???

Was this described as a bug or feature?  HLock has always locked a block for
me, and HUnlock has unlocked it.  It's never toggled the state.

steve

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 whoami?     Steve Christensen
 snail:      Apple Computer, 20525 Mariani Ave, MS-81CS, Cupertino, CA  95014
 Internet:   stevec@goofy.apple.com
 AppleLink:  stevec
 CompuServe: 76174,1712

philip@pescadero.Stanford.EDU (Philip Machanick) (09/22/90)

In article <16451@unix.SRI.COM>, mxmora@unix.SRI.COM (Matt Mora) writes:
> You need to use Hgetstate/Hsetstate to keep the handle as it were. If
you need
> to lock a handle by all means do it. Just save the state of the handle before
> hand and reset it back after you are done with it. Remeber that handles can 
> have more states than just being locked. Also you probably don't need to do 
> this with all handles, just the ones that you share with the ROM.

I actually prefer to use the Hgetstate/Hsetstate methodology generally. It's
easy to forget that a routine could be called from a place in your program
where a handle has already been locked. The only time I call hUnlock is
as part of initialization. After that, Hgetstate/Hsetstate will always get it
back to a safe condition after I've had to lock it.

Philip Machanick
philip@pescadero.stanford.edu

zellers@prisoner.Eng.Sun.COM (Steve Zellers) (09/22/90)

In article <10348@goofy.Apple.COM> stevec@Apple.COM (Steve Christensen) writes:
>jtt@cunixd.cc.columbia.edu (James T. Tanis) writes:
>>while reading the September issue of MacTutor, in the Mousehole echo,
>>I saw a disturbing exchange...
>>
>>the gist was, that HLock would _Unlock_ an object if it was locked to
>>begin with. Is this the case? Should I call HGetState first, to determine 
>>whether a handle is locked, and if so... with what mask???
>
>Was this described as a bug or feature?  HLock has always locked a block for
>me, and HUnlock has unlocked it.  It's never toggled the state.

  I've a feeling that this thread was based on the fact that, what with
all the data hiding and class library subclassing going on these days,
a Handle may be passed to a routine that may _or may not_ be locked.
In this case, it is _imperitive_ that you save it's state with HGetState
and smuk it back afterwards with HSetState if you need to ensure that
it's locked.
  If HLock toggled, all hell would break loose...
--
------------------------------------------------------------------------
Steve Zellers 					zellers@prisoner.sun.com
"And she wonders if the lizard likes its lettuce rare." -- The Residents

jtt@cunixd.cc.columbia.edu (James T. Tanis) (09/23/90)

Thanks for all the responses. Sure enough, The cause of my concern
was oop stuff. 

I suggest that anyone working on a browser for the TCL include a feature 
to flag locking and unlocking. If noone else is, and there is demand,
I'll see if I can remember enough YACC to do it...

-JT

guelzow@ccu.umanitoba.ca (Andreas J. Guelzow) (09/25/90)

In article <1990Sep21.043054.24649@cunixf.cc.columbia.edu> jtt@cunixd.cc.columbia.edu (James T. Tanis) writes:
>while reading the September issue of MacTutor, in the Mousehole echo,
>I saw a disturbing exchange...
>
>the gist was, that HLock would _Unlock_ an object if it was locked to
>begin with. Is this the case? Should I call HGetState first, to determine 
>whether a handle is locked, and if so... with what mask???
>
>For those interested, the exchange is on pg. 93 of the September issue.
>
>thanks!
The exchange in MacTutor has been misunderstood! The problem (?) is
that HUnlock will unlock the handle, i.e. calling HLock and later
HUnlock leaves the handle unlocked even if somewhere else it had been
locked previously (e.g. in a calling routine). This is a nice way to 
introduce bugs into a programme :-) .
Andreas Guelzow <guelzow@ccu.umanitoba.ca>