]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/base/AsyncJob.cc
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / base / AsyncJob.cc
index a43614f7d23b748f44b49b14d45305c5e260f802..f025c039421783f7d07ac60e7559490609091de3 100644 (file)
@@ -1,37 +1,43 @@
 /*
- * DEBUG: section 93  ICAP (RFC 3507) Client
+ * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
  */
 
+/* DEBUG: section 93    ICAP (RFC 3507) Client */
+
 #include "squid.h"
+#include "base/AsyncCall.h"
+#include "base/AsyncJob.h"
+#include "base/AsyncJobCalls.h"
+#include "base/TextException.h"
 #include "cbdata.h"
 #include "MemBuf.h"
-#include "TextException.h"
-#include "AsyncJob.h"
-#include "AsyncCall.h"
 
+#include <ostream>
 
-unsigned int AsyncJob::TheLastId = 0;
+InstanceIdDefinitions(AsyncJob, "job");
 
-AsyncJob *AsyncJob::AsyncStart(AsyncJob *job)
+AsyncJob::Pointer AsyncJob::Start(AsyncJob *j)
 {
-    assert(job);
-    CallJobHere(93, 5, job, AsyncJob::noteStart);
+    AsyncJob::Pointer job(j);
+    CallJobHere(93, 5, job, AsyncJob, start);
     return job;
 }
 
-AsyncJob::AsyncJob(const char *aTypeName): typeName(aTypeName), inCall(NULL), id(++TheLastId)
+AsyncJob::AsyncJob(const char *aTypeName) :
+    stopReason(NULL), typeName(aTypeName), inCall(NULL)
 {
-    debugs(93,3, "AsyncJob of type " << typeName << " constructed, this=" << this <<
-           " [async" << id << ']');
+    debugs(93,5, "AsyncJob constructed, this=" << this <<
+           " type=" << typeName << " [" << id << ']');
 }
 
 AsyncJob::~AsyncJob()
 {
-}
-
-void AsyncJob::noteStart()
-{
-    start();
+    debugs(93,5, "AsyncJob destructed, this=" << this <<
+           " type=" << typeName << " [" << id << ']');
 }
 
 void AsyncJob::start()
@@ -52,8 +58,9 @@ void AsyncJob::deleteThis(const char *aReason)
 
     // there is no call wrapper waiting for our return, so we fake it
     debugs(93, 5, typeName << " will delete this, reason: " << stopReason);
+    CbcPointer<AsyncJob> self(this);
     AsyncCall::Pointer fakeCall = asyncCall(93,4, "FAKE-deleteThis",
-                                            MemFun(this, &AsyncJob::deleteThis, aReason));
+                                            JobMemFun(self, &AsyncJob::deleteThis, aReason));
     inCall = fakeCall;
     callEnd();
 //    delete fakeCall;
@@ -97,7 +104,7 @@ bool AsyncJob::canBeCalled(AsyncCall &call) const
         // This may happen when we have bugs or some module is not calling
         // us asynchronously (comm used to do that).
         debugs(93, 5, HERE << inCall << " is in progress; " <<
-               call << " canot reenter the job.");
+               call << " cannot reenter the job.");
         return call.cancel("reentrant job call");
     }
 
@@ -116,8 +123,10 @@ void AsyncJob::callStart(AsyncCall &call)
            typeName << " status in:" << status());
 }
 
-void AsyncJob::callException(const std::exception &e)
+void
+AsyncJob::callException(const std::exception &ex)
 {
+    debugs(93, 2, ex.what());
     // we must be called asynchronously and hence, the caller must lock us
     Must(cbdataReferenceValid(toCbdata()));
 
@@ -154,70 +163,11 @@ const char *AsyncJob::status() const
 
     buf.append(" [", 2);
     if (stopReason != NULL) {
-        buf.Printf("Stopped, reason:");
-        buf.Printf("%s",stopReason);
+        buf.appendf("Stopped, reason:%s", stopReason);
     }
-    buf.Printf(" job%d]", id);
+    buf.appendf(" %s%u]", id.prefix(), id.value);
     buf.terminate();
 
     return buf.content();
 }
 
-
-/* JobDialer */
-
-JobDialer::JobDialer(AsyncJob *aJob): job(NULL), lock(NULL)
-{
-    if (aJob) {
-        lock = cbdataReference(aJob->toCbdata());
-        job = aJob;
-    }
-}
-
-JobDialer::JobDialer(const JobDialer &d): CallDialer(d),
-        job(NULL), lock(NULL)
-{
-    if (d.lock && cbdataReferenceValid(d.lock)) {
-        lock = cbdataReference(d.lock);
-        Must(d.job);
-        job = d.job;
-    }
-}
-
-JobDialer::~JobDialer()
-{
-    cbdataReferenceDone(lock); // lock may be NULL
-}
-
-
-bool
-JobDialer::canDial(AsyncCall &call)
-{
-    if (!lock)
-        return call.cancel("job was gone before the call");
-
-    if (!cbdataReferenceValid(lock))
-        return call.cancel("job gone after the call");
-
-    Must(job);
-    return job->canBeCalled(call);
-}
-
-void
-JobDialer::dial(AsyncCall &call)
-{
-    Must(lock && cbdataReferenceValid(lock)); // canDial() checks for this
-    Must(job);
-
-    job->callStart(call);
-
-    try {
-        doDial();
-    } catch (const std::exception &e) {
-        debugs(call.debugSection, 3,
-               HERE << call.name << " threw exception: " << e.what());
-        job->callException(e);
-    }
-
-    job->callEnd(); // may delete job
-}