2013-09-20 13:11:25 +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/. */
|
|
|
|
|
|
|
|
#ifndef CacheIOThread__h__
|
|
|
|
#define CacheIOThread__h__
|
|
|
|
|
|
|
|
#include "nsIThreadInternal.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "prthread.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "mozilla/Monitor.h"
|
2015-04-13 17:58:00 +03:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2015-06-01 16:36:44 +03:00
|
|
|
#include "mozilla/Atomics.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
class nsIRunnable;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
class CacheIOThread : public nsIThreadObserver
|
|
|
|
{
|
2014-06-24 20:36:44 +04:00
|
|
|
virtual ~CacheIOThread();
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSITHREADOBSERVER
|
|
|
|
|
|
|
|
CacheIOThread();
|
|
|
|
|
|
|
|
enum ELevel {
|
|
|
|
OPEN_PRIORITY,
|
|
|
|
READ_PRIORITY,
|
|
|
|
OPEN,
|
|
|
|
READ,
|
2014-01-22 21:54:52 +04:00
|
|
|
MANAGEMENT,
|
2014-04-06 22:45:18 +04:00
|
|
|
WRITE,
|
2016-01-05 19:50:00 +03:00
|
|
|
CLOSE = WRITE,
|
2014-03-07 15:22:59 +04:00
|
|
|
INDEX,
|
2014-02-18 21:26:48 +04:00
|
|
|
EVICT,
|
2014-02-27 03:11:42 +04:00
|
|
|
LAST_LEVEL,
|
|
|
|
|
|
|
|
// This is actually executed as the first level, but we want this enum
|
|
|
|
// value merely as an indicator while other values are used as indexes
|
|
|
|
// to the queue array. Hence put at end and not as the first.
|
|
|
|
XPCOM_LEVEL
|
2013-09-20 13:11:25 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
nsresult Init();
|
|
|
|
nsresult Dispatch(nsIRunnable* aRunnable, uint32_t aLevel);
|
2014-04-04 16:42:05 +04:00
|
|
|
// Makes sure that any previously posted event to OPEN or OPEN_PRIORITY
|
|
|
|
// levels (such as file opennings and dooms) are executed before aRunnable
|
|
|
|
// that is intended to evict stuff from the cache.
|
|
|
|
nsresult DispatchAfterPendingOpens(nsIRunnable* aRunnable);
|
2013-09-20 13:11:25 +04:00
|
|
|
bool IsCurrentThread();
|
2014-02-27 03:11:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callable only on this thread, checks if there is an event waiting in
|
|
|
|
* the event queue with a higher execution priority. If so, the result
|
|
|
|
* is true and the current event handler should break it's work and return
|
|
|
|
* from Run() method immediately. The event handler will be rerun again
|
|
|
|
* when all more priority events are processed. Events pending after this
|
|
|
|
* handler (i.e. the one that called YieldAndRerun()) will not execute sooner
|
|
|
|
* then this handler is executed w/o a call to YieldAndRerun().
|
|
|
|
*/
|
|
|
|
static bool YieldAndRerun()
|
|
|
|
{
|
|
|
|
return sSelf ? sSelf->YieldInternal() : false;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
nsresult Shutdown();
|
|
|
|
already_AddRefed<nsIEventTarget> Target();
|
|
|
|
|
2014-02-27 03:11:40 +04:00
|
|
|
// Memory reporting
|
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
|
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
private:
|
|
|
|
static void ThreadFunc(void* aClosure);
|
|
|
|
void ThreadFunc();
|
|
|
|
void LoopOneLevel(uint32_t aLevel);
|
|
|
|
bool EventsPending(uint32_t aLastLevel = LAST_LEVEL);
|
2014-04-04 16:42:05 +04:00
|
|
|
nsresult DispatchInternal(nsIRunnable* aRunnable, uint32_t aLevel);
|
2014-02-27 03:11:42 +04:00
|
|
|
bool YieldInternal();
|
|
|
|
|
|
|
|
static CacheIOThread* sSelf;
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
mozilla::Monitor mMonitor;
|
|
|
|
PRThread* mThread;
|
2015-11-13 20:49:29 +03:00
|
|
|
Atomic<nsIThread *> mXPCOMThread;
|
2015-06-01 16:36:44 +03:00
|
|
|
Atomic<uint32_t, Relaxed> mLowestLevelWaiting;
|
2014-02-27 03:11:42 +04:00
|
|
|
uint32_t mCurrentlyExecutingLevel;
|
2015-03-17 19:29:17 +03:00
|
|
|
nsTArray<nsCOMPtr<nsIRunnable> > mEventQueue[LAST_LEVEL];
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2015-06-01 16:59:17 +03:00
|
|
|
Atomic<bool, Relaxed> mHasXPCOMEvents;
|
2014-02-27 03:11:42 +04:00
|
|
|
bool mRerunCurrentEvent;
|
2013-09-20 13:11:25 +04:00
|
|
|
bool mShutdown;
|
2016-02-26 18:52:07 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool mInsideLoop;
|
|
|
|
#endif
|
2013-09-20 13:11:25 +04:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
#endif
|