[80] | 1 | // |
---|
| 2 | // ActiveDispatcher.h |
---|
| 3 | // |
---|
| 4 | // $Id: //poco/1.3/Foundation/include/Poco/ActiveDispatcher.h#2 $ |
---|
| 5 | // |
---|
| 6 | // Library: Foundation |
---|
| 7 | // Package: Threading |
---|
| 8 | // Module: ActiveObjects |
---|
| 9 | // |
---|
| 10 | // Definition of the ActiveDispatcher class. |
---|
| 11 | // |
---|
| 12 | // Copyright (c) 2006-2007, 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_ActiveDispatcher_INCLUDED |
---|
| 40 | #define Foundation_ActiveDispatcher_INCLUDED |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | #include "Poco/Foundation.h" |
---|
| 44 | #include "Poco/Runnable.h" |
---|
| 45 | #include "Poco/Thread.h" |
---|
| 46 | #include "Poco/ActiveStarter.h" |
---|
| 47 | #include "Poco/ActiveRunnable.h" |
---|
| 48 | #include "Poco/NotificationQueue.h" |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | namespace Poco { |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | class Foundation_API ActiveDispatcher: protected Runnable |
---|
| 55 | /// This class is used to implement an active object |
---|
| 56 | /// with strictly serialized method execution. |
---|
| 57 | /// |
---|
| 58 | /// An active object, with is an ordinary object |
---|
| 59 | /// containing ActiveMethod members, executes all |
---|
| 60 | /// active methods in their own thread. |
---|
| 61 | /// This behavior does not fit the "classic" |
---|
| 62 | /// definition of an active object, which serializes |
---|
| 63 | /// the execution of active methods (in other words, |
---|
| 64 | /// only one active method can be running at any given |
---|
| 65 | /// time). |
---|
| 66 | /// |
---|
| 67 | /// Using this class as a base class, the serializing |
---|
| 68 | /// behavior for active objects can be implemented. |
---|
| 69 | /// |
---|
| 70 | /// The following example shows how this is done: |
---|
| 71 | /// |
---|
| 72 | /// class ActiveObject: public ActiveDispatcher |
---|
| 73 | /// { |
---|
| 74 | /// public: |
---|
| 75 | /// ActiveObject(): |
---|
| 76 | /// exampleActiveMethod(this, &ActiveObject::exampleActiveMethodImpl) |
---|
| 77 | /// { |
---|
| 78 | /// } |
---|
| 79 | /// |
---|
| 80 | /// ActiveMethod<std::string, std::string, ActiveObject, ActiveStarter<ActiveDispatcher> > exampleActiveMethod; |
---|
| 81 | /// |
---|
| 82 | /// protected: |
---|
| 83 | /// std::string exampleActiveMethodImpl(const std::string& arg) |
---|
| 84 | /// { |
---|
| 85 | /// ... |
---|
| 86 | /// } |
---|
| 87 | /// }; |
---|
| 88 | /// |
---|
| 89 | /// The only things different from the example in |
---|
| 90 | /// ActiveMethod is that the ActiveObject in this case |
---|
| 91 | /// inherits from ActiveDispatcher, and that the ActiveMethod |
---|
| 92 | /// template for exampleActiveMethod has an additional parameter, |
---|
| 93 | /// specifying the specialized ActiveStarter for ActiveDispatcher. |
---|
| 94 | { |
---|
| 95 | public: |
---|
| 96 | ActiveDispatcher(); |
---|
| 97 | /// Creates the ActiveDispatcher. |
---|
| 98 | |
---|
| 99 | ActiveDispatcher(Thread::Priority prio); |
---|
| 100 | /// Creates the ActiveDispatcher and sets |
---|
| 101 | /// the priority of its thread. |
---|
| 102 | |
---|
| 103 | virtual ~ActiveDispatcher(); |
---|
| 104 | /// Destroys the ActiveDispatcher. |
---|
| 105 | |
---|
| 106 | void start(ActiveRunnableBase::Ptr pRunnable); |
---|
| 107 | /// Adds the Runnable to the dispatch queue. |
---|
| 108 | |
---|
| 109 | void cancel(); |
---|
| 110 | /// Cancels all queued methods. |
---|
| 111 | |
---|
| 112 | protected: |
---|
| 113 | void run(); |
---|
| 114 | void stop(); |
---|
| 115 | |
---|
| 116 | private: |
---|
| 117 | Thread _thread; |
---|
| 118 | NotificationQueue _queue; |
---|
| 119 | }; |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | template <> |
---|
| 123 | class ActiveStarter<ActiveDispatcher> |
---|
| 124 | /// A specialization of ActiveStarter |
---|
| 125 | /// for ActiveDispatcher. |
---|
| 126 | { |
---|
| 127 | public: |
---|
| 128 | static void start(ActiveDispatcher* pOwner, ActiveRunnableBase::Ptr pRunnable) |
---|
| 129 | { |
---|
| 130 | pOwner->start(pRunnable); |
---|
| 131 | } |
---|
| 132 | }; |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | } // namespace Poco |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | #endif // Foundation_ActiveDispatcher_INCLUDED |
---|