sketch new service start/stop API as needed for testbed
[oweals/gnunet.git] / src / util / mq.c
index b971fdefd95214787d73a127c3fa5c978bc8b548..3d6f27567cf2fa1fc7900615802ffea16acb5d4d 100644 (file)
@@ -154,6 +154,16 @@ struct GNUNET_MQ_Handle
    */
   struct GNUNET_SCHEDULER_Task *continue_task;
 
+  /**
+   * Functions to call on queue destruction; kept in a DLL.
+   */
+  struct GNUNET_MQ_DestroyNotificationHandle *dnh_head;
+
+  /**
+   * Functions to call on queue destruction; kept in a DLL.
+   */
+  struct GNUNET_MQ_DestroyNotificationHandle *dnh_tail;
+
   /**
    * Additional options buffer set for this queue by
    * #GNUNET_MQ_set_options().  Default is 0.
@@ -359,6 +369,7 @@ GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
   GNUNET_assert (NULL != mq);
   GNUNET_assert (NULL == ev->parent_queue);
 
+  mq->queue_length++;
   ev->parent_queue = mq;
   /* is the implementation busy? queue it! */
   if (NULL != mq->current_envelope)
@@ -366,11 +377,12 @@ GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
     GNUNET_CONTAINER_DLL_insert_tail (mq->envelope_head,
                                       mq->envelope_tail,
                                       ev);
-    mq->queue_length++;
     return;
   }
   mq->current_envelope = ev;
-  mq->send_impl (mq, ev->mh, mq->impl_state);
+  mq->send_impl (mq,
+                ev->mh,
+                mq->impl_state);
 }
 
 
@@ -422,6 +434,8 @@ impl_send_continue (void *cls)
   current_envelope = mq->current_envelope;
   GNUNET_assert (NULL != current_envelope);
   current_envelope->parent_queue = NULL;
+  GNUNET_assert (0 < mq->queue_length);
+  mq->queue_length--;
   if (NULL == mq->envelope_head)
   {
     mq->current_envelope = NULL;
@@ -432,7 +446,6 @@ impl_send_continue (void *cls)
     GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
                                  mq->envelope_tail,
                                  mq->current_envelope);
-    mq->queue_length--;
     mq->send_impl (mq,
                   mq->current_envelope->mh,
                   mq->impl_state);
@@ -939,19 +952,68 @@ GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
 }
 
 
+/**
+ * Call a callback once the envelope has been sent, that is,
+ * sending it can not be canceled anymore.
+ * There can be only one notify sent callback per envelope.
+ *
+ * @param ev message to call the notify callback for
+ * @param cb the notify callback
+ * @param cb_cls closure for the callback
+ */
 void
 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *mqm,
                        GNUNET_MQ_NotifyCallback cb,
-                       void *cls)
+                       void *cb_cls)
 {
   mqm->sent_cb = cb;
-  mqm->sent_cls = cls;
+  mqm->sent_cls = cb_cls;
 }
 
 
+/**
+ * Handle we return for callbacks registered to be
+ * notified when #GNUNET_MQ_destroy() is called on a queue.
+ */
+struct GNUNET_MQ_DestroyNotificationHandle
+{
+  /**
+   * Kept in a DLL.
+   */
+  struct GNUNET_MQ_DestroyNotificationHandle *prev;
+
+  /**
+   * Kept in a DLL.
+   */
+  struct GNUNET_MQ_DestroyNotificationHandle *next;
+
+  /**
+   * Queue to notify about.
+   */
+  struct GNUNET_MQ_Handle *mq;
+
+  /**
+   * Function to call.
+   */
+  GNUNET_SCHEDULER_TaskCallback cb;
+
+  /**
+   * Closure for @e cb.
+   */
+  void *cb_cls;
+};
+
+
+/**
+ * Destroy the message queue.
+ *
+ * @param mq message queue to destroy
+ */
 void
 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
 {
+  struct GNUNET_MQ_DestroyNotificationHandle *dnh;
+
   if (NULL != mq->destroy_impl)
   {
     mq->destroy_impl (mq, mq->impl_state);
@@ -970,10 +1032,10 @@ GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
     GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
                                 mq->envelope_tail,
                                 ev);
+    GNUNET_assert (0 < mq->queue_length);
     mq->queue_length--;
     GNUNET_MQ_discard (ev);
   }
