--------------------- PatchSet 5988 Date: 2007/10/22 19:24:27 Author: chtsanti Branch: async-calls Tag: (none) Log: -Implement the AsyncJobHolder class (a clone of the ICAPInitiatorHolder class). The AsyncJobHolder can used to associate AsyncJob with its cbdata. - Modify AsyncJob to work with AsyncJobHolder objects. Now AsyncJob passes as data to the squid Event code an AsyncJobHolder object. Members: src/ICAP/AsyncJob.cc:1.3.4.1->1.3.4.2 src/ICAP/AsyncJob.h:1.3.14.1->1.3.14.2 Index: squid3/src/ICAP/AsyncJob.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/ICAP/AsyncJob.cc,v retrieving revision 1.3.4.1 retrieving revision 1.3.4.2 diff -u -r1.3.4.1 -r1.3.4.2 --- squid3/src/ICAP/AsyncJob.cc 17 Oct 2007 21:59:30 -0000 1.3.4.1 +++ squid3/src/ICAP/AsyncJob.cc 22 Oct 2007 19:24:27 -0000 1.3.4.2 @@ -8,10 +8,55 @@ #include "AsyncJob.h" +/* AsyncJobHolder */ + +AsyncJobHolder::AsyncJobHolder(AsyncJob *aJob): + ptr(0), cbdata(0) +{ + if (aJob) { + cbdata = cbdataReference(aJob->toCbdata()); + ptr = aJob; + } +} + +AsyncJobHolder::AsyncJobHolder(const AsyncJobHolder &aJob): + ptr(0), cbdata(0) +{ + if (aJob != NULL && cbdataReferenceValid(aJob.cbdata)) { + cbdata = cbdataReference(aJob.cbdata); + ptr = aJob.ptr; + } +} + +AsyncJobHolder::~AsyncJobHolder() +{ + clear(); +} + +void AsyncJobHolder::clear() { + if (ptr) { + ptr = NULL; + cbdataReferenceDone(cbdata); + } +} + +// should not be used +AsyncJobHolder &AsyncJobHolder::operator =(const AsyncJobHolder &anInitiator) +{ + assert(false); + return *this; +} + + + AsyncJob *AsyncJob::AsyncStart(AsyncJob *job) { assert(job); - job = cbdataReference(job); // unlocked when done() in callEnd() - AsyncCall(93,5, job, AsyncJob::noteStart); + assert(cbdataReference(job->toCbdata())); + AsyncJobHolder *jobHolder=new AsyncJobHolder(job); + assert(jobHolder); + scheduleAsyncCall(93, 5, __FILE__, __LINE__, + jobHolder, "AsyncJob::noteStart", + &(AsyncJob::noteStartWrapper), false); return job; } @@ -32,9 +77,26 @@ AsyncCallExit(); } +void AsyncJob::noteStartWrapper(void *data) +{ + AsyncJobHolder *jobHolder=static_cast(data); + AsyncJob *objectPtr = static_cast(jobHolder->ptr); + void *cbdata = jobHolder->cbdata; + delete jobHolder; + + if(!cbdataReferenceValid(cbdata)) + return; + + if (enterAsyncCallWrapper(93, 3, data, "AsyncJob", "noteStart")) { + objectPtr->noteStart(); + exitAsyncCallWrapper(93, 3, data, "AsyncJob", "noteStart"); + } +} + + void AsyncJob::start() { - Must(cbdataReferenceValid(this)); // locked in AsyncStart + Must(cbdataReferenceValid(this->toCbdata())); // locked in AsyncStart } // XXX: temporary code to replace calls to "delete this" in jobs-in-transition. @@ -124,7 +186,7 @@ swanSong(); - void *cbdata = this; + void *cbdata = this->toCbdata(); delete this; // this is the only place where the object is deleted cbdataReferenceDone(cbdata); // locked by AsyncStart Index: squid3/src/ICAP/AsyncJob.h =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/ICAP/AsyncJob.h,v retrieving revision 1.3.14.1 retrieving revision 1.3.14.2 diff -u -r1.3.14.1 -r1.3.14.2 --- squid3/src/ICAP/AsyncJob.h 17 Oct 2007 21:59:30 -0000 1.3.14.1 +++ squid3/src/ICAP/AsyncJob.h 22 Oct 2007 19:24:27 -0000 1.3.14.2 @@ -1,6 +1,6 @@ /* - * $Id: AsyncJob.h,v 1.3.14.1 2007/10/17 21:59:30 rousskov Exp $ + * $Id: AsyncJob.h,v 1.3.14.2 2007/10/22 19:24:27 chtsanti Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -56,6 +56,31 @@ class TextException; +class AsyncJob; + +/* AsyncJob holder associtates an AsyncJob with its cbdata. It is used as + * a temporary hack to make cbdata work with virtual parents */ +class AsyncJobHolder { +public: + AsyncJobHolder(AsyncJob *aJob); + AsyncJobHolder(const AsyncJobHolder &aJob); + ~AsyncJobHolder(); + + void clear(); + + // to make comparison with NULL possible + operator void*() { return ptr; } + bool operator == (void *) const { return ptr == NULL; } + bool operator != (void *) const { return ptr != NULL; } + bool operator !() const { return !ptr; } + + AsyncJob *ptr; + void *cbdata; + +private: + AsyncJobHolder &operator =(const AsyncJobHolder &aJob); +}; + class AsyncJob { @@ -65,8 +90,9 @@ AsyncJob(const char *aTypeName); virtual ~AsyncJob(); + virtual void *toCbdata() = 0; void noteStart(); // calls virtual start - AsyncCallWrapper(93,3, AsyncJob, noteStart); + static void noteStartWrapper(void *data); protected: // XXX: temporary method to replace "delete this" in jobs-in-transition.