Bug 670967 - Part 1: Add mozilla::ScheduleMemoryPressureEvent(). r=bsmedberg

This commit is contained in:
Justin Lebar 2011-08-05 18:10:50 -04:00
Родитель c4a1ccf28b
Коммит 307a746c18
3 изменённых файлов: 46 добавлений и 0 удалений

Просмотреть файл

@ -64,6 +64,7 @@ CPPSRCS = \
$(NULL)
EXPORTS = \
nsThread.h \
nsProcess.h \
nsEventQueue.h \
nsThreadUtilsInternal.h \

Просмотреть файл

@ -45,7 +45,9 @@
#include "nsCOMPtr.h"
#include "prlog.h"
#include "nsThreadUtilsInternal.h"
#include "nsIObserverService.h"
#include "mozilla/HangMonitor.h"
#include "mozilla/Services.h"
#define HAVE_UALARM _BSD_SOURCE || (_XOPEN_SOURCE >= 500 || \
_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) && \
@ -81,6 +83,21 @@ NS_DECL_CI_INTERFACE_GETTER(nsThread)
nsIThreadObserver* nsThread::sGlobalObserver;
namespace mozilla {
static volatile bool sMemoryPressurePending = false;
/*
* It's important that this function not acquire any locks, nor do anything
* which might cause malloc to run.
*/
void ScheduleMemoryPressureEvent()
{
PR_ATOMIC_SET(&sMemoryPressurePending, true);
}
};
//-----------------------------------------------------------------------------
// Because we do not have our own nsIFactory, we have to implement nsIClassInfo
// somewhat manually.
@ -577,6 +594,22 @@ nsThread::ProcessNextEvent(bool mayWait, bool *result)
if (MAIN_THREAD == mIsMainThread && mayWait && !ShuttingDown())
HangMonitor::Suspend();
// Fire a memory pressure notification, if we're the main thread and one is
// pending.
if (MAIN_THREAD == mIsMainThread && !ShuttingDown()) {
bool mpPending = PR_ATOMIC_SET(&sMemoryPressurePending, false);
if (mpPending) {
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
if (os) {
os->NotifyObservers(nsnull, "memory-pressure",
NS_LITERAL_STRING("low-memory").get());
}
else {
NS_WARNING("Can't get observer service!");
}
}
}
bool notifyGlobalObserver = (sGlobalObserver != nsnull);
if (notifyGlobalObserver)
sGlobalObserver->OnProcessNextEvent(this, mayWait && !ShuttingDown(),

Просмотреть файл

@ -178,4 +178,16 @@ private:
nsresult mResult;
};
namespace mozilla {
/**
* This function causes the main thread to fire a memory pressure event at its
* next available opportunity.
*
* You may call this function from any thread.
*/
void ScheduleMemoryPressureEvent();
} // namespace mozilla
#endif // nsThread_h__