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