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"
|
2016-08-10 17:50:00 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
class nsIRunnable;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2016-08-10 17:50:00 +03:00
|
|
|
namespace detail {
|
|
|
|
// A class keeping platform specific information needed to watch and
|
|
|
|
// cancel any long blocking synchronous IO. Must be predeclared here
|
|
|
|
// since including windows.h breaks stuff with number of macro definition
|
|
|
|
// conflicts.
|
|
|
|
class BlockingIOWatcher;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
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();
|
|
|
|
|
2016-06-27 06:43:00 +03:00
|
|
|
typedef nsTArray<nsCOMPtr<nsIRunnable>> EventQueue;
|
|
|
|
|
|
|
|
enum ELevel : uint32_t {
|
2013-09-20 13:11:25 +04:00
|
|
|
OPEN_PRIORITY,
|
|
|
|
READ_PRIORITY,
|
2016-10-12 11:32:00 +03:00
|
|
|
MANAGEMENT, // Doesn't do any actual I/O
|
2013-09-20 13:11:25 +04:00
|
|
|
OPEN,
|
|
|
|
READ,
|
2016-10-12 11:32:00 +03:00
|
|
|
WRITE_PRIORITY,
|
2014-04-06 22:45:18 +04:00
|
|
|
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);
|
2016-06-29 05:24:54 +03:00
|
|
|
nsresult Dispatch(already_AddRefed<nsIRunnable>, 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
|
|
|
|
2016-11-10 18:14:34 +03:00
|
|
|
uint32_t QueueSize(bool highPriority);
|
|
|
|
|
2017-06-23 11:24:45 +03:00
|
|
|
uint32_t EventCounter() const { return mEventCounter; }
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-10 17:50:00 +03:00
|
|
|
void Shutdown();
|
|
|
|
// This method checks if there is a long blocking IO on the
|
|
|
|
// IO thread and tries to cancel it. It waits maximum of
|
|
|
|
// two seconds.
|
|
|
|
void CancelBlockingIO();
|
2013-09-20 13:11:25 +04:00
|
|
|
already_AddRefed<nsIEventTarget> Target();
|
|
|
|
|
2016-08-10 17:50:00 +03:00
|
|
|
// A stack class used to annotate running interruptable I/O event
|
|
|
|
class Cancelable
|
|
|
|
{
|
|
|
|
bool mCancelable;
|
|
|
|
public:
|
|
|
|
explicit Cancelable(bool aCancelable);
|
|
|
|
~Cancelable();
|
|
|
|
};
|
|
|
|
|
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);
|
2016-06-29 05:24:54 +03:00
|
|
|
nsresult DispatchInternal(already_AddRefed<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;
|
2016-08-10 17:50:00 +03:00
|
|
|
UniquePtr<detail::BlockingIOWatcher> mBlockingIOWatcher;
|
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;
|
2016-06-27 06:43:00 +03:00
|
|
|
|
2016-11-10 18:14:23 +03:00
|
|
|
// Keeps the length of the each event queue, since LoopOneLevel moves all
|
|
|
|
// events into a local array.
|
|
|
|
Atomic<int32_t> mQueueLength[LAST_LEVEL];
|
|
|
|
|
2016-06-27 06:43:00 +03:00
|
|
|
EventQueue mEventQueue[LAST_LEVEL];
|
2016-08-10 17:50:00 +03:00
|
|
|
// Raised when nsIEventTarget.Dispatch() is called on this thread
|
2015-06-01 16:59:17 +03:00
|
|
|
Atomic<bool, Relaxed> mHasXPCOMEvents;
|
2016-08-10 17:50:00 +03:00
|
|
|
// See YieldAndRerun() above
|
2014-02-27 03:11:42 +04:00
|
|
|
bool mRerunCurrentEvent;
|
2016-08-10 17:50:00 +03:00
|
|
|
// Signal to process all pending events and then shutdown
|
|
|
|
// Synchronized by mMonitor
|
2013-09-20 13:11:25 +04:00
|
|
|
bool mShutdown;
|
2016-08-10 17:50:00 +03:00
|
|
|
// If > 0 there is currently an I/O operation on the thread that
|
|
|
|
// can be canceled when after shutdown, see the Shutdown() method
|
|
|
|
// for usage. Made a counter to allow nesting of the Cancelable class.
|
|
|
|
Atomic<uint32_t, Relaxed> mIOCancelableEvents;
|
2017-06-23 11:24:45 +03:00
|
|
|
// Event counter that increases with every event processed.
|
|
|
|
Atomic<uint32_t, Relaxed> mEventCounter;
|
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
|