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
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/
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
16 * The Original Code is Mozilla code.
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.
23 * Darin Fisher <darin@meer.net>
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.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef nsEventQueue_h__
40 #define nsEventQueue_h__
44 #include "nsIRunnable.h"
46 // A threadsafe FIFO event queue...
47 class NS_COM nsEventQueue
53 // Returns "true" if the event queue has been completely constructed.
54 PRBool
IsInitialized() { return mMonitor
!= nsnull
; }
56 // This method adds a new event to the pending event queue. The event object
57 // is AddRef'd if this method succeeds. This method returns PR_TRUE if the
58 // event was stored in the event queue, and it returns PR_FALSE if it could
59 // not allocate sufficient memory.
60 PRBool
PutEvent(nsIRunnable
*event
);
62 // This method gets an event from the event queue. If mayWait is true, then
63 // the method will block the calling thread until an event is available. If
64 // the event is null, then the method returns immediately indicating whether
65 // or not an event is pending. When the resulting event is non-null, the
66 // caller is responsible for releasing the event object. This method does
67 // not alter the reference count of the resulting event.
68 PRBool
GetEvent(PRBool mayWait
, nsIRunnable
**event
);
70 // This method returns true if there is a pending event.
71 PRBool
HasPendingEvent() {
72 return GetEvent(PR_FALSE
, nsnull
);
75 // This method returns the next pending event or null.
76 PRBool
GetPendingEvent(nsIRunnable
**runnable
) {
77 return GetEvent(PR_FALSE
, runnable
);
80 // This method waits for and returns the next pending event.
81 PRBool
WaitPendingEvent(nsIRunnable
**runnable
) {
82 return GetEvent(PR_TRUE
, runnable
);
85 // Expose the event queue's monitor for "power users"
86 PRMonitor
*Monitor() {
93 return !mHead
|| (mHead
== mTail
&& mOffsetHead
== mOffsetTail
);
96 enum { EVENTS_PER_PAGE
= 250 };
98 // Page objects are linked together to form a simple deque.
100 struct Page
; friend struct Page
; // VC6!
103 nsIRunnable
*mEvents
[EVENTS_PER_PAGE
];
106 static Page
*NewPage() {
107 return static_cast<Page
*>(calloc(1, sizeof(Page
)));
110 static void FreePage(Page
*p
) {
119 PRUint16 mOffsetHead
; // offset into mHead where next item is removed
120 PRUint16 mOffsetTail
; // offset into mTail where next item is added
123 #endif // nsEventQueue_h__