tevent: Factor out context initialization
authorVolker Lendecke <vl@samba.org>
Mon, 5 Jun 2017 04:58:37 +0000 (06:58 +0200)
committerJeremy Allison <jra@samba.org>
Thu, 8 Jun 2017 18:38:19 +0000 (20:38 +0200)
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/tevent/tevent.c

index 65b101f28e5715d8b576b89c655e2d2662962c5b..68b9db605f3584bdcfff3cd320acde7652f5ee6d 100644 (file)
@@ -392,46 +392,26 @@ int tevent_common_context_destructor(struct tevent_context *ev)
        return 0;
 }
 
-/*
-  create a event_context structure for a specific implemementation.
-  This must be the first events call, and all subsequent calls pass
-  this event_context as the first element. Event handlers also
-  receive this as their first argument.
-
-  This function is for allowing third-party-applications to hook in gluecode
-  to their own event loop code, so that they can make async usage of our client libs
-
-  NOTE: use tevent_context_init() inside of samba!
-*/
-struct tevent_context *tevent_context_init_ops(TALLOC_CTX *mem_ctx,
-                                              const struct tevent_ops *ops,
-                                              void *additional_data)
+static int tevent_common_context_constructor(struct tevent_context *ev)
 {
-       struct tevent_context *ev;
        int ret;
 
-       ev = talloc_zero(mem_ctx, struct tevent_context);
-       if (!ev) return NULL;
-
 #ifdef HAVE_PTHREAD
 
        ret = pthread_once(&tevent_atfork_initialized, tevent_prep_atfork);
        if (ret != 0) {
-               talloc_free(ev);
-               return NULL;
+               return ret;
        }
 
        ret = pthread_mutex_init(&ev->scheduled_mutex, NULL);
        if (ret != 0) {
-               talloc_free(ev);
-               return NULL;
+               return ret;
        }
 
        ret = pthread_mutex_lock(&tevent_contexts_mutex);
        if (ret != 0) {
                pthread_mutex_destroy(&ev->scheduled_mutex);
-               talloc_free(ev);
-               return NULL;
+               return ret;
        }
 
        DLIST_ADD(tevent_contexts, ev);
@@ -440,11 +420,40 @@ struct tevent_context *tevent_context_init_ops(TALLOC_CTX *mem_ctx,
        if (ret != 0) {
                abort();
        }
-
 #endif
 
        talloc_set_destructor(ev, tevent_common_context_destructor);
 
+       return 0;
+}
+
+/*
+  create a event_context structure for a specific implemementation.
+  This must be the first events call, and all subsequent calls pass
+  this event_context as the first element. Event handlers also
+  receive this as their first argument.
+
+  This function is for allowing third-party-applications to hook in gluecode
+  to their own event loop code, so that they can make async usage of our client libs
+
+  NOTE: use tevent_context_init() inside of samba!
+*/
+struct tevent_context *tevent_context_init_ops(TALLOC_CTX *mem_ctx,
+                                              const struct tevent_ops *ops,
+                                              void *additional_data)
+{
+       struct tevent_context *ev;
+       int ret;
+
+       ev = talloc_zero(mem_ctx, struct tevent_context);
+       if (!ev) return NULL;
+
+       ret = tevent_common_context_constructor(ev);
+       if (ret != 0) {
+               talloc_free(ev);
+               return NULL;
+       }
+
        ev->ops = ops;
        ev->additional_data = additional_data;