AsyncJob.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 /* DEBUG: section 93 ICAP (RFC 3507) Client */
10 
11 #include "squid.h"
12 #include "base/AsyncCall.h"
13 #include "base/AsyncJob.h"
14 #include "base/AsyncJobCalls.h"
15 #include "base/TextException.h"
16 #include "cbdata.h"
17 #include "MemBuf.h"
18 
19 #include <ostream>
20 
22 
24 {
25  AsyncJob::Pointer job(j);
26  CallJobHere(93, 5, job, AsyncJob, start);
27  return job;
28 }
29 
30 AsyncJob::AsyncJob(const char *aTypeName) :
31  stopReason(NULL), typeName(aTypeName), inCall(NULL)
32 {
33  debugs(93,5, "AsyncJob constructed, this=" << this <<
34  " type=" << typeName << " [" << id << ']');
35 }
36 
38 {
39  debugs(93,5, "AsyncJob destructed, this=" << this <<
40  " type=" << typeName << " [" << id << ']');
41 }
42 
44 {
45 }
46 
47 // XXX: temporary code to replace calls to "delete this" in jobs-in-transition.
48 // Will be replaced with calls to mustStop() when transition is complete.
49 void AsyncJob::deleteThis(const char *aReason)
50 {
51  Must(aReason);
52  stopReason = aReason;
53  if (inCall != NULL) {
54  // if we are in-call, then the call wrapper will delete us
55  debugs(93, 4, typeName << " will NOT delete in-call job, reason: " << stopReason);
56  return;
57  }
58 
59  // there is no call wrapper waiting for our return, so we fake it
60  debugs(93, 5, typeName << " will delete this, reason: " << stopReason);
61  CbcPointer<AsyncJob> self(this);
62  AsyncCall::Pointer fakeCall = asyncCall(93,4, "FAKE-deleteThis",
63  JobMemFun(self, &AsyncJob::deleteThis, aReason));
64  inCall = fakeCall;
65  callEnd();
66 // delete fakeCall;
67 }
68 
69 void AsyncJob::mustStop(const char *aReason)
70 {
71  // XXX: temporary code to catch cases where mustStop is called outside
72  // of an async call context. Will be removed when that becomes impossible.
73  // Until then, this will cause memory leaks and possibly other problems.
74  if (!inCall) {
75  stopReason = aReason;
76  debugs(93, 5, typeName << " will STALL, reason: " << stopReason);
77  return;
78  }
79 
80  Must(inCall != NULL); // otherwise nobody will delete us if we are done()
81  Must(aReason);
82  if (!stopReason) {
83  stopReason = aReason;
84  debugs(93, 5, typeName << " will stop, reason: " << stopReason);
85  } else {
86  debugs(93, 5, typeName << " will stop, another reason: " << aReason);
87  }
88 }
89 
90 bool AsyncJob::done() const
91 {
92  // stopReason, set in mustStop(), overwrites all other conditions
93  return stopReason != NULL || doneAll();
94 }
95 
96 bool AsyncJob::doneAll() const
97 {
98  return true; // so that it is safe for kids to use
99 }
100 
102 {
103  if (inCall != NULL) {
104  // This may happen when we have bugs or some module is not calling
105  // us asynchronously (comm used to do that).
106  debugs(93, 5, HERE << inCall << " is in progress; " <<
107  call << " canot reenter the job.");
108  return call.cancel("reentrant job call");
109  }
110 
111  return true;
112 }
113 
115 {
116  // we must be called asynchronously and hence, the caller must lock us
118 
119  Must(!inCall); // see AsyncJob::canBeCalled
120 
121  inCall = &call; // XXX: ugly, but safe if callStart/callEnd,Ex are paired
123  typeName << " status in:" << status());
124 }
125 
126 void
127 AsyncJob::callException(const std::exception &ex)
128 {
129  debugs(93, 2, ex.what());
130  // we must be called asynchronously and hence, the caller must lock us
132 
133  mustStop("exception");
134 }
135 
137 {
138  if (done()) {
139  debugs(93, 5, *inCall << " ends job" << status());
140 
141  AsyncCall::Pointer inCallSaved = inCall;
142  void *thisSaved = this;
143 
144  swanSong();
145 
146  delete this; // this is the only place where the object is deleted
147 
148  // careful: this object does not exist any more
149  debugs(93, 6, HERE << *inCallSaved << " ended " << thisSaved);
150  return;
151  }
152 
154  typeName << " status out:" << status());
155  inCall = NULL;
156 }
157 
158 // returns a temporary string depicting transaction status, for debugging
159 const char *AsyncJob::status() const
160 {
161  static MemBuf buf;
162  buf.reset();
163 
164  buf.append(" [", 2);
165  if (stopReason != NULL) {
166  buf.appendf("Stopped, reason:%s", stopReason);
167  }
168  buf.appendf(" %s%u]", id.prefix(), id.value);
169  buf.terminate();
170 
171  return buf.content();
172 }
173 
NullaryMemFunT< C > JobMemFun(const CbcPointer< C > &job, typename NullaryMemFunT< C >::Method method)
bool done() const
the job is destroyed in callEnd() when done()
Definition: AsyncJob.cc:90
bool canBeCalled(AsyncCall &call) const
whether we can be called
Definition: AsyncJob.cc:101
virtual bool doneAll() const
whether positive goal has been reached
Definition: AsyncJob.cc:96
#define CallJobHere(debugSection, debugLevel, job, Class, method)
Definition: AsyncJobCalls.h:57
virtual void append(const char *c, int sz)
Definition: MemBuf.cc:225
InstanceIdDefinitions(AsyncJob,"job")
const int debugSection
Definition: AsyncCall.h:77
void mustStop(const char *aReason)
Definition: AsyncJob.cc:69
AsyncCall::Pointer inCall
the asynchronous call being handled, if any
Definition: AsyncJob.h:69
const int debugLevel
Definition: AsyncCall.h:78
AsyncJob(const char *aTypeName)
Definition: AsyncJob.cc:30
virtual void callException(const std::exception &e)
called when the job throws during an async call
Definition: AsyncJob.cc:127
virtual const char * status() const
internal cleanup; do not call directly
Definition: AsyncJob.cc:159
bool cancel(const char *reason)
Definition: AsyncCall.cc:52
AsyncCall * asyncCall(int aDebugSection, int aDebugLevel, const char *aName, const Dialer &aDialer)
Definition: AsyncCall.h:156
static Pointer Start(AsyncJob *job)
starts a freshly created job (i.e., makes the job asynchronous)
Definition: AsyncJob.cc:23
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
virtual void * toCbdata()=0
virtual void swanSong()
Definition: AsyncJob.h:55
void reset()
Definition: MemBuf.cc:141
void deleteThis(const char *aReason)
Definition: AsyncJob.cc:49
char * content()
start of the added data
Definition: MemBuf.h:41
void const char * buf
Definition: stub_helper.cc:16
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:147
#define Must(cond)
Definition: TextException.h:89
const char * stopReason
reason for forcing done() to be true
Definition: AsyncJob.h:67
virtual ~AsyncJob()
Definition: AsyncJob.cc:37
void appendf(const char *fmt,...) PRINTF_FORMAT_ARG2
Append operation with printf-style arguments.
Definition: Packable.h:61
Definition: MemBuf.h:23
int cbdataReferenceValid(const void *p)
Definition: cbdata.cc:412
virtual void callEnd()
called right after the called job method
Definition: AsyncJob.cc:136
virtual void start()
called by AsyncStart; do not call directly
Definition: AsyncJob.cc:43
const char * typeName
kid (leaf) class name, for debugging
Definition: AsyncJob.h:68
#define NULL
Definition: types.h:166
void callStart(AsyncCall &call)
Definition: AsyncJob.cc:114
void terminate()
Definition: MemBuf.cc:259

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors