From 63eb0920ef2261e98dc0ff6310c303a5d8c163ce Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 19 Sep 2016 14:31:14 +0000 Subject: [PATCH] towards adding mq destruction notification --- src/include/gnunet_mq_lib.h | 30 +++++++++++++ src/nse/gnunet-service-nse.c | 4 +- src/util/mq.c | 87 ++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 2 deletions(-) diff --git a/src/include/gnunet_mq_lib.h b/src/include/gnunet_mq_lib.h index 999ee4134..ff8c9ba1b 100644 --- a/src/include/gnunet_mq_lib.h +++ b/src/include/gnunet_mq_lib.h @@ -636,6 +636,36 @@ void GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq); +/** + * Handle we return for callbacks registered to be + * notified when #GNUNET_MQ_destroy() is called on a queue. + */ +struct GNUNET_MQ_DestroyNotificationHandle; + + +/** + * 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); + +/** + * 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); + + /** * Call the message message handler that was registered * for the type of the given message in the given message queue. diff --git a/src/nse/gnunet-service-nse.c b/src/nse/gnunet-service-nse.c index 1f06c0a75..bf7c79fcb 100644 --- a/src/nse/gnunet-service-nse.c +++ b/src/nse/gnunet-service-nse.c @@ -276,12 +276,12 @@ static unsigned int estimate_count; /** * Task scheduled to update our flood message for the next round. */ -static struct GNUNET_SCHEDULER_Task * flood_task; +static struct GNUNET_SCHEDULER_Task *flood_task; /** * Task scheduled to compute our proof. */ -static struct GNUNET_SCHEDULER_Task * proof_task; +static struct GNUNET_SCHEDULER_Task *proof_task; /** * Notification context, simplifies client broadcasts. diff --git a/src/util/mq.c b/src/util/mq.c index e9dba3d9d..01cdf764b 100644 --- a/src/util/mq.c +++ b/src/util/mq.c @@ -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. @@ -1173,4 +1183,81 @@ GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq, } +/** + * 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; +}; + + +/** + * 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 */ -- 2.25.1