From 991d3ec1e679bb6407f2a5820910d2968424183c Mon Sep 17 00:00:00 2001 From: Thiago de Arruda Date: Thu, 16 Jul 2015 23:10:04 -0300 Subject: event loop: New abstraction layer with refactored time/signal API - Add event loop abstraction module under src/nvim/event. The src/nvim/event/loop module replaces src/nvim/os/event - Remove direct dependency on libuv signal/timer API and use the new abstraction instead. - Replace all references to uv_default_loop() by &loop.uv, a new global variable that wraps libuv main event loop but allows the event loop functions to be reused in other contexts. --- src/nvim/os/shell.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/os/shell.c') diff --git a/src/nvim/os/shell.c b/src/nvim/os/shell.c index 48174533a6..9c0d5fca67 100644 --- a/src/nvim/os/shell.c +++ b/src/nvim/os/shell.c @@ -8,7 +8,7 @@ #include "nvim/ascii.h" #include "nvim/lib/kvec.h" #include "nvim/log.h" -#include "nvim/os/event.h" +#include "nvim/event/loop.h" #include "nvim/os/job.h" #include "nvim/os/rstream.h" #include "nvim/os/shell.h" -- cgit From ac2bd0256183fe4255e5fcccf37f860f037d43a6 Mon Sep 17 00:00:00 2001 From: Thiago de Arruda Date: Thu, 16 Jul 2015 23:10:15 -0300 Subject: rstream/wstream: Unify structures and simplify API - Simplify RStream/WStream API and make it more consistent with libuv. - Move into the event loop layer(event subdirectory) - Remove uv_helpers module. - Simplify job/process internal modules/API. - Unify RStream and WStream into a single structure. This is necessary because libuv streams can be readable and writable at the same time(and because the uv_helpers.c hack to associate multiple streams with libuv handle was removed) - Make struct definition public, allowing more flexible/simple memory management by users of the module. - Adapt channel/job modules to cope with the changes. --- src/nvim/os/shell.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/nvim/os/shell.c') diff --git a/src/nvim/os/shell.c b/src/nvim/os/shell.c index 9c0d5fca67..04ac9f1c03 100644 --- a/src/nvim/os/shell.c +++ b/src/nvim/os/shell.c @@ -10,7 +10,7 @@ #include "nvim/log.h" #include "nvim/event/loop.h" #include "nvim/os/job.h" -#include "nvim/os/rstream.h" +#include "nvim/event/rstream.h" #include "nvim/os/shell.h" #include "nvim/os/signal.h" #include "nvim/types.h" @@ -189,7 +189,7 @@ static int do_os_system(char **argv, { // the output buffer DynamicBuffer buf = DYNAMIC_BUFFER_INIT; - rstream_cb data_cb = system_data_cb; + stream_read_cb data_cb = system_data_cb; if (nread) { *nread = 0; } @@ -283,7 +283,7 @@ static void dynamic_buffer_ensure(DynamicBuffer *buf, size_t desired) buf->data = xrealloc(buf->data, buf->cap); } -static void system_data_cb(RStream *rstream, RBuffer *buf, void *data, bool eof) +static void system_data_cb(Stream *stream, RBuffer *buf, void *data, bool eof) { Job *job = data; DynamicBuffer *dbuf = job_data(job); @@ -294,7 +294,7 @@ static void system_data_cb(RStream *rstream, RBuffer *buf, void *data, bool eof) dbuf->len += nread; } -static void out_data_cb(RStream *rstream, RBuffer *buf, void *data, bool eof) +static void out_data_cb(Stream *stream, RBuffer *buf, void *data, bool eof) { RBUFFER_UNTIL_EMPTY(buf, ptr, len) { size_t written = write_output(ptr, len, false, @@ -470,7 +470,7 @@ static size_t write_output(char *output, size_t remaining, bool to_buffer, return (size_t)(output - start); } -static void shell_write_cb(WStream *wstream, void *data, int status) +static void shell_write_cb(Stream *stream, void *data, int status) { Job *job = data; job_close_in(job); -- cgit From aa9cb48bf08af14068178619414590254b263882 Mon Sep 17 00:00:00 2001 From: Thiago de Arruda Date: Fri, 17 Jul 2015 00:32:07 -0300 Subject: job: Replace by a better process abstraction layer - New libuv/pty process abstraction with simplified API and no globals. - Remove nvim/os/job*. Jobs are now a concept that apply only to programs spawned by vimscript job* functions. - Refactor shell.c/channel.c to use the new module, which brings a number of advantages: - Simplified API, less code - No slots in the user job table are used - Not possible to acidentally receive data from vimscript - Implement job table in eval.c, which is now a hash table with unilimited job slots and unique job ids. --- src/nvim/os/shell.c | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) (limited to 'src/nvim/os/shell.c') diff --git a/src/nvim/os/shell.c b/src/nvim/os/shell.c index 04ac9f1c03..e0d67d4951 100644 --- a/src/nvim/os/shell.c +++ b/src/nvim/os/shell.c @@ -9,7 +9,7 @@ #include "nvim/lib/kvec.h" #include "nvim/log.h" #include "nvim/event/loop.h" -#include "nvim/os/job.h" +#include "nvim/event/uv_process.h" #include "nvim/event/rstream.h" #include "nvim/os/shell.h" #include "nvim/os/signal.h" @@ -204,17 +204,15 @@ static int do_os_system(char **argv, char prog[MAXPATHL]; xstrlcpy(prog, argv[0], MAXPATHL); - int status; - JobOptions opts = JOB_OPTIONS_INIT; - opts.argv = argv; - opts.data = &buf; - opts.writable = input != NULL; - opts.stdout_cb = data_cb; - opts.stderr_cb = data_cb; - opts.exit_cb = NULL; - Job *job = job_start(opts, &status); - - if (status <= 0) { + Stream in, out, err; + UvProcess uvproc = uv_process_init(&buf); + Process *proc = &uvproc.process; + proc->argv = argv; + proc->in = input != NULL ? &in : NULL; + proc->out = &out; + proc->err = &err; + if (!process_spawn(&loop, proc)) { + loop_poll_events(&loop, 0); // Failed, probably due to `sh` not being executable if (!silent) { MSG_PUTS(_("\nCannot execute ")); @@ -224,28 +222,32 @@ static int do_os_system(char **argv, return -1; } + if (input != NULL) { + wstream_init(proc->in, 0); + } + rstream_init(proc->out, 0); + rstream_start(proc->out, data_cb); + rstream_init(proc->err, 0); + rstream_start(proc->err, data_cb); + // write the input, if any if (input) { WBuffer *input_buffer = wstream_new_buffer((char *) input, len, 1, NULL); - if (!job_write(job, input_buffer)) { - // couldn't write, stop the job and tell the user about it - job_stop(job); + if (!wstream_write(&in, input_buffer)) { + // couldn't write, stop the process and tell the user about it + process_stop(proc); return -1; } // close the input stream after everything is written - job_write_cb(job, shell_write_cb); - } else { - // close the input stream, let the process know that no more input is - // coming - job_close_in(job); + wstream_set_write_cb(&in, shell_write_cb); } // invoke busy_start here so event_poll_until wont change the busy state for // the UI ui_busy_start(); ui_flush(); - status = job_wait(job, -1); + int status = process_wait(proc, -1); ui_busy_stop(); // prepare the out parameters if requested @@ -285,8 +287,7 @@ static void dynamic_buffer_ensure(DynamicBuffer *buf, size_t desired) static void system_data_cb(Stream *stream, RBuffer *buf, void *data, bool eof) { - Job *job = data; - DynamicBuffer *dbuf = job_data(job); + DynamicBuffer *dbuf = data; size_t nread = buf->size; dynamic_buffer_ensure(dbuf, dbuf->len + nread + 1); @@ -472,6 +473,5 @@ static size_t write_output(char *output, size_t remaining, bool to_buffer, static void shell_write_cb(Stream *stream, void *data, int status) { - Job *job = data; - job_close_in(job); + stream_close(stream, NULL); } -- cgit