Bumping gaia.json for 8 gaia revision(s) a=gaia-bump
[gecko.git] / netwerk / cache2 / CacheIOThread.h
blob7bff5435a889e397d25d480e1a69ad133e81235a
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"
10 #include "prthread.h"
11 #include "nsTArray.h"
12 #include "nsAutoPtr.h"
13 #include "mozilla/Monitor.h"
15 class nsIRunnable;
17 namespace mozilla {
18 namespace net {
20 class CacheIOThread : public nsIThreadObserver
22 virtual ~CacheIOThread();
24 public:
25 NS_DECL_THREADSAFE_ISUPPORTS
26 NS_DECL_NSITHREADOBSERVER
28 CacheIOThread();
30 enum ELevel {
31 OPEN_PRIORITY,
32 READ_PRIORITY,
33 OPEN,
34 READ,
35 MANAGEMENT,
36 WRITE,
37 CLOSE,
38 INDEX,
39 EVICT,
40 LAST_LEVEL,
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.
45 XPCOM_LEVEL
48 nsresult Init();
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();
56 /**
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;
70 nsresult Shutdown();
71 already_AddRefed<nsIEventTarget> Target();
73 // Memory reporting
74 size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
75 size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
77 private:
78 static void ThreadFunc(void* aClosure);
79 void ThreadFunc();
80 void LoopOneLevel(uint32_t aLevel);
81 bool EventsPending(uint32_t aLastLevel = LAST_LEVEL);
82 nsresult DispatchInternal(nsIRunnable* aRunnable, uint32_t aLevel);
83 bool YieldInternal();
85 static CacheIOThread* sSelf;
87 mozilla::Monitor mMonitor;
88 PRThread* mThread;
89 nsCOMPtr<nsIThread> mXPCOMThread;
90 uint32_t mLowestLevelWaiting;
91 uint32_t mCurrentlyExecutingLevel;
92 nsTArray<nsRefPtr<nsIRunnable> > mEventQueue[LAST_LEVEL];
94 bool mHasXPCOMEvents;
95 bool mRerunCurrentEvent;
96 bool mShutdown;
99 } // net
100 } // mozilla
102 #endif