]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
CBDATA call Dialer template
authorAmos Jeffries <squid3@treenet.co.nz>
Sun, 4 Dec 2011 05:39:39 +0000 (22:39 -0700)
committerAmos Jeffries <squid3@treenet.co.nz>
Sun, 4 Dec 2011 05:39:39 +0000 (22:39 -0700)
This adds a template for dialing Unary CBDATA wrapper functions with
type safety. Avoiding the casting that currently occurs in wrappers and
allowing the AsyncCall APIs to be used for these callbacks.

src/base/AsyncCbdataCalls.h [new file with mode: 0644]
src/base/Makefile.am
src/helper.cc
src/helper.h

diff --git a/src/base/AsyncCbdataCalls.h b/src/base/AsyncCbdataCalls.h
new file mode 100644 (file)
index 0000000..f15763e
--- /dev/null
@@ -0,0 +1,37 @@
+#ifndef SQUID_BASE_ASYNCCBDATACALLS_H
+#define SQUID_BASE_ASYNCCBDATACALLS_H
+
+#include "base/AsyncCall.h"
+#include "base/CbcPointer.h"
+
+// dialer to run cbdata callback functions as Async Calls
+// to ease the transition of these cbdata objects to full Jobs
+template<class Argument1>
+class UnaryCbdataDialer : public CallDialer
+{
+public:
+    typedef void Handler(Argument1 *);
+
+    UnaryCbdataDialer(Handler *aHandler, Argument1 *aArg) :
+            arg1(aArg),
+            handler(aHandler)
+    {}
+    
+    virtual bool canDial(AsyncCall &call) { return arg1.valid(); }
+    void dial(AsyncCall &call) { handler(arg1.get()); }
+    virtual void print(std::ostream &os) const {  os << '(' << arg1 << ')'; }
+
+public:
+    CbcPointer<Argument1> arg1;
+    Handler *handler;
+};
+
+// helper function to simplify Dialer creation.
+template <class Argument1>
+UnaryCbdataDialer<Argument1>
+cbdataDialer(typename UnaryCbdataDialer<Argument1>::Handler *handler, Argument1 *arg1)
+{
+    return UnaryCbdataDialer<Argument1>(handler, arg1);
+}
+
+#endif
index 786671eae165d052b51cb901a973a549e36b20cf..3ad8e4d2ecb7536337b73ceebabadfc1da7a98b3 100644 (file)
@@ -7,6 +7,7 @@ noinst_LTLIBRARIES = libbase.la
 libbase_la_SOURCES = \
        AsyncCall.cc \
        AsyncCall.h \
+       AsyncCbdataCalls.h \
        AsyncJob.h \
        AsyncJob.cc \
        AsyncJobCalls.h \
index 724fd8b4d0a4808a11f9afa481bea6267466c45f..43e802faa73dbbf99fc5736a6ad8c9c199b3ecd7 100644 (file)
@@ -33,6 +33,7 @@
  */
 
 #include "squid.h"
+#include "base/AsyncCbdataCalls.h"
 #include "comm.h"
 #include "comm/Connection.h"
 #include "comm/Write.h"
@@ -53,8 +54,8 @@
 
 static IOCB helperHandleRead;
 static IOCB helperStatefulHandleRead;
-static CLCB helperServerFree;
-static CLCB helperStatefulServerFree;
+static void helperServerFree(helper_server *srv);
+static void helperStatefulServerFree(helper_stateful_server *srv);
 static void Enqueue(helper * hlp, helper_request *);
 static helper_request *Dequeue(helper * hlp);
 static helper_stateful_request *StatefulDequeue(statefulhelper * hlp);
@@ -233,7 +234,8 @@ helperOpenServers(helper * hlp)
         if (wfd != rfd)
             commSetNonBlocking(wfd);
 
