]>
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 | ||
12 | #ifndef __THREADH__ | |
13 | #define __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 | |
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 | |
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 | |
ee4f8c2a JS |
47 | wxMutex(); |
48 | ~wxMutex(); | |
10b959e3 JS |
49 | |
50 | // Lock the mutex. | |
ee4f8c2a | 51 | wxMutexError Lock(); |
10b959e3 | 52 | // Try to lock the mutex: if it can't, returns immediately with an error. |
ee4f8c2a | 53 | wxMutexError TryLock(); |
10b959e3 | 54 | // Unlock the mutex. |
ee4f8c2a | 55 | wxMutexError Unlock(); |
10b959e3 JS |
56 | |
57 | // Returns true if the mutex is locked. | |
ee4f8c2a | 58 | bool IsLocked() const { return (m_locked > 0); } |
10b959e3 JS |
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 | |
ee4f8c2a JS |
72 | wxCondition(); |
73 | ~wxCondition(); | |
10b959e3 | 74 | |
ee4f8c2a | 75 | // Waits indefinitely. |
10b959e3 JS |
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. | |
ee4f8c2a | 80 | void Signal(); |
10b959e3 | 81 | // Broadcasts to all "Waiters". |
ee4f8c2a | 82 | void Broadcast(); |
10b959e3 JS |
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. | |
ee4f8c2a JS |
93 | wxThread(); |
94 | virtual ~wxThread(); | |
10b959e3 JS |
95 | |
96 | // Create a new thread, this method should check there is only one thread | |
97 | // running by object. | |
ee4f8c2a | 98 | wxThreadError Create(); |
10b959e3 | 99 | |
ee4f8c2a JS |
100 | // Destroys the thread immediately if the defer flag isn't true. |
101 | wxThreadError Destroy(); | |
10b959e3 | 102 | |
ee4f8c2a | 103 | // Switches on the defer flag. |
10b959e3 JS |
104 | void DeferDestroy(bool on); |
105 | ||
106 | // Waits for the termination of the thread. | |
ee4f8c2a | 107 | void *Join(); |
10b959e3 JS |
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. | |
ee4f8c2a | 113 | int GetPriority() const; |
10b959e3 JS |
114 | |
115 | // Get the thread ID | |
ee4f8c2a | 116 | unsigned long GetID() const; |
10b959e3 JS |
117 | |
118 | // Returns true if the thread is alive. | |
ee4f8c2a | 119 | bool IsAlive() const; |
10b959e3 | 120 | // Returns true if the thread is the main thread (aka the GUI thread). |
ee4f8c2a | 121 | static bool IsMain(); |
10b959e3 JS |
122 | |
123 | // Called when thread exits. | |
ee4f8c2a | 124 | virtual void OnExit(); |
10b959e3 JS |
125 | protected: |
126 | // In case, the DIFFER flag is true, enables another thread to kill this one. | |
ee4f8c2a | 127 | void TestDestroy(); |
10b959e3 JS |
128 | // Exits from the current thread. |
129 | void Exit(void *status = NULL); | |
130 | private: | |
131 | // Entry point for the thread. | |
ee4f8c2a | 132 | virtual void *Entry() = 0; |
10b959e3 JS |
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 |