tevent: add tevent_queue_wait_send/recv()
authorGregor Beck <gbeck@sernet.de>
Thu, 19 Sep 2013 13:14:25 +0000 (15:14 +0200)
committerStefan Metzmacher <metze@samba.org>
Wed, 11 Dec 2013 21:46:09 +0000 (22:46 +0100)
Pair-Programmed-With: Stefan Metzmacher <metze@samba.org>

Signed-off-by: Gregor Beck <gbeck@sernet.de>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
lib/tevent/tevent.h
lib/tevent/tevent_queue.c

index 2bc4e2d7ad761fcbc5bc4854b97616a97683bd03..0705ff30bd44d3e3d128140e305261e6709a8262 100644 (file)
@@ -1592,6 +1592,39 @@ size_t tevent_queue_length(struct tevent_queue *queue);
  */
 bool tevent_queue_running(struct tevent_queue *queue);
 
+/**
+ * @brief Create a tevent subrequest that waits in a tevent_queue
+ *
+ * The idea is that always the same syntax for tevent requests.
+ *
+ * @param[in]  mem_ctx  The talloc memory context to use.
+ *
+ * @param[in]  ev       The event handle to setup the request.
+ *
+ * @param[in]  queue    The queue to wait in.
+ *
+ * @return              The new subrequest, NULL on error.
+ *
+ * @see tevent_queue_wait_recv()
+ */
+struct tevent_req *tevent_queue_wait_send(TALLOC_CTX *mem_ctx,
+                                         struct tevent_context *ev,
+                                         struct tevent_queue *queue);
+
+/**
+ * @brief Check if we no longer need to wait in the queue.
+ *
+ * This function needs to be called in the callback function set after calling
+ * tevent_queue_wait_send().
+ *
+ * @param[in]  req      The tevent request to check.
+ *
+ * @return              True on success, false otherwise.
+ *
+ * @see tevent_queue_wait_send()
+ */
+bool tevent_queue_wait_recv(struct tevent_req *req);
+
 typedef int (*tevent_nesting_hook)(struct tevent_context *ev,
                                   void *private_data,
                                   uint32_t level,
index 4750675802f26f6548e9c76e9b1c13933078a220..361fc7bc2e38fdd89b82e8371dd25fe412732909 100644 (file)
@@ -298,3 +298,55 @@ bool tevent_queue_running(struct tevent_queue *queue)
 {
        return queue->running;
 }
+
+struct tevent_queue_wait_state {
+       uint8_t dummy;
+};
+
+static void tevent_queue_wait_trigger(struct tevent_req *req,
+                                     void *private_data);
+
+struct tevent_req *tevent_queue_wait_send(TALLOC_CTX *mem_ctx,
+                                         struct tevent_context *ev,
+                                         struct tevent_queue *queue)
+{
+       struct tevent_req *req;
+       struct tevent_queue_wait_state *state;
+       bool ok;
+
+       req = tevent_req_create(mem_ctx, &state,
+                               struct tevent_queue_wait_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       ok = tevent_queue_add(queue, ev, req,
+                             tevent_queue_wait_trigger,
+                             NULL);
+       if (!ok) {
+               tevent_req_nomem(NULL, req);
+               return tevent_req_post(req, ev);
+       }
+
+       return req;
+}
+
+static void tevent_queue_wait_trigger(struct tevent_req *req,
+                                        void *private_data)
+{
+       tevent_req_done(req);
+}
+
+bool tevent_queue_wait_recv(struct tevent_req *req)
+{
+       enum tevent_req_state state;
+       uint64_t err;
+
+       if (tevent_req_is_error(req, &state, &err)) {
+               tevent_req_received(req);
+               return false;
+       }
+
+       tevent_req_received(req);
+       return true;
+}