1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "thread.h"
19 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
22 #include "wx/object.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
31 wxMUTEX_DEAD_LOCK
, // Mutex has been already locked by THE CALLING thread
32 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 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
53 class WXDLLEXPORT wxMutexInternal
;
54 class WXDLLEXPORT wxMutex
{
56 // constructor & destructor
62 // Try to lock the mutex: if it can't, returns immediately with an error.
63 wxMutexError
TryLock();
65 wxMutexError
Unlock();
67 // Returns true if the mutex is locked.
68 bool IsLocked() const { return (m_locked
> 0); }
71 friend class wxCondition
;
74 wxMutexInternal
*p_internal
;
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
80 class wxConditionInternal
;
81 class WXDLLEXPORT wxCondition
{
83 // constructor & destructor
87 // Waits indefinitely.
88 void Wait(wxMutex
& mutex
);
89 // Waits until a signal is raised or the timeout is elapsed.
90 bool Wait(wxMutex
& mutex
, unsigned long sec
, unsigned long nsec
);
91 // Raises a signal: only one "Waiter" is released.
93 // Broadcasts to all "Waiters".
97 wxConditionInternal
*p_internal
;
100 // ----------------------------------------------------------------------------
101 // Thread management class
102 // ----------------------------------------------------------------------------
103 class wxThreadInternal
;
104 class WXDLLEXPORT wxThread
{
106 // constructor & destructor.
110 // Create a new thread, this method should check there is only one thread
111 // running by object.
112 wxThreadError
Create();
114 // Destroys the thread immediately if the defer flag isn't true.
115 wxThreadError
Destroy();
117 // Pause a running thread
118 wxThreadError
Pause();
120 // Resume a paused thread
121 wxThreadError
Resume();
123 // Switches on the defer flag.
124 void DeferDestroy(bool on
);
126 // Waits for the termination of the thread.
129 // Sets the priority to "prio". (Warning: The priority can only be set before
130 // the thread is created)
131 void SetPriority(int prio
);
132 // Get the current priority.
133 int GetPriority() const;
136 unsigned long GetID() const;
138 // Returns true if the thread is alive.
139 bool IsAlive() const;
140 // Returns true if the thread is running (not paused, not killed).
141 bool IsRunning() const;
142 // Returns true if the thread is suspended
143 bool IsPaused() const { return IsAlive() && !IsRunning(); }
145 // Returns true if the thread is the main thread (aka the GUI thread).
146 static bool IsMain();
148 // Called when thread exits.
149 virtual void OnExit();
152 // In case, the DIFFER flag is true, enables another thread to kill this one.
154 // Exits from the current thread.
155 void Exit(void *status
= NULL
);
158 // Entry point for the thread.
159 virtual void *Entry() = 0;
162 friend class wxThreadInternal
;
164 wxThreadInternal
*p_internal
;
167 // ----------------------------------------------------------------------------
168 // Global functions and variables
169 // ----------------------------------------------------------------------------
171 // GUI mutex handling.
172 void WXDLLEXPORT
wxMutexGuiEnter();
173 void WXDLLEXPORT
wxMutexGuiLeave();
175 #endif // __THREADH__