]>
Commit | Line | Data |
---|---|---|
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 | ||
12 | #ifndef _WX_THREADH__ | |
13 | #define _WX_THREADH__ | |
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 | |
25 | MUTEX_BUSY, // Mutex has been already locked by ONE thread | |
26 | MUTEX_UNLOCKED | |
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 | |
33 | THREAD_NOT_RUNNING // The thread isn't running | |
34 | } wxThreadError; | |
35 | ||
36 | // defines the interval of priority. | |
37 | #define WXTHREAD_MIN_PRIORITY 0 | |
38 | #define WXTHREAD_DEFAULT_PRIORITY 50 | |
39 | #define WXTHREAD_MAX_PRIORITY 100 | |
40 | ||
41 | // --------------------------------------------------------------------------- | |
42 | // Mutex handler | |
43 | class wxMutexInternal; | |
44 | class WXDLLEXPORT wxMutex { | |
45 | public: | |
46 | // constructor & destructor | |
47 | wxMutex(); | |
48 | ~wxMutex(); | |
49 | ||
50 | // Lock the mutex. | |
51 | wxMutexError Lock(); | |
52 | // Try to lock the mutex: if it can't, returns immediately with an error. | |
53 | wxMutexError TryLock(); | |
54 | // Unlock the mutex. | |
55 | wxMutexError Unlock(); | |
56 | ||
57 | // Returns true if the mutex is locked. | |
58 | bool IsLocked() const { return (m_locked > 0); } | |
59 | protected: | |
60 | friend class wxCondition; | |
61 | ||
62 | int m_locked; | |
63 | wxMutexInternal *p_internal; | |
64 | }; | |
65 | ||
66 | // --------------------------------------------------------------------------- | |
67 | // Condition handler. | |
68 | class wxConditionInternal; | |
69 | class WXDLLEXPORT wxCondition { | |
70 | public: | |
71 | // constructor & destructor | |
72 | wxCondition(); | |
73 | ~wxCondition(); | |
74 | ||
75 | // Waits indefinitely. | |
76 | void Wait(wxMutex& mutex); | |
77 | // Waits until a signal is raised or the timeout is elapsed. | |
78 | bool Wait(wxMutex& mutex, unsigned long sec, unsigned long nsec); | |
79 | // Raises a signal: only one "Waiter" is released. | |
80 | void Signal(); | |
81 | // Broadcasts to all "Waiters". | |
82 | void Broadcast(); | |
83 | private: | |
84 | wxConditionInternal *p_internal; | |
85 | }; | |
86 | ||
87 | // --------------------------------------------------------------------------- | |
88 | // Thread management class | |
89 | class wxThreadInternal; | |
90 | class WXDLLEXPORT wxThread { | |
91 | public: | |
92 | // constructor & destructor. | |
93 | wxThread(); | |
94 | virtual ~wxThread(); | |
95 | ||
96 | // Create a new thread, this method should check there is only one thread | |
97 | // running by object. | |
98 | wxThreadError Create(); | |
99 | ||
100 | // Destroys the thread immediately if the defer flag isn't true. | |
101 | wxThreadError Destroy(); | |
102 | ||
103 | // Switches on the defer flag. | |
104 | void DeferDestroy(bool on); | |
105 | ||
106 | // Waits for the termination of the thread. | |
107 | void *Join(); | |
108 | ||
109 | // Sets the priority to "prio". (Warning: The priority can only be set before | |
110 | // the thread is created) | |
111 | void SetPriority(int prio); | |
112 | // Get the current priority. | |
113 | int GetPriority() const; | |
114 | ||
115 | // Get the thread ID | |
116 | unsigned long GetID() const; | |
117 | ||
118 | // Returns true if the thread is alive. | |
119 | bool IsAlive() const; | |
120 | // Returns true if the thread is the main thread (aka the GUI thread). | |
121 | static bool IsMain(); | |
122 | ||
123 | // Called when thread exits. | |
124 | virtual void OnExit(); | |
125 | protected: | |
126 | // In case, the DIFFER flag is true, enables another thread to kill this one. | |
127 | void TestDestroy(); | |
128 | // Exits from the current thread. | |
129 | void Exit(void *status = NULL); | |
130 | private: | |
131 | // Entry point for the thread. | |
132 | virtual void *Entry() = 0; | |
133 | ||
134 | private: | |
135 | friend class wxThreadInternal; | |
136 | ||
137 | wxThreadInternal *p_internal; | |
138 | }; | |
139 | ||
140 | // --------------------------------------------------------------------------- | |
141 | // Global variables | |
142 | ||
143 | // GUI mutex. | |
144 | WXDLLEXPORT_DATA(extern wxMutex) wxMainMutex; | |
145 | ||
146 | #endif |