]> git.saurik.com Git - wxWidgets.git/blob - src/motif/thread.cpp
Introduced wxSOCKET_INT to socket.cpp. The previous code wouldn't compile on Windows.
[wxWidgets.git] / src / motif / thread.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: thread.cpp
3 // Purpose: wxThread Implementation for Posix threads
4 // Author: Original from Wolfram Gloger/Guilhem Lavaux
5 // Modified by: Robert Roebling
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
12 #ifdef __GNUG__
13 #pragma implementation "thread.h"
14 #endif
15
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <pthread.h>
19 #include <errno.h>
20 #include "wx/module.h"
21 #include "wx/thread.h"
22 #include "wx/utils.h"
23 #include "wx/log.h"
24
25 #include <stdio.h>
26 #include <unistd.h>
27
28 // for select()
29 #include <sys/time.h>
30 #include <sys/types.h>
31 #ifdef __sgi
32 #include <bstring.h>
33 #endif
34
35 //--------------------------------------------------------------------
36 // constants
37 //--------------------------------------------------------------------
38
39 enum thread_state
40 {
41 STATE_IDLE = 0,
42 STATE_RUNNING,
43 STATE_PAUSING,
44 STATE_PAUSED,
45 STATE_CANCELED,
46 STATE_EXITED
47 };
48
49 //--------------------------------------------------------------------
50 // global data
51 //--------------------------------------------------------------------
52
53 static pthread_t p_mainid;
54
55 wxMutex *wxMainMutex = (wxMutex*) NULL; /* controls access to all GUI functions */
56
57 /* TODO for Xt */
58
59 static int p_thrd_pipe[2] = { -1, -1 };
60
61 //-------------------------------------------------------------------------
62 // global functions
63 //-------------------------------------------------------------------------
64
65 static void wxThreadGuiInit()
66 {
67 /* TODO for Xt */
68 }
69
70 static void wxThreadGuiExit()
71 {
72 /* TODO for Xt */
73 }
74
75 void wxMutexGuiEnter()
76 {
77 if (wxMainMutex)
78 wxMainMutex->Lock();
79 }
80
81 void wxMutexGuiLeave()
82 {
83 if (wxMainMutex)
84 wxMainMutex->Unlock();
85 }
86
87 //--------------------------------------------------------------------
88 // wxMutex (Posix implementation)
89 //--------------------------------------------------------------------
90
91 class wxMutexInternal
92 {
93 public:
94 pthread_mutex_t p_mutex;
95 };
96
97 wxMutex::wxMutex()
98 {
99 p_internal = new wxMutexInternal;
100 pthread_mutex_init(&(p_internal->p_mutex), NULL);
101 m_locked = 0;
102 }
103
104 wxMutex::~wxMutex()
105 {
106 if (m_locked > 0)
107 wxLogDebug( "wxMutex warning: freeing a locked mutex (%d locks)\n", m_locked );
108
109 pthread_mutex_destroy(&(p_internal->p_mutex));
110 delete p_internal;
111 }
112
113 wxMutexError wxMutex::Lock()
114 {
115 int err;
116
117 err = pthread_mutex_lock(&(p_internal->p_mutex));
118 if (err == EDEADLK)
119 return wxMUTEX_DEAD_LOCK;
120
121 m_locked++;
122 return wxMUTEX_NO_ERROR;
123 }
124
125 wxMutexError wxMutex::TryLock()
126 {
127 int err;
128
129 if (m_locked)
130 return wxMUTEX_BUSY;
131
132 err = pthread_mutex_trylock(&(p_internal->p_mutex));
133 switch (err)
134 {
135 case EBUSY: return wxMUTEX_BUSY;
136 }
137 m_locked++;
138 return wxMUTEX_NO_ERROR;
139 }
140
141 wxMutexError wxMutex::Unlock()
142 {
143 if (m_locked > 0)
144 m_locked--;
145 else
146 return wxMUTEX_UNLOCKED;
147
148 pthread_mutex_unlock(&(p_internal->p_mutex));
149 return wxMUTEX_NO_ERROR;
150 }
151
152 //--------------------------------------------------------------------
153 // wxCondition (Posix implementation)
154 //--------------------------------------------------------------------
155
156 class wxConditionInternal
157 {
158 public:
159 pthread_cond_t p_condition;
160 };
161
162 wxCondition::wxCondition()
163 {
164 p_internal = new wxConditionInternal;
165 pthread_cond_init(&(p_internal->p_condition), NULL);
166 }
167
168 wxCondition::~wxCondition()
169 {
170 pthread_cond_destroy(&(p_internal->p_condition));
171 delete p_internal;
172 }
173
174 void wxCondition::Wait(wxMutex& mutex)
175 {
176 pthread_cond_wait(&(p_internal->p_condition), &(mutex.p_internal->p_mutex));
177 }
178
179 bool wxCondition::Wait(wxMutex& mutex, unsigned long sec, unsigned long nsec)
180 {
181 struct timespec tspec;
182
183 tspec.tv_sec = time(NULL)+sec;
184 tspec.tv_nsec = nsec;
185 return (pthread_cond_timedwait(&(p_internal->p_condition), &(mutex.p_internal->p_mutex), &tspec) != ETIMEDOUT);
186 }
187
188 void wxCondition::Signal()
189 {
190 pthread_cond_signal(&(p_internal->p_condition));
191 }
192
193 void wxCondition::Broadcast()
194 {
195 pthread_cond_broadcast(&(p_internal->p_condition));
196 }
197
198 //--------------------------------------------------------------------
199 // wxThread (Posix implementation)
200 //--------------------------------------------------------------------
201
202 class wxThreadInternal
203 {
204 public:
205 wxThreadInternal() { state = STATE_IDLE; }
206 ~wxThreadInternal() {}
207 static void *PthreadStart(void *ptr);
208 pthread_t thread_id;
209 int state;
210 int prio;
211 int defer_destroy;
212 };
213
214 void *wxThreadInternal::PthreadStart(void *ptr)
215 {
216 wxThread *thread = (wxThread *)ptr;
217
218 // Call the main entry
219 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
220 void* status = thread->Entry();
221
222 thread->Exit(status);
223
224 return NULL;
225 }
226
227 wxThreadError wxThread::Create()
228 {
229 pthread_attr_t a;
230 int min_prio, max_prio, p;
231 struct sched_param sp;
232
233 if (p_internal->state != STATE_IDLE)
234 return wxTHREAD_RUNNING;
235
236 // Change thread priority
237 pthread_attr_init(&a);
238 pthread_attr_getschedpolicy(&a, &p);
239
240 min_prio = sched_get_priority_min(p);
241 max_prio = sched_get_priority_max(p);
242
243 pthread_attr_getschedparam(&a, &sp);
244 sp.sched_priority = min_prio +
245 (p_internal->prio*(max_prio-min_prio))/100;
246 pthread_attr_setschedparam(&a, &sp);
247
248 // this is the point of no return
249 p_internal->state = STATE_RUNNING;
250 if (pthread_create(&p_internal->thread_id, &a,
251 wxThreadInternal::PthreadStart, (void *)this) != 0)
252 {
253 p_internal->state = STATE_IDLE;
254 pthread_attr_destroy(&a);
255 return wxTHREAD_NO_RESOURCE;
256 }
257 pthread_attr_destroy(&a);
258
259 return wxTHREAD_NO_ERROR;
260 }
261
262 void wxThread::SetPriority(int prio)
263 {
264 if (p_internal->state == STATE_RUNNING)
265 return;
266
267 if (prio > 100) prio = 100;
268
269 if (prio < 0) prio = 0;
270
271 p_internal->prio = prio;
272 }
273
274 int wxThread::GetPriority() const
275 {
276 return p_internal->prio;
277 }
278
279 void wxThread::DeferDestroy(bool on)
280 {
281 if (on)
282 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
283 else
284 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
285 }
286
287 wxThreadError wxThread::Destroy()
288 {
289 int res = 0;
290
291 if (p_internal->state == STATE_RUNNING)
292 {
293 res = pthread_cancel(p_internal->thread_id);
294 if (res == 0)
295 p_internal->state = STATE_CANCELED;
296 }
297
298 return wxTHREAD_NO_ERROR;
299 }
300
301 wxThreadError wxThread::Pause()
302 {
303 if (p_internal->state != STATE_RUNNING)
304 return wxTHREAD_NOT_RUNNING;
305
306 if (!p_internal->defer_destroy)
307 return wxTHREAD_MISC_ERROR;
308
309 p_internal->state = STATE_PAUSING;
310 return wxTHREAD_NO_ERROR;
311 }
312
313 wxThreadError wxThread::Resume()
314 {
315 if (p_internal->state == STATE_PAUSING || p_internal->state == STATE_PAUSED)
316 p_internal->state = STATE_RUNNING;
317
318 return wxTHREAD_NO_ERROR;
319 }
320
321 void *wxThread::Join()
322 {
323 void* status = 0;
324
325 if (p_internal->state != STATE_IDLE)
326 {
327 bool do_unlock = wxThread::IsMain();
328
329 while (p_internal->state == STATE_RUNNING)
330 wxYield();
331
332 if (do_unlock) wxMainMutex->Unlock();
333
334 pthread_join(p_internal->thread_id, &status);
335
336 if (do_unlock) wxMainMutex->Lock();
337
338 p_internal->state = STATE_IDLE;
339 }
340
341 return status;
342 }
343
344 unsigned long wxThread::GetID() const
345 {
346 return p_internal->thread_id;
347 }
348
349 void wxThread::Exit(void *status)
350 {
351 wxThread* ptr = this;
352
353 /* THREAD_SEND_EXIT_MSG(ptr); TODO for Xt */
354
355 p_internal->state = STATE_EXITED;
356 pthread_exit(status);
357 }
358
359 void wxThread::TestDestroy()
360 {
361 if (p_internal->state == STATE_PAUSING)
362 {
363 p_internal->state = STATE_PAUSED;
364 while (p_internal->state == STATE_PAUSED)
365 {
366 pthread_testcancel();
367 usleep(1);
368 }
369 }
370 pthread_testcancel();
371 }
372
373 bool wxThread::IsMain()
374 {
375 return (bool)pthread_equal(pthread_self(), p_mainid);
376 }
377
378 bool wxThread::IsRunning() const
379 {
380 return (p_internal->state == STATE_RUNNING);
381 }
382
383 bool wxThread::IsAlive() const
384 {
385 return (p_internal->state == STATE_RUNNING) ||
386 (p_internal->state == STATE_PAUSING) ||
387 (p_internal->state == STATE_PAUSED);
388 }
389
390 wxThread::wxThread()
391 {
392 p_internal = new wxThreadInternal();
393 }
394
395 wxThread::~wxThread()
396 {
397 Destroy();
398 Join();
399 delete p_internal;
400 }
401
402 // The default callback just joins the thread and throws away the result.
403 void wxThread::OnExit()
404 {
405 Join();
406 }
407
408 //--------------------------------------------------------------------
409 // wxThreadModule
410 //--------------------------------------------------------------------
411
412 class wxThreadModule : public wxModule
413 {
414 DECLARE_DYNAMIC_CLASS(wxThreadModule)
415
416 public:
417 virtual bool OnInit()
418 {
419 wxMainMutex = new wxMutex();
420 wxThreadGuiInit();
421 p_mainid = pthread_self();
422 wxMainMutex->Lock();
423
424 return TRUE;
425 }
426
427 virtual void OnExit()
428 {
429 wxMainMutex->Unlock();
430 wxThreadGuiExit();
431 delete wxMainMutex;
432 }
433 };
434
435 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule)
436
437