Mercurial > hg > CbC > CbC_gcc
comparison gcc/lock-and-run.sh @ 145:1830386684a0
gcc-9.2.0
author | anatofuz |
---|---|
date | Thu, 13 Feb 2020 11:34:05 +0900 |
parents | 04ced10e8804 |
children |
comparison
equal
deleted
inserted
replaced
131:84e7813d76e9 | 145:1830386684a0 |
---|---|
1 #! /bin/sh | 1 #! /bin/sh |
2 # Shell-based mutex using mkdir. | 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. | |
3 | 6 |
7 self=`basename $0` | |
4 lockdir="$1" prog="$2"; shift 2 || exit 1 | 8 lockdir="$1" prog="$2"; shift 2 || exit 1 |
5 | 9 |
6 # Remember when we started trying to acquire the lock. | 10 # Remember when we started trying to acquire the lock. |
7 count=0 | 11 count=0 |
8 touch lock-stamp.$$ | |
9 | 12 |
10 trap 'rm -r "$lockdir" lock-stamp.$$' 0 | 13 err () { |
14 if test -f $lockdir/lock-$1.$$; then | |
15 rm -rf $lockdir | |
16 echo "$self: *** (PID $$) removed stale $lockdir" >&2 | |
17 | |
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 } | |
11 | 25 |
12 until mkdir "$lockdir" 2>/dev/null; do | 26 until mkdir "$lockdir" 2>/dev/null; do |
13 # Say something periodically so the user knows what's up. | 27 # Say something periodically so the user knows what's up. |
14 if [ `expr $count % 30` = 0 ]; then | 28 if [ `expr $count % 30` = 0 ]; then |
15 # Reset if the lock has been renewed. | 29 # Check for valid lock. |
16 if [ -n "`find \"$lockdir\" -newer lock-stamp.$$`" ]; then | 30 if pid=`cat $lockdir/pid 2>/dev/null` && kill -0 $pid 2>/dev/null; then |
17 touch lock-stamp.$$ | 31 echo "$self: (PID $$) waiting $count sec to acquire $lockdir from PID $pid" >&2 |
18 count=1 | 32 elif test -z "$pid"; then |
19 # Steal the lock after 5 minutes. | 33 echo "$self: (PID $$) cannot read $lockdir/pid" >&2 |
20 elif [ $count = 300 ]; then | 34 err nopid |
21 echo removing stale $lockdir >&2 | |
22 rm -r "$lockdir" | |
23 else | 35 else |
24 echo waiting to acquire $lockdir >&2 | 36 echo "$self: (PID $$) cannot signal $lockdir owner PID $pid" >&2 |
37 err dead | |
25 fi | 38 fi |
26 fi | 39 fi |
27 sleep 1 | 40 sleep 1 |
28 count=`expr $count + 1` | 41 count=`expr $count + 1` |
29 done | 42 done |
30 | 43 |
44 trap 'rm -rf "$lockdir"' 0 | |
45 echo $$ > $lockdir/pidT && mv $lockdir/pidT $lockdir/pid | |
46 echo "$self: (PID $$) acquired $lockdir after $count seconds" >&2 | |
47 | |
31 echo $prog "$@" | 48 echo $prog "$@" |
32 $prog "$@" | 49 $prog "$@" |
33 | 50 |
34 # The trap runs on exit. | 51 # The trap runs on exit. |