]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/base/AsyncJob.cc
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 93 ICAP (RFC 3507) Client */
12 #include "base/AsyncCall.h"
13 #include "base/AsyncJob.h"
14 #include "base/AsyncJobCalls.h"
15 #include "base/TextException.h"
21 InstanceIdDefinitions(AsyncJob
, "job");
23 AsyncJob::Pointer
AsyncJob::Start(AsyncJob
*j
)
25 AsyncJob::Pointer
job(j
);
26 CallJobHere(93, 5, job
, AsyncJob
, start
);
30 AsyncJob::AsyncJob(const char *aTypeName
) :
31 stopReason(NULL
), typeName(aTypeName
), inCall(NULL
)
33 debugs(93,5, "AsyncJob constructed, this=" << this <<
34 " type=" << typeName
<< " [" << id
<< ']');
39 debugs(93,5, "AsyncJob destructed, this=" << this <<
40 " type=" << typeName
<< " [" << id
<< ']');
43 void AsyncJob::start()
47 // XXX: temporary code to replace calls to "delete this" in jobs-in-transition.
48 // Will be replaced with calls to mustStop() when transition is complete.
49 void AsyncJob::deleteThis(const char *aReason
)
54 // if we are in-call, then the call wrapper will delete us
55 debugs(93, 4, typeName
<< " will NOT delete in-call job, reason: " << stopReason
);
59 // there is no call wrapper waiting for our return, so we fake it
60 debugs(93, 5, typeName
<< " will delete this, reason: " << stopReason
);
61 CbcPointer
<AsyncJob
> self(this);
62 AsyncCall::Pointer fakeCall
= asyncCall(93,4, "FAKE-deleteThis",
63 JobMemFun(self
, &AsyncJob::deleteThis
, aReason
));
69 void AsyncJob::mustStop(const char *aReason
)
71 // XXX: temporary code to catch cases where mustStop is called outside
72 // of an async call context. Will be removed when that becomes impossible.
73 // Until then, this will cause memory leaks and possibly other problems.
76 debugs(93, 5, typeName
<< " will STALL, reason: " << stopReason
);
80 Must(inCall
!= NULL
); // otherwise nobody will delete us if we are done()
84 debugs(93, 5, typeName
<< " will stop, reason: " << stopReason
);
86 debugs(93, 5, typeName
<< " will stop, another reason: " << aReason
);
90 bool AsyncJob::done() const
92 // stopReason, set in mustStop(), overwrites all other conditions
93 return stopReason
!= NULL
|| doneAll();
96 bool AsyncJob::doneAll() const
98 return true; // so that it is safe for kids to use
101 bool AsyncJob::canBeCalled(AsyncCall
&call
) const
103 if (inCall
!= NULL
) {
104 // This may happen when we have bugs or some module is not calling
105 // us asynchronously (comm used to do that).
106 debugs(93, 5, HERE
<< inCall
<< " is in progress; " <<
107 call
<< " canot reenter the job.");
108 return call
.cancel("reentrant job call");
114 void AsyncJob::callStart(AsyncCall
&call
)
116 // we must be called asynchronously and hence, the caller must lock us
117 Must(cbdataReferenceValid(toCbdata()));
119 Must(!inCall
); // see AsyncJob::canBeCalled
121 inCall
= &call
; // XXX: ugly, but safe if callStart/callEnd,Ex are paired
122 debugs(inCall
->debugSection
, inCall
->debugLevel
,
123 typeName
<< " status in:" << status());
126 void AsyncJob::callException(const std::exception
&e
)
128 // we must be called asynchronously and hence, the caller must lock us
129 Must(cbdataReferenceValid(toCbdata()));
131 mustStop("exception");
134 void AsyncJob::callEnd()
137 debugs(93, 5, *inCall
<< " ends job" << status());
139 AsyncCall::Pointer inCallSaved
= inCall
;
140 void *thisSaved
= this;
144 delete this; // this is the only place where the object is deleted
146 // careful: this object does not exist any more
147 debugs(93, 6, HERE
<< *inCallSaved
<< " ended " << thisSaved
);
151 debugs(inCall
->debugSection
, inCall
->debugLevel
,
152 typeName
<< " status out:" << status());
156 // returns a temporary string depicting transaction status, for debugging
157 const char *AsyncJob::status() const
163 if (stopReason
!= NULL
) {
164 buf
.Printf("Stopped, reason:");
165 buf
.Printf("%s",stopReason
);
167 buf
.Printf(" %s%u]", id
.Prefix
, id
.value
);
170 return buf
.content();