]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/threadpsx.cpp
9704e70e2544dc5a5e462147d31cf78297182373
[wxWidgets.git] / src / gtk1 / 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 <pthread.h>
18 #include <errno.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_PAUSING,
27 STATE_PAUSED,
28 STATE_CANCELED,
29 STATE_EXITED
30 };
31
32 /////////////////////////////////////////////////////////////////////////////
33 // Static variables
34 /////////////////////////////////////////////////////////////////////////////
35
36 static pthread_t p_mainid;
37 static wxMutex p_list_mutex;
38 static wxList p_threads_list;
39
40 wxMutex wxMainMutex; // controls access to all GUI functions
41
42 /////////////////////////////////////////////////////////////////////////////
43 // GUI thread manager
44 /////////////////////////////////////////////////////////////////////////////
45 #include "threadgui.inc"
46
47 /////////////////////////////////////////////////////////////////////////////
48 // wxThread: Posix Thread implementation (Mutex)
49 /////////////////////////////////////////////////////////////////////////////
50
51 class wxMutexInternal {
52 public:
53 pthread_mutex_t p_mutex;
54 };
55
56 wxMutex::wxMutex()
57 {
58 p_internal = new wxMutexInternal;
59 pthread_mutex_init(&(p_internal->p_mutex), NULL);
60 m_locked = 0;
61 }
62
63 wxMutex::~wxMutex()
64 {
65 if (m_locked > 0)
66 wxDebugMsg("wxMutex warning: freeing a locked mutex (%d locks)\n",
67 m_locked);
68
69 pthread_mutex_destroy(&(p_internal->p_mutex));
70 delete p_internal;
71 }
72
73 wxMutexError wxMutex::Lock()
74 {
75 int err;
76
77 err = pthread_mutex_lock(&(p_internal->p_mutex));
78 if (err == EDEADLK)
79 return MUTEX_DEAD_LOCK;
80 m_locked++;
81 return MUTEX_NO_ERROR;
82 }
83
84 wxMutexError wxMutex::TryLock()
85 {
86 int err;
87
88 if (m_locked)
89 return MUTEX_BUSY;
90 err = pthread_mutex_trylock(&(p_internal->p_mutex));
91 switch (err) {
92 case EBUSY: return MUTEX_BUSY;
93 }
94 m_locked++;
95 return MUTEX_NO_ERROR;
96 }
97
98 wxMutexError wxMutex::Unlock()
99 {
100 if (m_locked > 0)
101 m_locked--;
102 else
103 return MUTEX_UNLOCKED;
104 pthread_mutex_unlock(&(p_internal->p_mutex));
105 return MUTEX_NO_ERROR;
106 }
107
108 /////////////////////////////////////////////////////////////////////////////
109 // wxThread: Posix Thread implementation (Condition)
110 /////////////////////////////////////////////////////////////////////////////
111
112 class wxConditionInternal {
113 public:
114 pthread_cond_t p_condition;
115 };
116
117 wxCondition::wxCondition()
118 {
119 p_internal = new wxConditionInternal;
120 pthread_cond_init(&(p_internal->p_condition), NULL);
121 }
122
123 wxCondition::~wxCondition()
124 {
125 pthread_cond_destroy(&(p_internal->p_condition));
126 delete p_internal;
127 }
128
129 void wxCondition::Wait(wxMutex& mutex)
130 {
131 pthread_cond_wait(&(p_internal->p_condition), &(mutex.p_internal->p_mutex));
132 }
133
134 bool wxCondition::Wait(wxMutex& mutex, unsigned long sec, unsigned long nsec)
135 {
136 struct timespec tspec;
137
138 tspec.tv_sec = time(NULL)+sec;
139 tspec.tv_nsec = nsec;
140 return (pthread_cond_timedwait(&(p_internal->p_condition), &(mutex.p_internal->p_mutex), &tspec) != ETIMEDOUT);
141 }
142
143 void wxCondition::Signal()
144 {
145 pthread_cond_signal(&(p_internal->p_condition));
146 }
147
148 void wxCondition::Broadcast()
149 {
150 pthread_cond_broadcast(&(p_internal->p_condition));
151 }
152
153 /////////////////////////////////////////////////////////////////////////////
154 // wxThread: Posix Thread implementation (Thread)
155 /////////////////////////////////////////////////////////////////////////////
156
157 class wxThreadInternal {
158 public:
159 wxThreadInternal() { state = STATE_IDLE; }
160 ~wxThreadInternal() {}
161 static void *PthreadStart(void *ptr);
162 pthread_t thread_id;
163 int state;
164 int prio;
165 int defer_destroy;
166 int id;
167 };
168
169 void *wxThreadInternal::PthreadStart(void *ptr)
170 {
171 wxThread *thread = (wxThread *)ptr;
172
173 // Add the current thread to the list
174 p_list_mutex.Lock();
175 thread->p_internal->id = p_threads_list.Number();
176 p_threads_list.Append((wxObject *)thread);
177 p_list_mutex.Unlock();
178
179 // Call the main entry
180 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
181 void* status = thread->Entry();
182
183 // Delete the current thread from the list
184 p_list_mutex.Lock();
185 delete node_thread;
186 p_list_mutex.Unlock();
187
188 thread->Exit(status);
189
190 return NULL;
191 }
192
193 wxThreadError wxThread::Create()
194 {
195 pthread_attr_t a;
196 int min_prio, max_prio, p;
197 struct sched_param sp;
198
199 if (p_internal->state != STATE_IDLE)
200 return THREAD_RUNNING;
201
202 // Change thread priority
203 pthread_attr_init(&a);
204 pthread_attr_getschedpolicy(&a, &p);
205
206 min_prio = sched_get_priority_min(p);
207 max_prio = sched_get_priority_max(p);
208
209 pthread_attr_getschedparam(&a, &sp);
210 sp.sched_priority = min_prio +
211 (p_internal->prio*(max_prio-min_prio))/100;
212 pthread_attr_setschedparam(&a, &sp);
213
214 // this is the point of no return
215 p_internal->state = STATE_RUNNING;
216 if (pthread_create(&p_internal->thread_id, &a,
217 wxThreadInternal::PthreadStart, (void *)this) != 0) {
218 p_internal->state = STATE_IDLE;
219 pthread_attr_destroy(&a);
220 return THREAD_NO_RESOURCE;
221 }
222 pthread_attr_destroy(&a);
223
224 return THREAD_NO_ERROR;
225 }
226
227 void wxThread::SetPriority(int prio)
228 {
229 if (p_internal->state == STATE_RUNNING)
230 return;
231
232 if (prio > 100)
233 prio = 100;
234 if (prio < 0)
235 prio = 0;
236 p_internal->prio = prio;
237 }
238
239 int wxThread::GetPriority() const
240 {
241 return p_internal->prio;
242 }
243
244 void wxThread::DeferDestroy(bool on)
245 {
246 if (on)
247 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
248 else
249 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
250 }
251
252 wxThreadError wxThread::Destroy()
253 {
254 int res = 0;
255
256 if (p_internal->state == STATE_RUNNING) {
257 res = pthread_cancel(p_internal->thread_id);
258 if (res == 0)
259 p_internal->state = STATE_CANCELED;
260 }
261
262 return THREAD_NO_ERROR;
263 }
264
265 wxThreadError wxThread::Pause()
266 {
267 if (p_internal->state != STATE_RUNNING)
268 return THREAD_NOT_RUNNING;
269
270 if (!p_internal->defer_destroy)
271 return THREAD_MISC_ERROR;
272
273 p_internal->state = STATE_PAUSING;
274 return THREAD_NO_ERROR;
275 }
276
277 wxThreadError wxThread::Resume()
278 {
279 if (p_internal->state == STATE_PAUSING || p_internal->state == STATE_PAUSED)
280 p_internal->state = STATE_RUNNING;
281
282 return THREAD_NO_ERROR;
283 }
284
285 void *wxThread::Join()
286 {
287 void* status = 0;
288
289 if (p_internal->state != STATE_IDLE) {
290 bool do_unlock = wxThread::IsMain();
291
292 while (p_internal->state == STATE_RUNNING)
293 wxYield();
294
295 if (do_unlock)
296 wxMainMutex.Unlock();
297 pthread_join(p_internal->thread_id, &status);
298 if (do_unlock)
299 wxMainMutex.Lock();
300
301 p_list_mutex.Lock();
302 delete p_threads_list.Nth(p_internal->id);
303 p_list_mutex.Unlock();
304
305 p_internal->state = STATE_IDLE;
306 }
307 return status;
308 }
309
310 unsigned long wxThread::GetID() const
311 {
312 return p_internal->id;
313 }
314
315 wxThread *wxThread::GetThreadFromID(unsigned long id)
316 {
317 wxNode *node = p_threads_list.Nth(id);
318
319 if (!node)
320 return NULL;
321 return (wxThread *)node->Data();
322 }
323
324 void wxThread::Exit(void *status)
325 {
326 wxThread* ptr = this;
327
328 THREAD_SEND_EXIT_MSG(ptr);
329 p_internal->state = STATE_EXITED;
330 pthread_exit(status);
331 }
332
333 void wxThread::TestDestroy()
334 {
335 if (p_internal->state == STATE_PAUSING) {
336 p_internal->state = STATE_PAUSED;
337 while (p_internal->state == STATE_PAUSED) {
338 pthread_testcancel();
339 usleep(1);
340 }
341 }
342 pthread_testcancel();
343 }
344
345 bool wxThread::IsMain()
346 {
347 return (bool)pthread_equal(pthread_self(), p_mainid);
348 }
349
350 bool wxThread::IsRunning() const
351 {
352 return (p_internal->state == STATE_RUNNING);
353 }
354
355 bool wxThread::IsAlive() const
356 {
357 return (p_internal->state == STATE_RUNNING) ||
358 (p_internal->state == STATE_PAUSING) ||
359 (p_internal->state == STATE_PAUSED);
360 }
361
362 wxThread::wxThread()
363 {
364 p_internal = new wxThreadInternal();
365 }
366
367 wxThread::~wxThread()
368 {
369 Destroy();
370 Join();
371 delete p_internal;
372 }
373
374 // The default callback just joins the thread and throws away the result.
375 void wxThread::OnExit()
376 {
377 Join();
378 }
379
380 // Automatic initialization
381 class wxThreadModule : public wxModule {
382 DECLARE_DYNAMIC_CLASS(wxThreadModule)
383 public:
384 virtual bool OnInit() {
385 wxThreadGuiInit();
386 p_mainid = pthread_self();
387 p_threads_list = wxList(wxKEY_INTEGER);
388 wxMainMutex.Lock();
389
390 return TRUE;
391 }
392
393 virtual void OnExit() {
394 wxMainMutex.Unlock();
395 wxThreadGuiExit();
396 }
397 };
398
399 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule)