robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
4ed2e66d2e
master
1#include <assert.h> 2#include <stdbool.h> 3#include <stddef.h> 4#include <uv.h> 5#include <uv/version.h> 6 7#include "nvim/event/defs.h" 8#include "nvim/event/loop.h" 9#include "nvim/event/stream.h" 10#include "nvim/log.h" 11#include "nvim/memory.h" 12#include "nvim/types_defs.h" 13#ifdef MSWIN 14# include "nvim/os/os_win_console.h" 15#endif 16 17#include "event/stream.c.generated.h" 18 19// For compatibility with libuv < 1.19.0 (tested on 1.18.0) 20#if UV_VERSION_MINOR < 19 21# define uv_stream_get_write_queue_size (stream ) stream->write_queue_size 22#endif 23 24/// Sets the stream associated with `fd` to "blocking" mode. 25/// 26/// @return `0` on success, or libuv error code on failure. 27int stream_set_blocking (int fd ,bool blocking ) 28{ 29// Private loop to avoid conflict with existing watcher(s): 30// uv__io_stop: Assertion `loop->watchers[w->fd] == w' failed. 31uv_loop_t loop ; 32uv_pipe_t stream ; 33uv_loop_init (& loop ); 34uv_pipe_init (& loop ,& stream ,0 ); 35uv_pipe_open (& stream ,fd ); 36int retval = uv_stream_set_blocking ((uv_stream_t * )& stream ,blocking ); 37uv_close ((uv_handle_t * )& stream ,NULL ); 38uv_run (& loop ,UV_RUN_NOWAIT );// not necessary, but couldn't hurt. 39uv_loop_close (& loop ); 40return retval ; 41} 42 43void stream_init (Loop * loop ,Stream * stream ,int fd ,uv_stream_t * uvstream ) 44FUNC_ATTR_NONNULL_ARG (2 ) 45{ 46// The underlying stream is either a file or an existing uv stream. 47assert (uvstream == NULL ?fd >=0 && loop != NULL :fd < 0 && loop == NULL ); 48stream -> uvstream = uvstream ; 49 50if (fd >=0 ) { 51uv_handle_type type = uv_guess_handle (fd ); 52stream -> fd = fd ; 53 54if (type == UV_FILE ) { 55// Non-blocking file reads are simulated with an idle handle that reads in 56// chunks of the ring buffer size, giving time for other events to be 57// processed between reads. 58uv_idle_init (& loop -> uv ,& stream -> uv .idle ); 59stream -> uv .idle .data = stream ; 60#ifdef MSWIN 61} else if ( type == UV_TTY ) { 62uv_tty_init ( & loop -> uv , & stream -> uv . tty , fd , 0 ); 63uv_tty_set_mode ( & stream -> uv . tty , UV_TTY_MODE_RAW ); 64DWORD dwMode ; 65if ( GetConsoleMode ( stream -> uv . tty . handle , & dwMode )) { 66dwMode |= ENABLE_VIRTUAL_TERMINAL_INPUT ; 67SetConsoleMode ( stream -> uv . tty . handle , dwMode ); 68} 69stream -> uvstream = ( uv_stream_t * ) & stream -> uv . tty ; 70#endif 71} else { 72assert ( type == UV_NAMED_PIPE || type == UV_TTY ); 73uv_pipe_init ( & loop -> uv , & stream -> uv . pipe , 0 ); 74uv_pipe_open ( & stream -> uv . pipe , fd ); 75stream -> uvstream = ( uv_stream_t * ) & stream -> uv . pipe ; 76} 77} 78 79if ( stream -> uvstream ) { 80stream -> uvstream -> data = stream ; 81} 82 83stream -> fpos = 0 ; 84stream -> internal_data = NULL ; 85stream -> curmem = 0 ; 86stream -> maxmem = 0 ; 87stream -> pending_reqs = 0 ; 88stream -> write_cb = NULL ; 89stream -> close_cb = NULL ; 90stream -> internal_close_cb = NULL ; 91stream -> closed = false; 92stream -> events = NULL ; 93} 94 95void stream_may_close ( Stream * stream ) 96FUNC_ATTR_NONNULL_ARG ( 1 ) 97{ 98if ( stream -> closed ) { 99return; 100} 101DLOG ( "closing Stream: %p" , ( void * ) stream ); 102stream -> closed = true; 103 104#ifdef MSWIN 105if ( UV_TTY == uv_guess_handle ( stream -> fd )) { 106// Undo UV_TTY_MODE_RAW from stream_init(). #10801 107uv_tty_set_mode ( & stream -> uv . tty , UV_TTY_MODE_NORMAL ); 108} 109#endif 110 111if (! stream -> pending_reqs ) { 112stream_close_handle ( stream ); 113} // Else: rstream.c:read_event() or wstream.c:write_cb() will call stream_close_handle(). 114} 115 116void stream_close_handle ( Stream * stream ) 117FUNC_ATTR_NONNULL_ALL 118{ 119uv_handle_t * handle = NULL ; 120if ( stream -> uvstream ) { 121if ( uv_stream_get_write_queue_size ( stream -> uvstream ) > 0 ) { 122WLOG ( "closed Stream (%p) with %zu unwritten bytes" , 123( void * ) stream , 124uv_stream_get_write_queue_size ( stream -> uvstream )); 125} 126handle = ( uv_handle_t * ) stream -> uvstream ; 127} else { 128handle = ( uv_handle_t * ) & stream -> uv . idle ; 129} 130 131assert ( handle != NULL ); 132 133if ( stream -> before_close_cb ) { 134stream -> pending_reqs ++ ; 135stream -> before_close_cb ( stream , stream -> close_cb_data ); 136stream -> pending_reqs -- ; 137} 138if (! uv_is_closing ( handle )) { 139uv_close ( handle , close_cb ); 140} 141} 142 143static void close_cb ( uv_handle_t * handle ) 144{ 145Stream * stream = handle -> data ; 146// Check if handle->data is NULL here, in case this callback is called between 147// the handle's initialization and stream_init(). 148if ( stream && stream -> close_cb ) { 149stream -> close_cb ( stream , stream -> close_cb_data ); 150} 151if ( stream && stream -> internal_close_cb ) { 152stream -> internal_close_cb ( stream , stream -> internal_data ); 153 } 154}