-        comm_add_close_handler(rfd, helperServerFree, srv);
+        AsyncCall::Pointer closeCall = asyncCall(5,4, "helperServerFree", cbdataDialer(helperServerFree, srv));
+        comm_add_close_handler(rfd, closeCall);
 
         AsyncCall::Pointer call = commCbCall(5,4, "helperHandleRead",
                                              CommIoCbPtrFun(helperHandleRead, srv));
@@ -353,7 +355,8 @@ helperStatefulOpenServers(statefulhelper * hlp)
         if (wfd != rfd)
             commSetNonBlocking(wfd);
 
-        comm_add_close_handler(rfd, helperStatefulServerFree, srv);
+        AsyncCall::Pointer closeCall = asyncCall(5,4, "helperStatefulServerFree", cbdataDialer(helperStatefulServerFree, srv));
+        comm_add_close_handler(rfd, closeCall);
 
         AsyncCall::Pointer call = commCbCall(5,4, "helperStatefulHandleRead",
                                              CommIoCbPtrFun(helperStatefulHandleRead, srv));
@@ -669,9 +672,8 @@ helper::~helper()
 /* ====================================================================== */
 
 static void
-helperServerFree(const CommCloseCbParams &params)
+helperServerFree(helper_server *srv)
 {
-    helper_server *srv = (helper_server *)params.data;
     helper *hlp = srv->parent;
     helper_request *r;
     int i, concurrency = hlp->childs.concurrency;
@@ -704,7 +706,7 @@ helperServerFree(const CommCloseCbParams &params)
     if (!srv->flags.shutdown) {
         assert(hlp->childs.n_active > 0);
         hlp->childs.n_active--;
-        debugs(84, DBG_CRITICAL, "WARNING: " << hlp->id_name << " #" << srv->index + 1 << " (FD " << params.fd << ") exited");
+        debugs(84, DBG_CRITICAL, "WARNING: " << hlp->id_name << " #" << srv->index + 1 << " exited");
 
         if (hlp->childs.needNew() > 0) {
             debugs(80, 1, "Too few " << hlp->id_name << " processes are running (need " << hlp->childs.needNew() << "/" << hlp->childs.n_max << ")");
@@ -736,9 +738,8 @@ helperServerFree(const CommCloseCbParams &params)
 }
 
 static void
-helperStatefulServerFree(const CommCloseCbParams &params)
+helperStatefulServerFree(helper_stateful_server *srv)
 {
-    helper_stateful_server *srv = (helper_stateful_server *)params.data;
     statefulhelper *hlp = srv->parent;
     helper_stateful_request *r;
 
@@ -766,7 +767,7 @@ helperStatefulServerFree(const CommCloseCbParams &params)
     if (!srv->flags.shutdown) {
         assert( hlp->childs.n_active > 0);
         hlp->childs.n_active--;
-        debugs(84, 0, "WARNING: " << hlp->id_name << " #" << srv->index + 1 << " (FD " << params.fd << ") exited");
+        debugs(84, 0, "WARNING: " << hlp->id_name << " #" << srv->index + 1 << " exited");
 
         if (hlp->childs.needNew() > 0) {
             debugs(80, 1, "Too few " << hlp->id_name << " processes are running (need " << hlp->childs.needNew() << "/" << hlp->childs.n_max << ")");
index 7d40becbce8a46d9f0a01968a9fe74a1cb19a572..a0ddcded4ae9315a124ee2690a5b3b189b08f9f3 100644 (file)
@@ -34,6 +34,7 @@
 #define SQUID_HELPER_H
 
 #include "squid.h"
+#include "base/AsyncCall.h"
 #include "cbdata.h"
 #include "comm/forward.h"
 #include "ip/Address.h"
@@ -144,6 +145,9 @@ public:
         int uses;
         unsigned int pending;
     } stats;
+
+private:
+    CBDATA_CLASS2(helper_server);
 };
 
 class helper_stateful_request;
@@ -163,6 +167,9 @@ public:
         int releases;
     } stats;
     void *data;                        /* State data used by the calling routines */
+
+private:
+    CBDATA_CLASS2(helper_stateful_server);
 };
 
 class helper_request