+ /*
+ * Delete all the registered
+ * tevent_context/tevent_threaded_context
+ * pairs.
+ */
+ for (glue = pool->glue_list; glue != NULL; glue = pool->glue_list) {
+ /* The glue destructor removes it from the list */
+ TALLOC_FREE(glue);
+ }
+ pool->glue_list = NULL;
+
+ return 0;
+}
+
+static int pthreadpool_tevent_glue_destructor(
+ struct pthreadpool_tevent_glue *glue)
+{
+ if (glue->pool->glue_list != NULL) {
+ DLIST_REMOVE(glue->pool->glue_list, glue);
+ }
+
+ /* Ensure the ev_link destructor knows we're gone */
+ glue->ev_link->glue = NULL;
+
+ TALLOC_FREE(glue->ev_link);
+ TALLOC_FREE(glue->tctx);
+
+ return 0;
+}
+
+/*
+ * Destructor called either explicitly from
+ * pthreadpool_tevent_glue_destructor(), or indirectly
+ * when owning tevent_context is destroyed.
+ *
+ * When called from pthreadpool_tevent_glue_destructor()
+ * ev_link->glue is already NULL, so this does nothing.
+ *
+ * When called from talloc_free() of the owning
+ * tevent_context we must ensure we also remove the
+ * linked glue object from the list inside
+ * struct pthreadpool_tevent.
+ */
+static int pthreadpool_tevent_glue_link_destructor(
+ struct pthreadpool_tevent_glue_ev_link *ev_link)
+{
+ TALLOC_FREE(ev_link->glue);
+ return 0;
+}
+
+static int pthreadpool_tevent_register_ev(struct pthreadpool_tevent *pool,
+ struct tevent_context *ev)
+{
+ struct pthreadpool_tevent_glue *glue = NULL;
+ struct pthreadpool_tevent_glue_ev_link *ev_link = NULL;
+
+ /*
+ * See if this tevent_context was already registered by
+ * searching the glue object list. If so we have nothing
+ * to do here - we already have a tevent_context/tevent_threaded_context
+ * pair.
+ */
+ for (glue = pool->glue_list; glue != NULL; glue = glue->next) {
+ if (glue->ev == ev) {
+ return 0;
+ }
+ }
+
+ /*
+ * Event context not yet registered - create a new glue
+ * object containing a tevent_context/tevent_threaded_context
+ * pair and put it on the list to remember this registration.
+ * We also need a link object to ensure the event context
+ * can't go away without us knowing about it.
+ */
+ glue = talloc_zero(pool, struct pthreadpool_tevent_glue);
+ if (glue == NULL) {
+ return ENOMEM;
+ }
+ *glue = (struct pthreadpool_tevent_glue) {
+ .pool = pool,
+ .ev = ev,
+ };
+ talloc_set_destructor(glue, pthreadpool_tevent_glue_destructor);
+
+ /*
+ * Now allocate the link object to the event context. Note this
+ * is allocated OFF THE EVENT CONTEXT ITSELF, so if the event
+ * context is freed we are able to cleanup the glue object
+ * in the link object destructor.
+ */
+
+ ev_link = talloc_zero(ev, struct pthreadpool_tevent_glue_ev_link);
+ if (ev_link == NULL) {
+ TALLOC_FREE(glue);
+ return ENOMEM;
+ }
+ ev_link->glue = glue;
+ talloc_set_destructor(ev_link, pthreadpool_tevent_glue_link_destructor);
+
+ glue->ev_link = ev_link;
+
+#ifdef HAVE_PTHREAD
+ glue->tctx = tevent_threaded_context_create(pool, ev);
+ if (glue->tctx == NULL) {
+ TALLOC_FREE(ev_link);
+ TALLOC_FREE(glue);
+ return ENOMEM;
+ }
+#endif
+
+ DLIST_ADD(pool->glue_list, glue);