[Gambas-devel] Question about singleton class

Tobias Boege taboege at ...176...
Sat Sep 13 21:03:46 CEST 2014


On Sat, 13 Sep 2014, John Leake wrote:
> Hi All,
> 
> >> I would agree that a note in the docs is due that closing a lock Stream
> >> is the same as unlocking it and that you can close your stream in some
> >> subtle ways such as the above one.
> 
> This is the bug.  Calling Lock twice from the same proc/instance should
> not close the existing fd.

You can call Lock as often as resources permit to gain as many valid locks,
all to the same file or to many different files.

The error is in your program. You have a valid lock in "h" but by replacing
that object with another object you are closing the stream represented by
the former object (because its reference count reaches zero) which removes
the lock according to POSIX. In essence, you are releasing the lock when you
assign a new value to "h".

In this case this happens through the invocation of the Lock instruction
which opens a new stream and assigns it to "h". Then the above happens.

Lock has absolutely no means to detect what variable its return value is
written to to take obscure actions based on that information. Do you want it
to refuse to do its job whenever it notices that it is about to replace a
lock stream with another lock stream which it already opened at some point
during the process' lifetime? This isn't KISS anymore.

It is the same situation as assigning "h = Null". Do you think this should
keep the lock open, too? If you do, there is one thing you can do about it.
You can define your very personal lock class which will never close the
accompanying fd, even if the lock object is destroyed. The only way you
would close the fd is by an invocation of the Unlock instruction. Your last
question below sort of goes into this direction, but you can't implement
Lock and Unlock handlers in Gambas classes, so you would provide respective
Lock()/Unlock() methods from your class or write your own native component.

> How can a proc know if it already has a Lock ?
> 

Under the premise that you use Lock and Unlock properly, this is equivalent
to your lock stream being unequal to Null.

Or you can use lockf on your stream's Handle property to be sure. If a valid
stream object references a still valid lock. But, as I said, if you do
things properly, these two tests are equivalent.

"Properly" means here: before you call Lock, check if you already have it.
If yes, do nothing. After you call Unlock, set your variable to Null.

> Are you suggesting that before Lock is called the user should test to
> see if it already has Lock ?
> 

Yes, or you need to store all locks you acquire. As soon as one of these
objects gets destroyed, your lock is free again.

> In the meantime is there a recognised way of calling lockf() from within
> a Gambas source file ?
> 

Yep, Extern[0].

Regards,
Tobi

[0] http://gambaswiki.org/wiki/lang/extern, etc.

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the Devel mailing list