]> git.ipfire.org Git - thirdparty/squid.git/blob - src/base/AsyncJob.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / base / AsyncJob.cc
1 /*
2 * DEBUG: section 93 ICAP (RFC 3507) Client
3 */
4
5 #include "squid.h"
6 #include "base/AsyncCall.h"
7 #include "base/AsyncJob.h"
8 #include "base/AsyncJobCalls.h"
9 #include "base/TextException.h"
10 #include "cbdata.h"
11 #include "MemBuf.h"
12
13
14 unsigned int AsyncJob::TheLastId = 0;
15
16 AsyncJob::Pointer AsyncJob::Start(AsyncJob *j)
17 {
18 AsyncJob::Pointer job(j);
19 CallJobHere(93, 5, job, AsyncJob, start);
20 return job;
21 }
22
23 AsyncJob::AsyncJob(const char *aTypeName): typeName(aTypeName), inCall(NULL), id(++TheLastId)
24 {
25 debugs(93,3, "AsyncJob of type " << typeName << " constructed, this=" << this <<
26 " [async" << id << ']');
27 }
28
29 AsyncJob::~AsyncJob()
30 {
31 }
32
33 void AsyncJob::start()
34 {
35 }
36
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)
40 {
41 Must(aReason);
42 stopReason = aReason;
43 if (inCall != NULL) {
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);
46 return;
47 }
48
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));
54 inCall = fakeCall;
55 callEnd();
56 // delete fakeCall;
57 }
58
59 void AsyncJob::mustStop(const char *aReason)
60 {
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.
64 if (!inCall) {
65 stopReason = aReason;
66 debugs(93, 5, typeName << " will STALL, reason: " << stopReason);
67 return;
68 }
69
70 Must(inCall != NULL); // otherwise nobody will delete us if we are done()
71 Must(aReason);
72 if (!stopReason) {
73 stopReason = aReason;
74 debugs(93, 5, typeName << " will stop, reason: " << stopReason);
75 } else {
76 debugs(93, 5, typeName << " will stop, another reason: " << aReason);
77 }
78 }
79
80 bool AsyncJob::done() const
81 {
82 // stopReason, set in mustStop(), overwrites all other conditions
83 return stopReason != NULL || doneAll();
84 }
85
86 bool AsyncJob::doneAll() const
87 {
88 return true; // so that it is safe for kids to use
89 }
90
91 bool AsyncJob::canBeCalled(AsyncCall &call) const
92 {
93 if (inCall != NULL) {
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");
99 }
100
101 return true;
102 }
103
104 void AsyncJob::callStart(AsyncCall &call)
105 {
106 // we must be called asynchronously and hence, the caller must lock us
107 Must(cbdataReferenceValid(toCbdata()));
108
109 Must(!inCall); // see AsyncJob::canBeCalled
110
111 inCall = &call; // XXX: ugly, but safe if callStart/callEnd,Ex are paired
112 debugs(inCall->debugSection, inCall->debugLevel,
113 typeName << " status in:" << status());
114 }
115
116 void AsyncJob::callException(const std::exception &e)
117 {
118 // we must be called asynchronously and hence, the caller must lock us
119 Must(cbdataReferenceValid(toCbdata()));
120
121 mustStop("exception");
122 }
123
124 void AsyncJob::callEnd()
125 {
126 if (done()) {
127 debugs(93, 5, *inCall << " ends job" << status());
128
129 AsyncCall::Pointer inCallSaved = inCall;
130 void *thisSaved = this;
131
132 swanSong();
133
134 delete this; // this is the only place where the object is deleted
135
136 // careful: this object does not exist any more
137 debugs(93, 6, HERE << *inCallSaved << " ended " << thisSaved);
138 return;
139 }
140
141 debugs(inCall->debugSection, inCall->debugLevel,
142 typeName << " status out:" << status());
143 inCall = NULL;
144 }
145
146 // returns a temporary string depicting transaction status, for debugging
147 const char *AsyncJob::status() const
148 {
149 static MemBuf buf;
150 buf.reset();
151
152 buf.append(" [", 2);
153 if (stopReason != NULL) {
154 buf.Printf("Stopped, reason:");
155 buf.Printf("%s",stopReason);
156 }
157 buf.Printf(" job%d]", id);
158 buf.terminate();
159
160 return buf.content();
161 }
162
163