From 5d20a525523fdc4491cf22dcbfeff74a89ae50c3 Mon Sep 17 00:00:00 2001 From: zhengchao Date: Fri, 23 Nov 2018 20:15:01 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=A2=9E=E5=8A=A0promise=5Ff?= =?UTF-8?q?inish=E5=87=BD=E6=95=B0=EF=BC=8C=E5=AE=9E=E7=8E=B0future?= =?UTF-8?q?=E7=9A=84cancel=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/include/tfe_future.h | 3 ++ common/src/tfe_future.cpp | 57 +++++++++++++++++++++++++++++++------ platform/src/ssl_stream.cpp | 4 +-- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/common/include/tfe_future.h b/common/include/tfe_future.h index c6af055..4a74726 100644 --- a/common/include/tfe_future.h +++ b/common/include/tfe_future.h @@ -31,6 +31,9 @@ void future_destroy(struct future * f); struct promise * future_to_promise(struct future * f); void promise_failed(struct promise * p, enum e_future_error error, const char * what); void promise_success(struct promise * p, void * result); +void promise_finish(struct promise * p); +void promise_allow_many_successes(struct promise *p); + void promise_set_ctx(struct promise * p, void * ctx, promise_ctx_destroy_cb * cb); void * promise_get_ctx(struct promise * p); void * promise_dettach_ctx(struct promise * p); diff --git a/common/src/tfe_future.cpp b/common/src/tfe_future.cpp index d0c51f3..19f20f7 100644 --- a/common/src/tfe_future.cpp +++ b/common/src/tfe_future.cpp @@ -45,7 +45,9 @@ struct promise { struct future f; void * ctx; - int has_timeout; + char has_timeout; + char ref_cnt; + char may_success_many_times; promise_ctx_destroy_cb * cb_ctx_destroy; struct _future_promise_debug debug; }; @@ -117,11 +119,34 @@ void future_promise_library_init(const char* profile) g_is_FP_init=1; return; } +static struct promise * __future_to_promise(struct future * f) +{ + return (struct promise *) f; +} +static void __promise_destroy(struct promise *p) +{ + if (p->cb_ctx_destroy != NULL) + { + p->cb_ctx_destroy(p->ctx); + } + if(!g_FP_instance.no_stats) FS_operate(g_FP_instance.fs_handle,g_FP_instance.fsid_f_num, 0, FS_OP_SUB, 1); + memset(p, 0, sizeof(struct promise)); + free(p); + return; +} struct promise * future_to_promise(struct future * f) { + struct promise *p=__future_to_promise(f); + p->ref_cnt++; + assert(p->ref_cnt==2); return (struct promise *) f; } +void promise_allow_many_successes(struct promise *p) +{ + p->may_success_many_times=1; + return; +} struct field_get_set_args { MESA_htable_handle htable; @@ -165,6 +190,7 @@ struct future * future_create(const char* symbol, future_success_cb * cb_success p->f.user = user; p->f.cb_success = cb_success; p->f.cb_failed = cb_failed; + p->ref_cnt=1; strncpy(p->f.symbol,symbol,sizeof(p->f.symbol)); if(!g_FP_instance.no_stats) { @@ -185,16 +211,23 @@ void future_set_timeout(struct future * f, struct timeval timeout) p->has_timeout=1; return; } + void future_destroy(struct future * f) { - struct promise * p = future_to_promise(f); - if (p->cb_ctx_destroy != NULL) + struct promise * p = __future_to_promise(f); + p->ref_cnt--; + if(p->ref_cnt==0) { - p->cb_ctx_destroy(p->ctx); - } - if(!g_FP_instance.no_stats) FS_operate(g_FP_instance.fs_handle,g_FP_instance.fsid_f_num, 0, FS_OP_SUB, 1); - memset(p, 0, sizeof(struct promise)); - free(p); + __promise_destroy(p); + } +} +void promise_finish(struct promise * p) +{ + p->ref_cnt--; + if(p->ref_cnt==0) + { + __promise_destroy(p); + } } static void fp_stat_latency(struct _future_promise_debug* debug, int is_success) { @@ -220,6 +253,10 @@ void promise_failed(struct promise * p, enum e_future_error error, const char * { if(!g_FP_instance.no_stats) fp_stat_latency(&p->debug, 0); p->f.cb_failed(error, what, p->f.user); + if(!p->may_success_many_times) + { + promise_finish(p); + } return; } @@ -227,6 +264,10 @@ void promise_success(struct promise * p, void * result) { if(!g_FP_instance.no_stats) fp_stat_latency(&p->debug, 1); p->f.cb_success(result, p->f.user); + if(!p->may_success_many_times) + { + promise_finish(p); + } return; } diff --git a/platform/src/ssl_stream.cpp b/platform/src/ssl_stream.cpp index ada692a..0161843 100644 --- a/platform/src/ssl_stream.cpp +++ b/platform/src/ssl_stream.cpp @@ -707,10 +707,10 @@ failed: return; } -static void ssl_async_peek_client_hello(struct future * future, evutil_socket_t fd, struct event_base * evbase, +static void ssl_async_peek_client_hello(struct future * f, evutil_socket_t fd, struct event_base * evbase, void * logger) { - struct promise * p = future_to_promise(future); + struct promise * p = future_to_promise(f); struct peek_client_hello_ctx * ctx = ALLOC(struct peek_client_hello_ctx, 1); ctx->ev = event_new(evbase, fd, EV_READ, peek_client_hello_cb, p); ctx->logger = logger;