]> git.ipfire.org Git - thirdparty/squid.git/blob - src/base/AsyncCall.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / base / AsyncCall.cc
1 /*
2 * $Id$
3 */
4
5 #include "squid.h"
6 #include "base/AsyncCall.h"
7 #include "base/AsyncCallQueue.h"
8 #include "cbdata.h"
9 #include "Debug.h"
10 #include <ostream>
11
12 InstanceIdDefinitions(AsyncCall, "call");
13
14 /* AsyncCall */
15
16 AsyncCall::AsyncCall(int aDebugSection, int aDebugLevel,
17 const char *aName): name(aName), debugSection(aDebugSection),
18 debugLevel(aDebugLevel), theNext(0), isCanceled(NULL)
19 {
20 debugs(debugSection, debugLevel, "The AsyncCall " << name << " constructed, this=" << this <<
21 " [" << id << ']');
22 }
23
24 AsyncCall::~AsyncCall()
25 {
26 assert(!theNext); // AsyncCallQueue must clean
27 }
28
29 void
30 AsyncCall::make()
31 {
32 debugs(debugSection, debugLevel, HERE << "make call " << name <<
33 " [" << id << ']');
34 if (canFire()) {
35 fire();
36 return;
37 }
38
39 if (!isCanceled) // we did not cancel() when returning false from canFire()
40 isCanceled = "unknown reason";
41
42 debugs(debugSection, debugLevel, HERE << "will not call " << name <<
43 " [" << id << ']' << " because of " << isCanceled);
44 }
45
46 bool
47 AsyncCall::cancel(const char *reason)
48 {
49 debugs(debugSection, debugLevel, HERE << "will not call " << name <<
50 " [" << id << "] " << (isCanceled ? "also " : "") <<
51 "because " << reason);
52
53 isCanceled = reason;
54 return false;
55 }
56
57 bool
58 AsyncCall::canFire()
59 {
60 return !isCanceled;
61 }
62
63 /// \todo make this method const by providing a const getDialer()
64 void
65 AsyncCall::print(std::ostream &os)
66 {
67 os << name;
68 if (const CallDialer *dialer = getDialer())
69 dialer->print(os);
70 else
71 os << "(?" << this << "?)";
72 }
73
74 void
75 AsyncCall::dequeue(AsyncCall::Pointer &head, AsyncCall::Pointer &prev)
76 {
77 if (prev != NULL)
78 prev->setNext(Next());
79 else
80 head = Next();
81 setNext(NULL);
82 }
83
84 bool
85 ScheduleCall(const char *fileName, int fileLine, AsyncCall::Pointer &call)
86 {
87 debugs(call->debugSection, call->debugLevel, fileName << "(" << fileLine <<
88 ") will call " << *call << " [" << call->id << ']' );
89 AsyncCallQueue::Instance().schedule(call);
90 return true;
91 }
92