1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef CacheIOThread__h__
6 #define CacheIOThread__h__
8 #include "nsIThreadInternal.h"
9 #include "nsISupportsImpl.h"
12 #include "nsAutoPtr.h"
13 #include "mozilla/Monitor.h"
20 class CacheIOThread
: public nsIThreadObserver
22 virtual ~CacheIOThread();
25 NS_DECL_THREADSAFE_ISUPPORTS
26 NS_DECL_NSITHREADOBSERVER
42 // This is actually executed as the first level, but we want this enum
43 // value merely as an indicator while other values are used as indexes
44 // to the queue array. Hence put at end and not as the first.
49 nsresult
Dispatch(nsIRunnable
* aRunnable
, uint32_t aLevel
);
50 // Makes sure that any previously posted event to OPEN or OPEN_PRIORITY
51 // levels (such as file opennings and dooms) are executed before aRunnable
52 // that is intended to evict stuff from the cache.
53 nsresult
DispatchAfterPendingOpens(nsIRunnable
* aRunnable
);
54 bool IsCurrentThread();
57 * Callable only on this thread, checks if there is an event waiting in
58 * the event queue with a higher execution priority. If so, the result
59 * is true and the current event handler should break it's work and return
60 * from Run() method immediately. The event handler will be rerun again
61 * when all more priority events are processed. Events pending after this
62 * handler (i.e. the one that called YieldAndRerun()) will not execute sooner
63 * then this handler is executed w/o a call to YieldAndRerun().
65 static bool YieldAndRerun()
67 return sSelf
? sSelf
->YieldInternal() : false;
71 already_AddRefed
<nsIEventTarget
> Target();
74 size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf
) const;
75 size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf
) const;
78 static void ThreadFunc(void* aClosure
);
80 void LoopOneLevel(uint32_t aLevel
);
81 bool EventsPending(uint32_t aLastLevel
= LAST_LEVEL
);
82 nsresult
DispatchInternal(nsIRunnable
* aRunnable
, uint32_t aLevel
);
85 static CacheIOThread
* sSelf
;
87 mozilla::Monitor mMonitor
;
89 nsCOMPtr
<nsIThread
> mXPCOMThread
;
90 uint32_t mLowestLevelWaiting
;
91 uint32_t mCurrentlyExecutingLevel
;
92 nsTArray
<nsRefPtr
<nsIRunnable
> > mEventQueue
[LAST_LEVEL
];
95 bool mRerunCurrentEvent
;