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 __ZTTHREADQUEUE_H__ |
---|
24 | #define __ZTTHREADQUEUE_H__ |
---|
25 | |
---|
26 | #include "zthread/Singleton.h" |
---|
27 | #include "zthread/Guard.h" |
---|
28 | #include "FastLock.h" |
---|
29 | |
---|
30 | |
---|
31 | namespace ZThread { |
---|
32 | |
---|
33 | class ThreadImpl; |
---|
34 | |
---|
35 | /** |
---|
36 | * @class ThreadQueue |
---|
37 | * @version 2.3.0 |
---|
38 | * @author Eric Crahen <http://www.code-foo.com> |
---|
39 | * @date <2003-07-27T20:52:05-0400> |
---|
40 | * |
---|
41 | * A ThreadQueue accumulates references to user and reference threads. |
---|
42 | * These are threads that are running outside the scope of the Thread |
---|
43 | * object that created them. ZThreads doesn't have a central manager for |
---|
44 | * all threads (partly why I renamed the ThreadManager to someting more |
---|
45 | * appropriate). Instead, ZThreads will discover threads it did not create |
---|
46 | * and create a reference thread that allows ZThreads to interact with it. |
---|
47 | * Non user threads that are created by the user never have to touch the |
---|
48 | * ThreadQueue. |
---|
49 | */ |
---|
50 | class ThreadQueue : public Singleton<ThreadQueue, StaticInstantiation> { |
---|
51 | |
---|
52 | typedef std::deque<ThreadImpl*> ThreadList; |
---|
53 | typedef std::deque<Task> TaskList; |
---|
54 | |
---|
55 | //! Managed thread lists |
---|
56 | ThreadList _pendingThreads; |
---|
57 | ThreadList _referenceThreads; |
---|
58 | ThreadList _userThreads; |
---|
59 | |
---|
60 | //! Shutdown handlers |
---|
61 | TaskList _shutdownTasks; |
---|
62 | |
---|
63 | //! Serilize access to the thread list |
---|
64 | FastLock _lock; |
---|
65 | |
---|
66 | //! Reference thread waiting to cleanup any user & reference threads |
---|
67 | ThreadImpl* _waiter; |
---|
68 | |
---|
69 | public: |
---|
70 | |
---|
71 | ThreadQueue(); |
---|
72 | |
---|
73 | /** |
---|
74 | * The thread destroys a ThreadQueue will be a reference thread, |
---|
75 | * probably the main thread; but it could be another thread that |
---|
76 | * started and loaded the library. |
---|
77 | */ |
---|
78 | ~ThreadQueue(); |
---|
79 | |
---|
80 | /** |
---|
81 | * Insert a user-thread into the queue. User-threads are inserted as they |
---|
82 | * begin thier task. Once that task completes, user-threads are automatically |
---|
83 | * transitioned to pending-threads via <i>insertPendingThread()</i>. |
---|
84 | * |
---|
85 | * User-threads are known to be executing thier tasks and will be cancel()ed |
---|
86 | * as the ThreadQueue is destroyed when main() goes out of scope. This sends |
---|
87 | * a request to the task to complete soon. Once the task exits, the thread is |
---|
88 | * transitioned to pending-thread status. |
---|
89 | */ |
---|
90 | void insertUserThread(ThreadImpl*); |
---|
91 | |
---|
92 | /** |
---|
93 | * Insert a pending-thread into the queue. |
---|
94 | * |
---|
95 | * Pending-threads are known to have completed thier tasks and thier |
---|
96 | * resources are reclaimed (lazily) as more threads are started or as the |
---|
97 | * ThreadQueue is destroyed. |
---|
98 | */ |
---|
99 | void insertPendingThread(ThreadImpl*); |
---|
100 | |
---|
101 | |
---|
102 | /** |
---|
103 | * Insert reference thread. Reference threads are not removed until |
---|
104 | * the ThreadQueue goes out of scope. |
---|
105 | */ |
---|
106 | void insertReferenceThread(ThreadImpl*); |
---|
107 | |
---|
108 | /** |
---|
109 | * Insert a task to be run before threads are joined. |
---|
110 | * Any items inserted after the ThreadQueue desctructor has begun to |
---|
111 | * execute will be run() immediately. |
---|
112 | */ |
---|
113 | void insertShutdownTask(Task&); |
---|
114 | |
---|
115 | /** |
---|
116 | * Remove an existing shutdown task. |
---|
117 | */ |
---|
118 | bool removeShutdownTask(const Task&); |
---|
119 | |
---|
120 | private: |
---|
121 | |
---|
122 | void pollPendingThreads(); |
---|
123 | |
---|
124 | void pollUserThreads(); |
---|
125 | |
---|
126 | void pollReferenceThreads(); |
---|
127 | |
---|
128 | }; |
---|
129 | |
---|
130 | |
---|
131 | } // namespace ZThread |
---|
132 | |
---|
133 | |
---|
134 | #endif // __ZTTHREADQUEUE_H__ |
---|