]>
Commit | Line | Data |
---|---|---|
10b959e3 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: thread.h | |
3 | // Purpose: Thread API | |
4 | // Author: Guilhem Lavaux | |
5 | // Modified by: | |
6 | // Created: 04/13/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Guilhem Lavaux | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
c2dd8380 GL |
12 | #ifndef __THREADH__ |
13 | #define __THREADH__ | |
10b959e3 JS |
14 | |
15 | #ifdef __GNUG__ | |
16 | #pragma interface "thread.h" | |
17 | #endif | |
18 | ||
19 | #include "wx/object.h" | |
20 | #include "wx/setup.h" | |
21 | ||
22 | typedef enum { | |
23 | MUTEX_NO_ERROR=0, | |
24 | MUTEX_DEAD_LOCK, // Mutex has been already locked by THE CALLING thread | |
b89156b5 GL |
25 | MUTEX_BUSY, // Mutex has been already locked by ONE thread |
26 | MUTEX_UNLOCKED | |
10b959e3 JS |
27 | } wxMutexError; |
28 | ||
29 | typedef enum { | |
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 | |
c2dd8380 GL |
33 | THREAD_NOT_RUNNING, // The thread isn't running |
34 | THREAD_MISC_ERROR // Some other error | |
10b959e3 JS |
35 | } wxThreadError; |
36 | ||
37 | // defines the interval of priority. | |
38 | #define WXTHREAD_MIN_PRIORITY 0 | |
39 | #define WXTHREAD_DEFAULT_PRIORITY 50 | |
40 | #define WXTHREAD_MAX_PRIORITY 100 | |
41 | ||
42 | // --------------------------------------------------------------------------- | |
43 | // Mutex handler | |
c2dd8380 | 44 | class WXDLLEXPORT wxMutexInternal; |
10b959e3 JS |
45 | class WXDLLEXPORT wxMutex { |
46 | public: | |
47 | // constructor & destructor | |
ee4f8c2a JS |
48 | wxMutex(); |
49 | ~wxMutex(); | |
10b959e3 JS |
50 | |
51 | // Lock the mutex. | |
ee4f8c2a | 52 | wxMutexError Lock(); |
10b959e3 | 53 | // Try to lock the mutex: if it can't, returns immediately with an error. |
ee4f8c2a | 54 | wxMutexError TryLock(); |
10b959e3 | 55 | // Unlock the mutex. |
ee4f8c2a | 56 | wxMutexError Unlock(); |
10b959e3 JS |
57 | |
58 | // Returns true if the mutex is locked. | |
ee4f8c2a | 59 | bool IsLocked() const { return (m_locked > 0); } |
10b959e3 JS |
60 | protected: |
61 | friend class wxCondition; | |
62 | ||
63 | int m_locked; | |
64 | wxMutexInternal *p_internal; | |
65 | }; | |
66 | ||
67 | // --------------------------------------------------------------------------- | |
68 | // Condition handler. | |
69 | class wxConditionInternal; | |
70 | class WXDLLEXPORT wxCondition { | |
71 | public: | |
72 | // constructor & destructor | |
ee4f8c2a JS |
73 | wxCondition(); |
74 | ~wxCondition(); | |
10b959e3 | 75 | |
ee4f8c2a | 76 | // Waits indefinitely. |
10b959e3 JS |
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. | |
ee4f8c2a | 81 | void Signal(); |
10b959e3 | 82 | // Broadcasts to all "Waiters". |
ee4f8c2a | 83 | void Broadcast(); |
10b959e3 JS |
84 | private: |
85 | wxConditionInternal *p_internal; | |
86 | }; | |
87 | ||
88 | // --------------------------------------------------------------------------- | |
89 | // Thread management class | |
90 | class wxThreadInternal; | |
91 | class WXDLLEXPORT wxThread { | |
92 | public: | |
93 | // constructor & destructor. | |
ee4f8c2a JS |
94 | wxThread(); |
95 | virtual ~wxThread(); | |
10b959e3 JS |
96 | |
97 | // Create a new thread, this method should check there is only one thread | |
98 | // running by object. | |
ee4f8c2a | 99 | wxThreadError Create(); |
10b959e3 | 100 | |
ee4f8c2a JS |
101 | // Destroys the thread immediately if the defer flag isn't true. |
102 | wxThreadError Destroy(); | |
10b959e3 | 103 | |
c2dd8380 GL |
104 | // Pause a running thread |
105 | wxThreadError Pause(); | |
106 | ||
107 | // Resume a paused thread | |
108 | wxThreadError Resume(); | |
109 | ||
ee4f8c2a | 110 | // Switches on the defer flag. |
10b959e3 JS |
111 | void DeferDestroy(bool on); |
112 | ||
113 | // Waits for the termination of the thread. | |
ee4f8c2a | 114 | void *Join(); |
10b959e3 JS |
115 | |
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. | |
ee4f8c2a | 120 | int GetPriority() const; |
10b959e3 JS |
121 | |
122 | // Get the thread ID | |
ee4f8c2a | 123 | unsigned long GetID() const; |
10b959e3 JS |
124 | |
125 | // Returns true if the thread is alive. | |
ee4f8c2a | 126 | bool IsAlive() const; |
c2dd8380 GL |
127 | // Returns true if the thread is running (not paused, not killed). |
128 | bool IsRunning() const; | |
10b959e3 | 129 | // Returns true if the thread is the main thread (aka the GUI thread). |
ee4f8c2a | 130 | static bool IsMain(); |
10b959e3 JS |
131 | |
132 | // Called when thread exits. | |
ee4f8c2a | 133 | virtual void OnExit(); |
c2dd8380 GL |
134 | |
135 | // Returns the wxThread object which corresponds to the ID. | |
136 | static wxThread *GetThreadFromID(unsigned long id); | |
10b959e3 JS |
137 | protected: |
138 | // In case, the DIFFER flag is true, enables another thread to kill this one. | |
ee4f8c2a | 139 | void TestDestroy(); |
10b959e3 JS |
140 | // Exits from the current thread. |
141 | void Exit(void *status = NULL); | |
142 | private: | |
143 | // Entry point for the thread. | |
ee4f8c2a | 144 | virtual void *Entry() = 0; |
10b959e3 JS |
145 | |
146 | private: | |
147 | friend class wxThreadInternal; | |
148 | ||
149 | wxThreadInternal *p_internal; | |
150 | }; | |
151 | ||
152 | // --------------------------------------------------------------------------- | |
153 | // Global variables | |
154 | ||
75ed1d15 GL |
155 | // GUI mutex handling. |
156 | void WXDLLEXPORT wxMutexGuiEnter(); | |
157 | void WXDLLEXPORT wxMutexGuiLeave(); | |
10b959e3 JS |
158 | |
159 | #endif |