1 | // |
---|
2 | // ActiveDispatcher.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/ActiveDispatcher.cpp#2 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Threading |
---|
8 | // Module: ActiveObjects |
---|
9 | // |
---|
10 | // Copyright (c) 2006-2007, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include "Poco/ActiveDispatcher.h" |
---|
38 | #include "Poco/Notification.h" |
---|
39 | #include "Poco/AutoPtr.h" |
---|
40 | |
---|
41 | |
---|
42 | namespace Poco { |
---|
43 | |
---|
44 | |
---|
45 | namespace |
---|
46 | { |
---|
47 | class MethodNotification: public Notification |
---|
48 | { |
---|
49 | public: |
---|
50 | MethodNotification(ActiveRunnableBase::Ptr pRunnable): |
---|
51 | _pRunnable(pRunnable) |
---|
52 | { |
---|
53 | } |
---|
54 | |
---|
55 | ActiveRunnableBase::Ptr runnable() const |
---|
56 | { |
---|
57 | return _pRunnable; |
---|
58 | } |
---|
59 | |
---|
60 | private: |
---|
61 | ActiveRunnableBase::Ptr _pRunnable; |
---|
62 | }; |
---|
63 | |
---|
64 | class StopNotification: public Notification |
---|
65 | { |
---|
66 | }; |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | ActiveDispatcher::ActiveDispatcher() |
---|
71 | { |
---|
72 | _thread.start(*this); |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | ActiveDispatcher::ActiveDispatcher(Thread::Priority prio) |
---|
77 | { |
---|
78 | _thread.setPriority(prio); |
---|
79 | _thread.start(*this); |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | ActiveDispatcher::~ActiveDispatcher() |
---|
84 | { |
---|
85 | try |
---|
86 | { |
---|
87 | stop(); |
---|
88 | } |
---|
89 | catch (...) |
---|
90 | { |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | void ActiveDispatcher::start(ActiveRunnableBase::Ptr pRunnable) |
---|
96 | { |
---|
97 | poco_check_ptr (pRunnable); |
---|
98 | |
---|
99 | _queue.enqueueNotification(new MethodNotification(pRunnable)); |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | void ActiveDispatcher::cancel() |
---|
104 | { |
---|
105 | _queue.clear(); |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | void ActiveDispatcher::run() |
---|
110 | { |
---|
111 | AutoPtr<Notification> pNf = _queue.waitDequeueNotification(); |
---|
112 | while (pNf && !dynamic_cast<StopNotification*>(pNf.get())) |
---|
113 | { |
---|
114 | MethodNotification* pMethodNf = dynamic_cast<MethodNotification*>(pNf.get()); |
---|
115 | poco_check_ptr (pMethodNf); |
---|
116 | ActiveRunnableBase::Ptr pRunnable = pMethodNf->runnable(); |
---|
117 | pRunnable->duplicate(); // run will release |
---|
118 | pRunnable->run(); |
---|
119 | pNf = _queue.waitDequeueNotification(); |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | void ActiveDispatcher::stop() |
---|
125 | { |
---|
126 | _queue.clear(); |
---|
127 | _queue.wakeUpAll(); |
---|
128 | _queue.enqueueNotification(new StopNotification); |
---|
129 | _thread.join(); |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | } // namespace Poco |
---|