]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ICAP/AsyncJob.h
Polished BodyPipe API. No runtime changes expected.
[thirdparty/squid.git] / src / ICAP / AsyncJob.h
CommitLineData
3e5c8cf4 1 /*
63be0a78 2 * $Id: AsyncJob.h,v 1.4 2008/02/26 21:49:41 amosjeffries Exp $
3e5c8cf4 3 */
c824c43b 4
5#ifndef SQUID_ASYNC_JOB_H
6#define SQUID_ASYNC_JOB_H
7
63be0a78 8/// \todo move src/ICAP/AsyncJob.* to src/
3e5c8cf4 9
10
c824c43b 11#include "AsyncCall.h"
12
63be0a78 13/**
14 \defgroup AsyncJobAPI Async-Jobs API
15 \par
c824c43b 16 * AsyncJob is an API and a base for a class that implements a stand-alone
17 * "job", "task", or "logical processing thread" which receives asynchronous
18 * calls.
19 *
20 * Implementations should wrap each method receiving an asynchronous call in
21 * a pair of macros: AsyncCallEnter and AsyncCallExit. These macros:
22 * - provide call debugging
23 * - trap exceptions and terminate the task if an exception occurs
24 * - ensure that only one asynchronous call is active per object
25 * Most of the work is done by AsyncJob class methods. Macros just provide
26 * an enter/try/catch/exit framework.
27 *
28 * Eventually, the macros can and perhaps should be replaced with call/event
29 * processing code so that individual job classes do not have to wrap all
30 * asynchronous calls.
31 */
32
33class TextException;
34
63be0a78 35/// \ingroup AsyncJobAPI
c824c43b 36class AsyncJob
37{
38
39public:
40 static AsyncJob *AsyncStart(AsyncJob *job); // use this to start jobs
41
42 AsyncJob(const char *aTypeName);
43 virtual ~AsyncJob();
44
3e5c8cf4 45 virtual void *toCbdata() = 0;
c824c43b 46 void noteStart(); // calls virtual start
c824c43b 47
48protected:
3e5c8cf4 49 // XXX: temporary method to replace "delete this" in jobs-in-transition.
50 // Will be replaced with calls to mustStop() when transition is complete.
51 void deleteThis(const char *aReason);
52
c824c43b 53 void mustStop(const char *aReason); // force done() for a reason
54
55 bool done() const; // the job is destroyed in callEnd() when done()
56
3e5c8cf4 57 virtual void start();
58 virtual bool doneAll() const; // return true when done
59 virtual void swanSong() {}; // perform internal cleanup
60 virtual const char *status() const; // for debugging
c824c43b 61
3e5c8cf4 62public:
c824c43b 63 // asynchronous call maintenance
3e5c8cf4 64 bool canBeCalled(AsyncCall &call) const;
65 void callStart(AsyncCall &call);
9fcd0cd4 66 virtual void callException(const TextException &e);
c824c43b 67 virtual void callEnd();
68
3e5c8cf4 69protected:
c824c43b 70 const char *stopReason; // reason for forcing done() to be true
71 const char *typeName; // kid (leaf) class name, for debugging
3e5c8cf4 72 AsyncCall::Pointer inCall; // the asynchronous call being handled, if any
73 const unsigned int id;
74
75private:
76 static unsigned int TheLastId;
77};
78
79
63be0a78 80/**
81 \ingroup AsyncJobAPI
3e5c8cf4 82 * This is a base class for all job call dialers. It does all the job
83 * dialing logic (debugging, handling exceptions, etc.) except for calling
84 * the job method. The latter is not possible without templates and we
85 * want to keep this class simple and template-free. Thus, we add a dial()
86 * virtual method that the JobCallT template below will implement for us,
87 * calling the job.
88 */
89class JobDialer: public CallDialer
90{
91public:
92 JobDialer(AsyncJob *aJob);
93 JobDialer(const JobDialer &d);
94 virtual ~JobDialer();
95
96 virtual bool canDial(AsyncCall &call);
97 void dial(AsyncCall &call);
98
99 AsyncJob *job;
100 void *lock; // job's cbdata
101
102protected:
103 virtual void doDial() = 0; // actually calls the job method
104
105private:
106 // not implemented and should not be needed
107 JobDialer &operator =(const JobDialer &);
c824c43b 108};
109
3e5c8cf4 110#include "AsyncJobCalls.h"
111
112template <class Dialer>
113bool
114CallJob(int debugSection, int debugLevel, const char *fileName, int fileLine,
115 const char *callName, const Dialer &dialer)
116{
117 AsyncCall::Pointer call = asyncCall(debugSection, debugLevel, callName, dialer);
118 return ScheduleCall(fileName, fileLine, call);
119}
120
121
122#define CallJobHere(debugSection, debugLevel, job, method) \
123 CallJob((debugSection), (debugLevel), __FILE__, __LINE__, #method, \
124 MemFun((job), &method))
c824c43b 125
3e5c8cf4 126#define CallJobHere1(debugSection, debugLevel, job, method, arg1) \
127 CallJob((debugSection), (debugLevel), __FILE__, __LINE__, #method, \
128 MemFun((job), &method, (arg1)))
c824c43b 129
130
131#endif /* SQUID_ASYNC_JOB_H */