2013-08-11 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / lock-and-run.sh
blob3a6a84c253ae1f2fcdea7ccfb586b0796e3199d5
1 #! /bin/sh
2 # Shell-based mutex using mkdir.
4 lockdir="$1" prog="$2"; shift 2 || exit 1
6 # Remember when we started trying to acquire the lock.
7 count=0
8 touch lock-stamp.$$
10 trap 'rm -r "$lockdir" lock-stamp.$$' 0
12 until mkdir "$lockdir" 2>/dev/null; do
13 # Say something periodically so the user knows what's up.
14 if [ `expr $count % 30` = 0 ]; then
15 # Reset if the lock has been renewed.
16 if [ -n "`find \"$lockdir\" -newer lock-stamp.$$`" ]; then
17 touch lock-stamp.$$
18 count=1
19 # Steal the lock after 5 minutes.
20 elif [ $count = 300 ]; then
21 echo removing stale $lockdir >&2
22 rm -r "$lockdir"
23 else
24 echo waiting to acquire $lockdir >&2
27 sleep 1
28 count=`expr $count + 1`
29 done
31 echo $prog "$@"
32 $prog "$@"
34 # The trap runs on exit.