massive worker simplification (figure out during massive hangover)

pull/445/head
romkatv 5 years ago
parent f1314f9072
commit 2aa1d07b4a

@ -1,11 +1,11 @@
# invoked in worker: _p9k_worker_main <timeout> # invoked in worker: _p9k_worker_main <timeout>
function _p9k_worker_main() { function _p9k_worker_main() {
emulate -L zsh
setopt no_hist_expand extended_glob no_prompt_bang prompt_percent prompt_subst no_aliases no_bgnice
zmodload zsh/system || return
zmodload zsh/zselect || return zmodload zsh/zselect || return
! { zselect -t0 || (( $? != 1 )) } || return ! { zselect -t0 || (( $? != 1 )) } || return
mkfifo $_p9k__worker_file_prefix.fifo || return
echo -nE - s${1}$'\x1e' || return
exec 0<$_p9k__worker_file_prefix.fifo || return
rm $_p9k__worker_file_prefix.fifo || return
function _p9k_worker_reply_begin() { print -nr -- e } function _p9k_worker_reply_begin() { print -nr -- e }
function _p9k_worker_reply_end() { print -nr -- $'\x1e' } function _p9k_worker_reply_end() { print -nr -- $'\x1e' }
@ -83,44 +83,14 @@ typeset -g _p9k__worker_req_fd
typeset -g _p9k__worker_resp_fd typeset -g _p9k__worker_resp_fd
typeset -g _p9k__worker_shell_pid typeset -g _p9k__worker_shell_pid
typeset -g _p9k__worker_file_prefix typeset -g _p9k__worker_file_prefix
typeset -gaU _p9k__worker_params
typeset -gaU _p9k__worker_functions
typeset -gA _p9k__worker_request_map typeset -gA _p9k__worker_request_map
typeset -ga _p9k__worker_request_queue typeset -ga _p9k__worker_request_queue
function _p9k_worker_print_params() {
local names=(${@:/(#m)*/${${${+parameters[$MATCH]}:#0}:+$MATCH}})
(( ! $#names )) && return
print -n -- '\x1f' && typeset -p -- $names && print -n -- '\x1f\x1f\x1e'
}
function _p9k_worker_print_functions() {
local names=(${@:/(#m)*/${${${+functions[$MATCH]}:#0}:+$MATCH}})
(( ! $#names )) && return
print -n -- '\x1f' && functions -- $names && print -n -- '\x1f\x1f\x1e'
}
# invoked in master: _p9k_worker_send_params [param]... # invoked in master: _p9k_worker_send_params [param]...
function _p9k_worker_send_params() { function _p9k_worker_send_params() { }
if [[ -n $_p9k__worker_req_fd ]]; then
_p9k_worker_print_params ${(u)@} >&$_p9k__worker_req_fd && return
_p9k_worker_stop
return 1
else
_p9k__worker_params+=($@)
fi
}
# invoked in master: _p9k_worker_send_functions [function-name]... # invoked in master: _p9k_worker_send_functions [function-name]...
function _p9k_worker_send_functions() { function _p9k_worker_send_functions() { }
if [[ -n $_p9k__worker_req_fd ]]; then
_p9k_worker_print_functions ${(u)@} >&$_p9k__worker_req_fd && return
_p9k_worker_stop
return 1
else
_p9k__worker_functions+=($@)
fi
}
# invoked in master: _p9k_worker_invoke <request-id> <cond> <async> <sync> # invoked in master: _p9k_worker_invoke <request-id> <cond> <async> <sync>
function _p9k_worker_invoke() { function _p9k_worker_invoke() {
@ -165,8 +135,6 @@ function _p9k_worker_stop() {
_p9k__worker_req_fd= _p9k__worker_req_fd=
_p9k__worker_resp_fd= _p9k__worker_resp_fd=
_p9k__worker_shell_pid= _p9k__worker_shell_pid=
_p9k__worker_params=()
_p9k__worker_functions=()
_p9k__worker_request_map=() _p9k__worker_request_map=()
_p9k__worker_request_queue=() _p9k__worker_request_queue=()
return 0 return 0
@ -214,25 +182,6 @@ function _p9k_worker_receive() {
[[ $arg == <1-> ]] || return [[ $arg == <1-> ]] || return
_p9k__worker_pid=$arg _p9k__worker_pid=$arg
sysopen -w -o cloexec -u _p9k__worker_req_fd $_p9k__worker_file_prefix.fifo || return sysopen -w -o cloexec -u _p9k__worker_req_fd $_p9k__worker_file_prefix.fifo || return
{
local init="
zmodload zsh/datetime
zmodload zsh/mathfunc
zmodload zsh/parameter
zmodload zsh/system
zmodload zsh/termcap
zmodload zsh/terminfo
zmodload zsh/zleparameter
zmodload -F zsh/stat b:zstat
zmodload -F zsh/net/socket b:zsocket
zmodload -F zsh/files b:zf_mv b:zf_rm
autoload -Uz is-at-least
() { $functions[_p9k_worker_main] }"
print -r -- ${init//$'\n'/$'\x1e'} || return
_p9k_worker_print_params $_p9k__worker_params || return
_p9k_worker_print_functions $_p9k__worker_functions || return
_p9k__worker_params=()
_p9k__worker_functions()
local req= local req=
for req in $_p9k__worker_request_queue; do for req in $_p9k__worker_request_queue; do
if [[ $req != *$'\x1e' ]]; then if [[ $req != *$'\x1e' ]]; then
@ -243,7 +192,6 @@ function _p9k_worker_receive() {
print -rnu $_p9k__worker_req_fd -- $req || return print -rnu $_p9k__worker_req_fd -- $req || return
done done
_p9k__worker_request_queue=() _p9k__worker_request_queue=()
} >&$_p9k__worker_req_fd
;; ;;
*) *)
return 1 return 1
@ -259,42 +207,24 @@ function _p9k_worker_receive() {
} }
function _p9k_worker_start() { function _p9k_worker_start() {
setopt no_bgnice setopt no_bgnice monitor
{ {
[[ -n $_p9k__worker_resp_fd ]] && return [[ -n $_p9k__worker_resp_fd ]] && return
_p9k__worker_file_prefix=${TMPDIR:-/tmp}/p10k.worker.$EUID.$$.$EPOCHSECONDS _p9k__worker_file_prefix=${TMPDIR:-/tmp}/p10k.worker.$EUID.$$.$EPOCHSECONDS
sysopen -r -o cloexec -u _p9k__worker_resp_fd <(
if [[ -n $_POWERLEVEL9K_WORKER_LOG_LEVEL ]]; then if [[ -n $_POWERLEVEL9K_WORKER_LOG_LEVEL ]]; then
local trace=x exec 2>$_p9k__worker_file_prefix.log
local log_file=$file_prefix.log setopt xtrace
else else
local trace= exec 2>/dev/null
local log_file=/dev/null
fi fi
# todo: remove
log_file=/tmp/log # todo: remove exec 2>>/tmp/log
trace=x setopt xtrace
local pid=$sysparams[pid]
local fifo=$_p9k__worker_file_prefix.fifo _p9k_worker_main $pid &
local zsh=${${:-/proc/self/exe}:A} exec =true) || return
[[ -x $zsh ]] || zsh=zsh
local bootstrap='
"emulate" "-L" "zsh" "-o" "no_aliases" "-o" "no_bgnice"
{
local fifo='${(q)fifo}'
{
zmodload zsh/system &&
mkfifo $fifo &&
exec >&4 &&
echo -n "s$sysparams[pid]\x1e" &&
exec 0<$fifo || exit
} always { rm -f -- $fifo }
IFS= read -r && eval ${REPLY//$'"'\x1e'"'/$'"'\n'"'}
} &!
exec true'
sysopen -r -o cloexec -u _p9k__worker_resp_fd <(
_p9k_worker_bootstrap=${bootstrap// ##} </dev/null 4>&1 &>>$log_file \
exec $zsh -${trace}dfmc '"eval" "$_p9k_worker_bootstrap"') || return
zle -F $_p9k__worker_resp_fd _p9k_worker_receive zle -F $_p9k__worker_resp_fd _p9k_worker_receive
_p9k__worker_shell_pid=$sysparams[pid] _p9k__worker_shell_pid=$sysparams[pid]
add-zsh-hook zshexit _p9k_worker_cleanup add-zsh-hook zshexit _p9k_worker_cleanup

Loading…
Cancel
Save