Browse Source

Add a simpler (but slower) path for closures for where it makes sense

Craig Tiller 10 years ago
parent
commit
3ff551bf70
2 changed files with 27 additions and 0 deletions
  1. 24 0
      src/core/iomgr/closure.c
  2. 3 0
      src/core/iomgr/closure.h

+ 24 - 0
src/core/iomgr/closure.c

@@ -33,6 +33,8 @@
 
 #include "src/core/iomgr/closure.h"
 
+#include <grpc/support/alloc.h>
+
 void grpc_closure_init(grpc_closure *closure, grpc_iomgr_cb_func cb,
                        void *cb_arg) {
   closure->cb = cb;
@@ -69,3 +71,25 @@ void grpc_closure_list_move(grpc_closure_list *src, grpc_closure_list *dst) {
   }
   src->head = src->tail = NULL;
 }
+
+typedef struct {
+  grpc_iomgr_cb_func cb;
+  void *cb_arg;
+  grpc_closure wrapper;
+} wrapped_closure;
+
+static void closure_wrapper(grpc_exec_ctx *exec_ctx, void *arg, int success) {
+  wrapped_closure *wc = arg;
+  grpc_iomgr_cb_func cb = wc->cb;
+  void *cb_arg = wc->cb_arg;
+  gpr_free(wc);
+  cb(exec_ctx, cb_arg, success);
+}
+
+grpc_closure *grpc_closure_create(grpc_iomgr_cb_func cb, void *cb_arg) {
+  wrapped_closure *wc = gpr_malloc(sizeof(*wc));
+  wc->cb = cb;
+  wc->cb_arg = cb_arg;
+  grpc_closure_init(&wc->wrapper, closure_wrapper, wc);
+  return &wc->wrapper;
+}

+ 3 - 0
src/core/iomgr/closure.h

@@ -77,6 +77,9 @@ struct grpc_closure {
 void grpc_closure_init(grpc_closure *closure, grpc_iomgr_cb_func cb,
                        void *cb_arg);
 
+/* Create a heap allocated closure: try to avoid except for very rare events */
+grpc_closure *grpc_closure_create(grpc_iomgr_cb_func cb, void *cb_arg);
+
 #define GRPC_CLOSURE_LIST_INIT \
   { NULL, NULL }