Improve locking a bit.

This commit is contained in:
fewtarius 2023-08-03 14:47:27 +00:00
parent c797fc9020
commit e77e0091ac
No known key found for this signature in database
GPG key ID: F4AE55305D1B8C1A

View file

@ -68,12 +68,21 @@ function get_setting() {
return
}
function del_setting() {
while [ -f "${J_CONF_LOCK}" ]
function wait_lock() {
while true
do
sleep .1
if (set -o noclobber; echo "$$" > "${J_CONF_LOCK}") 2>/dev/null
then
trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT
break
else
sleep 1
fi
done
touch "${J_CONF_LOCK}"
}
function del_setting() {
wait_lock
if [[ "${1}" =~ ^[[:alnum:]] ]]
then
sed -i "/^${1}=/d" "${J_CONF}"
@ -82,31 +91,23 @@ function del_setting() {
}
function sort_settings() {
while [ -f "${J_CONF_LOCK}" ]
do
sleep .1
done
touch "${J_CONF_LOCK}"
wait_lock
cat "${J_CONF}" | grep ^[a-z0-9] | sort >"${J_CONF}.tmp"
mv "${J_CONF}.tmp" "${J_CONF}"
rm -f "${J_CONF_LOCK}"
}
function set_setting() {
while [ -f "${J_CONF_LOCK}" ]
do
sleep .1
done
touch "${J_CONF_LOCK}"
if [[ "${1}" =~ ^[[:alnum:]] ]]
then
del_setting "${1}"
if [ ! "${2}" = "default" ]
then
wait_lock
echo "${1}=${2}" >> "${J_CONF}"
rm -f "${J_CONF_LOCK}"
fi
fi
rm -f "${J_CONF_LOCK}"
}
function set_audio() {
@ -174,4 +175,3 @@ function get_aspect_ratio() {
esac
echo ${ASPECT}
}