]> git.saurik.com Git - wxWidgets.git/blame - src/palmos/thread.cpp
no real change: put event handlers together at the end of the file
[wxWidgets.git] / src / palmos / thread.cpp
CommitLineData
ffecfa5a
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/palmos/thread.cpp
3// Purpose: wxThread Implementation
e2731512 4// Author: William Osborne - minimal working wxPalmOS port
ffecfa5a
JS
5// Modified by:
6// Created: 10/13/04
e2731512 7// RCS-ID: $Id$
ffecfa5a
JS
8// Copyright: (c) William Osborne
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
ffecfa5a
JS
12// ----------------------------------------------------------------------------
13// headers
14// ----------------------------------------------------------------------------
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#if defined(__BORLANDC__)
20 #pragma hdrstop
21#endif
22
02761f6c
WS
23#if wxUSE_THREADS
24
25#include "wx/thread.h"
26
ffecfa5a
JS
27#ifndef WX_PRECOMP
28 #include "wx/intl.h"
29 #include "wx/app.h"
02761f6c 30 #include "wx/module.h"
ffecfa5a
JS
31#endif
32
ffecfa5a
JS
33#include "wx/apptrait.h"
34
35#include "wx/palmos/private.h"
36#include "wx/palmos/missing.h"
37
ffecfa5a
JS
38// must have this symbol defined to get _beginthread/_endthread declarations
39#ifndef _MT
40 #define _MT
41#endif
42
43#if defined(__BORLANDC__)
44 #if !defined(__MT__)
45 // I can't set -tWM in the IDE (anyone?) so have to do this
46 #define __MT__
47 #endif
48
49 #if !defined(__MFC_COMPAT__)
50 // Needed to know about _beginthreadex etc..
51 #define __MFC_COMPAT__
52 #endif
53#endif // BC++
54
55// define wxUSE_BEGIN_THREAD if the compiler has _beginthreadex() function
56// which should be used instead of Win32 ::CreateThread() if possible
57#if defined(__VISUALC__) || \
58 (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500)) || \
59 (defined(__GNUG__) && defined(__MSVCRT__)) || \
60 defined(__WATCOMC__) || defined(__MWERKS__)
61
62#ifndef __WXWINCE__
63 #undef wxUSE_BEGIN_THREAD
64 #define wxUSE_BEGIN_THREAD
65#endif
66
67#endif
68
69#ifdef wxUSE_BEGIN_THREAD
70 // the return type of the thread function entry point
71 typedef unsigned THREAD_RETVAL;
72
73 // the calling convention of the thread function entry point
74 #define THREAD_CALLCONV __stdcall
75#else
76 // the settings for CreateThread()
77 typedef DWORD THREAD_RETVAL;
78 #define THREAD_CALLCONV WINAPI
79#endif
80
81// ----------------------------------------------------------------------------
82// constants
83// ----------------------------------------------------------------------------
84
85// the possible states of the thread ("=>" shows all possible transitions from
86// this state)
87enum wxThreadState
88{
89 STATE_NEW, // didn't start execution yet (=> RUNNING)
90 STATE_RUNNING, // thread is running (=> PAUSED, CANCELED)
91 STATE_PAUSED, // thread is temporarily suspended (=> RUNNING)
92 STATE_CANCELED, // thread should terminate a.s.a.p. (=> EXITED)
93 STATE_EXITED // thread is terminating
94};
95
96// ----------------------------------------------------------------------------
97// this module globals
98// ----------------------------------------------------------------------------
99
100// TLS index of the slot where we store the pointer to the current thread
101static DWORD gs_tlsThisThread = 0xFFFFFFFF;
102
103// id of the main thread - the one which can call GUI functions without first
104// calling wxMutexGuiEnter()
105static DWORD gs_idMainThread = 0;
106
107// if it's false, some secondary thread is holding the GUI lock
108static bool gs_bGuiOwnedByMainThread = true;
109
110// critical section which controls access to all GUI functions: any secondary
111// thread (i.e. except the main one) must enter this crit section before doing
112// any GUI calls
113static wxCriticalSection *gs_critsectGui = NULL;
114
115// critical section which protects gs_nWaitingForGui variable
116static wxCriticalSection *gs_critsectWaitingForGui = NULL;
117
118// critical section which serializes WinThreadStart() and WaitForTerminate()
119// (this is a potential bottleneck, we use a single crit sect for all threads
120// in the system, but normally time spent inside it should be quite short)
121static wxCriticalSection *gs_critsectThreadDelete = NULL;
122
123// number of threads waiting for GUI in wxMutexGuiEnter()
124static size_t gs_nWaitingForGui = 0;
125
126// are we waiting for a thread termination?
127static bool gs_waitingForThread = false;
128
129// ============================================================================
130// Windows implementation of thread and related classes
131// ============================================================================
132
133// ----------------------------------------------------------------------------
134// wxCriticalSection
135// ----------------------------------------------------------------------------
136
137wxCriticalSection::wxCriticalSection()
138{
139}
140
141wxCriticalSection::~wxCriticalSection()
142{
143}
144
145void wxCriticalSection::Enter()
146{
147}
148
149void wxCriticalSection::Leave()
150{
151}
152
153// ----------------------------------------------------------------------------
154// wxMutex
155// ----------------------------------------------------------------------------
156
157class wxMutexInternal
158{
159public:
160 wxMutexInternal(wxMutexType mutexType);
161 ~wxMutexInternal();
162
163 bool IsOk() const { return m_mutex != NULL; }
164
165 wxMutexError Lock() { return LockTimeout(INFINITE); }
166 wxMutexError TryLock() { return LockTimeout(0); }
167 wxMutexError Unlock();
168
169private:
170 wxMutexError LockTimeout(DWORD milliseconds);
171
172 HANDLE m_mutex;
173
174 DECLARE_NO_COPY_CLASS(wxMutexInternal)
175};
176
177// all mutexes are recursive under Win32 so we don't use mutexType
178wxMutexInternal::wxMutexInternal(wxMutexType WXUNUSED(mutexType))
179{
180}
181
182wxMutexInternal::~wxMutexInternal()
183{
184}
185
186wxMutexError wxMutexInternal::LockTimeout(DWORD milliseconds)
187{
188 return wxMUTEX_NO_ERROR;
189}
190
191wxMutexError wxMutexInternal::Unlock()
192{
193 return wxMUTEX_NO_ERROR;
194}
195
196// --------------------------------------------------------------------------
197// wxSemaphore
198// --------------------------------------------------------------------------
199
200// a trivial wrapper around Win32 semaphore
201class wxSemaphoreInternal
202{
203public:
204 wxSemaphoreInternal(int initialcount, int maxcount);
205 ~wxSemaphoreInternal();
206
207 bool IsOk() const { return m_semaphore != NULL; }
208
209 wxSemaError Wait() { return WaitTimeout(INFINITE); }
210
211 wxSemaError TryWait()
212 {
213 wxSemaError rc = WaitTimeout(0);
214 if ( rc == wxSEMA_TIMEOUT )
215 rc = wxSEMA_BUSY;
216
217 return rc;
218 }
219
220 wxSemaError WaitTimeout(unsigned long milliseconds);
221
222 wxSemaError Post();
223
224private:
225 HANDLE m_semaphore;
226
227 DECLARE_NO_COPY_CLASS(wxSemaphoreInternal)
228};
229
230wxSemaphoreInternal::wxSemaphoreInternal(int initialcount, int maxcount)
231{
232}
233
234wxSemaphoreInternal::~wxSemaphoreInternal()
235{
236}
237
238wxSemaError wxSemaphoreInternal::WaitTimeout(unsigned long milliseconds)
239{
240 return wxSEMA_NO_ERROR;
241}
242
243wxSemaError wxSemaphoreInternal::Post()
244{
245 return wxSEMA_NO_ERROR;
246}
247
248// ----------------------------------------------------------------------------
249// wxThread implementation
250// ----------------------------------------------------------------------------
251
252// wxThreadInternal class
253// ----------------------
254
255class wxThreadInternal
256{
257public:
258 wxThreadInternal(wxThread *thread)
259 {
260 m_thread = thread;
261 m_hThread = 0;
262 m_state = STATE_NEW;
263 m_priority = WXTHREAD_DEFAULT_PRIORITY;
264 m_nRef = 1;
265 }
266
267 ~wxThreadInternal()
268 {
269 Free();
270 }
271
272 void Free()
273 {
274 if ( m_hThread )
275 {
276 if ( !::CloseHandle(m_hThread) )
277 {
278 wxLogLastError(wxT("CloseHandle(thread)"));
279 }
280
281 m_hThread = 0;
282 }
283 }
284
285 // create a new (suspended) thread (for the given thread object)
286 bool Create(wxThread *thread, unsigned int stackSize);
287
288 // wait for the thread to terminate, either by itself, or by asking it
289 // (politely, this is not Kill()!) to do it
290 wxThreadError WaitForTerminate(wxCriticalSection& cs,
291 wxThread::ExitCode *pRc,
292 wxThread *threadToDelete = NULL);
293
294 // kill the thread unconditionally
295 wxThreadError Kill();
296
297 // suspend/resume/terminate
298 bool Suspend();
299 bool Resume();
300 void Cancel() { m_state = STATE_CANCELED; }
301
302 // thread state
303 void SetState(wxThreadState state) { m_state = state; }
304 wxThreadState GetState() const { return m_state; }
305
306 // thread priority
307 void SetPriority(unsigned int priority);
308 unsigned int GetPriority() const { return m_priority; }
309
310 // thread handle and id
311 HANDLE GetHandle() const { return m_hThread; }
312 DWORD GetId() const { return m_tid; }
313
314 // thread function
315 static THREAD_RETVAL THREAD_CALLCONV WinThreadStart(void *thread);
316
317 void KeepAlive()
318 {
319 if ( m_thread->IsDetached() )
320 ::InterlockedIncrement(&m_nRef);
321 }
322
323 void LetDie()
324 {
325 if ( m_thread->IsDetached() && !::InterlockedDecrement(&m_nRef) )
326 delete m_thread;
327 }
328
329private:
330 // the thread we're associated with
331 wxThread *m_thread;
332
333 HANDLE m_hThread; // handle of the thread
334 wxThreadState m_state; // state, see wxThreadState enum
335 unsigned int m_priority; // thread priority in "wx" units
336 DWORD m_tid; // thread id
337
338 // number of threads which need this thread to remain alive, when the count
339 // reaches 0 we kill the owning wxThread -- and die ourselves with it
340 LONG m_nRef;
341
342 DECLARE_NO_COPY_CLASS(wxThreadInternal)
343};
344
345// small class which keeps a thread alive during its lifetime
346class wxThreadKeepAlive
347{
348public:
349 wxThreadKeepAlive(wxThreadInternal& thrImpl) : m_thrImpl(thrImpl)
350 { m_thrImpl.KeepAlive(); }
351 ~wxThreadKeepAlive()
352 { m_thrImpl.LetDie(); }
353
354private:
355 wxThreadInternal& m_thrImpl;
356};
357
358
359THREAD_RETVAL THREAD_CALLCONV wxThreadInternal::WinThreadStart(void *param)
360{
361 THREAD_RETVAL rc;
362
363 return rc;
364}
365
366void wxThreadInternal::SetPriority(unsigned int priority)
367{
368}
369
370bool wxThreadInternal::Create(wxThread *thread, unsigned int stackSize)
371{
372 return false;
373}
374
375wxThreadError wxThreadInternal::Kill()
376{
377 return wxTHREAD_NO_ERROR;
378}
379
380wxThreadError
381wxThreadInternal::WaitForTerminate(wxCriticalSection& cs,
382 wxThread::ExitCode *pRc,
383 wxThread *threadToDelete)
384{
385 return wxTHREAD_NO_ERROR;
386}
387
388bool wxThreadInternal::Suspend()
389{
390 return true;
391}
392
393bool wxThreadInternal::Resume()
394{
395 return true;
396}
397
398// static functions
399// ----------------
400
401wxThread *wxThread::This()
402{
403 return NULL;
404}
405
406bool wxThread::IsMain()
407{
408 return true;
409}
410
411void wxThread::Yield()
412{
413}
414
ffecfa5a
JS
415int wxThread::GetCPUCount()
416{
417 return 1;
418}
419
420unsigned long wxThread::GetCurrentId()
421{
422 return 0;
423}
424
425bool wxThread::SetConcurrency(size_t level)
426{
427 return true;
428}
429
430// ctor and dtor
431// -------------
432
433wxThread::wxThread(wxThreadKind kind)
434{
435}
436
437wxThread::~wxThread()
438{
439}
440
441// create/start thread
442// -------------------
443
444wxThreadError wxThread::Create(unsigned int stackSize)
445{
446 return wxTHREAD_NO_ERROR;
447}
448
449wxThreadError wxThread::Run()
450{
451 return wxTHREAD_RUNNING;
452}
453
454// suspend/resume thread
455// ---------------------
456
457wxThreadError wxThread::Pause()
458{
459 return wxTHREAD_NO_ERROR;
460}
461
462wxThreadError wxThread::Resume()
463{
464 return wxTHREAD_NO_ERROR;
465}
466
467// stopping thread
468// ---------------
469
470wxThread::ExitCode wxThread::Wait()
471{
472 return 0;
473}
474
475wxThreadError wxThread::Delete(ExitCode *pRc)
476{
477 return wxTHREAD_NO_ERROR;
478}
479
480wxThreadError wxThread::Kill()
481{
482 return wxTHREAD_NO_ERROR;
483}
484
485void wxThread::Exit(ExitCode status)
486{
487}
488
489// priority setting
490// ----------------
491
492void wxThread::SetPriority(unsigned int prio)
493{
494}
495
496unsigned int wxThread::GetPriority() const
497{
498 return 1;
499}
500
501unsigned long wxThread::GetId() const
502{
503 return 0;
504}
505
506bool wxThread::IsRunning() const
507{
508 return true;
509}
510
511bool wxThread::IsAlive() const
512{
513 return true;
514}
515
516bool wxThread::IsPaused() const
517{
518 return false;
519}
520
521bool wxThread::TestDestroy()
522{
523 return true;
524}
525
526// ----------------------------------------------------------------------------
527// Automatic initialization for thread module
528// ----------------------------------------------------------------------------
529
530class wxThreadModule : public wxModule
531{
532public:
533 virtual bool OnInit();
534 virtual void OnExit();
535
536private:
537 DECLARE_DYNAMIC_CLASS(wxThreadModule)
538};
539
540IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule)
541
542bool wxThreadModule::OnInit()
543{
544 return true;
545}
546
547void wxThreadModule::OnExit()
548{
549}
550
551// ----------------------------------------------------------------------------
552// under Windows, these functions are implemented using a critical section and
553// not a mutex, so the names are a bit confusing
554// ----------------------------------------------------------------------------
555
d254213e 556void wxMutexGuiEnterImpl()
ffecfa5a
JS
557{
558}
559
d254213e 560void wxMutexGuiLeaveImpl()
ffecfa5a
JS
561{
562}
563
564void WXDLLIMPEXP_BASE wxMutexGuiLeaveOrEnter()
565{
566}
567
568bool WXDLLIMPEXP_BASE wxGuiOwnedByMainThread()
569{
570 return true;
571}
572
573// wake up the main thread if it's in ::GetMessage()
574void WXDLLIMPEXP_BASE wxWakeUpMainThread()
575{
576}
577
578bool WXDLLIMPEXP_BASE wxIsWaitingForThread()
579{
580 return false;
581}
582
583// ----------------------------------------------------------------------------
584// include common implementation code
585// ----------------------------------------------------------------------------
586
587#include "wx/thrimpl.cpp"
588
589#endif // wxUSE_THREADS