--- /dev/null
+
+/*
+ * $Id: AsyncCallQueue.cc,v 1.1 2008/02/13 06:01:39 rousskov Exp $
+ *
+ * DEBUG: section 41 Event Processing
+ *
+ */
+
+#include "AsyncCallQueue.h"
+#include "AsyncCall.h"
+
+AsyncCallQueue *AsyncCallQueue::TheInstance = 0;
+
+
+AsyncCallQueue::AsyncCallQueue(): theHead(NULL), theTail(NULL)
+{
+}
+
+void AsyncCallQueue::schedule(AsyncCall::Pointer &call)
+{
+ assert(call != NULL);
+ assert(!call->theNext);
+ if (theHead != NULL) { // append
+ assert(!theTail->theNext);
+ theTail->theNext = call;
+ theTail = call;
+ } else { // create queue from cratch
+ theHead = theTail = call;
+ }
+}
+
+// Fire all scheduled calls; returns true if at least one call was fired.
+// The calls may be added while the current call is in progress.
+bool
+AsyncCallQueue::fire()
+{
+ const bool made = theHead != NULL;
+ while (theHead != NULL)
+ fireNext();
+ return made;
+}
+
+void
+AsyncCallQueue::fireNext()
+{
+ AsyncCall::Pointer call = theHead;
+ theHead = call->theNext;
+ call->theNext = NULL;
+ if (theTail == call)
+ theTail = NULL;
+
+ debugs(call->debugSection, call->debugLevel, "entering " << *call);
+ call->make();
+ debugs(call->debugSection, call->debugLevel, "leaving " << *call);
+}
+
+AsyncCallQueue &
+AsyncCallQueue::Instance()
+{
+ // TODO: how to remove this frequent check while supporting early calls?
+ if (!TheInstance)
+ TheInstance = new AsyncCallQueue();
+
+ return *TheInstance;
+}
+
--- /dev/null
+
+/*
+ * $Id: AsyncCallQueue.h,v 1.1 2008/02/13 06:01:39 rousskov Exp $
+ *
+ */
+
+#ifndef SQUID_ASYNCCALLQUEUE_H
+#define SQUID_ASYNCCALLQUEUE_H
+
+#include "squid.h"
+#include "AsyncCall.h"
+
+//class AsyncCall;
+
+// The queue of asynchronous calls. All calls are fired during a single main
+// loop iteration until the queue is exhausted
+class AsyncCallQueue
+{
+public:
+ // there is only one queue
+ static AsyncCallQueue &Instance();
+
+ // make this async call when we get a chance
+ void schedule(AsyncCall::Pointer &call);
+
+ // fire all scheduled calls; returns true if at least one was fired
+ bool fire();
+
+private:
+ AsyncCallQueue();
+
+ void fireNext();
+
+ AsyncCall::Pointer theHead;
+ AsyncCall::Pointer theTail;
+
+ static AsyncCallQueue *TheInstance;
+};
+
+#endif /* SQUID_ASYNCCALLQUEUE_H */