2020-01-25 08:52:12 +02:00
|
|
|
# invoked in worker: _p9k_worker_main <pgid>
|
2020-01-17 20:01:17 +02:00
|
|
|
function _p9k_worker_main() {
|
2020-01-24 20:47:01 +02:00
|
|
|
mkfifo $_p9k__worker_file_prefix.fifo || return
|
2020-01-25 12:12:30 +02:00
|
|
|
echo -nE - s$_p9k_worker_pgid$'\x1e' || return
|
2020-01-24 20:47:01 +02:00
|
|
|
exec 0<$_p9k__worker_file_prefix.fifo || return
|
2020-01-25 13:18:15 +02:00
|
|
|
zf_rm $_p9k__worker_file_prefix.fifo || return
|
2020-01-18 12:09:18 +02:00
|
|
|
|
2020-01-25 12:01:14 +02:00
|
|
|
local -i reset
|
2020-01-21 00:37:03 +02:00
|
|
|
local req fd
|
2020-01-18 12:09:18 +02:00
|
|
|
local -a ready
|
2020-01-25 12:01:14 +02:00
|
|
|
local _p9k_worker_request_id
|
|
|
|
local -A _p9k_worker_fds # fd => id$'\x1f'callback
|
|
|
|
local -A _p9k_worker_inflight # id => inflight count
|
|
|
|
|
2020-01-25 12:12:30 +02:00
|
|
|
function _p9k_worker_reply() {
|
|
|
|
print -nr -- e${(pj:\n:)@}$'\x1e' || kill -- -$_p9k_worker_pgid
|
|
|
|
}
|
2020-01-25 12:01:14 +02:00
|
|
|
|
|
|
|
# usage: _p9k_worker_async <work> <callback>
|
|
|
|
function _p9k_worker_async() {
|
|
|
|
local fd async=$1
|
|
|
|
sysopen -r -o cloexec -u fd <(
|
|
|
|
() { eval $async; } && print -n '\x1e') || return
|
|
|
|
(( ++_p9k_worker_inflight[$_p9k_worker_request_id] ))
|
|
|
|
_p9k_worker_fds[$fd]=$_p9k_worker_request_id$'\x1f'$2
|
|
|
|
}
|
2020-01-18 16:41:43 +02:00
|
|
|
|
2020-01-25 12:12:30 +02:00
|
|
|
trap '' PIPE
|
|
|
|
|
2020-01-19 12:53:10 +02:00
|
|
|
{
|
2020-01-25 12:01:14 +02:00
|
|
|
while zselect -a ready 0 ${(k)_p9k_worker_fds}; do
|
2020-01-19 12:53:10 +02:00
|
|
|
[[ $ready[1] == -r ]] || return
|
|
|
|
for fd in ${ready:1}; do
|
|
|
|
if [[ $fd == 0 ]]; then
|
2020-01-21 00:37:03 +02:00
|
|
|
local buf=
|
2020-01-19 12:53:10 +02:00
|
|
|
while true; do
|
|
|
|
sysread -t 0 'buf[$#buf+1]' && continue
|
|
|
|
(( $? == 4 )) || return
|
|
|
|
[[ $buf[-1] == (|$'\x1e') ]] && break
|
|
|
|
sysread 'buf[$#buf+1]' || return
|
|
|
|
done
|
|
|
|
for req in ${(ps:\x1e:)buf}; do
|
2020-01-25 12:01:14 +02:00
|
|
|
_p9k_worker_request_id=${req%%$'\x1f'*}
|
|
|
|
() { eval $req[$#_p9k_worker_request_id+2,-1] }
|
|
|
|
(( $+_p9k_worker_inflight[$_p9k_worker_request_id] )) && continue
|
|
|
|
print -rn -- d$_p9k_worker_request_id$'\x1e' || return
|
2020-01-19 12:53:10 +02:00
|
|
|
done
|
|
|
|
else
|
2020-01-25 12:01:14 +02:00
|
|
|
local REPLY=
|
2020-01-19 12:53:10 +02:00
|
|
|
while true; do
|
2020-01-25 12:01:14 +02:00
|
|
|
sysread -i $fd 'REPLY[$#REPLY+1]' && continue
|
|
|
|
(( $? == 5 )) || return
|
2020-01-19 12:53:10 +02:00
|
|
|
break
|
|
|
|
done
|
2020-01-25 12:01:14 +02:00
|
|
|
local cb=$_p9k_worker_fds[$fd]
|
|
|
|
_p9k_worker_request_id=${cb%%$'\x1f'*}
|
|
|
|
unset "_p9k_worker_fds[$fd]"
|
|
|
|
exec {fd}>&-
|
|
|
|
if [[ $REPLY == *$'\x1e' ]]; then
|
|
|
|
REPLY[-1]=""
|
|
|
|
() { eval $cb[$#_p9k_worker_request_id+2,-1] }
|
2020-01-20 21:12:05 +02:00
|
|
|
fi
|
2020-01-25 12:01:14 +02:00
|
|
|
if (( --_p9k_worker_inflight[$_p9k_worker_request_id] == 0 )); then
|
|
|
|
unset "_p9k_worker_inflight[$_p9k_worker_request_id]"
|
|
|
|
print -rn -- d$_p9k_worker_request_id$'\x1e' || return
|
2020-01-18 12:09:18 +02:00
|
|
|
fi
|
|
|
|
fi
|
2020-01-19 12:53:10 +02:00
|
|
|
done
|
2020-01-18 13:08:48 +02:00
|
|
|
done
|
2020-01-19 12:53:10 +02:00
|
|
|
} always {
|
2020-01-25 12:12:30 +02:00
|
|
|
kill -- -$_p9k_worker_pgid
|
2020-01-19 12:53:10 +02:00
|
|
|
}
|
2020-01-17 20:01:17 +02:00
|
|
|
}
|
|
|
|
|
2020-01-25 12:01:14 +02:00
|
|
|
# invoked in master: _p9k_worker_invoke <request-id> <list>
|
2020-01-17 20:01:17 +02:00
|
|
|
function _p9k_worker_invoke() {
|
2020-01-25 12:01:14 +02:00
|
|
|
[[ -n $_p9k__worker_resp_fd ]] || return
|
|
|
|
local req=$1$'\x1f'$2$'\x1e'
|
|
|
|
if [[ -n $_p9k__worker_req_fd && $+_p9k__worker_request_map[$1] == 0 ]]; then
|
|
|
|
_p9k__worker_request_map[$1]=
|
|
|
|
print -rnu $_p9k__worker_req_fd -- $req
|
2020-01-17 20:01:17 +02:00
|
|
|
else
|
2020-01-25 12:01:14 +02:00
|
|
|
_p9k__worker_request_map[$1]=$req
|
2020-01-17 20:01:17 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function _p9k_worker_cleanup() {
|
2020-02-01 23:03:42 +02:00
|
|
|
eval "$__p9k_intro"
|
2020-01-17 20:01:17 +02:00
|
|
|
[[ $_p9k__worker_shell_pid == $sysparams[pid] ]] && _p9k_worker_stop
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
function _p9k_worker_stop() {
|
|
|
|
add-zsh-hook -D zshexit _p9k_worker_cleanup
|
|
|
|
[[ -n $_p9k__worker_resp_fd ]] && zle -F $_p9k__worker_resp_fd
|
|
|
|
[[ -n $_p9k__worker_resp_fd ]] && exec {_p9k__worker_resp_fd}>&-
|
|
|
|
[[ -n $_p9k__worker_req_fd ]] && exec {_p9k__worker_req_fd}>&-
|
|
|
|
[[ -n $_p9k__worker_pid ]] && kill -- -$_p9k__worker_pid 2>/dev/null
|
2020-01-25 13:18:15 +02:00
|
|
|
[[ -n $_p9k__worker_file_prefix ]] && zf_rm -f $_p9k__worker_file_prefix.fifo
|
2020-01-17 20:01:17 +02:00
|
|
|
_p9k__worker_pid=
|
|
|
|
_p9k__worker_req_fd=
|
|
|
|
_p9k__worker_resp_fd=
|
|
|
|
_p9k__worker_shell_pid=
|
2020-01-18 12:09:18 +02:00
|
|
|
_p9k__worker_request_map=()
|
2020-01-17 20:01:17 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
function _p9k_worker_receive() {
|
2020-02-01 23:03:42 +02:00
|
|
|
eval "$__p9k_intro"
|
2020-01-17 20:01:17 +02:00
|
|
|
|
2020-01-25 13:51:46 +02:00
|
|
|
[[ -z $_p9k__worker_resp_fd ]] && return
|
|
|
|
|
2020-01-17 20:01:17 +02:00
|
|
|
{
|
|
|
|
(( $# <= 1 )) || return
|
|
|
|
|
2020-01-25 12:01:14 +02:00
|
|
|
local buf resp
|
2020-01-17 20:01:17 +02:00
|
|
|
while true; do
|
|
|
|
sysread -t 0 -i $_p9k__worker_resp_fd 'buf[$#buf+1]' && continue
|
|
|
|
(( $? == 4 )) || return
|
2020-01-25 12:01:14 +02:00
|
|
|
[[ $buf == (|*$'\x1e')$'\x05'# ]] && break
|
2020-01-17 20:01:17 +02:00
|
|
|
sysread -i $_p9k__worker_resp_fd 'buf[$#buf+1]' || return
|
|
|
|
done
|
|
|
|
|
2020-02-03 12:47:39 +02:00
|
|
|
local -i reset max_reset
|
2020-01-25 09:03:08 +02:00
|
|
|
for resp in ${(ps:\x1e:)${buf//$'\x05'}}; do
|
2020-01-17 20:01:17 +02:00
|
|
|
local arg=$resp[2,-1]
|
|
|
|
case $resp[1] in
|
|
|
|
d)
|
2020-01-18 12:09:18 +02:00
|
|
|
local req=$_p9k__worker_request_map[$arg]
|
2020-01-17 20:01:17 +02:00
|
|
|
if [[ -n $req ]]; then
|
2020-01-18 12:09:18 +02:00
|
|
|
_p9k__worker_request_map[$arg]=
|
|
|
|
print -rnu $_p9k__worker_req_fd -- $req || return
|
2020-01-17 20:01:17 +02:00
|
|
|
else
|
2020-01-18 12:09:18 +02:00
|
|
|
unset "_p9k__worker_request_map[$arg]"
|
2020-01-17 20:01:17 +02:00
|
|
|
fi
|
|
|
|
;;
|
|
|
|
e)
|
|
|
|
() { eval $arg }
|
2020-02-03 12:47:39 +02:00
|
|
|
(( reset > max_reset )) && max_reset=reset
|
2020-01-17 20:01:17 +02:00
|
|
|
;;
|
|
|
|
s)
|
|
|
|
[[ -z $_p9k__worker_pid ]] || return
|
|
|
|
[[ $arg == <1-> ]] || return
|
|
|
|
_p9k__worker_pid=$arg
|
|
|
|
sysopen -w -o cloexec -u _p9k__worker_req_fd $_p9k__worker_file_prefix.fifo || return
|
2020-01-24 20:47:01 +02:00
|
|
|
local req=
|
2020-01-25 12:01:14 +02:00
|
|
|
for req in $_p9k__worker_request_map; do
|
2020-01-24 20:47:01 +02:00
|
|
|
print -rnu $_p9k__worker_req_fd -- $req || return
|
|
|
|
done
|
2020-01-25 12:01:14 +02:00
|
|
|
_p9k__worker_request_map=({${(k)^_p9k__worker_request_map},''})
|
2020-01-17 20:01:17 +02:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2020-02-03 12:47:39 +02:00
|
|
|
if (( max_reset == 2 )); then
|
2020-01-25 12:01:14 +02:00
|
|
|
_p9k_refresh_reason=worker
|
|
|
|
_p9k_set_prompt
|
|
|
|
_p9k_refresh_reason=''
|
|
|
|
fi
|
2020-02-03 12:47:39 +02:00
|
|
|
(( max_reset )) && _p9k_reset_prompt
|
2020-01-17 20:01:17 +02:00
|
|
|
return 0
|
|
|
|
} always {
|
|
|
|
(( $? )) && _p9k_worker_stop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _p9k_worker_start() {
|
2020-01-26 17:54:52 +02:00
|
|
|
setopt monitor || return
|
2020-01-17 20:01:17 +02:00
|
|
|
{
|
|
|
|
[[ -n $_p9k__worker_resp_fd ]] && return
|
|
|
|
_p9k__worker_file_prefix=${TMPDIR:-/tmp}/p10k.worker.$EUID.$$.$EPOCHSECONDS
|
|
|
|
|
|
|
|
sysopen -r -o cloexec -u _p9k__worker_resp_fd <(
|
2020-01-24 20:47:01 +02:00
|
|
|
if [[ -n $_POWERLEVEL9K_WORKER_LOG_LEVEL ]]; then
|
|
|
|
exec 2>$_p9k__worker_file_prefix.log
|
|
|
|
setopt xtrace
|
|
|
|
else
|
|
|
|
exec 2>/dev/null
|
|
|
|
fi
|
2020-01-25 08:52:12 +02:00
|
|
|
zmodload zsh/zselect || return
|
|
|
|
! { zselect -t0 || (( $? != 1 )) } || return
|
2020-01-25 12:12:30 +02:00
|
|
|
local _p9k_worker_pgid=$sysparams[pid]
|
|
|
|
_p9k_worker_main &
|
2020-01-25 08:52:12 +02:00
|
|
|
{
|
|
|
|
trap '' PIPE
|
2020-01-25 09:03:08 +02:00
|
|
|
while syswrite $'\x05'; do zselect -t 1000; done
|
2020-01-25 13:18:15 +02:00
|
|
|
zf_rm -f $_p9k__worker_file_prefix.fifo
|
2020-01-25 12:12:30 +02:00
|
|
|
kill -- -$_p9k_worker_pgid
|
2020-01-25 08:52:12 +02:00
|
|
|
} &
|
2020-01-24 20:47:01 +02:00
|
|
|
exec =true) || return
|
2020-01-17 20:01:17 +02:00
|
|
|
zle -F $_p9k__worker_resp_fd _p9k_worker_receive
|
|
|
|
_p9k__worker_shell_pid=$sysparams[pid]
|
|
|
|
add-zsh-hook zshexit _p9k_worker_cleanup
|
|
|
|
} always {
|
|
|
|
(( $? )) && _p9k_worker_stop
|
|
|
|
}
|
|
|
|
}
|