sub flock_lock { my ($fh, $excl, $wait) = @_; my $flag = $excl ? $flock_LOCK_EX : $flock_LOCK_SH; if ($wait) { my $trylimit = $Vend::Cfg->{Limit}{file_lock_retries} || 5; my $failedcount; while ( ! flock($fh, $flag) and $failedcount < $trylimit ) { $failedcount++; select(undef,undef,undef,0.05 * $failedcount); } die "Could not lock file after $trylimit tries: $!\n" if ($failedcount == $trylimit); return 1; } else { if (! flock($fh, $flag | $flock_LOCK_NB)) { if ($!{EAGAIN} or $!{EWOULDBLOCK}) { return 0; } else { die "Could not lock file: $!\n"; } } return 1; } }