2018-08-21 16:11:50 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include <tfe_future.h>
|
|
|
|
|
#include <tfe_utils.h>
|
|
|
|
|
|
|
|
|
|
struct future
|
2018-08-14 19:44:34 +08:00
|
|
|
{
|
2018-08-21 16:11:50 +08:00
|
|
|
void * user;
|
|
|
|
|
future_success_cb * cb_success;
|
|
|
|
|
future_failed_cb * cb_failed;
|
2018-08-14 19:44:34 +08:00
|
|
|
};
|
2018-08-21 16:11:50 +08:00
|
|
|
|
|
|
|
|
struct promise
|
2018-08-14 19:44:34 +08:00
|
|
|
{
|
2018-08-21 16:11:50 +08:00
|
|
|
struct future f;
|
|
|
|
|
void * ctx;
|
|
|
|
|
promise_ctx_destroy_cb * cb_ctx_destroy;
|
2018-08-14 19:44:34 +08:00
|
|
|
};
|
|
|
|
|
|
2018-08-21 16:11:50 +08:00
|
|
|
struct future * promise_to_future(struct promise * p)
|
2018-08-14 19:44:34 +08:00
|
|
|
{
|
2018-08-21 16:11:50 +08:00
|
|
|
return &p->f;
|
2018-08-14 19:44:34 +08:00
|
|
|
}
|
2018-08-21 16:11:50 +08:00
|
|
|
|
|
|
|
|
struct promise * future_to_promise(struct future * f)
|
2018-08-14 19:44:34 +08:00
|
|
|
{
|
2018-08-21 16:11:50 +08:00
|
|
|
return (struct promise *) f;
|
2018-08-14 19:44:34 +08:00
|
|
|
}
|
2018-08-21 16:11:50 +08:00
|
|
|
|
|
|
|
|
struct future * future_create(future_success_cb * cb_success, future_failed_cb * cb_failed, void * user)
|
2018-08-14 19:44:34 +08:00
|
|
|
{
|
2018-08-21 16:11:50 +08:00
|
|
|
struct promise * p = ALLOC(struct promise, 1);
|
|
|
|
|
p->f.user = user;
|
|
|
|
|
p->f.cb_success = cb_success;
|
|
|
|
|
p->f.cb_failed = cb_failed;
|
|
|
|
|
return &p->f;
|
2018-08-14 19:44:34 +08:00
|
|
|
}
|
2018-08-21 16:11:50 +08:00
|
|
|
|
2018-08-14 19:44:34 +08:00
|
|
|
void future_destroy(struct future * f)
|
|
|
|
|
{
|
2018-08-21 16:11:50 +08:00
|
|
|
struct promise * promise = future_to_promise(f);
|
|
|
|
|
if (promise->cb_ctx_destroy != NULL)
|
2018-08-14 19:44:34 +08:00
|
|
|
{
|
|
|
|
|
promise->cb_ctx_destroy(promise);
|
|
|
|
|
}
|
2018-08-21 16:11:50 +08:00
|
|
|
|
|
|
|
|
memset(promise, 0, sizeof(struct promise));
|
2018-08-14 19:44:34 +08:00
|
|
|
free(promise);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-21 16:11:50 +08:00
|
|
|
void promise_failed(struct promise * p, enum e_future_error error, const char * what)
|
2018-08-14 19:44:34 +08:00
|
|
|
{
|
2018-08-21 16:11:50 +08:00
|
|
|
p->f.cb_failed(error, what, p->f.user);
|
2018-08-14 19:44:34 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2018-08-21 16:11:50 +08:00
|
|
|
|
|
|
|
|
void promise_success(struct promise * p, void * result)
|
2018-08-14 19:44:34 +08:00
|
|
|
{
|
|
|
|
|
p->f.cb_success(result, p->f.user);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-08-21 16:11:50 +08:00
|
|
|
|
|
|
|
|
void promise_set_ctx(struct promise * p, void * ctx, promise_ctx_destroy_cb * cb)
|
2018-08-14 19:44:34 +08:00
|
|
|
{
|
2018-08-21 16:11:50 +08:00
|
|
|
p->ctx = ctx;
|
|
|
|
|
p->cb_ctx_destroy = cb;
|
2018-08-14 19:44:34 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2018-08-21 16:11:50 +08:00
|
|
|
|
|
|
|
|
void * promise_get_ctx(struct promise * p)
|
2018-08-14 19:44:34 +08:00
|
|
|
{
|
|
|
|
|
return p->ctx;
|
|
|
|
|
}
|
2018-08-21 16:11:50 +08:00
|
|
|
|
|
|
|
|
void * promise_dettach_ctx(struct promise * p)
|
2018-08-14 19:44:34 +08:00
|
|
|
{
|
2018-08-21 16:11:50 +08:00
|
|
|
void * ctx = p->ctx;
|
|
|
|
|
p->ctx = NULL;
|
|
|
|
|
p->cb_ctx_destroy = NULL;
|
2018-08-14 19:44:34 +08:00
|
|
|
return ctx;
|
|
|
|
|
}
|