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 __ZTTHREADOPS_H__ |
---|
24 | #define __ZTTHREADOPS_H__ |
---|
25 | |
---|
26 | #include "zthread/Priority.h" |
---|
27 | #include <windows.h> |
---|
28 | #include <assert.h> |
---|
29 | |
---|
30 | namespace ZThread { |
---|
31 | |
---|
32 | class Runnable; |
---|
33 | |
---|
34 | /** |
---|
35 | * @class ThreadOps |
---|
36 | * @author Eric Crahen <http://www.code-foo.com> |
---|
37 | * @date <2003-07-16T23:33:59-0400> |
---|
38 | * @version 2.2.8 |
---|
39 | * |
---|
40 | * This class is an abstraction used to perform various operations on a |
---|
41 | * native WIN32 thread. |
---|
42 | */ |
---|
43 | class ThreadOps { |
---|
44 | |
---|
45 | //! Dispatch function for native thread |
---|
46 | static unsigned int __stdcall _dispatch(void*); |
---|
47 | |
---|
48 | //! TID while the thread is executing. |
---|
49 | HANDLE _hThread; |
---|
50 | DWORD _tid; |
---|
51 | |
---|
52 | ThreadOps(DWORD tid) : _tid(tid) { } |
---|
53 | |
---|
54 | public: |
---|
55 | |
---|
56 | const static ThreadOps INVALID; |
---|
57 | |
---|
58 | /** |
---|
59 | * Create a new ThreadOps to represent a native thread. |
---|
60 | */ |
---|
61 | ThreadOps() : _tid(0), _hThread(NULL) {} |
---|
62 | |
---|
63 | |
---|
64 | inline bool operator==(const ThreadOps& ops) const { |
---|
65 | return _tid == ops._tid; |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | static ThreadOps self() { |
---|
70 | return ThreadOps(::GetCurrentThreadId()); |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * Update the native tid for this thread so it matches the current |
---|
75 | * thread. |
---|
76 | */ |
---|
77 | static void activate(ThreadOps* ops) { |
---|
78 | |
---|
79 | assert(ops); |
---|
80 | assert(ops->_tid == 0); |
---|
81 | |
---|
82 | ops->_tid = ::GetCurrentThreadId(); |
---|
83 | |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Test if this object representative of the currently executing |
---|
88 | * native thread. |
---|
89 | * |
---|
90 | * @return bool true if successful |
---|
91 | */ |
---|
92 | static bool isCurrent(ThreadOps* ops) { |
---|
93 | |
---|
94 | assert(ops); |
---|
95 | |
---|
96 | return ops->_tid == ::GetCurrentThreadId(); |
---|
97 | |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * Join a native thread. |
---|
102 | * |
---|
103 | * @return bool true if successful |
---|
104 | */ |
---|
105 | static bool join(ThreadOps*); |
---|
106 | |
---|
107 | /** |
---|
108 | * Force the current native thread to yield, letting the scheduler |
---|
109 | * give the CPU time to another thread. |
---|
110 | * |
---|
111 | * @return bool true if successful |
---|
112 | */ |
---|
113 | static bool yield(); |
---|
114 | |
---|
115 | /** |
---|
116 | * Set the priority for the native thread if supported by the |
---|
117 | * system. |
---|
118 | * |
---|
119 | * @param PRIORITY requested priority |
---|
120 | * @return bool false if unsuccessful |
---|
121 | */ |
---|
122 | static bool setPriority(ThreadOps*, Priority); |
---|
123 | |
---|
124 | /** |
---|
125 | * Set the priority for the native thread if supported by the |
---|
126 | * system. |
---|
127 | * |
---|
128 | * @param Thread::PRIORITY& current priority |
---|
129 | * @return bool false if unsuccessful |
---|
130 | */ |
---|
131 | static bool getPriority(ThreadOps*, Priority&); |
---|
132 | |
---|
133 | protected: |
---|
134 | |
---|
135 | /** |
---|
136 | * Spawn a native thread. |
---|
137 | * |
---|
138 | * @param ThreadImpl* parent thread |
---|
139 | * @param ThreadImpl* child thread being started. |
---|
140 | * @param Runnable* task being executed. |
---|
141 | * |
---|
142 | * @return bool true if successful |
---|
143 | */ |
---|
144 | bool spawn(Runnable*); |
---|
145 | |
---|
146 | |
---|
147 | }; |
---|
148 | |
---|
149 | |
---|
150 | } |
---|
151 | |
---|
152 | #endif |
---|