]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/base/AsyncJob.cc
2 * DEBUG: section 93 ICAP (RFC 3507) Client
6 #include "base/AsyncCall.h"
7 #include "base/AsyncJob.h"
8 #include "base/AsyncJobCalls.h"
9 #include "base/TextException.h"
14 unsigned int AsyncJob::TheLastId
= 0;
16 AsyncJob::Pointer
AsyncJob::Start(AsyncJob
*j
)
18 AsyncJob::Pointer
job(j
);
19 CallJobHere(93, 5, job
, AsyncJob
, start
);
23 AsyncJob::AsyncJob(const char *aTypeName
): typeName(aTypeName
), inCall(NULL
), id(++TheLastId
)
25 debugs(93,3, "AsyncJob of type " << typeName
<< " constructed, this=" << this <<
26 " [async" << id
<< ']');
33 void AsyncJob::start()
37 // XXX: temporary code to replace calls to "delete this" in jobs-in-transition.
38 // Will be replaced with calls to mustStop() when transition is complete.
39 void AsyncJob::deleteThis(const char *aReason
)
44 // if we are in-call, then the call wrapper will delete us
45 debugs(93, 4, typeName
<< " will NOT delete in-call job, reason: " << stopReason
);
49 // there is no call wrapper waiting for our return, so we fake it
50 debugs(93, 5, typeName
<< " will delete this, reason: " << stopReason
);
51 CbcPointer
<AsyncJob
> self(this);
52 AsyncCall::Pointer fakeCall
= asyncCall(93,4, "FAKE-deleteThis",
53 JobMemFun(self
, &AsyncJob::deleteThis
, aReason
));
59 void AsyncJob::mustStop(const char *aReason
)
61 // XXX: temporary code to catch cases where mustStop is called outside
62 // of an async call context. Will be removed when that becomes impossible.
63 // Until then, this will cause memory leaks and possibly other problems.
66 debugs(93, 5, typeName
<< " will STALL, reason: " << stopReason
);
70 Must(inCall
!= NULL
); // otherwise nobody will delete us if we are done()
74 debugs(93, 5, typeName
<< " will stop, reason: " << stopReason
);
76 debugs(93, 5, typeName
<< " will stop, another reason: " << aReason
);
80 bool AsyncJob::done() const
82 // stopReason, set in mustStop(), overwrites all other conditions
83 return stopReason
!= NULL
|| doneAll();
86 bool AsyncJob::doneAll() const
88 return true; // so that it is safe for kids to use
91 bool AsyncJob::canBeCalled(AsyncCall
&call
) const
94 // This may happen when we have bugs or some module is not calling
95 // us asynchronously (comm used to do that).
96 debugs(93, 5, HERE
<< inCall
<< " is in progress; " <<
97 call
<< " canot reenter the job.");
98 return call
.cancel("reentrant job call");
104 void AsyncJob::callStart(AsyncCall
&call
)
106 // we must be called asynchronously and hence, the caller must lock us
107 Must(cbdataReferenceValid(toCbdata()));
109 Must(!inCall
); // see AsyncJob::canBeCalled
111 inCall
= &call
; // XXX: ugly, but safe if callStart/callEnd,Ex are paired
112 debugs(inCall
->debugSection
, inCall
->debugLevel
,
113 typeName
<< " status in:" << status());
116 void AsyncJob::callException(const std::exception
&e
)
118 // we must be called asynchronously and hence, the caller must lock us
119 Must(cbdataReferenceValid(toCbdata()));
121 mustStop("exception");
124 void AsyncJob::callEnd()
127 debugs(93, 5, *inCall
<< " ends job" << status());
129 AsyncCall::Pointer inCallSaved
= inCall
;
130 void *thisSaved
= this;
134 delete this; // this is the only place where the object is deleted
136 // careful: this object does not exist any more
137 debugs(93, 6, HERE
<< *inCallSaved
<< " ended " << thisSaved
);
141 debugs(inCall
->debugSection
, inCall
->debugLevel
,
142 typeName
<< " status out:" << status());
146 // returns a temporary string depicting transaction status, for debugging
147 const char *AsyncJob::status() const
153 if (stopReason
!= NULL
) {
154 buf
.Printf("Stopped, reason:");
155 buf
.Printf("%s",stopReason
);
157 buf
.Printf(" job%d]", id
);
160 return buf
.content();