1package Plucene::Utils;
2
3=head1 NAME
4
5Plucene::Utils - Utility class for Plucene
6
7=head1 SYNOPSIS
8
9	use Plucene::Utils;
10
11	do_locked($sub, $lock);
12
13=head1 DESCRIPTION
14
15Utilities to help with Plucene.
16
17=head1 METHODS
18
19=cut
20
21use strict;
22use warnings;
23
24use Carp;
25use Fcntl qw(O_EXCL O_CREAT O_WRONLY);
26
27use base 'Exporter';
28our @EXPORT = qw( do_locked );
29
30=head2 do_locked
31
32	do_locked($sub, $lock);
33
34=cut
35
36sub do_locked (&$) {
37	my ($sub, $lock) = @_;
38	local *FH;
39	for (1 .. 5) {
40		sysopen FH, $lock, O_EXCL | O_CREAT | O_WRONLY
41			and goto got_lock;
42		sleep 1;
43		warn "I had to sleep to get a lock on $lock";
44	}
45	carp "Couldn't get lock $lock: $!";
46	got_lock:
47	$sub->();
48	close *FH;
49	unlink $lock;
50}
51
521;
53