This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
tango-tfe/common/src/tfe_future.cpp

84 lines
1.5 KiB
C++
Raw Normal View History

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <tfe_future.h>
#include <tfe_utils.h>
struct future
{
void * user;
future_success_cb * cb_success;
future_failed_cb * cb_failed;
};
struct promise
{
struct future f;
void * ctx;
promise_ctx_destroy_cb * cb_ctx_destroy;
};
struct future * promise_to_future(struct promise * p)
{
return &p->f;
}
struct promise * future_to_promise(struct future * f)
{
return (struct promise *) f;
}
struct future * future_create(future_success_cb * cb_success, future_failed_cb * cb_failed, void * user)
{
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;
}
void future_destroy(struct future * f)
{
struct promise * promise = future_to_promise(f);
if (promise->cb_ctx_destroy != NULL)
{
promise->cb_ctx_destroy(promise);
}
memset(promise, 0, sizeof(struct promise));
free(promise);
}
void promise_failed(struct promise * p, enum e_future_error error, const char * what)
{
p->f.cb_failed(error, what, p->f.user);
return;
}
void promise_success(struct promise * p, void * result)
{
p->f.cb_success(result, p->f.user);
return;
}
void promise_set_ctx(struct promise * p, void * ctx, promise_ctx_destroy_cb * cb)
{
p->ctx = ctx;
p->cb_ctx_destroy = cb;
return;
}
void * promise_get_ctx(struct promise * p)
{
return p->ctx;
}
void * promise_dettach_ctx(struct promise * p)
{
void * ctx = p->ctx;
p->ctx = NULL;
p->cb_ctx_destroy = NULL;
return ctx;
}