wstream: write completion callback

Now modules using the wstream can find out what's happening to their writes.
This commit is contained in:
Nicolas Hillegeer
2014-07-23 14:20:36 +02:00
committed by Thiago de Arruda
parent 9f624f0937
commit 06cc046e30
2 changed files with 43 additions and 0 deletions

View File

@@ -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);

View File

@@ -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