robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
9c15a382de
master
1#include <assert.h> 2#include <stdbool.h> 3#include <string.h> 4#include <uv.h> 5 6#include "nvim/event/multiqueue.h" 7#include "nvim/event/rstream.h" 8#include "nvim/event/stream.h" 9#include "nvim/log.h" 10#include "nvim/macros_defs.h" 11#include "nvim/memory.h" 12#include "nvim/memory_defs.h" 13#include "nvim/os/os_defs.h" 14#include "nvim/types_defs.h" 15 16#include "event/rstream.c.generated.h" 17 18void rstream_init_fd (Loop * loop ,RStream * stream ,int fd ) 19FUNC_ATTR_NONNULL_ARG (1 ,2 ) 20{ 21stream_init (loop ,& stream -> s ,fd ,NULL ); 22rstream_init (stream ); 23} 24 25void rstream_init_stream (RStream * stream ,uv_stream_t * uvstream ) 26FUNC_ATTR_NONNULL_ARG (1 ,2 ) 27{ 28stream_init (NULL ,& stream -> s ,-1 ,uvstream ); 29rstream_init (stream ); 30} 31 32void rstream_init (RStream * stream ) 33FUNC_ATTR_NONNULL_ARG (1 ) 34{ 35stream -> read_cb = NULL ; 36stream -> num_bytes = 0 ; 37stream -> buffer = alloc_block (); 38stream -> read_pos = stream -> write_pos = stream -> buffer ; 39stream -> s .close_cb = rstream_close_cb ; 40stream -> s .close_cb_data = stream ; 41} 42 43void rstream_start_inner (RStream * stream ) 44FUNC_ATTR_NONNULL_ARG (1 ) 45{ 46if (stream -> s .uvstream ) { 47uv_read_start (stream -> s .uvstream ,alloc_cb ,read_cb ); 48 }else { 49uv_idle_start (& stream -> s .uv .idle ,fread_idle_cb ); 50 } 51} 52 53/// Starts watching for events from a `Stream` instance. 54/// 55/// @param stream The `Stream` instance 56void rstream_start (RStream * stream ,stream_read_cb cb ,void * data ) 57FUNC_ATTR_NONNULL_ARG (1 ) 58{ 59stream -> read_cb = cb ; 60stream -> s .cb_data = data ; 61stream -> want_read = true; 62if (!stream -> paused_full ) { 63rstream_start_inner (stream ); 64 } 65} 66 67/// Stops watching for events from a `Stream` instance. 68/// 69/// @param stream The `Stream` instance 70void rstream_stop_inner (RStream * stream ) 71FUNC_ATTR_NONNULL_ALL 72{ 73if (stream -> s .uvstream ) { 74uv_read_stop (stream -> s .uvstream ); 75 }else { 76uv_idle_stop (& stream -> s .uv .idle ); 77 } 78} 79 80/// Stops watching for events from a `Stream` instance. 81/// 82/// @param stream The `Stream` instance 83void rstream_stop (RStream * stream ) 84FUNC_ATTR_NONNULL_ALL 85{ 86rstream_stop_inner (stream ); 87stream -> want_read = false; 88} 89 90// Callbacks used by libuv 91 92/// Called by libuv to allocate memory for reading. 93static void alloc_cb (uv_handle_t * handle ,size_t suggested ,uv_buf_t * buf ) 94{ 95RStream * stream = handle -> data ; 96buf -> base = stream -> write_pos ; 97// `uv_buf_t.len` happens to have different size on Windows (as a treat) 98buf -> len = UV_BUF_LEN (rstream_space (stream )); 99} 100 101/// Callback invoked by libuv after it copies the data into the buffer provided 102/// by `alloc_cb`. This is also called on EOF or when `alloc_cb` returns a 103/// 0-length buffer. 104static void read_cb (uv_stream_t * uvstream ,ssize_t cnt ,const uv_buf_t * buf ) 105{ 106RStream * stream = uvstream -> data ; 107 108if (cnt <=0 ) { 109// cnt == 0 means libuv asked for a buffer and decided it wasn't needed: 110// http://docs.libuv.org/en/latest/stream.html#c.uv_read_start. 111// 112// We don't need to do anything with the buffer because the next call 113// to `alloc_cb` will return the same unused pointer (`rbuffer_produced` 114// won't be called) 115if (cnt == UV_ENOBUFS || cnt == 0 ) { 116return ; 117 }else if (cnt == UV_EOF && uvstream -> type == UV_TTY ) { 118// The TTY driver might signal EOF without closing the stream 119invoke_read_cb (stream , true); 120 }else { 121DLOG ("closing Stream (%p): %s (%s)" , (void * )stream , 122uv_err_name ((int )cnt ),os_strerror ((int )cnt )); 123// Read error or EOF, either way stop the stream and invoke the callback 124// with eof == true 125uv_read_stop (uvstream ); 126invoke_read_cb (stream , true); 127 } 128return ; 129 } 130 131// at this point we're sure that cnt is positive, no error occurred 132size_t nread = (size_t )cnt ; 133stream -> num_bytes += nread ; 134stream -> write_pos += cnt ; 135invoke_read_cb (stream , false); 136} 137 138static size_t rstream_space (RStream * stream ) 139{ 140return (size_t )((stream -> buffer + ARENA_BLOCK_SIZE )- stream -> write_pos ); 141} 142 143/// Called by the by the 'idle' handle to emulate a reading event 144/// 145/// Idle callbacks are invoked once per event loop: 146/// - to perform some very low priority activity. 147/// - to keep the loop "alive" (so there is always an event to process) 148static void fread_idle_cb (uv_idle_t * handle ) 149{ 150uv_fs_t req ; 151RStream * stream = handle -> data ; 152 153stream -> uvbuf .base = stream -> write_pos ; 154// `uv_buf_t.len` happens to have different size on Windows. 155stream -> uvbuf .len = UV_BUF_LEN (rstream_space (stream )); 156 157// Synchronous read 158uv_fs_read (handle -> loop ,& req ,stream -> s .fd ,& stream -> uvbuf ,1 ,stream -> s .fpos ,NULL ); 159 160uv_fs_req_cleanup (& req ); 161 162if (req .result <=0 ) { 163uv_idle_stop (& stream -> s .uv .idle ); 164invoke_read_cb (stream , true); 165return ; 166 } 167 168// no errors (req.result (ssize_t) is positive), it's safe to use. 169stream -> write_pos += req .result ; 170stream -> s .fpos += req .result ; 171invoke_read_cb (stream , false); 172} 173 174static void read_event (void ** argv ) 175{ 176RStream * stream = argv [0 ]; 177stream -> pending_read = false; 178if (stream -> read_cb ) { 179size_t available = rstream_available (stream ); 180size_t consumed = stream -> read_cb (stream ,stream -> read_pos ,available ,stream -> s .cb_data , 181stream -> did_eof ); 182assert (consumed <=available ); 183rstream_consume (stream ,consumed ); 184 } 185stream -> s .pending_reqs -- ; 186if (stream -> s .closed && !stream -> s .pending_reqs ) { 187// Last pending read; free the stream. 188stream_close_handle (& stream -> s ); 189 } 190} 191 192size_t rstream_available (RStream * stream ) 193{ 194return (size_t )(stream -> write_pos - stream -> read_pos ); 195} 196 197void rstream_consume (RStream * stream ,size_t consumed ) 198{ 199stream -> read_pos += consumed ; 200size_t remaining = (size_t )(stream -> write_pos - stream -> read_pos ); 201if (remaining > 0 && stream -> read_pos > stream -> buffer ) { 202memmove (stream -> buffer ,stream -> read_pos ,remaining ); 203stream -> read_pos = stream -> buffer ; 204stream -> write_pos = stream -> buffer + remaining ; 205 }else if (remaining == 0 ) { 206stream -> read_pos = stream -> write_pos = stream -> buffer ; 207 } 208 209if (stream -> want_read && stream -> paused_full && rstream_space (stream )) { 210assert (stream -> read_cb ); 211stream -> paused_full = false; 212rstream_start_inner (stream ); 213 } 214} 215 216static void invoke_read_cb (RStream * stream ,bool eof ) 217{ 218stream -> did_eof |=eof ; 219 220if (!rstream_space (stream )) { 221rstream_stop_inner (stream ); 222stream -> paused_full = true; 223 } 224 225// we cannot use pending_reqs as a socket can have both pending reads and writes 226if (stream -> pending_read ) { 227return ; 228 } 229 230// Don't let the stream be closed before the event is processed. 231stream -> s .pending_reqs ++ ; 232stream -> pending_read = true; 233CREATE_EVENT (stream -> s .events ,read_event ,stream ); 234} 235 236static void rstream_close_cb (Stream * s ,void * data ) 237{ 238RStream * stream = data ; 239assert (stream && s == & stream -> s ); 240if (stream -> buffer ) { 241free_block (stream -> buffer ); 242 } 243} 244 245void rstream_may_close (RStream * stream ) 246{ 247stream_may_close (& stream -> s ); 248}