aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/nvim/os/wstream.c31
-rw-r--r--src/nvim/os/wstream_defs.h12
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