]>
Commit | Line | Data |
---|---|---|
be5f9e46 | 1 | #! /bin/sh |
2 | # Shell-based mutex using mkdir. | |
3 | ||
a9f1d053 | 4 | lockdir="$1" prog="$2"; shift 2 || exit 1 |
5 | ||
be5f9e46 | 6 | # Remember when we started trying to acquire the lock. |
a9f1d053 | 7 | count=0 |
be5f9e46 | 8 | touch lock-stamp.$$ |
a9f1d053 | 9 | |
be5f9e46 | 10 | trap 'rm -r "$lockdir" lock-stamp.$$' 0 |
a9f1d053 | 11 | |
be5f9e46 | 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 | |
25 | fi | |
26 | fi | |
27 | sleep 1 | |
28 | count=`expr $count + 1` | |
29 | done | |
a9f1d053 | 30 | |
be5f9e46 | 31 | echo $prog "$@" |
32 | $prog "$@" | |
a9f1d053 | 33 | |
be5f9e46 | 34 | # The trap runs on exit. |