2012-06-15 21:34:07 +00:00
|
|
|
#!/mod/bin/jimsh
|
|
|
|
|
|
|
|
proc _lock_to_port {id} {
|
|
|
|
set len [string length $id]
|
|
|
|
set hash $len
|
|
|
|
|
|
|
|
for {set i 0} {$i < $len} {incr i} {
|
|
|
|
scan [string index $id $i] "%c" asc
|
|
|
|
set hash [expr (($hash<<5)^($hash>>27))^$asc];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [expr (65535 - ($hash % 101))]
|
|
|
|
}
|
|
|
|
|
|
|
|
proc _lock_dummy {newsock addr port} { }
|
|
|
|
|
2014-02-10 23:03:36 +00:00
|
|
|
proc acquire_lock {id {timeout 0}} {
|
2012-06-15 21:34:07 +00:00
|
|
|
global _locks
|
|
|
|
|
|
|
|
set port [_lock_to_port $id]
|
|
|
|
|
2014-02-10 23:03:36 +00:00
|
|
|
incr timeout [clock seconds]
|
|
|
|
|
|
|
|
while {[catch {set socket [\
|
|
|
|
socket stream.server "127.0.0.1:$port"]} msg]} {
|
|
|
|
if {[clock seconds] > $timeout} { return 0 }
|
|
|
|
sleep 0.2
|
2012-06-15 21:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set _locks($id) $socket
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
proc release_lock {id} {
|
|
|
|
global _locks
|
|
|
|
|
|
|
|
if {[catch {close $_locks($id)} msg]} {
|
|
|
|
puts "Error releasing lock: $msg"
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
unset _locks($id)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|