1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "thread.h"
19 #include "wx/object.h"
24 MUTEX_DEAD_LOCK
, // Mutex has been already locked by THE CALLING thread
25 MUTEX_BUSY
, // Mutex has been already locked by ONE thread
30 THREAD_NO_ERROR
=0, // No error
31 THREAD_NO_RESOURCE
, // No resource left to create a new thread
32 THREAD_RUNNING
, // The thread is already running
33 THREAD_NOT_RUNNING
, // The thread isn't running
34 THREAD_MISC_ERROR
// Some other error
37 // defines the interval of priority.
38 #define WXTHREAD_MIN_PRIORITY 0
39 #define WXTHREAD_DEFAULT_PRIORITY 50
40 #define WXTHREAD_MAX_PRIORITY 100
42 // ---------------------------------------------------------------------------
44 class WXDLLEXPORT wxMutexInternal
;
45 class WXDLLEXPORT wxMutex
{
47 // constructor & destructor
53 // Try to lock the mutex: if it can't, returns immediately with an error.
54 wxMutexError
TryLock();
56 wxMutexError
Unlock();
58 // Returns true if the mutex is locked.
59 bool IsLocked() const { return (m_locked
> 0); }
61 friend class wxCondition
;
64 wxMutexInternal
*p_internal
;
67 // ---------------------------------------------------------------------------
69 class wxConditionInternal
;
70 class WXDLLEXPORT wxCondition
{
72 // constructor & destructor
76 // Waits indefinitely.
77 void Wait(wxMutex
& mutex
);
78 // Waits until a signal is raised or the timeout is elapsed.
79 bool Wait(wxMutex
& mutex
, unsigned long sec
, unsigned long nsec
);
80 // Raises a signal: only one "Waiter" is released.
82 // Broadcasts to all "Waiters".
85 wxConditionInternal
*p_internal
;
88 // ---------------------------------------------------------------------------
89 // Thread management class
90 class wxThreadInternal
;
91 class WXDLLEXPORT wxThread
{
93 // constructor & destructor.
97 // Create a new thread, this method should check there is only one thread
99 wxThreadError
Create();
101 // Destroys the thread immediately if the defer flag isn't true.
102 wxThreadError
Destroy();
104 // Pause a running thread
105 wxThreadError
Pause();
107 // Resume a paused thread
108 wxThreadError
Resume();
110 // Switches on the defer flag.
111 void DeferDestroy(bool on
);
113 // Waits for the termination of the thread.
116 // Sets the priority to "prio". (Warning: The priority can only be set before
117 // the thread is created)
118 void SetPriority(int prio
);
119 // Get the current priority.
120 int GetPriority() const;
123 unsigned long GetID() const;
125 // Returns true if the thread is alive.
126 bool IsAlive() const;
127 // Returns true if the thread is running (not paused, not killed).
128 bool IsRunning() const;
129 // Returns true if the thread is the main thread (aka the GUI thread).
130 static bool IsMain();
132 // Called when thread exits.
133 virtual void OnExit();
135 // Returns the wxThread object which corresponds to the ID.
136 static wxThread
*GetThreadFromID(unsigned long id
);
138 // In case, the DIFFER flag is true, enables another thread to kill this one.
140 // Exits from the current thread.
141 void Exit(void *status
= NULL
);
143 // Entry point for the thread.
144 virtual void *Entry() = 0;
147 friend class wxThreadInternal
;
149 wxThreadInternal
*p_internal
;
152 // ---------------------------------------------------------------------------
155 // GUI mutex handling.
156 void WXDLLEXPORT
wxMutexGuiEnter();
157 void WXDLLEXPORT
wxMutexGuiLeave();