[Gambas-devel] Question about singleton class

John Leake jleake at ...741...
Sat Sep 13 20:17:59 CEST 2014


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.  The semantics of Lock should be such that a
no error return from the function tells the caller that they have a Lock
and the Lock should be the same as the first call without any gaps where
another proc can gain access.

To restate all OS locking functions, if a Write Lock is granted then the
caller is guaranteed to have exclusive access no mater what happens that
until the Unlock is called or the proc terminates.

There is no semantic reference to Unlock when a program call Lock.
> 
> I will add a warning in the documentation about that.
> 
What are you going to say ?

Watch out programmers, if you call Lock twice it will allow two
independent procs to believe that they both have exclusive access.

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

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

>>
>> By no means I meant that. Apparently, removing a lock and removing a
>> *lock file* are two different things.
>>
> 
> Yes.

Of course they are.

If this is going to be fixed then why not make things simple for the
programmer and take care if these details within the Lock and Unlock
implementation. This way when unlock is called the file can
automatically removed.


> 
> LOCK = Create a file if it does not exist, open it, and then do a 
> lockf() on the entire file contents.
> 
> UNLOCK = Just close the file descriptor.
> 
> The only problem I see is that LOCK truncates the file to zero bytes, 
> which is not necessary. I may change that in the future.
> 
In the meantime is there a recognised way of calling lockf() from within
a Gambas source file ?

Best regards,
John Leake

P.S. Just another observation what does Lock and Unlock have to do with
streams ?

I am trying to implement a system wide singleton design pattern here.




More information about the Devel mailing list