[REBOL] Re: how to do simple exclusive open -- don't use negative logic
From: petr:krenzelok:trz:cz at: 6-Jun-2001 20:44
----- Original Message -----
From: "Anton" <[arolls--bigpond--net--au]>
To: <[rebol-list--rebol--com]>
Sent: Wednesday, June 06, 2001 7:39 PM
Subject: [REBOL] Re: how to do simple exclusive open -- don't use negative
logic
> I would suggest you *not* use negative logic,
> as in:
>
> either not exists? semaphore-port [ ; ... does semaphore already exists?
>
> The above question is incorrectly placed, I think.
Ah, maybe my non english background?
Well, I put my brain into more thinking and built more robust solution. Now
pairs of file semaphores and their opened ports are stored:
lock: context [
sem-port-spec: func [path file-name][to-file join to-string path head
change find copy file-name "." ".SEM"]
locked: copy []
get-lock: func [path file-name][
semaphore: sem-port-spec path file-name
; file doesn't exist, or does exist, but semaphore can be closed
(dead one)
either all [(not exists? semaphore) (not remove-sem-file semaphore)][
; store pair of file-name, opened port
append locked semaphore
append locked open/new/direct semaphore
true
][false] ; can't get lock, sorry ...
]
close-lock: func [path file-name][
semaphore: sem-port-spec path file-name
if found? find locked semaphore [
close select locked semaphore
remove remove find locked semaphore
]
either remove-sem-file semaphore [true][false]
]
remove-sem-file: func [semaphore][either not error? try [delete
semaphore][true][false]]
]
usage:
lock/get-lock %whatever-path "file-name.txt"
print lock/locked
lock/close-lock %whatever-path "file-name.txt
btw: how would you put following line into "non negative" one? :-))
either all [(not exists? semaphore) (not remove-sem-file semaphore)][
either not any [(exists? semaphore) (remove-sem-file semaphore)] ???
If so, I like the first one anyway :-)
Cheers,
-pekr-