zmodload zsh/zselect
function wait_max_jobs() {
	if [[ "$1" != <-> || "$1" -le 0 ]]; then
		echo "usage: $0 <max_jobs>" >&2
		echo "  blocks until there are no more than <max_jobs> background jobs" >&2
		return 1
	fi

	while [ "${(%)#:+%j}" -ge "$1" ]; do
		zselect -t 25
	done
	return 0
}
