]> git.ipfire.org Git - thirdparty/squid.git/blame - src/base/AsyncCall.cc
Maintenance: Removed most NULLs using modernize-use-nullptr (#1075)
[thirdparty/squid.git] / src / base / AsyncCall.cc
CommitLineData
bbc27441 1/*
bf95c10a 2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
bbc27441
AJ
3 *
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.
7 */
8
582c2af2 9#include "squid.h"
882255af
AR
10#include "base/AsyncCall.h"
11#include "base/AsyncCallQueue.h"
ccfbe8f4 12#include "base/CodeContext.h"
3e5c8cf4 13#include "cbdata.h"
675b8408 14#include "debug/Stream.h"
d977f933
AJ
15#include <ostream>
16
52ed047a 17InstanceIdDefinitions(AsyncCall, "call");
3e5c8cf4 18
3e5c8cf4 19/* AsyncCall */
854d4d81 20
ccfbe8f4
AR
21AsyncCall::AsyncCall(int aDebugSection, int aDebugLevel, const char *aName):
22 name(aName),
23 codeContext(CodeContext::Current()),
24 debugSection(aDebugSection),
25 debugLevel(aDebugLevel),
26 theNext(nullptr),
27 isCanceled(nullptr)
854d4d81 28{
3e5c8cf4 29 debugs(debugSection, debugLevel, "The AsyncCall " << name << " constructed, this=" << this <<
52ed047a 30 " [" << id << ']');
854d4d81 31}
32
3e5c8cf4 33AsyncCall::~AsyncCall()
854d4d81 34{
3e5c8cf4 35 assert(!theNext); // AsyncCallQueue must clean
854d4d81 36}
37
3e5c8cf4 38void
39AsyncCall::make()
854d4d81 40{
bf95c10a 41 debugs(debugSection, debugLevel, "make call " << name <<
52ed047a 42 " [" << id << ']');
3e5c8cf4 43 if (canFire()) {
44 fire();
45 return;
46 }
854d4d81 47
3e5c8cf4 48 if (!isCanceled) // we did not cancel() when returning false from canFire()
49 isCanceled = "unknown reason";
854d4d81 50
bf95c10a 51 debugs(debugSection, debugLevel, "will not call " << name <<
52ed047a 52 " [" << id << ']' << " because of " << isCanceled);
3e5c8cf4 53}
854d4d81 54
3e5c8cf4 55bool
56AsyncCall::cancel(const char *reason)
57{
bf95c10a 58 debugs(debugSection, debugLevel, "will not call " << name <<
3b79b74e
AR
59 " [" << id << "] " << (isCanceled ? "also " : "") <<
60 "because " << reason);
61
3e5c8cf4 62 isCanceled = reason;
63 return false;
64}
854d4d81 65
3e5c8cf4 66bool
67AsyncCall::canFire()
68{
69 return !isCanceled;
70}
854d4d81 71
9837567d 72// TODO: make this method const by providing a const getDialer()
3e5c8cf4 73void
74AsyncCall::print(std::ostream &os)
75{
76 os << name;
77 if (const CallDialer *dialer = getDialer())
78 dialer->print(os);
79 else
80 os << "(?" << this << "?)";
81}
854d4d81 82
37cba319
AR
83void
84AsyncCall::dequeue(AsyncCall::Pointer &head, AsyncCall::Pointer &prev)
85{
aee3523a 86 if (prev != nullptr)
37cba319
AR
87 prev->setNext(Next());
88 else
89 head = Next();
aee3523a 90 setNext(nullptr);
37cba319
AR
91}
92
3e5c8cf4 93bool
26ac0430 94ScheduleCall(const char *fileName, int fileLine, AsyncCall::Pointer &call)
3e5c8cf4 95{
96 debugs(call->debugSection, call->debugLevel, fileName << "(" << fileLine <<
52ed047a 97 ") will call " << *call << " [" << call->id << ']' );
ccfbe8f4
AR
98
99 // Support callback creators that did not get their context from service A,
100 // but the current caller (service B) got that context from another source.
101 if (!call->codeContext)
102 call->codeContext = CodeContext::Current();
103
3e5c8cf4 104 AsyncCallQueue::Instance().schedule(call);
105 return true;
854d4d81 106}
107