File: wait_timestamp.pl

package info (click to toggle)
makepp 2.0.98.5-2.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 2,744 kB
  • sloc: perl: 15,893; makefile: 38; javascript: 25; sh: 1
file content (46 lines) | stat: -rw-r--r-- 1,442 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Wait till both the local clock and the timestamps of files are newer than
# those of the given files.  If no files are given, wait till the next second.

sub wait_timestamp(@) {
  my $ts = 0;
  my $tmp;
  # Pick out the newest file.
  for( @_ ) {
    $tmp = (stat)[9];
    $ts = $tmp if $tmp && $tmp > $ts;
  }
  # If none was given, default to now.
  $ts ||= time;
  my $hires = eval { require Time::HiRes };
  # Wait until the next second starts, if we can.
  if( $hires ) {
    $tmp = Time::HiRes::time();
    Time::HiRes::sleep( int( $tmp + 1 ) - $tmp );
    # Just in case something got rounded down, go forward in tiny steps:
    Time::HiRes::sleep( .01 ) while $ts >= time;
  } else {
    # More expensive than sleep, but we waste little real time.
    select undef, undef, undef, .1 while $ts >= time;
  }
  # If this is a remote file system, timestamps may not yet have changed.
  # So recheck in .1 second steps.
  while() {
    open my $fh, ">wait_timestamp.$$";
    # NOTE: Overwriting an existing empty file may not update its mod time,
    # so we also have to write to it.
    print $fh "x";
    last if (stat $fh)[9] > $ts;
    if( $hires ) {
      Time::HiRes::sleep( .1 );
    } else {
      select undef, undef, undef, .1;
    }
  }
  unlink "wait_timestamp.$$";
}

# If called as a standalone program or pseudo-standalone from run(), call the
# function.
wait_timestamp( @ARGV ) if !caller or caller eq 'Mpp::Subs';

1;