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"
21 #include "wx/module.h"
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
30 wxMUTEX_DEAD_LOCK
, // Mutex has been already locked by THE CALLING thread
31 wxMUTEX_BUSY
, // Mutex has been already locked by ONE thread
38 wxTHREAD_NO_ERROR
= 0, // No error
39 wxTHREAD_NO_RESOURCE
, // No resource left to create a new thread
40 wxTHREAD_RUNNING
, // The thread is already running
41 wxTHREAD_NOT_RUNNING
, // The thread isn't running
42 wxTHREAD_MISC_ERROR
// Some other error
45 /* defines the interval of priority. */
46 #define WXTHREAD_MIN_PRIORITY 0
47 #define WXTHREAD_DEFAULT_PRIORITY 50
48 #define WXTHREAD_MAX_PRIORITY 100
50 // ----------------------------------------------------------------------------
51 // GUI mutex handling.
52 // ----------------------------------------------------------------------------
54 void WXDLLEXPORT
wxMutexGuiEnter();
55 void WXDLLEXPORT
wxMutexGuiLeave();
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 class WXDLLEXPORT wxMutexInternal
;
62 class WXDLLEXPORT wxMutex
65 // constructor & destructor
71 // Try to lock the mutex: if it can't, returns immediately with an error.
72 wxMutexError
TryLock();
74 wxMutexError
Unlock();
76 // Returns true if the mutex is locked.
77 bool IsLocked() const { return (m_locked
> 0); }
80 friend class wxCondition
;
83 wxMutexInternal
*p_internal
;
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 class wxConditionInternal
;
91 class WXDLLEXPORT wxCondition
94 // constructor & destructor
98 // Waits indefinitely.
99 void Wait(wxMutex
& mutex
);
100 // Waits until a signal is raised or the timeout is elapsed.
101 bool Wait(wxMutex
& mutex
, unsigned long sec
, unsigned long nsec
);
102 // Raises a signal: only one "Waiter" is released.
104 // Broadcasts to all "Waiters".
108 wxConditionInternal
*p_internal
;
111 // ----------------------------------------------------------------------------
112 // Thread management class
113 // ----------------------------------------------------------------------------
115 class wxThreadInternal
;
116 class WXDLLEXPORT wxThread
119 // constructor & destructor.
123 // Create a new thread, this method should check there is only one thread
124 // running by object.
125 wxThreadError
Create();
127 // Destroys the thread immediately if the defer flag isn't true.
128 wxThreadError
Destroy();
130 // Pause a running thread
131 wxThreadError
Pause();
133 // Resume a paused thread
134 wxThreadError
Resume();
136 // Switches on the defer flag.
137 void DeferDestroy(bool on
);
139 // Waits for the termination of the thread.
142 // Sets the priority to "prio". (Warning: The priority can only be set before
143 // the thread is created)
144 void SetPriority(int prio
);
145 // Get the current priority.
146 int GetPriority() const;
149 unsigned long GetID() const;
151 // Returns true if the thread is alive.
152 bool IsAlive() const;
153 // Returns true if the thread is running (not paused, not killed).
154 bool IsRunning() const;
155 // Returns true if the thread is suspended
156 bool IsPaused() const { return IsAlive() && !IsRunning(); }
158 // Returns true if the thread is the main thread (aka the GUI thread).
159 static bool IsMain();
161 // Called when thread exits.
162 virtual void OnExit();
165 // In case, the DIFFER flag is true, enables another thread to kill this one.
167 // Exits from the current thread.
168 void Exit(void *status
= NULL
);
171 // Entry point for the thread.
172 virtual void *Entry() = 0;
175 friend class wxThreadInternal
;
177 wxThreadInternal
*p_internal
;
180 // ----------------------------------------------------------------------------
181 // Automatic initialization
182 // ----------------------------------------------------------------------------
184 class wxThreadModule
: public wxModule
186 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
191 virtual bool OnInit();
192 virtual void OnExit();
197 #endif // __THREADH__