1 | // |
---|
2 | // Activity.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/Activity.h#2 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Threading |
---|
8 | // Module: ActiveObjects |
---|
9 | // |
---|
10 | // Definition of the Activity template class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_Activity_INCLUDED |
---|
40 | #define Foundation_Activity_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/RunnableAdapter.h" |
---|
45 | #include "Poco/ThreadPool.h" |
---|
46 | #include "Poco/Event.h" |
---|
47 | #include "Poco/Mutex.h" |
---|
48 | |
---|
49 | |
---|
50 | namespace Poco { |
---|
51 | |
---|
52 | |
---|
53 | template <class C> |
---|
54 | class Activity: public Runnable |
---|
55 | /// This template class helps to implement active objects. |
---|
56 | /// An active object uses threads to decouple method |
---|
57 | /// execution from method invocation, or to perform tasks |
---|
58 | /// autonomously, without intervention of a caller. |
---|
59 | /// |
---|
60 | /// An activity is a (typically longer running) method |
---|
61 | /// that executes within its own task. Activities can |
---|
62 | /// be started automatically (upon object construction) |
---|
63 | /// or manually at a later time. Activities can also |
---|
64 | /// be stopped at any time. However, to make stopping |
---|
65 | /// an activity work, the method implementing the |
---|
66 | /// activity has to check periodically whether it |
---|
67 | /// has been requested to stop, and if so, return. |
---|
68 | /// Activities are stopped before the object they belong to is |
---|
69 | /// destroyed. Methods implementing activities cannot have arguments |
---|
70 | /// or return values. |
---|
71 | /// |
---|
72 | /// Activity objects are used as follows: |
---|
73 | /// |
---|
74 | /// class ActiveObject |
---|
75 | /// { |
---|
76 | /// public: |
---|
77 | /// ActiveObject(): |
---|
78 | /// _activity(this, &ActiveObject::runActivity) |
---|
79 | /// { |
---|
80 | /// ... |
---|
81 | /// } |
---|
82 | /// |
---|
83 | /// ... |
---|
84 | /// |
---|
85 | /// protected: |
---|
86 | /// void runActivity() |
---|
87 | /// { |
---|
88 | /// while (!_activity.isStopped()) |
---|
89 | /// { |
---|
90 | /// ... |
---|
91 | /// } |
---|
92 | /// } |
---|
93 | /// |
---|
94 | /// private: |
---|
95 | /// Activity<ActiveObject> _activity; |
---|
96 | /// }; |
---|
97 | { |
---|
98 | public: |
---|
99 | typedef RunnableAdapter<C> RunnableAdapterType; |
---|
100 | typedef typename RunnableAdapterType::Callback Callback; |
---|
101 | |
---|
102 | Activity(C* pOwner, Callback method): |
---|
103 | _pOwner(pOwner), |
---|
104 | _runnable(*pOwner, method), |
---|
105 | _stopped(true), |
---|
106 | _running(false), |
---|
107 | _done(false) |
---|
108 | /// Creates the activity. Call start() to |
---|
109 | /// start it. |
---|
110 | { |
---|
111 | poco_check_ptr (pOwner); |
---|
112 | } |
---|
113 | |
---|
114 | ~Activity() |
---|
115 | /// Stops and destroys the activity. |
---|
116 | { |
---|
117 | stop(); |
---|
118 | wait(); |
---|
119 | } |
---|
120 | |
---|
121 | void start() |
---|
122 | /// Starts the activity by acquiring a |
---|
123 | /// thread for it from the default thread pool. |
---|
124 | { |
---|
125 | FastMutex::ScopedLock lock(_mutex); |
---|
126 | |
---|
127 | if (!_running) |
---|
128 | { |
---|
129 | _done.reset(); |
---|
130 | _stopped = false; |
---|
131 | _running = true; |
---|
132 | try |
---|
133 | { |
---|
134 | ThreadPool::defaultPool().start(*this); |
---|
135 | } |
---|
136 | catch (...) |
---|
137 | { |
---|
138 | _running = false; |
---|
139 | throw; |
---|
140 | } |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | void stop() |
---|
145 | /// Requests to stop the activity. |
---|
146 | { |
---|
147 | FastMutex::ScopedLock lock(_mutex); |
---|
148 | |
---|
149 | _stopped = true; |
---|
150 | } |
---|
151 | |
---|
152 | void wait() |
---|
153 | /// Waits for the activity to complete. |
---|
154 | { |
---|
155 | if (_running) |
---|
156 | { |
---|
157 | _done.wait(); |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | void wait(long milliseconds) |
---|
162 | /// Waits the given interval for the activity to complete. |
---|
163 | /// An TimeoutException is thrown if the activity does not |
---|
164 | /// complete within the given interval. |
---|
165 | { |
---|
166 | if (_running) |
---|
167 | { |
---|
168 | _done.wait(milliseconds); |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | bool isStopped() const |
---|
173 | /// Returns true if the activity has been requested to stop. |
---|
174 | { |
---|
175 | return _stopped; |
---|
176 | } |
---|
177 | |
---|
178 | bool isRunning() const |
---|
179 | /// Returns true if the activity is running. |
---|
180 | { |
---|
181 | return _running; |
---|
182 | } |
---|
183 | |
---|
184 | protected: |
---|
185 | void run() |
---|
186 | { |
---|
187 | try |
---|
188 | { |
---|
189 | _runnable.run(); |
---|
190 | } |
---|
191 | catch (...) |
---|
192 | { |
---|
193 | _running = false; |
---|
194 | _done.set(); |
---|
195 | throw; |
---|
196 | } |
---|
197 | _running = false; |
---|
198 | _done.set(); |
---|
199 | } |
---|
200 | |
---|
201 | private: |
---|
202 | Activity(); |
---|
203 | Activity(const Activity&); |
---|
204 | Activity& operator = (const Activity&); |
---|
205 | |
---|
206 | C* _pOwner; |
---|
207 | RunnableAdapterType _runnable; |
---|
208 | volatile bool _stopped; |
---|
209 | volatile bool _running; |
---|
210 | Event _done; |
---|
211 | FastMutex _mutex; |
---|
212 | }; |
---|
213 | |
---|
214 | |
---|
215 | } // namespace Poco |
---|
216 | |
---|
217 | |
---|
218 | #endif // Foundation_Activity_INCLUDED |
---|