| 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 | thread->Exit(status); |
| 184 | |
| 185 | return NULL; |
| 186 | } |
| 187 | |
| 188 | wxThreadError wxThread::Create() |
| 189 | { |
| 190 | pthread_attr_t a; |
| 191 | int min_prio, max_prio, p; |
| 192 | struct sched_param sp; |
| 193 | |
| 194 | if (p_internal->state != STATE_IDLE) |
| 195 | return THREAD_RUNNING; |
| 196 | |
| 197 | // Change thread priority |
| 198 | pthread_attr_init(&a); |
| 199 | pthread_attr_getschedpolicy(&a, &p); |
| 200 | |
| 201 | min_prio = sched_get_priority_min(p); |
| 202 | max_prio = sched_get_priority_max(p); |
| 203 | |
| 204 | pthread_attr_getschedparam(&a, &sp); |
| 205 | sp.sched_priority = min_prio + |
| 206 | (p_internal->prio*(max_prio-min_prio))/100; |
| 207 | pthread_attr_setschedparam(&a, &sp); |
| 208 | |
| 209 | // this is the point of no return |
| 210 | p_internal->state = STATE_RUNNING; |
| 211 | if (pthread_create(&p_internal->thread_id, &a, |
| 212 | wxThreadInternal::PthreadStart, (void *)this) != 0) { |
| 213 | p_internal->state = STATE_IDLE; |
| 214 | pthread_attr_destroy(&a); |
| 215 | return THREAD_NO_RESOURCE; |
| 216 | } |
| 217 | pthread_attr_destroy(&a); |
| 218 | |
| 219 | return THREAD_NO_ERROR; |
| 220 | } |
| 221 | |
| 222 | void wxThread::SetPriority(int prio) |
| 223 | { |
| 224 | if (p_internal->state == STATE_RUNNING) |
| 225 | return; |
| 226 | |
| 227 | if (prio > 100) |
| 228 | prio = 100; |
| 229 | if (prio < 0) |
| 230 | prio = 0; |
| 231 | p_internal->prio = prio; |
| 232 | } |
| 233 | |
| 234 | int wxThread::GetPriority() const |
| 235 | { |
| 236 | return p_internal->prio; |
| 237 | } |
| 238 | |
| 239 | void wxThread::DeferDestroy(bool on) |
| 240 | { |
| 241 | if (on) |
| 242 | pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); |
| 243 | else |
| 244 | pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); |
| 245 | } |
| 246 | |
| 247 | wxThreadError wxThread::Destroy() |
| 248 | { |
| 249 | int res = 0; |
| 250 | |
| 251 | if (p_internal->state == STATE_RUNNING) { |
| 252 | res = pthread_cancel(p_internal->thread_id); |
| 253 | if (res == 0) |
| 254 | p_internal->state = STATE_CANCELED; |
| 255 | } |
| 256 | |
| 257 | return THREAD_NO_ERROR; |
| 258 | } |
| 259 | |
| 260 | wxThreadError wxThread::Pause() |
| 261 | { |
| 262 | if (p_internal->state != STATE_RUNNING) |
| 263 | return THREAD_NOT_RUNNING; |
| 264 | |
| 265 | if (!p_internal->defer_destroy) |
| 266 | return THREAD_MISC_ERROR; |
| 267 | |
| 268 | p_internal->state = STATE_PAUSING; |
| 269 | return THREAD_NO_ERROR; |
| 270 | } |
| 271 | |
| 272 | wxThreadError wxThread::Resume() |
| 273 | { |
| 274 | if (p_internal->state == STATE_PAUSING || p_internal->state == STATE_PAUSED) |
| 275 | p_internal->state = STATE_RUNNING; |
| 276 | |
| 277 | return THREAD_NO_ERROR; |
| 278 | } |
| 279 | |
| 280 | void *wxThread::Join() |
| 281 | { |
| 282 | void* status = 0; |
| 283 | |
| 284 | if (p_internal->state != STATE_IDLE) { |
| 285 | bool do_unlock = wxThread::IsMain(); |
| 286 | |
| 287 | while (p_internal->state == STATE_RUNNING) |
| 288 | wxYield(); |
| 289 | |
| 290 | if (do_unlock) |
| 291 | wxMainMutex->Unlock(); |
| 292 | pthread_join(p_internal->thread_id, &status); |
| 293 | if (do_unlock) |
| 294 | wxMainMutex->Lock(); |
| 295 | |
| 296 | p_list_mutex.Lock(); |
| 297 | delete p_threads_list.Nth(p_internal->id); |
| 298 | p_list_mutex.Unlock(); |
| 299 | |
| 300 | p_internal->state = STATE_IDLE; |
| 301 | } |
| 302 | return status; |
| 303 | } |
| 304 | |
| 305 | unsigned long wxThread::GetID() const |
| 306 | { |
| 307 | return p_internal->id; |
| 308 | } |
| 309 | |
| 310 | wxThread *wxThread::GetThreadFromID(unsigned long id) |
| 311 | { |
| 312 | wxNode *node = p_threads_list.Nth(id); |
| 313 | |
| 314 | if (!node) |
| 315 | return NULL; |
| 316 | return (wxThread *)node->Data(); |
| 317 | } |
| 318 | |
| 319 | void wxThread::Exit(void *status) |
| 320 | { |
| 321 | wxThread* ptr = this; |
| 322 | |
| 323 | THREAD_SEND_EXIT_MSG(ptr); |
| 324 | p_internal->state = STATE_EXITED; |
| 325 | pthread_exit(status); |
| 326 | } |
| 327 | |
| 328 | void wxThread::TestDestroy() |
| 329 | { |
| 330 | if (p_internal->state == STATE_PAUSING) { |
| 331 | p_internal->state = STATE_PAUSED; |
| 332 | while (p_internal->state == STATE_PAUSED) { |
| 333 | pthread_testcancel(); |
| 334 | usleep(1); |
| 335 | } |
| 336 | } |
| 337 | pthread_testcancel(); |
| 338 | } |
| 339 | |
| 340 | bool wxThread::IsMain() |
| 341 | { |
| 342 | return (bool)pthread_equal(pthread_self(), p_mainid); |
| 343 | } |
| 344 | |
| 345 | bool wxThread::IsRunning() const |
| 346 | { |
| 347 | return (p_internal->state == STATE_RUNNING); |
| 348 | } |
| 349 | |
| 350 | bool wxThread::IsAlive() const |
| 351 | { |
| 352 | return (p_internal->state == STATE_RUNNING) || |
| 353 | (p_internal->state == STATE_PAUSING) || |
| 354 | (p_internal->state == STATE_PAUSED); |
| 355 | } |
| 356 | |
| 357 | wxThread::wxThread() |
| 358 | { |
| 359 | p_internal = new wxThreadInternal(); |
| 360 | } |
| 361 | |
| 362 | wxThread::~wxThread() |
| 363 | { |
| 364 | Destroy(); |
| 365 | Join(); |
| 366 | delete p_internal; |
| 367 | } |
| 368 | |
| 369 | // The default callback just joins the thread and throws away the result. |
| 370 | void wxThread::OnExit() |
| 371 | { |
| 372 | Join(); |
| 373 | } |
| 374 | |
| 375 | // Automatic initialization |
| 376 | class wxThreadModule : public wxModule { |
| 377 | DECLARE_DYNAMIC_CLASS(wxThreadModule) |
| 378 | public: |
| 379 | virtual bool OnInit() { |
| 380 | wxMainMutex = new wxMutex(); |
| 381 | wxThreadGuiInit(); |
| 382 | p_mainid = pthread_self(); |
| 383 | p_threads_list = wxList(wxKEY_INTEGER); |
| 384 | wxMainMutex->Lock(); |
| 385 | |
| 386 | return TRUE; |
| 387 | } |
| 388 | |
| 389 | virtual void OnExit() { |
| 390 | wxMainMutex->Unlock(); |
| 391 | wxThreadGuiExit(); |
| 392 | delete wxMainMutex; |
| 393 | } |
| 394 | }; |
| 395 | |
| 396 | IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule) |