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