]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/threadpsx.cpp
688ef8f29f473d0bcbd41b92900ce5dafb79873c
[wxWidgets.git] / src / gtk / threadpsx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: threadpsx.cpp
3 // Purpose: wxThread (Posix) Implementation
4 // Author: Original from Wolfram Gloger/Guilhem Lavaux
5 // Modified by:
6 // Created: 04/22/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Wolfram Gloger (1996, 1997); Guilhem Lavaux (1998)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11 #ifdef __GNUG__
12 #pragma implementation "thread.h"
13 #endif
14
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <sched.h>
18 #include <pthread.h>
19 #include "wx/thread.h"
20 #include "wx/module.h"
21 #include "wx/utils.h"
22
23 enum thread_state {
24 STATE_IDLE = 0,
25 STATE_RUNNING,
26 STATE_CANCELED,
27 STATE_EXITED
28 };
29
30 /////////////////////////////////////////////////////////////////////////////
31 // Static variables
32 /////////////////////////////////////////////////////////////////////////////
33
34 static pthread_t p_mainid;
35 wxMutex wxMainMutex; // controls access to all GUI functions
36
37 /////////////////////////////////////////////////////////////////////////////
38 // GUI thread manager
39 /////////////////////////////////////////////////////////////////////////////
40 #include "threadgui.inc"
41
42 /////////////////////////////////////////////////////////////////////////////
43 // wxThread: Posix Thread implementation (Mutex)
44 /////////////////////////////////////////////////////////////////////////////
45
46 class wxMutexInternal {
47 public:
48 pthread_mutex_t p_mutex;
49 };
50
51 wxMutex::wxMutex()
52 {
53 p_internal = new wxMutexInternal;
54 pthread_mutex_init(&(p_internal->p_mutex), NULL);
55 m_locked = 0;
56 }
57
58 wxMutex::~wxMutex()
59 {
60 if (m_locked > 0)
61 wxDebugMsg("wxMutex warning: freeing a locked mutex (%d locks)\n",
62 m_locked);
63
64 pthread_mutex_destroy(&(p_internal->p_mutex));
65 delete p_internal;
66 }
67
68 wxMutexError wxMutex::Lock()
69 {
70 int err;
71
72 err = pthread_mutex_lock(&(p_internal->p_mutex));
73 if (err == EDEADLK)
74 return MUTEX_DEAD_LOCK;
75 m_locked++;
76 return MUTEX_NO_ERROR;
77 }
78
79 wxMutexError wxMutex::TryLock()
80 {
81 int err;
82
83 if (m_locked)
84 return MUTEX_BUSY;
85 err = pthread_mutex_trylock(&(p_internal->p_mutex));
86 switch (err) {
87 case EBUSY: return MUTEX_BUSY;
88 }
89 m_locked++;
90 return MUTEX_NO_ERROR;
91 }
92
93 wxMutexError wxMutex::Unlock()
94 {
95 if (m_locked > 0)
96 m_locked--;
97 else
98 return MUTEX_UNLOCKED;
99 pthread_mutex_unlock(&(p_internal->p_mutex));
100 return MUTEX_NO_ERROR;
101 }
102
103 /////////////////////////////////////////////////////////////////////////////
104 // wxThread: Posix Thread implementation (Condition)
105 /////////////////////////////////////////////////////////////////////////////
106
107 class wxConditionInternal {
108 public:
109 pthread_cond_t p_condition;
110 };
111
112 wxCondition::wxCondition()
113 {
114 p_internal = new wxConditionInternal;
115 pthread_cond_init(&(p_internal->p_condition), NULL);
116 }
117
118 wxCondition::~wxCondition()
119 {
120 pthread_cond_destroy(&(p_internal->p_condition));
121 delete p_internal;
122 }
123
124 void wxCondition::Wait(wxMutex& mutex)
125 {
126 pthread_cond_wait(&(p_internal->p_condition), &(mutex.p_internal->p_mutex));
127 }
128
129 bool wxCondition::Wait(wxMutex& mutex, unsigned long sec, unsigned long nsec)
130 {
131 struct timespec tspec;
132
133 tspec.tv_sec = time(NULL)+sec;
134 tspec.tv_nsec = nsec;
135 return (pthread_cond_timedwait(&(p_internal->p_condition), &(mutex.p_internal->p_mutex), &tspec) != ETIMEDOUT);
136 }
137
138 void wxCondition::Signal()
139 {
140 pthread_cond_signal(&(p_internal->p_condition));
141 }
142
143 void wxCondition::Broadcast()
144 {
145 pthread_cond_broadcast(&(p_internal->p_condition));
146 }
147
148 /////////////////////////////////////////////////////////////////////////////
149 // wxThread: Posix Thread implementation (Thread)
150 /////////////////////////////////////////////////////////////////////////////
151
152 class wxThreadInternal {
153 public:
154 wxThreadInternal() { state = STATE_IDLE; }
155 ~wxThreadInternal() {}
156 static void *PthreadStart(void *ptr);
157 pthread_t thread_id;
158 int state;
159 int prio;
160 };
161
162 void *wxThreadInternal::PthreadStart(void *ptr)
163 {
164 wxThread *thread = (wxThread *)ptr;
165
166 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
167 void* status = thread->Entry();
168 thread->Exit(status);
169
170 return NULL;
171 }
172
173 wxThreadError wxThread::Create()
174 {
175 pthread_attr_t a;
176 int min_prio, max_prio, p;
177 struct sched_param sp;
178
179 if (p_internal->state != STATE_IDLE)
180 return THREAD_RUNNING;
181
182 // Change thread priority
183 pthread_attr_init(&a);
184 pthread_attr_getschedpolicy(&a, &p);
185
186 min_prio = sched_get_priority_min(p);
187 max_prio = sched_get_priority_max(p);
188
189 pthread_attr_getschedparam(&a, &sp);
190 sp.sched_priority = min_prio +
191 (p_internal->prio*(max_prio-min_prio))/100;
192 pthread_attr_setschedparam(&a, &sp);
193
194 // this is the point of no return
195 p_internal->state = STATE_RUNNING;
196 if (pthread_create(&p_internal->thread_id, &a,
197 wxThreadInternal::PthreadStart, (void *)this) != 0) {
198 p_internal->state = STATE_IDLE;
199 pthread_attr_destroy(&a);
200 return THREAD_NO_RESOURCE;
201 }
202 pthread_attr_destroy(&a);
203 return THREAD_NO_ERROR;
204 }
205
206 void wxThread::SetPriority(int prio)
207 {
208 if (p_internal->state == STATE_RUNNING)
209 return;
210
211 if (prio > 100)
212 prio = 100;
213 if (prio < 0)
214 prio = 0;
215 p_internal->prio = prio;
216 }
217
218 int wxThread::GetPriority() const
219 {
220 return p_internal->prio;
221 }
222
223 void wxThread::DeferDestroy(bool on)
224 {
225 if (on)
226 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
227 else
228 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
229 }
230
231 wxThreadError wxThread::Destroy()
232 {
233 int res = 0;
234
235 if (p_internal->state == STATE_RUNNING) {
236 res = pthread_cancel(p_internal->thread_id);
237 if (res == 0)
238 p_internal->state = STATE_CANCELED;
239 }
240 return THREAD_NO_ERROR;
241 }
242
243 void *wxThread::Join()
244 {
245 void* status = 0;
246
247 if (p_internal->state != STATE_IDLE) {
248 bool do_unlock = wxThread::IsMain();
249
250 while (p_internal->state == STATE_RUNNING)
251 wxYield();
252
253 if (do_unlock)
254 wxMainMutex.Unlock();
255 pthread_join(p_internal->thread_id, &status);
256 if (do_unlock)
257 wxMainMutex.Lock();
258 p_internal->state = STATE_IDLE;
259 }
260 return status;
261 }
262
263 unsigned long wxThread::GetID() const
264 {
265 return (unsigned long)p_internal->thread_id;
266 }
267
268 void wxThread::Exit(void *status)
269 {
270 wxThread* ptr = this;
271
272 THREAD_SEND_EXIT_MSG(ptr);
273 p_internal->state = STATE_EXITED;
274 pthread_exit(status);
275 }
276
277 void wxThread::TestDestroy()
278 {
279 pthread_testcancel();
280 }
281
282 bool wxThread::IsMain()
283 {
284 return (bool)pthread_equal(pthread_self(), p_mainid);
285 }
286
287 wxThread::wxThread()
288 {
289 p_internal = new wxThreadInternal();
290 }
291
292 wxThread::~wxThread()
293 {
294 Destroy();
295 Join();
296 delete p_internal;
297 }
298
299 // The default callback just joins the thread and throws away the result.
300 void wxThread::OnExit()
301 {
302 }
303
304 // Automatic initialization
305 class wxThreadModule : public wxModule {
306 DECLARE_DYNAMIC_CLASS(wxThreadModule)
307 public:
308 virtual bool OnInit() {
309 wxThreadGuiInit();
310 p_mainid = pthread_self();
311 wxMainMutex.Lock();
312
313 return TRUE;
314 }
315
316 virtual void OnExit() {
317 wxMainMutex.Unlock();
318 wxThreadGuiExit();
319 }
320 };
321
322 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule)