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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors