]>
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 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "thread.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
18 #include "wx/thread.h"
24 #include <sys/prctl.h>
25 #include "wx/module.h"
40 /////////////////////////////////////////////////////////////////////////////
42 /////////////////////////////////////////////////////////////////////////////
47 #include "threadgui.inc"
49 /////////////////////////////////////////////////////////////////////////////
50 // Unix implementations (SGI threads)
51 /////////////////////////////////////////////////////////////////////////////
53 class wxMutexInternal
{
61 p_internal
= new wxMutexInternal
;
62 init_lock(&(p_internal
->p_mutex
));
68 wxLogDebug( "wxMutex warning: freeing a locked mutex (%d locks)\n", m_locked
);
72 wxMutexError
wxMutex::Lock()
74 spin_lock(&(p_internal
->p_mutex
));
76 return wxMUTEX_NO_ERROR
;
79 wxMutexError
wxMutex::TryLock()
81 if (acquire_lock(&(p_internal
->p_mutex
)) != 0)
84 return wxMUTEX_NO_ERROR
;
87 wxMutexError
wxMutex::Unlock()
90 return wxMUTEX_UNLOCKED
;
91 release_lock(&(p_internal
->p_mutex
));
93 return wxMUTEX_NO_ERROR
;
96 // GL: Don't know how it works on SGI. Wolfram ?
98 wxCondition::wxCondition() {}
99 wxCondition::~wxCondition() {}
100 int wxCondition::Wait(wxMutex
& WXUNUSED(mutex
)) { return 0;}
101 int wxCondition::Wait(wxMutex
& WXUNUSED(mutex
), unsigned long WXUNUSED(sec
),
102 unsigned long WXUNUSED(nsec
)) { return 0; }
103 int wxCondition::Signal() { return 0; }
104 int wxCondition::Broadcast() { return 0; }
109 wxThreadPrivate() { thread_id
= 0; state
= STATE_IDLE
; }
110 ~wxThreadPrivate() {}
111 static void SprocStart(void *ptr
);
112 static void SignalHandler(int sig
);
114 int state
, thread_id
;
118 void wxThreadPrivate::SprocStart(void *ptr
)
122 wxThread
*thr
= (wxThread
*)ptr
;
124 thr
->p_internal
->thread_id
= getpid();
125 thr
->p_internal
->exit_status
= 0;
126 status
= thr
->Entry();
130 void wxThread::Exit(void* status
)
132 wxThread
* ptr
= this;
133 THREAD_SEND_EXIT_MSG(ptr
);
134 p_internal
->state
= STATE_EXITED
;
135 p_internal
->exit_status
= status
;
139 wxThreadError
wxThread::Create()
141 if (p_internal
->state
!= STATE_IDLE
)
142 return wxTHREAD_RUNNING
;
143 p_internal
->state
= STATE_RUNNING
;
144 if (sproc(p_internal
->SprocStart
, PR_SALL
, this) < 0) {
145 p_internal
->state
= STATE_IDLE
;
146 return wxTHREAD_NO_RESOURCE
;
148 return wxTHREAD_NO_ERROR
;
151 wxThreadError
wxThread::Destroy()
153 if (p_internal
->state
== STATE_RUNNING
)
154 p_internal
->state
= STATE_CANCELED
;
156 return wxTHREAD_NO_ERROR
;
159 wxThreadError
wxThread::Pause()
161 return wxTHREAD_NO_ERROR
;
164 wxThreadError
wxThread::Resume()
166 return wxTHREAD_NO_ERROR
;
169 void *wxThread::Join()
171 if (p_internal
->state
!= STATE_IDLE
) {
172 bool do_unlock
= wxThread::IsMain();
176 wxMainMutex
->Unlock();
177 waitpid(p_internal
->thread_id
, &stat
, 0);
180 if (!WIFEXITED(stat
) && !WIFSIGNALED(stat
))
182 p_internal
->state
= STATE_IDLE
;
183 return p_internal
->exit_status
;
188 unsigned long wxThread::GetID() const
190 return (unsigned long)p_internal
->thread_id
;
193 void wxThread::TestDestroy()
195 if (p_internal
->state
== STATE_CANCELED
) {
196 p_internal
->exit_status
= 0;
201 void wxThread::SetPriority(int prio
)
205 int wxThread::GetPriority() const
210 bool wxThread::IsMain()
212 return (int)getpid() == main_id
;
215 bool wxThread::IsAlive() const
217 return (p_internal
->state
== STATE_RUNNING
);
220 bool wxThread::IsRunning() const
222 return (p_internal
->state
== STATE_RUNNING
);
227 p_internal
= new wxThreadPrivate();
230 wxThread::~wxThread()
237 // The default callback just joins the thread and throws away the result.
238 void wxThread::OnExit()
243 // Global initialization
245 class wxThreadModule
: public wxModule
248 virtual bool OnInit();
249 virtual void OnExit();
252 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
255 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
257 bool wxThreadModule::OnInit()
259 wxMainMutex
= new wxMutex();
261 p_mainid
= (int)getpid();
266 void wxThreadModule::OnExit()
268 wxMainMutex
->Unlock();