-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsemaphore.sh
More file actions
executable file
·193 lines (155 loc) · 5.63 KB
/
Copy pathsemaphore.sh
File metadata and controls
executable file
·193 lines (155 loc) · 5.63 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env bash
#
# Copyright (c) 2013, Qualcomm Innovation Center, Inc. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause
#
# Design:
#
# This semaphore is fairly simple, it allocates "slots" numbered from
# 1 to max and uses a locker to grab a lock for any numbered slot it
# can. Holding a slot lock means acquiring the semaphore, since the
# slots are limited to max, at most max users will acquire the semaphore
# at once. Release a lock and a slot frees up for another user to grab.
#
args() { # action needed optional [args]...
local func=$1 needed=$2 optional=$3 n s min=0 supplied=0 ; shift 3
for n in $needed ; do min=$((min+1)) ; done
for s in "$@" ; do supplied=$((supplied +1)) ; done
[ $supplied -ge $min ] && return
usage "'$func' takes <$needed> [$optional], given ($*)"
}
q() { "$@" 2>/dev/null ; } # execute a cmd and quiet the stderr
d() { [ "$DEBUG" = "DEBUG" ] && { echo "$(date) " ; } } # > date(debug) | nothing
debug() { [ "$DEBUG" = "DEBUG" ] && echo "$(d)$@" >&2 ; }
info() { debug "$@" ; [ "$DEBUG" = "INFO" ] && echo "$(d)$@" >&2 ; }
error() { echo "$(d) ERROR - $1" >&2 ; exit $2 ; }
random_slots() { # max > 1..max(in some random order)
[ $1 -eq 1 ] && { echo 1 ; return ; }
local start=$(( (RANDOM % ($1 -1)) +1 ))
seq $start $1
[ $start -gt 1 ] && seq 1 $((start -1))
}
_acquire() { # [--nocheck] max [id] # => 10 critical error (stop spinning!)
local nocheck=() ; [ "$1" = "--nocheck" ] && { nocheck=("$1"); shift ; }
local max=$1 id=() slot locked
[ -n "$2" ] && id+=("$2")
for slot in $(random_slots "$max") ; do
acquire_slot "${nocheck[@]}" "$slot" "${id[@]}" ; locked=$?
[ $locked -eq 0 ] || [ $locked -gt 9 ] && return $locked
done
info "$SEMAPHORE failed to be acquired by $id (max $max)"
return 1
}
# ----------
acquire_slot() { # [--nocheck] slot [id] # => 10 critical error (stop spinning!)
local fast='' ; [ "$1" = "--nocheck" ] && { fast='fast_'; shift ; }
args acquire_slot "slot" "id" "$@"
local slot=$1 id=() locked
[ -n "$2" ] && id+=("$2")
"${LOCKER[@]}" "$fast"lock "$SEMAPHORE/$slot" "${id[@]}" ; locked=$?
if [ $locked -eq 0 ] ; then
info "$SEMAPHORE ${fast}acquired by $id"
fi
return $locked
}
fast_acquire() { # max [id] # => 10 critical error (stop spinning!)
args fast_acquire "max" "id" "$@"
_acquire --nocheck "$@"
}
acquire() { # max [id] # => 10 critical error (stop spinning!)
args acquire "max" "id" "$@"
_acquire --nocheck "$@" ; local locked=$?
[ $locked -eq 0 -o $locked -gt 9 ] && return $locked
_acquire "$@"
}
release() { # [id]
local id=() lock
[ -n "$1" ] && id+=("$1")
for lock in "$SEMAPHORE"/* ; do
[ "$lock" = "$SEMAPHORE"/'*' ] && continue
"${LOCKER[@]}" is_mine "$lock" "${id[@]}" || continue
"${LOCKER[@]}" unlock "$lock" "${id[@]}"
rmdir -- "$SEMAPHORE" 2> /dev/null
info "$SEMAPHORE released by $id"
return 0
done
error "$SEMAPHORE not held by $id" 19
}
owners() { # > uids...
local lock
for lock in "$SEMAPHORE"/* ; do
[ "$lock" = "$SEMAPHORE"/'*' ] && continue
"${LOCKER[@]}" owner "$lock"
done
}
owner() { # slot > uid
args owner "slot" "" "$@"
"${LOCKER[@]}" owner "$SEMAPHORE/$1"
}
slot() { # [id] > slot
local id=() lock
[ -n "$1" ] && id+=("$1")
for lock in "$SEMAPHORE"/* ; do
[ "$lock" = "$SEMAPHORE"/'*' ] && continue
"${LOCKER[@]}" is_mine "$lock" "${id[@]}" || continue
echo "$(basename -- "$lock")"
return
done
}
# ----------
usage() { # error_message
local prog=$(basename -- "$0")
cat >&2 <<EOF
usage: $prog <locker> acquire <semaphore_path> max [id]
$prog <locker> fast_acquire <semaphore_path> max [id]
$prog <locker> acquire_slot <semaphore_path> slot [id]
$prog <locker> release <semaphore_path> [id]
$prog <locker> owners <semaphore_path> > uids
$prog <locker> owner <semaphore_path> <slot> > uid
$prog <locker> slot <semaphore_path> id > slot
A filesystem locker based semaphore manager.
<locker> can consist of:
<cmd [--locker-arg|-a arg]...>
or
-l|--local for safe local host only locking.
The <locker> command must understand the following lock commands:
lock <lock_path> [id] [seconds]
unlock <lock_path> [id] [seconds]
fast_lock <lock_path> [id]
owner <lock_path> > uid
<semaphore_path> filesystem path which all semaphore users have write
access to.
<max> The maximum semaphore count
EOF
[ $# -gt 0 ] && echo "Error - $@" >&2
exit 10
}
MYPATH=$(readlink -e -- "$0")
MYDIR=$(dirname -- "$MYPATH")
LOCKER=()
LOCKER_TESTER=()
while [ $# -gt 0 ] ; do
case "$1" in
-u|-h|--help) usage ;;
-di|--info) DEBUG=INFO ; TEST_DEBUG=$1 ;;
-d|--debug) DEBUG=DEBUG ; TEST_DEBUG=$1 ;;
-l|--local) LOCKER=("$MYDIR"/lock_local.sh)
LOCKER_TESTER=("$MYDIR"/test/lock_local.sh) ;;
-a|--locker-arg) LOCKER=("${LOCKER[@]}" "$2") ; shift ;;
*) if [ -z "$LOCKER" ] ; then
LOCKER=("$1")
elif [ -z "$ACTION" ] ; then
ACTION=$1
elif [ -z "$SEMAPHORE" ] ; then
SEMAPHORE=$1
else
break
fi
;;
esac
shift
done
[ "$DEBUG" = "DEBUG" ] && LOCKER=("${LOCKER[@]}" --debug)
[ -z "$ACTION" ] && usage "ACTION required"
[ -z "$SEMAPHORE" ] && usage "SEMAPHORE required for $ACTION"
"$ACTION" "$@"