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