diff options
author | romkatv <roman.perepelitsa@gmail.com> | 2020-01-25 13:01:14 +0300 |
---|---|---|
committer | romkatv <roman.perepelitsa@gmail.com> | 2020-01-25 13:01:14 +0300 |
commit | 565492b10eaca7f80bbca7d55709c2d62617efa0 (patch) | |
tree | 586c7ea4b602fde7858f8716c9d8124bd2441f13 /internal/worker.zsh | |
parent | 148ecf7fc398b9a82c4ac8bbc938409bbd1c55fe (diff) |
migrate over from the old worker api; use worker in battery on macOS
Diffstat (limited to 'internal/worker.zsh')
-rw-r--r-- | internal/worker.zsh | 181 |
1 files changed, 63 insertions, 118 deletions
diff --git a/internal/worker.zsh b/internal/worker.zsh index 66ebc4d1..f1e4181e 100644 --- a/internal/worker.zsh +++ b/internal/worker.zsh @@ -7,20 +7,30 @@ function _p9k_worker_main() { 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_end() { print -nr -- $'\x1e' } - function _p9k_worker_reply() { print -nr -- e${(pj:\n:)@}$'\x1e' } - typeset -g IFS=$' \t\n\0' + local -i reset local req fd local -a ready - local -A inflight # fd => id$'\x1f'sync - local -A last_async - local -ri _p9k_worker_runs_me=1 + local _p9k_worker_request_id + local -A _p9k_worker_fds # fd => id$'\x1f'callback + local -A _p9k_worker_inflight # id => inflight count + + function _p9k_worker_reply_begin() { print -nr -- e } + function _p9k_worker_reply_end() { print -nr -- $'\x1e' } + function _p9k_worker_reply() { print -nr -- e${(pj:\n:)@}$'\x1e' } + + # 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 + } { - while zselect -a ready 0 ${(k)inflight}; do + while zselect -a ready 0 ${(k)_p9k_worker_fds}; do [[ $ready[1] == -r ]] || return for fd in ${ready:1}; do if [[ $fd == 0 ]]; then @@ -32,44 +42,30 @@ function _p9k_worker_main() { sysread 'buf[$#buf+1]' || return done for req in ${(ps:\x1e:)buf}; do - local parts=("${(@ps:\x1f:)req}") # id cond async sync - if () { eval $parts[2] }; then - if [[ -n $parts[3] ]]; then - sysopen -r -o cloexec -u fd <( - () { eval $parts[3]; } && print -n '\x1e') || return - inflight[$fd]=$parts[1]$'\x1f'$parts[4] - continue - fi - () { eval $parts[4] } - fi - if [[ -n $parts[1] ]]; then - print -rn -- d$parts[1]$'\x1e' || return - fi + _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 done else - local cur= + local REPLY= while true; do - sysread -i $fd 'cur[$#cur+1]' && continue - (( $? == 5 )) || return + sysread -i $fd 'REPLY[$#REPLY+1]' && continue + (( $? == 5 )) || return break done - local parts=("${(@ps:\x1f:)inflight[$fd]}") # id sync - if [[ $cur == *$'\x1e' ]]; then - cur[-1]="" - () { - local prev - if [[ -n $parts[1] ]]; then - prev=$last_async[$parts[1]] - last_async[$parts[1]]=$cur - fi - eval $parts[2] - } + 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] } fi - if [[ -n $parts[1] ]]; then - print -rn -- d$parts[1]$'\x1e' || return + 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 fi - unset "inflight[$fd]" - exec {fd}>&- fi done done @@ -84,33 +80,16 @@ typeset -g _p9k__worker_resp_fd typeset -g _p9k__worker_shell_pid typeset -g _p9k__worker_file_prefix typeset -gA _p9k__worker_request_map -typeset -ga _p9k__worker_request_queue -# todo: remove these -function _p9k_worker_send_params() { } -function _p9k_worker_send_functions() { } - -# invoked in master: _p9k_worker_invoke <request-id> <cond> <async> <sync> +# invoked in master: _p9k_worker_invoke <request-id> <list> function _p9k_worker_invoke() { - if [[ -n $_p9k__worker_resp_fd ]]; then - local req=$1$'\x1f'$2$'\x1f'$3$'\x1f'$4$'\x1e' - if [[ -n $_p9k__worker_req_fd && $+_p9k__worker_request_map[$1] == 0 ]]; then - [[ -n $1 ]] && _p9k__worker_request_map[$1]= - print -rnu $_p9k__worker_req_fd -- $req - return - fi - if [[ -n $1 ]]; then - (( $+_p9k__worker_request_map[$1] )) || _p9k__worker_request_queue+=$1 - _p9k__worker_request_map[$1]=$req - else - _p9k__worker_request_queue+=$req - fi + [[ -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 else - if () { eval $2 }; then - local REPLY= - () { eval $3 } - () { eval $4 } - fi + _p9k__worker_request_map[$1]=$req fi } @@ -134,7 +113,6 @@ function _p9k_worker_stop() { _p9k__worker_resp_fd= _p9k__worker_shell_pid= _p9k__worker_request_map=() - _p9k__worker_request_queue=() return 0 } @@ -145,14 +123,15 @@ function _p9k_worker_receive() { { (( $# <= 1 )) || return - local buf resp reset + local buf resp while true; do sysread -t 0 -i $_p9k__worker_resp_fd 'buf[$#buf+1]' && continue (( $? == 4 )) || return - [[ $buf[-1] == (|$'\x1e') ]] && break + [[ $buf == (|*$'\x1e')$'\x05'# ]] && break sysread -i $_p9k__worker_resp_fd 'buf[$#buf+1]' || return done + local -i reset for resp in ${(ps:\x1e:)${buf//$'\x05'}}; do local arg=$resp[2,-1] case $resp[1] in @@ -172,7 +151,6 @@ function _p9k_worker_receive() { echo "latency: $latency ms" >>/tmp/log start_time=0 fi - reset=1 () { eval $arg } ;; s) @@ -181,15 +159,10 @@ function _p9k_worker_receive() { _p9k__worker_pid=$arg sysopen -w -o cloexec -u _p9k__worker_req_fd $_p9k__worker_file_prefix.fifo || return local req= - for req in $_p9k__worker_request_queue; do - if [[ $req != *$'\x1e' ]]; then - local id=$req - req=$_p9k__worker_request_map[$id] - _p9k__worker_request_map[$id]= - fi + for req in $_p9k__worker_request_map; do print -rnu $_p9k__worker_req_fd -- $req || return done - _p9k__worker_request_queue=() + _p9k__worker_request_map=({${(k)^_p9k__worker_request_map},''}) ;; *) return 1 @@ -197,6 +170,11 @@ function _p9k_worker_receive() { esac done + if (( reset == 2 )); then + _p9k_refresh_reason=worker + _p9k_set_prompt + _p9k_refresh_reason='' + fi (( reset )) && _p9k_reset_prompt return 0 } always { @@ -246,62 +224,29 @@ function _p9k_reset_prompt() { zle && zle reset-prompt && zle -R } -emulate -L zsh -o prompt_subst # -o xtrace +emulate -L zsh -o prompt_subst -o interactive_comments # -o xtrace -POWERLEVEL9K_WORKER_LOG_LEVEL=DEBUG +# POWERLEVEL9K_WORKER_LOG_LEVEL=DEBUG zmodload zsh/datetime zmodload zsh/system autoload -Uz add-zsh-hook -typeset -F start_time=EPOCHREALTIME -_p9k_worker_start -echo -E - $((1000*(EPOCHREALTIME-start_time))) - -function foo_cond() { - typeset -gi foo_counter - typeset -g foo="[$bar] cond $1 $((foo_counter++))" +function foo_compute() { + _p9k_worker_reply 'echo sync latency: $((1000*(EPOCHREALTIME-'$1'))) >>/tmp/log' + _p9k_worker_async foo_async "foo_sync $1" } function foo_async() { - sleep 1 - REPLY="$foo / async $1" } function foo_sync() { - REPLY+=" / sync $1" - _p9k_worker_reply "typeset -g foo=${(q)REPLY}" -} - -() { - typeset -g RPROMPT='$foo %*' - typeset -g bar='lol' - _p9k_worker_send_params bar - - local f - for f in foo_{cond,async,sync}; do - _p9k_worker_invoke "" "function $f() { $functions[$f] }" "" "" - done - - () { - local -i i - for i in {1..10}; do - _p9k_worker_invoke foo$i "foo_cond c$i\$\{" "foo_async a$i\$\{" "foo_sync s$i\$\{" - done - } -} - -function in_worker() { - _p9k_worker_reply 'echo roundtrip: $((1000*(EPOCHREALTIME-'$1'))) >>/tmp/log' + _p9k_worker_reply 'echo async latency: $((1000*(EPOCHREALTIME-'$1'))) >>/tmp/log' } -_p9k_worker_invoke "" "function in_worker() { $functions[in_worker] }" "" "" -_p9k_worker_invoke w "in_worker $EPOCHREALTIME" "" "" -# for i in {1..100}; do _p9k_worker_invoke w$i "in_worker $EPOCHREALTIME"; done +typeset -F start_time=EPOCHREALTIME +_p9k_worker_start +_p9k_worker_invoke first '_p9k_worker_reply ""' +echo -E - $((1000*(EPOCHREALTIME-start_time))) -# TODO: -# -# - Segment API: _p9k_prompt_foo_worker_{params,cond,async,sync}. -# - _p9k_worker_request -- cacheable variable that contains full request to worker. -# - _p9k_set_prompt sends stuff to worker or evals it. -# - _p9k_on_expand has _REALTIME check at the top and sends keep-alive to worker. +bm() { _p9k_worker_invoke foo$1 "foo_compute $EPOCHREALTIME" } |