michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsEventQueue_h__ michael@0: #define nsEventQueue_h__ michael@0: michael@0: #include michael@0: #include "mozilla/ReentrantMonitor.h" michael@0: #include "nsIRunnable.h" michael@0: michael@0: // A threadsafe FIFO event queue... michael@0: class nsEventQueue michael@0: { michael@0: typedef mozilla::ReentrantMonitor ReentrantMonitor; michael@0: michael@0: public: michael@0: nsEventQueue(); michael@0: ~nsEventQueue(); michael@0: michael@0: // This method adds a new event to the pending event queue. The event object michael@0: // is AddRef'd if this method succeeds. This method returns true if the michael@0: // event was stored in the event queue, and it returns false if it could michael@0: // not allocate sufficient memory. michael@0: bool PutEvent(nsIRunnable *event); michael@0: michael@0: // This method gets an event from the event queue. If mayWait is true, then michael@0: // the method will block the calling thread until an event is available. If michael@0: // the event is null, then the method returns immediately indicating whether michael@0: // or not an event is pending. When the resulting event is non-null, the michael@0: // caller is responsible for releasing the event object. This method does michael@0: // not alter the reference count of the resulting event. michael@0: bool GetEvent(bool mayWait, nsIRunnable **event); michael@0: michael@0: // This method returns true if there is a pending event. michael@0: bool HasPendingEvent() { michael@0: return GetEvent(false, nullptr); michael@0: } michael@0: michael@0: // This method returns the next pending event or null. michael@0: bool GetPendingEvent(nsIRunnable **runnable) { michael@0: return GetEvent(false, runnable); michael@0: } michael@0: michael@0: // This method waits for and returns the next pending event. michael@0: bool WaitPendingEvent(nsIRunnable **runnable) { michael@0: return GetEvent(true, runnable); michael@0: } michael@0: michael@0: // Expose the event queue's monitor for "power users" michael@0: ReentrantMonitor& GetReentrantMonitor() { michael@0: return mReentrantMonitor; michael@0: } michael@0: michael@0: private: michael@0: michael@0: bool IsEmpty() { michael@0: return !mHead || (mHead == mTail && mOffsetHead == mOffsetTail); michael@0: } michael@0: michael@0: enum { EVENTS_PER_PAGE = 255 }; michael@0: michael@0: // Page objects are linked together to form a simple deque. michael@0: michael@0: struct Page { michael@0: struct Page *mNext; michael@0: nsIRunnable *mEvents[EVENTS_PER_PAGE]; michael@0: }; michael@0: michael@0: static_assert((sizeof(Page) & (sizeof(Page) - 1)) == 0, michael@0: "sizeof(Page) should be a power of two to avoid heap slop."); michael@0: michael@0: static Page *NewPage() { michael@0: return static_cast(calloc(1, sizeof(Page))); michael@0: } michael@0: michael@0: static void FreePage(Page *p) { michael@0: free(p); michael@0: } michael@0: michael@0: ReentrantMonitor mReentrantMonitor; michael@0: michael@0: Page *mHead; michael@0: Page *mTail; michael@0: michael@0: uint16_t mOffsetHead; // offset into mHead where next item is removed michael@0: uint16_t mOffsetTail; // offset into mTail where next item is added michael@0: }; michael@0: michael@0: #endif // nsEventQueue_h__