]>
Commit | Line | Data |
---|---|---|
7c351dad GL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: threadsgi.cpp | |
3 | // Purpose: wxThread (SGI) Implementation | |
4 | // Author: Original from Wolfram Gloger/Guilhem Lavaux | |
5 | // Modified by: | |
6 | // Created: 04/22/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Wolfram Gloger (1996, 1997); Guilhem Lavaux (1998) | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "thread.h" | |
13 | #endif | |
14 | ||
15 | #include <stdio.h> | |
16 | #include <unistd.h> | |
17 | ||
18 | #include <signal.h> | |
19 | #include <sys/wait.h> | |
20 | #include <sys/prctl.h> | |
82052aff GL |
21 | #include "wx/thread.h" |
22 | #include "wx/module.h" | |
23 | #include "wx/utils.h" | |
3069ac4e | 24 | #include "wx/log.h" |
7c351dad GL |
25 | |
26 | enum thread_state { | |
27 | STATE_IDLE = 0, | |
28 | STATE_RUNNING, | |
29 | STATE_CANCELED, | |
30 | STATE_EXITED | |
31 | }; | |
32 | ||
33 | ///////////////////////////////////////////////////////////////////////////// | |
34 | // Static variables | |
35 | ///////////////////////////////////////////////////////////////////////////// | |
36 | ||
37 | static int p_mainid; | |
6773ae19 | 38 | wxMutex *wxMainMutex; |
7c351dad GL |
39 | |
40 | #include "threadgui.inc" | |
41 | ||
42 | ///////////////////////////////////////////////////////////////////////////// | |
43 | // Unix implementations (SGI threads) | |
44 | ///////////////////////////////////////////////////////////////////////////// | |
45 | ||
46 | class wxMutexInternal { | |
47 | public: | |
48 | abilock_t p_mutex; | |
49 | }; | |
50 | ||
51 | wxMutex::wxMutex() | |
52 | { | |
b89156b5 | 53 | m_locked = 0; |
7c351dad GL |
54 | p_internal = new wxMutexInternal; |
55 | init_lock(&(p_internal->p_mutex)); | |
56 | } | |
57 | ||
58 | wxMutex::~wxMutex() | |
59 | { | |
b89156b5 | 60 | if (m_locked > 0) |
3069ac4e | 61 | wxLogDebug( "wxMutex warning: freeing a locked mutex (%d locks)\n", m_locked ); |
b89156b5 | 62 | delete p_internal; |
7c351dad GL |
63 | } |
64 | ||
b89156b5 | 65 | wxMutexError wxMutex::Lock() |
7c351dad GL |
66 | { |
67 | spin_lock(&(p_internal->p_mutex)); | |
b89156b5 | 68 | m_locked++; |
cffee23b | 69 | return wxMUTEX_NO_ERROR; |
7c351dad GL |
70 | } |
71 | ||
b89156b5 | 72 | wxMutexError wxMutex::TryLock() |
7c351dad GL |
73 | { |
74 | if (acquire_lock(&(p_internal->p_mutex)) != 0) | |
cffee23b | 75 | return wxMUTEX_BUSY; |
b89156b5 | 76 | m_locked++; |
cffee23b | 77 | return wxMUTEX_NO_ERROR; |
7c351dad GL |
78 | } |
79 | ||
b89156b5 | 80 | wxMutexError wxMutex::Unlock() |
7c351dad | 81 | { |
b89156b5 | 82 | if (m_locked == 0) |
cffee23b | 83 | return wxMUTEX_UNLOCKED; |
7c351dad | 84 | release_lock(&(p_internal->p_mutex)); |
b89156b5 | 85 | m_locked--; |
cffee23b | 86 | return wxMUTEX_NO_ERROR; |
7c351dad GL |
87 | } |
88 | ||
b89156b5 | 89 | // GL: Don't know how it works on SGI. Wolfram ? |
7c351dad | 90 | |
ee4f8c2a JS |
91 | wxCondition::wxCondition() {} |
92 | wxCondition::~wxCondition() {} | |
7c351dad GL |
93 | int wxCondition::Wait(wxMutex& WXUNUSED(mutex)) { return 0;} |
94 | int wxCondition::Wait(wxMutex& WXUNUSED(mutex), unsigned long WXUNUSED(sec), | |
95 | unsigned long WXUNUSED(nsec)) { return 0; } | |
ee4f8c2a JS |
96 | int wxCondition::Signal() { return 0; } |
97 | int wxCondition::Broadcast() { return 0; } | |
7c351dad GL |
98 | |
99 | class | |
100 | wxThreadPrivate { | |
101 | public: | |
102 | wxThreadPrivate() { thread_id = 0; state = STATE_IDLE; } | |
103 | ~wxThreadPrivate() {} | |
104 | static void SprocStart(void *ptr); | |
105 | static void SignalHandler(int sig); | |
106 | public: | |
107 | int state, thread_id; | |
108 | void* exit_status; | |
109 | }; | |
110 | ||
111 | void wxThreadPrivate::SprocStart(void *ptr) | |
112 | { | |
113 | void* status; | |
114 | ||
115 | wxThread *thr = (wxThread *)ptr; | |
116 | ||
117 | thr->p_internal->thread_id = getpid(); | |
118 | thr->p_internal->exit_status = 0; | |
119 | status = thr->Entry(); | |
120 | thr->Exit(status); | |
121 | } | |
122 | ||
123 | void wxThread::Exit(void* status) | |
124 | { | |
125 | wxThread* ptr = this; | |
126 | THREAD_SEND_EXIT_MSG(ptr); | |
127 | p_internal->state = STATE_EXITED; | |
128 | p_internal->exit_status = status; | |
129 | _exit(0); | |
130 | } | |
131 | ||
b89156b5 | 132 | wxThreadError wxThread::Create() |
7c351dad GL |
133 | { |
134 | if (p_internal->state != STATE_IDLE) | |
cffee23b | 135 | return wxTHREAD_RUNNING; |
7c351dad GL |
136 | p_internal->state = STATE_RUNNING; |
137 | if (sproc(p_internal->SprocStart, PR_SALL, this) < 0) { | |
138 | p_internal->state = STATE_IDLE; | |
cffee23b | 139 | return wxTHREAD_NO_RESOURCE; |
7c351dad | 140 | } |
cffee23b | 141 | return wxTHREAD_NO_ERROR; |
7c351dad GL |
142 | } |
143 | ||
c2dd8380 | 144 | wxThreadError wxThread::Destroy() |
7c351dad GL |
145 | { |
146 | if (p_internal->state == STATE_RUNNING) | |
147 | p_internal->state = STATE_CANCELED; | |
c2dd8380 | 148 | |
cffee23b | 149 | return wxTHREAD_NO_ERROR; |
c2dd8380 GL |
150 | } |
151 | ||
152 | wxThreadError wxThread::Pause() | |
153 | { | |
cffee23b | 154 | return wxTHREAD_NO_ERROR; |
c2dd8380 GL |
155 | } |
156 | ||
157 | wxThreadError wxThread::Resume() | |
158 | { | |
cffee23b | 159 | return wxTHREAD_NO_ERROR; |
7c351dad GL |
160 | } |
161 | ||
162 | void *wxThread::Join() | |
163 | { | |
164 | if (p_internal->state != STATE_IDLE) { | |
165 | bool do_unlock = wxThread::IsMain(); | |
166 | int stat; | |
167 | ||
168 | if (do_unlock) | |
6773ae19 | 169 | wxMainMutex->Unlock(); |
7c351dad GL |
170 | waitpid(p_internal->thread_id, &stat, 0); |
171 | if (do_unlock) | |
6773ae19 | 172 | wxMainMutex->Lock(); |
7c351dad GL |
173 | if (!WIFEXITED(stat) && !WIFSIGNALED(stat)) |
174 | return 0; | |
175 | p_internal->state = STATE_IDLE; | |
176 | return p_internal->exit_status; | |
177 | } | |
178 | return 0; | |
179 | } | |
180 | ||
ee4f8c2a | 181 | unsigned long wxThread::GetID() const |
7c351dad GL |
182 | { |
183 | return (unsigned long)p_internal->thread_id; | |
184 | } | |
185 | ||
186 | void wxThread::TestDestroy() | |
187 | { | |
188 | if (p_internal->state == STATE_CANCELED) { | |
189 | p_internal->exit_status = 0; | |
190 | _exit(0); | |
191 | } | |
192 | } | |
193 | ||
194 | void wxThread::SetPriority(int prio) | |
195 | { | |
196 | } | |
197 | ||
ee4f8c2a | 198 | int wxThread::GetPriority() const |
7c351dad | 199 | { |
ee4f8c2a | 200 | return 0; |
7c351dad GL |
201 | } |
202 | ||
c2dd8380 | 203 | bool wxThread::IsMain() |
7c351dad GL |
204 | { |
205 | return (int)getpid() == main_id; | |
206 | } | |
207 | ||
c2dd8380 GL |
208 | bool wxThread::IsAlive() const |
209 | { | |
210 | return (p_internal->state == STATE_RUNNING); | |
211 | } | |
212 | ||
213 | bool wxThread::IsRunning() const | |
214 | { | |
215 | return (p_internal->state == STATE_RUNNING); | |
216 | } | |
217 | ||
7c351dad GL |
218 | wxThread::wxThread() |
219 | { | |
220 | p_internal = new wxThreadPrivate(); | |
221 | } | |
222 | ||
223 | wxThread::~wxThread() | |
224 | { | |
225 | Cancel(); | |
226 | Join(); | |
227 | delete p_internal; | |
228 | } | |
229 | ||
230 | // The default callback just joins the thread and throws away the result. | |
231 | void wxThread::OnExit() | |
232 | { | |
233 | Join(); | |
234 | } | |
235 | ||
236 | // Global initialization | |
237 | class wxThreadModule : public wxModule { | |
238 | DECLARE_DYNAMIC_CLASS(wxThreadModule) | |
239 | public: | |
ee4f8c2a | 240 | virtual bool OnInit() { |
6773ae19 | 241 | wxMainMutex = new wxMutex(); |
7c351dad GL |
242 | wxThreadGuiInit(); |
243 | p_mainid = (int)getpid(); | |
6773ae19 | 244 | wxMainMutex->Lock(); |
7c351dad GL |
245 | } |
246 | ||
ee4f8c2a | 247 | virtual void OnExit() { |
6773ae19 | 248 | wxMainMutex->Unlock(); |
7c351dad | 249 | wxThreadGuiExit(); |
6773ae19 | 250 | delete wxMainMutex; |
7c351dad GL |
251 | } |
252 | }; | |
253 | ||
254 | IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule) |