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 | |
---|
28 | #include <assert.h> |
---|
29 | #include <CoreServices/CoreServices.h> |
---|
30 | //#include <Multiprocessing.h> |
---|
31 | //#include <MultiprocessingInfo.h> |
---|
32 | |
---|
33 | namespace ZThread { |
---|
34 | |
---|
35 | class Runnable; |
---|
36 | |
---|
37 | /** |
---|
38 | * @class ThreadOps |
---|
39 | * @author Eric Crahen <http://www.code-foo.com> |
---|
40 | * @date <2003-07-16T23:26:01-0400> |
---|
41 | * @version 2.2.0 |
---|
42 | * |
---|
43 | * This class is an abstraction used to perform various operations on a |
---|
44 | * native POSIX thread. |
---|
45 | */ |
---|
46 | class ThreadOps { |
---|
47 | |
---|
48 | //! Keep track of the pthreads handle for the native thread |
---|
49 | MPQueueID _queue; |
---|
50 | MPTaskID _tid; |
---|
51 | |
---|
52 | ThreadOps(MPTaskID tid) : _queue(0), _tid(tid) { } |
---|
53 | |
---|
54 | static OSStatus _dispatch(void*); |
---|
55 | |
---|
56 | public: |
---|
57 | |
---|
58 | const static ThreadOps INVALID; |
---|
59 | |
---|
60 | /** |
---|
61 | * Create a new ThreadOps to manipulate a native thread. |
---|
62 | */ |
---|
63 | ThreadOps(); |
---|
64 | |
---|
65 | ThreadOps(const ThreadOps& ops) : _queue(0), _tid(ops._tid) {} |
---|
66 | |
---|
67 | ~ThreadOps() throw(); |
---|
68 | |
---|
69 | inline bool operator==(const ThreadOps& ops) const { |
---|
70 | return ops._tid == _tid; |
---|
71 | } |
---|
72 | |
---|
73 | const ThreadOps& operator=(const ThreadOps& ops) { |
---|
74 | |
---|
75 | assert(_queue == 0); |
---|
76 | _tid = ops._tid; |
---|
77 | |
---|
78 | return *this; |
---|
79 | |
---|
80 | } |
---|
81 | |
---|
82 | static ThreadOps self() { |
---|
83 | return ThreadOps(MPCurrentTaskID()); |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Activating an instance of ThreadOps will map it onto the currently |
---|
88 | * executing thread. |
---|
89 | */ |
---|
90 | static void activate(ThreadOps* ops) { |
---|
91 | |
---|
92 | assert(ops); |
---|
93 | assert(ops->_tid == 0); |
---|
94 | |
---|
95 | ops->_tid = MPCurrentTaskID(); |
---|
96 | |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Test if this object represents the currently executing |
---|
101 | * native thread. |
---|
102 | * |
---|
103 | * @return bool true if successful |
---|
104 | */ |
---|
105 | |
---|
106 | static bool isCurrent(ThreadOps* ops) { |
---|
107 | |
---|
108 | assert(ops); |
---|
109 | |
---|
110 | return MPCurrentTaskID() == ops->_tid; |
---|
111 | |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * Join a native thread. |
---|
116 | * |
---|
117 | * @return bool true if successful |
---|
118 | */ |
---|
119 | static bool join(ThreadOps*); |
---|
120 | |
---|
121 | /** |
---|
122 | * Force the current native thread to yield, letting the scheduler |
---|
123 | * give the CPU time to another thread. |
---|
124 | * |
---|
125 | * @return bool true if successful, false if the operation can't |
---|
126 | * be supported. |
---|
127 | */ |
---|
128 | static bool yield(); |
---|
129 | |
---|
130 | /** |
---|
131 | * Set the priority for the native thread if supported by the |
---|
132 | * system. |
---|
133 | * |
---|
134 | * @param PRIORITY requested priority |
---|
135 | * @return bool false if unsuccessful |
---|
136 | */ |
---|
137 | static bool setPriority(ThreadOps*, Priority); |
---|
138 | |
---|
139 | /** |
---|
140 | * Set the priority for the native thread if supported by the |
---|
141 | * system. |
---|
142 | * |
---|
143 | * @param Thread::PRIORITY& current priority |
---|
144 | * @return bool false if unsuccessful |
---|
145 | */ |
---|
146 | static bool getPriority(ThreadOps*, Priority&); |
---|
147 | |
---|
148 | protected: |
---|
149 | |
---|
150 | /** |
---|
151 | * Spawn a native thread. |
---|
152 | * |
---|
153 | * @param ThreadImpl* parent thread |
---|
154 | * @param ThreadImpl* child thread being started. |
---|
155 | * @param Runnable* task being executed. |
---|
156 | * |
---|
157 | * @return bool true if successful |
---|
158 | */ |
---|
159 | bool spawn(Runnable*); |
---|
160 | |
---|
161 | }; |
---|
162 | |
---|
163 | |
---|
164 | } |
---|
165 | |
---|
166 | #endif // __ZTTHREADOPS_H__ |
---|
167 | |
---|