11#! /usr/bin/env bash
22
3- # setup the semaphore environment for the loading file
4- bats_semaphore_setup () {
5- export -f bats_semaphore_get_free_slot_count
6- export -f bats_semaphore_acquire_while_locked
7- export BATS_SEMAPHORE_DIR=" $BATS_RUN_TMPDIR /semaphores"
3+ bats_run_under_flock () {
4+ flock " $BATS_SEMAPHORE_DIR " " $@ "
5+ }
86
9- if command -v flock > /dev/null; then
10- bats_run_under_lock () {
11- flock " $BATS_SEMAPHORE_DIR " " $@ "
12- }
13- elif command -v shlock > /dev/null; then
14- bats_run_under_lock () {
7+ bats_run_under_shlock () {
158 local lockfile=" $BATS_SEMAPHORE_DIR /shlock.lock"
169 while ! shlock -p $$ -f " $lockfile " ; do
1710 sleep 1
@@ -23,6 +16,17 @@ bats_semaphore_setup() {
2316 rm -f " $lockfile "
2417 return $status
2518 }
19+
20+ # setup the semaphore environment for the loading file
21+ bats_semaphore_setup () {
22+ export -f bats_semaphore_get_free_slot_count
23+ export -f bats_semaphore_acquire_while_locked
24+ export BATS_SEMAPHORE_DIR=" $BATS_RUN_TMPDIR /semaphores"
25+
26+ if command -v flock > /dev/null; then
27+ BATS_LOCKING_IMPLEMENTATION=flock
28+ elif command -v shlock > /dev/null; then
29+ BATS_LOCKING_IMPLEMENTATION=shlock
2630 else
2731 printf " ERROR: flock/shlock is required for parallelization within files!\n" >&2
2832 exit 1
@@ -87,7 +91,9 @@ bats_semaphore_acquire_slot() {
8791 while true ; do
8892 # don't lock for reading, we are fine with spuriously getting no free slot
8993 if [[ $( bats_semaphore_get_free_slot_count) -gt 0 ]]; then
90- bats_run_under_lock bash -c bats_semaphore_acquire_while_locked && break
94+ bats_run_under_" $BATS_LOCKING_IMPLEMENTATION " \
95+ bash -c bats_semaphore_acquire_while_locked \
96+ && break
9197 fi
9298 sleep 1
9399 done
0 commit comments