1 | /* |
---|
2 | * Copyright (c) 2005, Eric Crahen |
---|
3 | * |
---|
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
5 | * of this software and associated documentation files (the "Software"), to deal |
---|
6 | * in the Software without restriction, including without limitation the rights |
---|
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
8 | * copies of the Software, and to permit persons to whom the Software is furnished |
---|
9 | * to do so, subject to the following conditions: |
---|
10 | * |
---|
11 | * The above copyright notice and this permission notice shall be included in all |
---|
12 | * copies or substantial portions of the Software. |
---|
13 | * |
---|
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
---|
18 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
---|
19 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | #ifndef __ZTTHREADEDEXECUTOR_H__ |
---|
24 | #define __ZTTHREADEDEXECUTOR_H__ |
---|
25 | |
---|
26 | #include "zthread/Executor.h" |
---|
27 | #include "zthread/CountedPtr.h" |
---|
28 | |
---|
29 | namespace ZThread { |
---|
30 | |
---|
31 | namespace { class ExecutorImpl; } |
---|
32 | |
---|
33 | /** |
---|
34 | * @class ThreadedExecutor |
---|
35 | * |
---|
36 | * @author Eric Crahen <http://www.code-foo.com> |
---|
37 | * @date <2003-07-16T22:39:13-0400> |
---|
38 | * @version 2.3.0 |
---|
39 | * |
---|
40 | * A ThreadedExecutor spawns a new thread to execute each task submitted. |
---|
41 | * A ThreadedExecutor supports the following optional operations, |
---|
42 | * |
---|
43 | * - <em>cancel</em>()ing a ThreadedExecutor will cause it to stop accepting |
---|
44 | * new tasks. |
---|
45 | * |
---|
46 | * - <em>interrupt</em>()ing a ThreadedExecutor will cause the any thread running |
---|
47 | * a task which was submitted prior to the invocation of this function to |
---|
48 | * be interrupted during the execution of that task. |
---|
49 | * |
---|
50 | * - <em>wait</em>()ing on a ThreadedExecutor will block the calling thread |
---|
51 | * until all tasks that were submitted prior to the invocation of this function |
---|
52 | * have completed. |
---|
53 | * |
---|
54 | * @see Executor. |
---|
55 | */ |
---|
56 | class ThreadedExecutor : public Executor { |
---|
57 | |
---|
58 | CountedPtr< ExecutorImpl > _impl; |
---|
59 | |
---|
60 | public: |
---|
61 | |
---|
62 | //! Create a new ThreadedExecutor |
---|
63 | ThreadedExecutor(); |
---|
64 | |
---|
65 | //! Destroy a ThreadedExecutor |
---|
66 | virtual ~ThreadedExecutor(); |
---|
67 | |
---|
68 | /** |
---|
69 | * Interrupting a ThreadedExecutor will cause an interrupt() to be sent |
---|
70 | * to every Task that has been submitted at the time this function is |
---|
71 | * called. Tasks that are submitted after this function is called will |
---|
72 | * not be interrupt()ed; unless this function is invoked again(). |
---|
73 | */ |
---|
74 | virtual void interrupt(); |
---|
75 | |
---|
76 | /** |
---|
77 | * Submit a task to this Executor. This will not block the current thread |
---|
78 | * for very long. A new thread will be created and the task will be run() |
---|
79 | * within the context of that new thread. |
---|
80 | * |
---|
81 | * @exception Cancellation_Exception thrown if this Executor has been canceled. |
---|
82 | * The Task being submitted will not be executed by this Executor. |
---|
83 | * |
---|
84 | * @see Executor::execute(const Task&) |
---|
85 | */ |
---|
86 | virtual void execute(const Task&); |
---|
87 | |
---|
88 | /** |
---|
89 | * @see Cancelable::cancel() |
---|
90 | */ |
---|
91 | virtual void cancel(); |
---|
92 | |
---|
93 | /** |
---|
94 | * @see Cancelable::isCanceled() |
---|
95 | */ |
---|
96 | virtual bool isCanceled(); |
---|
97 | |
---|
98 | /** |
---|
99 | * Waiting on a ThreadedExecutor will block the current thread until all |
---|
100 | * tasks submitted to the Executor up until the time this function was called |
---|
101 | * have completed. |
---|
102 | * |
---|
103 | * Tasks submitted after this function is called will not delay a thread that |
---|
104 | * was already waiting on the Executor any longer. In order to wait for |
---|
105 | * those tasks to complete as well, another wait() would be needed. |
---|
106 | * |
---|
107 | * @exception Interrupted_Exception thrown if the calling thread is interrupted |
---|
108 | * before the set of tasks for which it was waiting complete. |
---|
109 | * |
---|
110 | * @see Waitable::wait() |
---|
111 | */ |
---|
112 | virtual void wait(); |
---|
113 | |
---|
114 | /** |
---|
115 | * Operates the same as ThreadedExecutor::wait() but with a timeout. |
---|
116 | * |
---|
117 | * @param timeout maximum amount of time, in milliseconds, to wait for the |
---|
118 | * currently submitted set of Tasks to complete. |
---|
119 | * |
---|
120 | * @exception Interrupted_Exception thrown if the calling thread is interrupt()ed |
---|
121 | * while waiting for the current set of Tasks to complete. |
---|
122 | * |
---|
123 | * @return |
---|
124 | * - <em>true</em> if the set of tasks running at the time this function is invoked complete |
---|
125 | * before <i>timeout</i> milliseconds elapse. |
---|
126 | * - <em>false</em> othewise. |
---|
127 | * |
---|
128 | * @see Waitable::wait(unsigned long timeout) |
---|
129 | */ |
---|
130 | virtual bool wait(unsigned long timeout); |
---|
131 | |
---|
132 | }; /* ThreadedExecutor */ |
---|
133 | |
---|
134 | } // namespace ZThread |
---|
135 | |
---|
136 | #endif // __ZTTHREADEDEXECUTOR_H__ |
---|