]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/threadsgi.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxThread (SGI) Implementation
4 // Author: Original from Wolfram Gloger/Guilhem Lavaux
8 // Copyright: (c) Wolfram Gloger (1996, 1997); Guilhem Lavaux (1998)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
15 #include "wx/thread.h"
21 #include <sys/prctl.h>
22 #include "wx/module.h"
37 /////////////////////////////////////////////////////////////////////////////
39 /////////////////////////////////////////////////////////////////////////////
44 #include "threadgui.inc"
46 /////////////////////////////////////////////////////////////////////////////
47 // Unix implementations (SGI threads)
48 /////////////////////////////////////////////////////////////////////////////
50 class wxMutexInternal
{
58 p_internal
= new wxMutexInternal
;
59 init_lock(&(p_internal
->p_mutex
));
65 wxLogDebug( "wxMutex warning: freeing a locked mutex (%d locks)\n", m_locked
);
69 wxMutexError
wxMutex::Lock()
71 spin_lock(&(p_internal
->p_mutex
));
73 return wxMUTEX_NO_ERROR
;
76 wxMutexError
wxMutex::TryLock()
78 if (acquire_lock(&(p_internal
->p_mutex
)) != 0)
81 return wxMUTEX_NO_ERROR
;
84 wxMutexError
wxMutex::Unlock()
87 return wxMUTEX_UNLOCKED
;
88 release_lock(&(p_internal
->p_mutex
));
90 return wxMUTEX_NO_ERROR
;
93 // GL: Don't know how it works on SGI. Wolfram ?
95 wxCondition::wxCondition() {}
96 wxCondition::~wxCondition() {}
97 int wxCondition::Wait(wxMutex
& WXUNUSED(mutex
)) { return 0;}
98 int wxCondition::Wait(wxMutex
& WXUNUSED(mutex
), unsigned long WXUNUSED(sec
),
99 unsigned long WXUNUSED(nsec
)) { return 0; }
100 int wxCondition::Signal() { return 0; }
101 int wxCondition::Broadcast() { return 0; }
106 wxThreadPrivate() { thread_id
= 0; state
= STATE_IDLE
; }
107 ~wxThreadPrivate() {}
108 static void SprocStart(void *ptr
);
109 static void SignalHandler(int sig
);
111 int state
, thread_id
;
115 void wxThreadPrivate::SprocStart(void *ptr
)
119 wxThread
*thr
= (wxThread
*)ptr
;
121 thr
->p_internal
->thread_id
= getpid();
122 thr
->p_internal
->exit_status
= 0;
123 status
= thr
->Entry();
127 void wxThread::Exit(void* status
)
129 wxThread
* ptr
= this;
130 THREAD_SEND_EXIT_MSG(ptr
);
131 p_internal
->state
= STATE_EXITED
;
132 p_internal
->exit_status
= status
;
136 wxThreadError
wxThread::Create()
138 if (p_internal
->state
!= STATE_IDLE
)
139 return wxTHREAD_RUNNING
;
140 p_internal
->state
= STATE_RUNNING
;
141 if (sproc(p_internal
->SprocStart
, PR_SALL
, this) < 0) {
142 p_internal
->state
= STATE_IDLE
;
143 return wxTHREAD_NO_RESOURCE
;
145 return wxTHREAD_NO_ERROR
;
148 wxThreadError
wxThread::Destroy()
150 if (p_internal
->state
== STATE_RUNNING
)
151 p_internal
->state
= STATE_CANCELED
;
153 return wxTHREAD_NO_ERROR
;
156 wxThreadError
wxThread::Pause()
158 return wxTHREAD_NO_ERROR
;
161 wxThreadError
wxThread::Resume()
163 return wxTHREAD_NO_ERROR
;
166 void *wxThread::Join()
168 if (p_internal
->state
!= STATE_IDLE
) {
169 bool do_unlock
= wxThread::IsMain();
173 wxMainMutex
->Unlock();
174 waitpid(p_internal
->thread_id
, &stat
, 0);
177 if (!WIFEXITED(stat
) && !WIFSIGNALED(stat
))
179 p_internal
->state
= STATE_IDLE
;
180 return p_internal
->exit_status
;
185 unsigned long wxThread::GetID() const
187 return (unsigned long)p_internal
->thread_id
;
190 void wxThread::TestDestroy()
192 if (p_internal
->state
== STATE_CANCELED
) {
193 p_internal
->exit_status
= 0;
198 void wxThread::SetPriority(int prio
)
202 int wxThread::GetPriority() const
207 bool wxThread::IsMain()
209 return (int)getpid() == main_id
;
212 bool wxThread::IsAlive() const
214 return (p_internal
->state
== STATE_RUNNING
);
217 bool wxThread::IsRunning() const
219 return (p_internal
->state
== STATE_RUNNING
);
224 p_internal
= new wxThreadPrivate();
227 wxThread::~wxThread()
234 // The default callback just joins the thread and throws away the result.
235 void wxThread::OnExit()
240 // Global initialization
242 class wxThreadModule
: public wxModule
245 virtual bool OnInit();
246 virtual void OnExit();
249 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
252 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
254 bool wxThreadModule::OnInit()
256 wxMainMutex
= new wxMutex();
258 p_mainid
= (int)getpid();
263 void wxThreadModule::OnExit()
265 wxMainMutex
->Unlock();