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