diff options
author | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
commit | 38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch) | |
tree | adca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llcommon/llworkerthread.h | |
parent | README.txt (diff) | |
download | meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2 meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz |
Second Life viewer sources 1.13.2.12
Diffstat (limited to 'linden/indra/llcommon/llworkerthread.h')
-rw-r--r-- | linden/indra/llcommon/llworkerthread.h | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/linden/indra/llcommon/llworkerthread.h b/linden/indra/llcommon/llworkerthread.h new file mode 100644 index 0000000..f01e67b --- /dev/null +++ b/linden/indra/llcommon/llworkerthread.h | |||
@@ -0,0 +1,187 @@ | |||
1 | /** | ||
2 | * @file llworkerthread.h | ||
3 | * | ||
4 | * Copyright (c) 2004-2007, Linden Research, Inc. | ||
5 | * | ||
6 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
7 | * to you under the terms of the GNU General Public License, version 2.0 | ||
8 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
9 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
10 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
11 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
12 | * | ||
13 | * There are special exceptions to the terms and conditions of the GPL as | ||
14 | * it is applied to this Source Code. View the full text of the exception | ||
15 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
16 | * online at http://secondlife.com/developers/opensource/flossexception | ||
17 | * | ||
18 | * By copying, modifying or distributing this software, you acknowledge | ||
19 | * that you have read and understood your obligations described above, | ||
20 | * and agree to abide by those obligations. | ||
21 | * | ||
22 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
23 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
24 | * COMPLETENESS OR PERFORMANCE. | ||
25 | */ | ||
26 | |||
27 | #ifndef LL_LLWORKERTHREAD_H | ||
28 | #define LL_LLWORKERTHREAD_H | ||
29 | |||
30 | #include <queue> | ||
31 | #include <string> | ||
32 | #include <map> | ||
33 | #include <set> | ||
34 | |||
35 | #include "llqueuedthread.h" | ||
36 | |||
37 | #define USE_FRAME_CALLBACK_MANAGER 0 | ||
38 | |||
39 | //============================================================================ | ||
40 | |||
41 | class LLWorkerClass; | ||
42 | |||
43 | //============================================================================ | ||
44 | // Note: ~LLWorkerThread is O(N) N=# of worker threads, assumed to be small | ||
45 | // It is assumed that LLWorkerThreads are rarely created/destroyed. | ||
46 | |||
47 | class LLWorkerThread : public LLQueuedThread | ||
48 | { | ||
49 | public: | ||
50 | class Request : public LLQueuedThread::QueuedRequest | ||
51 | { | ||
52 | protected: | ||
53 | ~Request() {}; // use deleteRequest() | ||
54 | |||
55 | public: | ||
56 | Request(handle_t handle, U32 priority, LLWorkerClass* workerclass, S32 param); | ||
57 | |||
58 | S32 getParam() | ||
59 | { | ||
60 | return mParam; | ||
61 | } | ||
62 | LLWorkerClass* getWorkerClass() | ||
63 | { | ||
64 | return mWorkerClass; | ||
65 | } | ||
66 | |||
67 | /*virtual*/ void deleteRequest(); | ||
68 | |||
69 | private: | ||
70 | LLWorkerClass* mWorkerClass; | ||
71 | S32 mParam; | ||
72 | }; | ||
73 | |||
74 | public: | ||
75 | LLWorkerThread(bool threaded = true, bool runalways = true); | ||
76 | ~LLWorkerThread(); | ||
77 | |||
78 | protected: | ||
79 | /*virtual*/ bool processRequest(QueuedRequest* req); | ||
80 | |||
81 | public: | ||
82 | handle_t add(LLWorkerClass* workerclass, S32 param, U32 priority = PRIORITY_NORMAL); | ||
83 | |||
84 | static void initClass(bool local_is_threaded = true, bool local_run_always = true); // Setup sLocal | ||
85 | static S32 updateClass(U32 ms_elapsed); | ||
86 | static S32 getAllPending(); | ||
87 | static void pauseAll(); | ||
88 | static void waitOnAllPending(); | ||
89 | static void cleanupClass(); // Delete sLocal | ||
90 | |||
91 | public: | ||
92 | static LLWorkerThread* sLocal; // Default worker thread | ||
93 | static std::set<LLWorkerThread*> sThreadList; // array of threads (includes sLocal) | ||
94 | }; | ||
95 | |||
96 | //============================================================================ | ||
97 | |||
98 | // This is a base class which any class with worker functions should derive from. | ||
99 | // Example Usage: | ||
100 | // LLMyWorkerClass* foo = new LLMyWorkerClass(); | ||
101 | // foo->fetchData(); // calls addWork() | ||
102 | // while(1) // main loop | ||
103 | // { | ||
104 | // if (foo->hasData()) // calls checkWork() | ||
105 | // foo->processData(); | ||
106 | // } | ||
107 | // | ||
108 | // WorkerClasses only have one set of work functions. If they need to do multiple | ||
109 | // background tasks, use 'param' to switch amnong them. | ||
110 | // Only one background task can be active at a time (per instance). | ||
111 | // i.e. don't call addWork() if haveWork() returns true | ||
112 | |||
113 | class LLWorkerClass | ||
114 | { | ||
115 | public: | ||
116 | typedef LLWorkerThread::handle_t handle_t; | ||
117 | enum FLAGS | ||
118 | { | ||
119 | WCF_WORKING = 0x01, | ||
120 | WCF_ABORT_REQUESTED = 0x80 | ||
121 | }; | ||
122 | |||
123 | public: | ||
124 | LLWorkerClass(LLWorkerThread* workerthread, const std::string& name); | ||
125 | virtual ~LLWorkerClass(); | ||
126 | |||
127 | // pure virtual, called from WORKER THREAD, returns TRUE if done | ||
128 | virtual bool doWork(S32 param)=0; // Called from LLWorkerThread::processRequest() | ||
129 | |||
130 | // called from WORKER THREAD | ||
131 | void setWorking(bool working) { working ? setFlags(WCF_WORKING) : clearFlags(WCF_WORKING); } | ||
132 | |||
133 | bool isWorking() { return getFlags(WCF_WORKING); } | ||
134 | bool wasAborted() { return getFlags(WCF_ABORT_REQUESTED); } | ||
135 | |||
136 | const std::string& getName() const { return mWorkerClassName; } | ||
137 | |||
138 | protected: | ||
139 | // Call from doWork only to avoid eating up cpu time. | ||
140 | // Returns true if work has been aborted | ||
141 | // yields the current thread and calls mWorkerThread->checkPause() | ||
142 | bool yield(); | ||
143 | |||
144 | void setWorkerThread(LLWorkerThread* workerthread); | ||
145 | |||
146 | // addWork(): calls startWork, adds doWork() to queue | ||
147 | void addWork(S32 param, U32 priority = LLWorkerThread::PRIORITY_NORMAL); | ||
148 | |||
149 | // abortWork(): requests that work be aborted | ||
150 | void abortWork(); | ||
151 | |||
152 | // checkWork(): if doWork is complete or aborted, call endWork() and return true | ||
153 | bool checkWork(); | ||
154 | |||
155 | // haveWork(): return true if mWorkHandle != null | ||
156 | bool haveWork() { return mWorkHandle != LLWorkerThread::nullHandle(); } | ||
157 | |||
158 | // killWork(): aborts work and waits for the abort to process | ||
159 | void killWork(); | ||
160 | |||
161 | // setPriority(): changes the priority of a request | ||
162 | void setPriority(U32 priority); | ||
163 | |||
164 | private: | ||
165 | void setFlags(U32 flags) { mWorkFlags = mWorkFlags | flags; } | ||
166 | void clearFlags(U32 flags) { mWorkFlags = mWorkFlags & ~flags; } | ||
167 | U32 getFlags() { return mWorkFlags; } | ||
168 | bool getFlags(U32 flags) { return mWorkFlags & flags ? true : false; } | ||
169 | |||
170 | private: | ||
171 | // pure virtuals | ||
172 | virtual void startWork(S32 param)=0; // called from addWork() (MAIN THREAD) | ||
173 | virtual void endWork(S32 param, bool aborted)=0; // called from doWork() (MAIN THREAD) | ||
174 | |||
175 | protected: | ||
176 | LLWorkerThread* mWorkerThread; | ||
177 | std::string mWorkerClassName; | ||
178 | handle_t mWorkHandle; | ||
179 | |||
180 | private: | ||
181 | LLAtomicU32 mWorkFlags; | ||
182 | }; | ||
183 | |||
184 | //============================================================================ | ||
185 | |||
186 | |||
187 | #endif // LL_LLWORKERTHREAD_H | ||