diff options
author | Nicolas Hillegeer <nicolas@hillegeer.com> | 2014-07-23 14:20:36 +0200 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-07-27 14:00:44 -0300 |
commit | 06cc046e30936cc78fb4c3ca03b7e4333bf397f8 (patch) | |
tree | 8fe0090bd22811796c925ece435dfcaab7c93a3d | |
parent | 9f624f0937ded2cbb94a4f4f3fc6721d1a9a9cfc (diff) | |
download | rneovim-06cc046e30936cc78fb4c3ca03b7e4333bf397f8.tar.gz rneovim-06cc046e30936cc78fb4c3ca03b7e4333bf397f8.tar.bz2 rneovim-06cc046e30936cc78fb4c3ca03b7e4333bf397f8.zip |
wstream: write completion callback
Now modules using the wstream can find out what's happening to their writes.
-rw-r--r-- | src/nvim/os/wstream.c | 31 | ||||
-rw-r--r-- | src/nvim/os/wstream_defs.h | 12 |
2 files changed, 43 insertions, 0 deletions
diff --git a/src/nvim/os/wstream.c b/src/nvim/os/wstream.c index 0978d33a10..3886085a3b 100644 --- a/src/nvim/os/wstream.c +++ b/src/nvim/os/wstream.c @@ -21,6 +21,9 @@ struct wstream { // Number of pending requests size_t pending_reqs; bool freed; + // (optional) Write callback and data + wstream_cb cb; + void *data; }; struct wbuffer { @@ -57,6 +60,7 @@ WStream * wstream_new(size_t maxmem) rv->curmem = 0; rv->pending_reqs = 0; rv->freed = false; + rv->cb = NULL; return rv; } @@ -83,6 +87,25 @@ void wstream_set_stream(WStream *wstream, uv_stream_t *stream) wstream->stream = stream; } +/// Sets a callback that will be called on completion of a write request, +/// indicating failure/success. +/// +/// This affects all requests currently in-flight as well. Overwrites any +/// possible earlier callback. +/// +/// @note This callback will not fire if the write request couldn't even be +/// queued properly (i.e.: when `wstream_write() returns an error`). +/// +/// @param wstream The `WStream` instance +/// @param cb The callback +/// @param data User-provided data that will be passed to `cb` +void wstream_set_write_cb(WStream *wstream, wstream_cb cb, void *data) + FUNC_ATTR_NONNULL_ARG(1) +{ + wstream->cb = cb; + wstream->data = data; +} + /// Queues data for writing to the backing file descriptor of a `WStream` /// instance. This will fail if the write would cause the WStream use more /// memory than specified by `maxmem`. @@ -162,6 +185,14 @@ static void write_cb(uv_write_t *req, int status) release_wbuffer(data->buffer); data->wstream->pending_reqs--; + + if (data->wstream->cb) { + data->wstream->cb(data->wstream, + data->wstream->data, + data->wstream->pending_reqs, + status); + } + if (data->wstream->freed && data->wstream->pending_reqs == 0) { // Last pending write, free the wstream; free(data->wstream); diff --git a/src/nvim/os/wstream_defs.h b/src/nvim/os/wstream_defs.h index 1bf61ffce1..e42481f283 100644 --- a/src/nvim/os/wstream_defs.h +++ b/src/nvim/os/wstream_defs.h @@ -5,5 +5,17 @@ typedef struct wbuffer WBuffer; typedef struct wstream WStream; typedef void (*wbuffer_data_finalizer)(void *data); +/// Type of function called when the WStream has information about a write +/// request. +/// +/// @param wstream The `WStream` instance +/// @param data User-defined data +/// @param pending The number of write requests that are still pending +/// @param status 0 on success, anything else indicates failure +typedef void (*wstream_cb)(WStream *wstream, + void *data, + size_t pending, + int status); + #endif // NVIM_OS_WSTREAM_DEFS_H |