|
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/. */ |
|
4 |
|
5 #ifndef CacheIOThread__h__ |
|
6 #define CacheIOThread__h__ |
|
7 |
|
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" |
|
14 |
|
15 class nsIRunnable; |
|
16 |
|
17 namespace mozilla { |
|
18 namespace net { |
|
19 |
|
20 class CacheIOThread : public nsIThreadObserver |
|
21 { |
|
22 public: |
|
23 NS_DECL_THREADSAFE_ISUPPORTS |
|
24 NS_DECL_NSITHREADOBSERVER |
|
25 |
|
26 CacheIOThread(); |
|
27 virtual ~CacheIOThread(); |
|
28 |
|
29 enum ELevel { |
|
30 OPEN_PRIORITY, |
|
31 READ_PRIORITY, |
|
32 OPEN, |
|
33 READ, |
|
34 MANAGEMENT, |
|
35 WRITE, |
|
36 CLOSE, |
|
37 INDEX, |
|
38 EVICT, |
|
39 LAST_LEVEL, |
|
40 |
|
41 // This is actually executed as the first level, but we want this enum |
|
42 // value merely as an indicator while other values are used as indexes |
|
43 // to the queue array. Hence put at end and not as the first. |
|
44 XPCOM_LEVEL |
|
45 }; |
|
46 |
|
47 nsresult Init(); |
|
48 nsresult Dispatch(nsIRunnable* aRunnable, uint32_t aLevel); |
|
49 // Makes sure that any previously posted event to OPEN or OPEN_PRIORITY |
|
50 // levels (such as file opennings and dooms) are executed before aRunnable |
|
51 // that is intended to evict stuff from the cache. |
|
52 nsresult DispatchAfterPendingOpens(nsIRunnable* aRunnable); |
|
53 bool IsCurrentThread(); |
|
54 |
|
55 /** |
|
56 * Callable only on this thread, checks if there is an event waiting in |
|
57 * the event queue with a higher execution priority. If so, the result |
|
58 * is true and the current event handler should break it's work and return |
|
59 * from Run() method immediately. The event handler will be rerun again |
|
60 * when all more priority events are processed. Events pending after this |
|
61 * handler (i.e. the one that called YieldAndRerun()) will not execute sooner |
|
62 * then this handler is executed w/o a call to YieldAndRerun(). |
|
63 */ |
|
64 static bool YieldAndRerun() |
|
65 { |
|
66 return sSelf ? sSelf->YieldInternal() : false; |
|
67 } |
|
68 |
|
69 nsresult Shutdown(); |
|
70 already_AddRefed<nsIEventTarget> Target(); |
|
71 |
|
72 // Memory reporting |
|
73 size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
|
74 size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
|
75 |
|
76 private: |
|
77 static void ThreadFunc(void* aClosure); |
|
78 void ThreadFunc(); |
|
79 void LoopOneLevel(uint32_t aLevel); |
|
80 bool EventsPending(uint32_t aLastLevel = LAST_LEVEL); |
|
81 nsresult DispatchInternal(nsIRunnable* aRunnable, uint32_t aLevel); |
|
82 bool YieldInternal(); |
|
83 |
|
84 static CacheIOThread* sSelf; |
|
85 |
|
86 mozilla::Monitor mMonitor; |
|
87 PRThread* mThread; |
|
88 nsCOMPtr<nsIThread> mXPCOMThread; |
|
89 uint32_t mLowestLevelWaiting; |
|
90 uint32_t mCurrentlyExecutingLevel; |
|
91 nsTArray<nsRefPtr<nsIRunnable> > mEventQueue[LAST_LEVEL]; |
|
92 |
|
93 bool mHasXPCOMEvents; |
|
94 bool mRerunCurrentEvent; |
|
95 bool mShutdown; |
|
96 }; |
|
97 |
|
98 } // net |
|
99 } // mozilla |
|
100 |
|
101 #endif |