1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is Mozilla code.
17 : *
18 : * The Initial Developer of the Original Code is Google Inc.
19 : * Portions created by the Initial Developer are Copyright (C) 2006
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Darin Fisher <darin@meer.net>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef nsEventQueue_h__
40 : #define nsEventQueue_h__
41 :
42 : #include <stdlib.h>
43 : #include "mozilla/ReentrantMonitor.h"
44 : #include "nsIRunnable.h"
45 :
46 : // A threadsafe FIFO event queue...
47 : class nsEventQueue
48 : {
49 : typedef mozilla::ReentrantMonitor ReentrantMonitor;
50 :
51 : public:
52 : nsEventQueue();
53 : ~nsEventQueue();
54 :
55 : // This method adds a new event to the pending event queue. The event object
56 : // is AddRef'd if this method succeeds. This method returns true if the
57 : // event was stored in the event queue, and it returns false if it could
58 : // not allocate sufficient memory.
59 : bool PutEvent(nsIRunnable *event);
60 :
61 : // This method gets an event from the event queue. If mayWait is true, then
62 : // the method will block the calling thread until an event is available. If
63 : // the event is null, then the method returns immediately indicating whether
64 : // or not an event is pending. When the resulting event is non-null, the
65 : // caller is responsible for releasing the event object. This method does
66 : // not alter the reference count of the resulting event.
67 : bool GetEvent(bool mayWait, nsIRunnable **event);
68 :
69 : // This method returns true if there is a pending event.
70 6106 : bool HasPendingEvent() {
71 6106 : return GetEvent(false, nsnull);
72 : }
73 :
74 : // This method returns the next pending event or null.
75 0 : bool GetPendingEvent(nsIRunnable **runnable) {
76 0 : return GetEvent(false, runnable);
77 : }
78 :
79 : // This method waits for and returns the next pending event.
80 : bool WaitPendingEvent(nsIRunnable **runnable) {
81 : return GetEvent(true, runnable);
82 : }
83 :
84 : // Expose the event queue's monitor for "power users"
85 12450 : ReentrantMonitor& GetReentrantMonitor() {
86 12450 : return mReentrantMonitor;
87 : }
88 :
89 : private:
90 :
91 885089 : bool IsEmpty() {
92 885089 : return !mHead || (mHead == mTail && mOffsetHead == mOffsetTail);
93 : }
94 :
95 : enum { EVENTS_PER_PAGE = 250 };
96 :
97 : // Page objects are linked together to form a simple deque.
98 :
99 : struct Page {
100 : struct Page *mNext;
101 : nsIRunnable *mEvents[EVENTS_PER_PAGE];
102 : };
103 :
104 8284 : static Page *NewPage() {
105 8284 : return static_cast<Page *>(calloc(1, sizeof(Page)));
106 : }
107 :
108 8273 : static void FreePage(Page *p) {
109 8273 : free(p);
110 8273 : }
111 :
112 : ReentrantMonitor mReentrantMonitor;
113 :
114 : Page *mHead;
115 : Page *mTail;
116 :
117 : PRUint16 mOffsetHead; // offset into mHead where next item is removed
118 : PRUint16 mOffsetTail; // offset into mTail where next item is added
119 : };
120 :
121 : #endif // nsEventQueue_h__
|