]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/threadpsx.cpp
Sorry, I went and removed consts as per the style guide :-)
[wxWidgets.git] / src / gtk / threadpsx.cpp
CommitLineData
7c351dad
GL
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>
82052aff
GL
19#include "wx/thread.h"
20#include "wx/module.h"
21#include "wx/utils.h"
7c351dad
GL
22
23enum thread_state {
24 STATE_IDLE = 0,
25 STATE_RUNNING,
26 STATE_CANCELED,
27 STATE_EXITED
28};
29
30/////////////////////////////////////////////////////////////////////////////
31// Static variables
32/////////////////////////////////////////////////////////////////////////////
33
7c351dad
GL
34static pthread_t p_mainid;
35wxMutex 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
46class wxMutexInternal {
47public:
48 pthread_mutex_t p_mutex;
49};
50
ee4f8c2a 51wxMutex::wxMutex()
7c351dad
GL
52{
53 p_internal = new wxMutexInternal;
54 pthread_mutex_init(&(p_internal->p_mutex), NULL);
b89156b5 55 m_locked = 0;
7c351dad
GL
56}
57
ee4f8c2a 58wxMutex::~wxMutex()
7c351dad 59{
b89156b5
GL
60 if (m_locked > 0)
61 wxDebugMsg("wxMutex warning: freeing a locked mutex (%d locks)\n",
62 m_locked);
63
7c351dad
GL
64 pthread_mutex_destroy(&(p_internal->p_mutex));
65 delete p_internal;
66}
67
ee4f8c2a 68wxMutexError wxMutex::Lock()
7c351dad
GL
69{
70 int err;
71
72 err = pthread_mutex_lock(&(p_internal->p_mutex));
b89156b5
GL
73 if (err == EDEADLK)
74 return MUTEX_DEAD_LOCK;
7c351dad
GL
75 m_locked++;
76 return MUTEX_NO_ERROR;
77}
78
ee4f8c2a 79wxMutexError wxMutex::TryLock()
7c351dad
GL
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
ee4f8c2a 93wxMutexError wxMutex::Unlock()
7c351dad 94{
b89156b5
GL
95 if (m_locked > 0)
96 m_locked--;
97 else
98 return MUTEX_UNLOCKED;
7c351dad
GL
99 pthread_mutex_unlock(&(p_internal->p_mutex));
100 return MUTEX_NO_ERROR;
101}
102
103/////////////////////////////////////////////////////////////////////////////
104// wxThread: Posix Thread implementation (Condition)
105/////////////////////////////////////////////////////////////////////////////
106
107class wxConditionInternal {
108public:
109 pthread_cond_t p_condition;
110};
111
ee4f8c2a 112wxCondition::wxCondition()
7c351dad
GL
113{
114 p_internal = new wxConditionInternal;
115 pthread_cond_init(&(p_internal->p_condition), NULL);
116}
117
ee4f8c2a 118wxCondition::~wxCondition()
7c351dad
GL
119{
120 pthread_cond_destroy(&(p_internal->p_condition));
121 delete p_internal;
122}
123
124void wxCondition::Wait(wxMutex& mutex)
125{
126 pthread_cond_wait(&(p_internal->p_condition), &(mutex.p_internal->p_mutex));
127}
128
129bool 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
ee4f8c2a 138void wxCondition::Signal()
7c351dad
GL
139{
140 pthread_cond_signal(&(p_internal->p_condition));
141}
142
ee4f8c2a 143void wxCondition::Broadcast()
7c351dad
GL
144{
145 pthread_cond_broadcast(&(p_internal->p_condition));
146}
147
148/////////////////////////////////////////////////////////////////////////////
149// wxThread: Posix Thread implementation (Thread)
150/////////////////////////////////////////////////////////////////////////////
151
152class wxThreadInternal {
153public:
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
162void *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
173wxThreadError 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
206void 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
ee4f8c2a 218int wxThread::GetPriority() const
7c351dad
GL
219{
220 return p_internal->prio;
221}
222
223void 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
ee4f8c2a 231wxThreadError wxThread::Destroy()
7c351dad
GL
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
243void *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
ee4f8c2a 263unsigned long wxThread::GetID() const
7c351dad
GL
264{
265 return (unsigned long)p_internal->thread_id;
266}
267
268void 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
277void wxThread::TestDestroy()
278{
279 pthread_testcancel();
280}
281
38009d39 282bool wxThread::IsMain()
7c351dad
GL
283{
284 return (bool)pthread_equal(pthread_self(), p_mainid);
285}
286
287wxThread::wxThread()
288{
289 p_internal = new wxThreadInternal();
290}
291
292wxThread::~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.
300void wxThread::OnExit()
301{
302}
303
304// Automatic initialization
305class wxThreadModule : public wxModule {
306 DECLARE_DYNAMIC_CLASS(wxThreadModule)
307public:
ee4f8c2a 308 virtual bool OnInit() {
7c351dad
GL
309 wxThreadGuiInit();
310 p_mainid = pthread_self();
311 wxMainMutex.Lock();
312
313 return TRUE;
314 }
315
ee4f8c2a 316 virtual void OnExit() {
7c351dad
GL
317 wxMainMutex.Unlock();
318 wxThreadGuiExit();
319 }
320};
321
322IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule)