2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-05-05 03:35:47 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
#ifndef nsEventQueue_h__
|
|
|
|
#define nsEventQueue_h__
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2011-04-29 23:21:57 +04:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsIRunnable.h"
|
1999-05-05 03:35:47 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// A threadsafe FIFO event queue...
|
2011-08-18 17:46:39 +04:00
|
|
|
class nsEventQueue
|
1999-05-05 03:35:47 +04:00
|
|
|
{
|
2011-04-29 23:21:57 +04:00
|
|
|
typedef mozilla::ReentrantMonitor ReentrantMonitor;
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
|
1999-05-05 03:35:47 +04:00
|
|
|
public:
|
2006-05-10 21:30:15 +04:00
|
|
|
nsEventQueue();
|
|
|
|
~nsEventQueue();
|
|
|
|
|
2014-05-23 23:53:14 +04:00
|
|
|
// This method adds a new event to the pending event queue. The queue holds
|
|
|
|
// a strong reference to the event after this method returns. This method
|
|
|
|
// cannot fail.
|
2014-05-27 11:15:35 +04:00
|
|
|
void PutEvent(nsIRunnable* aEvent);
|
1999-05-05 03:35:47 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// This method gets an event from the event queue. If mayWait is true, then
|
|
|
|
// the method will block the calling thread until an event is available. If
|
|
|
|
// the event is null, then the method returns immediately indicating whether
|
|
|
|
// or not an event is pending. When the resulting event is non-null, the
|
|
|
|
// caller is responsible for releasing the event object. This method does
|
|
|
|
// not alter the reference count of the resulting event.
|
2014-05-27 11:15:35 +04:00
|
|
|
bool GetEvent(bool aMayWait, nsIRunnable** aEvent);
|
1999-05-05 03:35:47 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// This method returns true if there is a pending event.
|
2014-05-27 11:15:35 +04:00
|
|
|
bool HasPendingEvent()
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
return GetEvent(false, nullptr);
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
1999-05-05 03:35:47 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// This method returns the next pending event or null.
|
2014-05-27 11:15:35 +04:00
|
|
|
bool GetPendingEvent(nsIRunnable** runnable)
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return GetEvent(false, runnable);
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
1999-05-05 03:35:47 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// Expose the event queue's monitor for "power users"
|
2014-05-27 11:15:35 +04:00
|
|
|
ReentrantMonitor& GetReentrantMonitor()
|
|
|
|
{
|
2011-04-29 23:21:57 +04:00
|
|
|
return mReentrantMonitor;
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
1999-05-05 03:35:47 +04:00
|
|
|
|
1999-10-18 18:59:57 +04:00
|
|
|
private:
|
2004-01-15 09:14:18 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
bool IsEmpty()
|
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
return !mHead || (mHead == mTail && mOffsetHead == mOffsetTail);
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
EVENTS_PER_PAGE = 255
|
|
|
|
};
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// Page objects are linked together to form a simple deque.
|
1999-10-18 18:59:57 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
struct Page
|
|
|
|
{
|
|
|
|
struct Page* mNext;
|
|
|
|
nsIRunnable* mEvents[EVENTS_PER_PAGE];
|
2006-05-10 21:30:15 +04:00
|
|
|
};
|
|
|
|
|
2013-09-03 06:30:19 +04:00
|
|
|
static_assert((sizeof(Page) & (sizeof(Page) - 1)) == 0,
|
|
|
|
"sizeof(Page) should be a power of two to avoid heap slop.");
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
static Page* NewPage()
|
|
|
|
{
|
|
|
|
return static_cast<Page*>(moz_xcalloc(1, sizeof(Page)));
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
static void FreePage(Page* aPage)
|
|
|
|
{
|
|
|
|
free(aPage);
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitor mReentrantMonitor;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
Page* mHead;
|
|
|
|
Page* mTail;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t mOffsetHead; // offset into mHead where next item is removed
|
|
|
|
uint16_t mOffsetTail; // offset into mTail where next item is added
|
1999-10-18 18:59:57 +04:00
|
|
|
};
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
#endif // nsEventQueue_h__
|