From e53b9585dd30d0d2920de8edba23c5d4a11e970f Mon Sep 17 00:00:00 2001 From: Markus Makela Date: Tue, 22 Nov 2016 10:55:03 +0200 Subject: [PATCH] Add mapping function for DCBs The dcb_foreach allows a function to be mapped to all DCBs in MaxScale. This allows the list of DCBs to be iterated in a safe manner without having to worry about internal locking of the DCB mechanism. --- include/maxscale/dcb.h | 11 +++++++++++ server/core/dcb.c | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/include/maxscale/dcb.h b/include/maxscale/dcb.h index 2f4797de5..42836bddc 100644 --- a/include/maxscale/dcb.h +++ b/include/maxscale/dcb.h @@ -380,6 +380,17 @@ void dcb_append_readqueue(DCB *dcb, GWBUF *buffer); void dcb_enable_session_timeouts(); void dcb_process_idle_sessions(int thr); +/** + * @brief Call a function for each connected DCB + * + * @param func Function to call. The function should return @c true to continue iteration + * and @c false to stop iteration earlier. The first parameter is a DCB and the second + * is the value of @c data that the user provided. + * @param data User provided data passed as the second parameter to @c func + * @return True if all DCBs were iterated, false if the callback returned false + */ +bool dcb_foreach(bool (*func)(DCB *, void *), void *data); + /** * DCB flags values */ diff --git a/server/core/dcb.c b/server/core/dcb.c index c02cefc8b..c4f4446ef 100644 --- a/server/core/dcb.c +++ b/server/core/dcb.c @@ -3522,3 +3522,27 @@ void dcb_process_idle_sessions(int thr) } } } + +bool dcb_foreach(bool(*func)(DCB *, void *), void *data) +{ + + int nthr = config_threadcount(); + bool more = true; + + for (int i = 0; i < nthr && more; i++) + { + spinlock_acquire(&all_dcbs_lock[i]); + + for (DCB *dcb = all_dcbs[i]; dcb && more; dcb = dcb->memdata.next) + { + if (!func(dcb, data)) + { + more = false; + } + } + + spinlock_release(&all_dcbs_lock[i]); + } + + return more; +}