Hello Peeps!

I've been thinking about a new libuv handle type - uv_promise_t with the 
following api:

typedef enum {
  UV_PROMISE_BROKEN = -1,
  UV_PROMISE_FULFILLED
} uv_promise_status;
 
typedef struct {
  uv_promise_status status;
  void* result;
} uv_promise_result_t;
 
typedef struct uv_promise_s uv_promise_t;
typedef struct uv_promise_result_s uv_promise_result_t;
typedef void (*uv_promise_cb)(uv_promise_t* self,
                              uv_promise_status status,
                              void* result);
 
/*
 * uv_promise_t is a subclass of uv_handle_t
 * 
 * uv_promise_t is the opposite of uv_async_t, and is used to send
 * notifications to other theads from the event loop thread.
 */
struct uv_promise_s {
  UV_HANDLE_FIELDS
  uv_promise_cb promise_cb;
  uv_promise_status status;
  void* result;
  UV_PROMISE_PRIVATE_FIELDS
};
 
UV_EXTERN int uv_promise_init(uv_loop_t*,
                              uv_promise_t* handle,
                              uv_promise_cb callback);
 
/*
 * uv_promise_fulfil is used to fulfil a promise from inside the event loop
 *
 * returns 0 on success, UV_EINVAL if the promise has aleardy been fulfilled
 * or broken.
 */
UV_EXTERN int uv_promise_fulfil(uv_promise_t* handle, void* result);
 
/*
 * uv_promise_break is used to break a promise from inside the event loop
 *
 * returns 0 on success, UV_EINVAL if the promise has aleardy been fulfilled
 * or broken.
 */
UV_EXTERN int uv_promise_break(uv_promise_t* handle, int status);
 
/*
 * uv_promise_wait will block until a promise has been fulfilled or broken.
 * uv_promise_result can be called after to obtain a uv_promise_result_t.
 */
UV_EXTERN void uv_promise_wait(uv_promise_t* handle);
 
/*
 * same as uv_promise_wait, but returns EAGAIN if promise is not yet ready.
 */
UV_EXTERN int uv_promise_trywait(uv_promise_t* handle);
 
/*
 * returns result of promise execution if one is available.
 * for use after a successful call to uv_promise_wait or uv_promise_trywait.
 */
UV_EXTERN uv_promise_result_t uv_promise_result(uv_promise_t* handle);


This primitive is designed to make communication from the event loop thread 
with application threads simpler (the opposite of uv_async_t).

I would love your feedback here or on the public gist 
- https://gist.github.com/avalanche123/98cdbc79feb0feaea33f

Cheers,

Bulat

-- 
You received this message because you are subscribed to the Google Groups 
"libuv" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/d/optout.

Reply via email to