Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef nsEventQueue_h__ |
michael@0 | 8 | #define nsEventQueue_h__ |
michael@0 | 9 | |
michael@0 | 10 | #include <stdlib.h> |
michael@0 | 11 | #include "mozilla/ReentrantMonitor.h" |
michael@0 | 12 | #include "nsIRunnable.h" |
michael@0 | 13 | |
michael@0 | 14 | // A threadsafe FIFO event queue... |
michael@0 | 15 | class nsEventQueue |
michael@0 | 16 | { |
michael@0 | 17 | typedef mozilla::ReentrantMonitor ReentrantMonitor; |
michael@0 | 18 | |
michael@0 | 19 | public: |
michael@0 | 20 | nsEventQueue(); |
michael@0 | 21 | ~nsEventQueue(); |
michael@0 | 22 | |
michael@0 | 23 | // This method adds a new event to the pending event queue. The event object |
michael@0 | 24 | // is AddRef'd if this method succeeds. This method returns true if the |
michael@0 | 25 | // event was stored in the event queue, and it returns false if it could |
michael@0 | 26 | // not allocate sufficient memory. |
michael@0 | 27 | bool PutEvent(nsIRunnable *event); |
michael@0 | 28 | |
michael@0 | 29 | // This method gets an event from the event queue. If mayWait is true, then |
michael@0 | 30 | // the method will block the calling thread until an event is available. If |
michael@0 | 31 | // the event is null, then the method returns immediately indicating whether |
michael@0 | 32 | // or not an event is pending. When the resulting event is non-null, the |
michael@0 | 33 | // caller is responsible for releasing the event object. This method does |
michael@0 | 34 | // not alter the reference count of the resulting event. |
michael@0 | 35 | bool GetEvent(bool mayWait, nsIRunnable **event); |
michael@0 | 36 | |
michael@0 | 37 | // This method returns true if there is a pending event. |
michael@0 | 38 | bool HasPendingEvent() { |
michael@0 | 39 | return GetEvent(false, nullptr); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | // This method returns the next pending event or null. |
michael@0 | 43 | bool GetPendingEvent(nsIRunnable **runnable) { |
michael@0 | 44 | return GetEvent(false, runnable); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | // This method waits for and returns the next pending event. |
michael@0 | 48 | bool WaitPendingEvent(nsIRunnable **runnable) { |
michael@0 | 49 | return GetEvent(true, runnable); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | // Expose the event queue's monitor for "power users" |
michael@0 | 53 | ReentrantMonitor& GetReentrantMonitor() { |
michael@0 | 54 | return mReentrantMonitor; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | private: |
michael@0 | 58 | |
michael@0 | 59 | bool IsEmpty() { |
michael@0 | 60 | return !mHead || (mHead == mTail && mOffsetHead == mOffsetTail); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | enum { EVENTS_PER_PAGE = 255 }; |
michael@0 | 64 | |
michael@0 | 65 | // Page objects are linked together to form a simple deque. |
michael@0 | 66 | |
michael@0 | 67 | struct Page { |
michael@0 | 68 | struct Page *mNext; |
michael@0 | 69 | nsIRunnable *mEvents[EVENTS_PER_PAGE]; |
michael@0 | 70 | }; |
michael@0 | 71 | |
michael@0 | 72 | static_assert((sizeof(Page) & (sizeof(Page) - 1)) == 0, |
michael@0 | 73 | "sizeof(Page) should be a power of two to avoid heap slop."); |
michael@0 | 74 | |
michael@0 | 75 | static Page *NewPage() { |
michael@0 | 76 | return static_cast<Page *>(calloc(1, sizeof(Page))); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | static void FreePage(Page *p) { |
michael@0 | 80 | free(p); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | ReentrantMonitor mReentrantMonitor; |
michael@0 | 84 | |
michael@0 | 85 | Page *mHead; |
michael@0 | 86 | Page *mTail; |
michael@0 | 87 | |
michael@0 | 88 | uint16_t mOffsetHead; // offset into mHead where next item is removed |
michael@0 | 89 | uint16_t mOffsetTail; // offset into mTail where next item is added |
michael@0 | 90 | }; |
michael@0 | 91 | |
michael@0 | 92 | #endif // nsEventQueue_h__ |