AsyncCall.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
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 
9 #ifndef SQUID_SRC_BASE_ASYNCCALL_H
10 #define SQUID_SRC_BASE_ASYNCCALL_H
11 
12 #include "base/CodeContext.h"
13 #include "base/forward.h"
14 #include "base/InstanceId.h"
15 #include "event.h"
16 #include "RefCount.h"
17 
38 class CallDialer;
39 
40 class AsyncCall: public RefCountable
41 {
42 public:
44 
45  AsyncCall(int aDebugSection, int aDebugLevel, const char *aName);
46  ~AsyncCall() override;
47 
48  void make(); // fire if we can; handles general call debugging
49 
50  // can be called from canFire() for debugging; always returns false
51  bool cancel(const char *reason);
52 
53  bool canceled() const { return isCanceled != nullptr; }
54 
55  virtual CallDialer *getDialer() = 0;
56 
57  void print(std::ostream &os);
58 
61 
63  theNext = aNext;
64  }
65 
67  return theNext;
68  }
69 
70 public:
71  const char *const name;
72 
75 
76  const int debugSection;
77  const int debugLevel;
79 
80 protected:
81  virtual bool canFire();
82 
83  virtual void fire() = 0;
84 
86 
87 private:
88  const char *isCanceled; // set to the cancellation reason by cancel()
89 
90  // not implemented to prevent nil calls from being passed around and unknowingly scheduled, for now.
91  AsyncCall();
92  AsyncCall(const AsyncCall &);
93 };
94 
95 inline
96 std::ostream &operator <<(std::ostream &os, AsyncCall &call)
97 {
98  call.print(os);
99  return os;
100 }
101 
107 {
108 public:
110  virtual ~CallDialer() {}
111 
112  // TODO: Add these for clarity when CommCbFunPtrCallT is gone
113  //virtual bool canDial(AsyncCall &call) = 0;
114  //virtual void dial(AsyncCall &call) = 0;
115 
116  virtual void print(std::ostream &os) const = 0;
117 };
118 
123 template <class DialerClass>
124 class AsyncCallT: public AsyncCall
125 {
126 public:
127  using Dialer = DialerClass;
128 
129  AsyncCallT(int aDebugSection, int aDebugLevel, const char *aName,
130  const Dialer &aDialer): AsyncCall(aDebugSection, aDebugLevel, aName),
131  dialer(aDialer) {}
132 
135  dialer(o.dialer) {}
136 
137  ~AsyncCallT() override {}
138 
139  CallDialer *getDialer() override { return &dialer; }
140 
142 
143 protected:
144  bool canFire() override {
145  return AsyncCall::canFire() &&
146  dialer.canDial(*this);
147  }
148  void fire() override { dialer.dial(*this); }
149 
150 private:
151  AsyncCallT & operator=(const AsyncCallT &); // not defined. call assignments not permitted.
152 };
153 
154 template <class Dialer>
156 asyncCall(int aDebugSection, int aDebugLevel, const char *aName,
157  const Dialer &aDialer)
158 {
159  return new AsyncCallT<Dialer>(aDebugSection, aDebugLevel, aName, aDialer);
160 }
161 
163 bool ScheduleCall(const char *fileName, int fileLine, const AsyncCall::Pointer &);
164 
166 #define ScheduleCallHere(call) ScheduleCall(__FILE__, __LINE__, (call))
167 
168 #endif /* SQUID_SRC_BASE_ASYNCCALL_H */
169 
bool cancel(const char *reason)
Definition: AsyncCall.cc:56
~AsyncCall() override
Definition: AsyncCall.cc:33
CodeContext::Pointer codeContext
what the callee is expected to work on
Definition: AsyncCall.h:74
void setNext(AsyncCall::Pointer aNext)
Definition: AsyncCall.h:62
CallDialer * getDialer() override
Definition: AsyncCall.h:139
RefCount< AsyncCall > AsyncCallPointer
Definition: forward.h:31
virtual CallDialer * getDialer()=0
virtual bool canFire()
Definition: AsyncCall.cc:67
void print(std::ostream &os)
Definition: AsyncCall.cc:74
RefCount< AsyncCallT< Dialer > > asyncCall(int aDebugSection, int aDebugLevel, const char *aName, const Dialer &aDialer)
Definition: AsyncCall.h:156
~AsyncCallT() override
Definition: AsyncCall.h:137
AsyncCallT(const AsyncCallT< Dialer > &o)
Definition: AsyncCall.h:133
const int debugLevel
Definition: AsyncCall.h:77
const InstanceId< AsyncCall > id
Definition: AsyncCall.h:78
void fire() override
Definition: AsyncCall.h:148
virtual void fire()=0
const int debugSection
Definition: AsyncCall.h:76
Dialer dialer
Definition: AsyncCall.h:141
AsyncCallT(int aDebugSection, int aDebugLevel, const char *aName, const Dialer &aDialer)
Definition: AsyncCall.h:129
bool canFire() override
Definition: AsyncCall.h:144
bool canceled() const
Definition: AsyncCall.h:53
virtual void print(std::ostream &os) const =0
AsyncCallT & operator=(const AsyncCallT &)
const char *const name
Definition: AsyncCall.h:71
void make()
Definition: AsyncCall.cc:39
virtual ~CallDialer()
Definition: AsyncCall.h:110
squidaio_request_t * head
Definition: aiops.cc:127
std::ostream & operator<<(std::ostream &os, AsyncCall &call)
Definition: AsyncCall.h:96
const char * isCanceled
Definition: AsyncCall.h:88
bool ScheduleCall(const char *fileName, int fileLine, const AsyncCall::Pointer &)
Definition: AsyncCall.cc:94
AsyncCall::Pointer & Next()
Definition: AsyncCall.h:66
AsyncCall::Pointer theNext
for AsyncCallList and similar lists
Definition: AsyncCall.h:85
void dequeue(AsyncCall::Pointer &head, AsyncCall::Pointer &prev)
remove us from the queue; we are head unless we are queued after prev
Definition: AsyncCall.cc:84

 

Introduction

Documentation

Support

Miscellaneous