]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/lock-and-run.sh
gcov: rename 2 options.
[thirdparty/gcc.git] / gcc / lock-and-run.sh
CommitLineData
427b248d 1#! /bin/sh
d720518f
JM
2# Shell-based mutex using mkdir. This script is used in make to prefer
3# serialized execution to avoid consuming too much RAM. If reusing it,
4# bear in mind that the lock-breaking logic is not race-free, so disable
5# it in err() if concurrent execution could cause more serious problems.
427b248d 6
d720518f 7self=`basename $0`
1e5b0633
JM
8lockdir="$1" prog="$2"; shift 2 || exit 1
9
427b248d 10# Remember when we started trying to acquire the lock.
1e5b0633 11count=0
1e5b0633 12
d720518f
JM
13err () {
14 if test -f $lockdir/lock-$1.$$; then
d720518f 15 rm -rf $lockdir
8c726579
JM
16 echo "$self: *** (PID $$) removed stale $lockdir" >&2
17
d720518f
JM
18 # Possible variant for uses where races are more problematic:
19 #echo "$self: *** (PID $$) giving up, maybe rm -r $lockdir" >&2
20 #exit 42
21 else
22 touch $lockdir/lock-$1.$$
23 fi
24}
1e5b0633 25
427b248d
JM
26until mkdir "$lockdir" 2>/dev/null; do
27 # Say something periodically so the user knows what's up.
28 if [ `expr $count % 30` = 0 ]; then
d720518f
JM
29 # Check for valid lock.
30 if pid=`cat $lockdir/pid 2>/dev/null` && kill -0 $pid 2>/dev/null; then
31 echo "$self: (PID $$) waiting $count sec to acquire $lockdir from PID $pid" >&2
32 elif test -z "$pid"; then
33 echo "$self: (PID $$) cannot read $lockdir/pid" >&2
34 err nopid
427b248d 35 else
d720518f
JM
36 echo "$self: (PID $$) cannot signal $lockdir owner PID $pid" >&2
37 err dead
427b248d
JM
38 fi
39 fi
40 sleep 1
41 count=`expr $count + 1`
42done
1e5b0633 43
d720518f
JM
44trap 'rm -rf "$lockdir"' 0
45echo $$ > $lockdir/pidT && mv $lockdir/pidT $lockdir/pid
46echo "$self: (PID $$) acquired $lockdir after $count seconds" >&2
47
427b248d
JM
48echo $prog "$@"
49$prog "$@"
1e5b0633 50
427b248d 51# The trap runs on exit.