]> git.ipfire.org Git - thirdparty/squid.git/blob - src/base/AsyncCallQueue.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / base / AsyncCallQueue.cc
1
2 /*
3 * DEBUG: section 41 Event Processing
4 *
5 */
6
7 #include "squid.h"
8 #include "base/AsyncCall.h"
9 #include "base/AsyncCallQueue.h"
10 #include "Debug.h"
11
12 AsyncCallQueue *AsyncCallQueue::TheInstance = 0;
13
14 AsyncCallQueue::AsyncCallQueue(): theHead(NULL), theTail(NULL)
15 {
16 }
17
18 void AsyncCallQueue::schedule(AsyncCall::Pointer &call)
19 {
20 assert(call != NULL);
21 assert(!call->theNext);
22 if (theHead != NULL) { // append
23 assert(!theTail->theNext);
24 theTail->theNext = call;
25 theTail = call;
26 } else { // create queue from cratch
27 theHead = theTail = call;
28 }
29 }
30
31 // Fire all scheduled calls; returns true if at least one call was fired.
32 // The calls may be added while the current call is in progress.
33 bool
34 AsyncCallQueue::fire()
35 {
36 const bool made = theHead != NULL;
37 while (theHead != NULL)
38 fireNext();
39 return made;
40 }
41
42 void
43 AsyncCallQueue::fireNext()
44 {
45 AsyncCall::Pointer call = theHead;
46 theHead = call->theNext;
47 call->theNext = NULL;
48 if (theTail == call)
49 theTail = NULL;
50
51 debugs(call->debugSection, call->debugLevel, "entering " << *call);
52 call->make();
53 debugs(call->debugSection, call->debugLevel, "leaving " << *call);
54 }
55
56 AsyncCallQueue &
57 AsyncCallQueue::Instance()
58 {
59 // TODO: how to remove this frequent check while supporting early calls?
60 if (!TheInstance)
61 TheInstance = new AsyncCallQueue();
62
63 return *TheInstance;
64 }
65