-  GNUNET_assert (0 == mq->queue_length);
   if (NULL != mq->current_envelope)
   {
     /* we can only discard envelopes that
@@ -981,6 +1043,14 @@ GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
     mq->current_envelope->parent_queue = NULL;
     GNUNET_MQ_discard (mq->current_envelope);
     mq->current_envelope = NULL;
+    GNUNET_assert (0 < mq->queue_length);
+    mq->queue_length--;
+  }
+  GNUNET_assert (0 == mq->queue_length);
+  while (NULL != (dnh = mq->dnh_head))
+  {
+    dnh->cb (dnh->cb_cls);
+    GNUNET_MQ_destroy_notify_cancel (dnh);
   }
   if (NULL != mq->assoc_map)
   {
@@ -1039,6 +1109,8 @@ GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
   {
     // complex case, we already started with transmitting
     // the message
+    GNUNET_assert (0 < mq->queue_length);
+    mq->queue_length--;
     mq->cancel_impl (mq,
                     mq->impl_state);
     // continue sending the next message, if any
@@ -1052,7 +1124,6 @@ GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
       GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
                                    mq->envelope_tail,
                                    mq->current_envelope);
-      mq->queue_length--;
       mq->send_impl (mq,
                     mq->current_envelope->mh,
                     mq->impl_state);
@@ -1064,6 +1135,7 @@ GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
     GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
                                 mq->envelope_tail,
                                 ev);
+    GNUNET_assert (0 < mq->queue_length);
     mq->queue_length--;
   }
 
@@ -1096,7 +1168,10 @@ GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq)
 struct GNUNET_MQ_Envelope *
 GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq)
 {
-  return mq->envelope_tail;
+  if (NULL != mq->envelope_tail)
+    return mq->envelope_tail;
+
+  return mq->current_envelope;
 }
 
 
@@ -1165,4 +1240,48 @@ GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
 }
 
 
+/**
+ * Register function to be called whenever @a mq is being
+ * destroyed.
+ *
+ * @param mq message queue to watch
+ * @param cb function to call on @a mq destruction
+ * @param cb_cls closure for @a cb
+ * @return handle for #GNUNET_MQ_destroy_notify_cancel().
+ */
+struct GNUNET_MQ_DestroyNotificationHandle *
+GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
+                         GNUNET_SCHEDULER_TaskCallback cb,
+                         void *cb_cls)
+{
+  struct GNUNET_MQ_DestroyNotificationHandle *dnh;
+
+  dnh = GNUNET_new (struct GNUNET_MQ_DestroyNotificationHandle);
+  dnh->mq = mq;
+  dnh->cb = cb;
+  dnh->cb_cls = cb_cls;
+  GNUNET_CONTAINER_DLL_insert (mq->dnh_head,
+                              mq->dnh_tail,
+                              dnh);
+  return dnh;
+}
+
+
+/**
+ * Cancel registration from #GNUNET_MQ_destroy_notify().
+ *
+ * @param dnh handle for registration to cancel
+ */
+void
+GNUNET_MQ_destroy_notify_cancel (struct GNUNET_MQ_DestroyNotificationHandle *dnh)
+{
+  struct GNUNET_MQ_Handle *mq = dnh->mq;
+
+  GNUNET_CONTAINER_DLL_remove (mq->dnh_head,
+                              mq->dnh_tail,
+                              dnh);
+  GNUNET_free (dnh);
+}
+
+
 /* end of mq.c */