1 | // |
---|
2 | // Thread_POSIX.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/Thread_POSIX.h#10 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Threading |
---|
8 | // Module: Thread |
---|
9 | // |
---|
10 | // Definition of the ThreadImpl class for POSIX Threads. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-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_Thread_POSIX_INCLUDED |
---|
40 | #define Foundation_Thread_POSIX_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/Runnable.h" |
---|
45 | #include "Poco/SignalHandler.h" |
---|
46 | #include "Poco/Event.h" |
---|
47 | #include "Poco/RefCountedObject.h" |
---|
48 | #include "Poco/AutoPtr.h" |
---|
49 | #include <pthread.h> |
---|
50 | // must be limits.h (not <climits>) for PTHREAD_STACK_MIN on Solaris |
---|
51 | #include <limits.h> |
---|
52 | #if !defined(POCO_NO_SYS_SELECT_H) |
---|
53 | #include <sys/select.h> |
---|
54 | #endif |
---|
55 | #include <errno.h> |
---|
56 | |
---|
57 | |
---|
58 | namespace Poco { |
---|
59 | |
---|
60 | |
---|
61 | class Foundation_API ThreadImpl |
---|
62 | { |
---|
63 | public: |
---|
64 | typedef pthread_t TIDImpl; |
---|
65 | typedef void (*Callable)(void*); |
---|
66 | |
---|
67 | enum Priority |
---|
68 | { |
---|
69 | PRIO_LOWEST_IMPL, |
---|
70 | PRIO_LOW_IMPL, |
---|
71 | PRIO_NORMAL_IMPL, |
---|
72 | PRIO_HIGH_IMPL, |
---|
73 | PRIO_HIGHEST_IMPL |
---|
74 | }; |
---|
75 | |
---|
76 | struct CallbackData: public RefCountedObject |
---|
77 | { |
---|
78 | CallbackData(): callback(0), pData(0) |
---|
79 | { |
---|
80 | } |
---|
81 | |
---|
82 | Callable callback; |
---|
83 | void* pData; |
---|
84 | }; |
---|
85 | |
---|
86 | ThreadImpl(); |
---|
87 | ~ThreadImpl(); |
---|
88 | |
---|
89 | TIDImpl tidImpl() const; |
---|
90 | void setPriorityImpl(int prio); |
---|
91 | int getPriorityImpl() const; |
---|
92 | void setOSPriorityImpl(int prio); |
---|
93 | int getOSPriorityImpl() const; |
---|
94 | static int getMinOSPriorityImpl(); |
---|
95 | static int getMaxOSPriorityImpl(); |
---|
96 | void setStackSizeImpl(int size); |
---|
97 | int getStackSizeImpl() const; |
---|
98 | void startImpl(Runnable& target); |
---|
99 | void startImpl(Callable target, void* pData = 0); |
---|
100 | |
---|
101 | void joinImpl(); |
---|
102 | bool joinImpl(long milliseconds); |
---|
103 | bool isRunningImpl() const; |
---|
104 | static void sleepImpl(long milliseconds); |
---|
105 | static void yieldImpl(); |
---|
106 | static ThreadImpl* currentImpl(); |
---|
107 | static TIDImpl currentTidImpl(); |
---|
108 | |
---|
109 | protected: |
---|
110 | static void* runnableEntry(void* pThread); |
---|
111 | static void* callableEntry(void* pThread); |
---|
112 | static int mapPrio(int prio); |
---|
113 | static int reverseMapPrio(int osPrio); |
---|
114 | |
---|
115 | private: |
---|
116 | class CurrentThreadHolder |
---|
117 | { |
---|
118 | public: |
---|
119 | CurrentThreadHolder() |
---|
120 | { |
---|
121 | if (pthread_key_create(&_key, NULL)) |
---|
122 | throw SystemException("cannot allocate thread context key"); |
---|
123 | } |
---|
124 | ~CurrentThreadHolder() |
---|
125 | { |
---|
126 | pthread_key_delete(_key); |
---|
127 | } |
---|
128 | ThreadImpl* get() const |
---|
129 | { |
---|
130 | return reinterpret_cast<ThreadImpl*>(pthread_getspecific(_key)); |
---|
131 | } |
---|
132 | void set(ThreadImpl* pThread) |
---|
133 | { |
---|
134 | pthread_setspecific(_key, pThread); |
---|
135 | } |
---|
136 | |
---|
137 | private: |
---|
138 | pthread_key_t _key; |
---|
139 | }; |
---|
140 | |
---|
141 | struct ThreadData: public RefCountedObject |
---|
142 | { |
---|
143 | ThreadData(): |
---|
144 | pRunnableTarget(0), |
---|
145 | pCallbackTarget(0), |
---|
146 | thread(0), |
---|
147 | prio(PRIO_NORMAL_IMPL), |
---|
148 | done(false), |
---|
149 | stackSize(POCO_THREAD_STACK_SIZE) |
---|
150 | { |
---|
151 | } |
---|
152 | |
---|
153 | Runnable* pRunnableTarget; |
---|
154 | AutoPtr<CallbackData> pCallbackTarget; |
---|
155 | pthread_t thread; |
---|
156 | int prio; |
---|
157 | int osPrio; |
---|
158 | Event done; |
---|
159 | std::size_t stackSize; |
---|
160 | }; |
---|
161 | |
---|
162 | AutoPtr<ThreadData> _pData; |
---|
163 | |
---|
164 | static CurrentThreadHolder _currentThreadHolder; |
---|
165 | |
---|
166 | #if defined(POCO_OS_FAMILY_UNIX) |
---|
167 | SignalHandler::JumpBufferVec _jumpBufferVec; |
---|
168 | friend class SignalHandler; |
---|
169 | #endif |
---|
170 | }; |
---|
171 | |
---|
172 | |
---|
173 | // |
---|
174 | // inlines |
---|
175 | // |
---|
176 | inline int ThreadImpl::getPriorityImpl() const |
---|
177 | { |
---|
178 | return _pData->prio; |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | inline int ThreadImpl::getOSPriorityImpl() const |
---|
183 | { |
---|
184 | return _pData->osPrio; |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | inline bool ThreadImpl::isRunningImpl() const |
---|
189 | { |
---|
190 | return _pData->pRunnableTarget != 0 || |
---|
191 | (_pData->pCallbackTarget.get() != 0 && _pData->pCallbackTarget->callback != 0); |
---|
192 | } |
---|
193 | |
---|
194 | |
---|
195 | inline void ThreadImpl::yieldImpl() |
---|
196 | { |
---|
197 | sched_yield(); |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | inline int ThreadImpl::getStackSizeImpl() const |
---|
202 | { |
---|
203 | return _pData->stackSize; |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | inline ThreadImpl::TIDImpl ThreadImpl::tidImpl() const |
---|
208 | { |
---|
209 | return _pData->thread; |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | } // namespace Poco |
---|
214 | |
---|
215 | |
---|
216 | #endif // Foundation_Thread_POSIX_INCLUDED |
---|