]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/threadsgi.cpp
* Posix/SGI/No threads added
[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>
21
22enum thread_state {
23 STATE_IDLE = 0,
24 STATE_RUNNING,
25 STATE_CANCELED,
26 STATE_EXITED
27};
28
29/////////////////////////////////////////////////////////////////////////////
30// Static variables
31/////////////////////////////////////////////////////////////////////////////
32
33static int p_mainid;
34wxMutex wxMainMutex;
35
36#include "threadgui.inc"
37
38/////////////////////////////////////////////////////////////////////////////
39// Unix implementations (SGI threads)
40/////////////////////////////////////////////////////////////////////////////
41
42class wxMutexInternal {
43public:
44 abilock_t p_mutex;
45};
46
47wxMutex::wxMutex()
48{
49 p_internal = new wxMutexInternal;
50 init_lock(&(p_internal->p_mutex));
51}
52
53wxMutex::~wxMutex()
54{
55}
56
57wxMutex::MutexError wxMutex::Lock(void)
58{
59 spin_lock(&(p_internal->p_mutex));
60 return NO_ERROR;
61}
62
63wxMutex::MutexError wxMutex::TryLock(void)
64{
65 if (acquire_lock(&(p_internal->p_mutex)) != 0)
66 return BUSY;
67 return NO_ERROR;
68}
69
70wxMutex::MutexError wxMutex::Unlock(void)
71{
72 release_lock(&(p_internal->p_mutex));
73 return NO_ERROR;
74}
75
76// GLH: Don't now how it works on SGI. Wolfram ?
77
78wxCondition::wxCondition(void) {}
79wxCondition::~wxCondition(void) {}
80int wxCondition::Wait(wxMutex& WXUNUSED(mutex)) { return 0;}
81int wxCondition::Wait(wxMutex& WXUNUSED(mutex), unsigned long WXUNUSED(sec),
82 unsigned long WXUNUSED(nsec)) { return 0; }
83int wxCondition::Signal(void) { return 0; }
84int wxCondition::Broadcast(void) { return 0; }
85
86class
87wxThreadPrivate {
88public:
89 wxThreadPrivate() { thread_id = 0; state = STATE_IDLE; }
90 ~wxThreadPrivate() {}
91 static void SprocStart(void *ptr);
92 static void SignalHandler(int sig);
93public:
94 int state, thread_id;
95 void* exit_status;
96};
97
98void wxThreadPrivate::SprocStart(void *ptr)
99{
100 void* status;
101
102 wxThread *thr = (wxThread *)ptr;
103
104 thr->p_internal->thread_id = getpid();
105 thr->p_internal->exit_status = 0;
106 status = thr->Entry();
107 thr->Exit(status);
108}
109
110void wxThread::Exit(void* status)
111{
112 wxThread* ptr = this;
113 THREAD_SEND_EXIT_MSG(ptr);
114 p_internal->state = STATE_EXITED;
115 p_internal->exit_status = status;
116 _exit(0);
117}
118
119wxThread::ThreadError wxThread::Create()
120{
121 if (p_internal->state != STATE_IDLE)
122 return RUNNING;
123 p_internal->state = STATE_RUNNING;
124 if (sproc(p_internal->SprocStart, PR_SALL, this) < 0) {
125 p_internal->state = STATE_IDLE;
126 return NO_RESOURCE;
127 }
128 return NO_ERROR;
129}
130
131void wxThread::Destroy()
132{
133 if (p_internal->state == STATE_RUNNING)
134 p_internal->state = STATE_CANCELED;
135}
136
137void *wxThread::Join()
138{
139 if (p_internal->state != STATE_IDLE) {
140 bool do_unlock = wxThread::IsMain();
141 int stat;
142
143 if (do_unlock)
144 wxMainMutex.Unlock();
145 waitpid(p_internal->thread_id, &stat, 0);
146 if (do_unlock)
147 wxMainMutex.Lock();
148 if (!WIFEXITED(stat) && !WIFSIGNALED(stat))
149 return 0;
150 p_internal->state = STATE_IDLE;
151 return p_internal->exit_status;
152 }
153 return 0;
154}
155
156unsigned long wxThread::GetID()
157{
158 return (unsigned long)p_internal->thread_id;
159}
160
161void wxThread::TestDestroy()
162{
163 if (p_internal->state == STATE_CANCELED) {
164 p_internal->exit_status = 0;
165 _exit(0);
166 }
167}
168
169void wxThread::SetPriority(int prio)
170{
171}
172
173int wxThread::GetPriority(void)
174{
175}
176
177bool wxThreadIsMain()
178{
179 return (int)getpid() == main_id;
180}
181
182wxThread::wxThread()
183{
184 p_internal = new wxThreadPrivate();
185}
186
187wxThread::~wxThread()
188{
189 Cancel();
190 Join();
191 delete p_internal;
192}
193
194// The default callback just joins the thread and throws away the result.
195void wxThread::OnExit()
196{
197 Join();
198}
199
200// Global initialization
201class wxThreadModule : public wxModule {
202 DECLARE_DYNAMIC_CLASS(wxThreadModule)
203public:
204 virtual bool OnInit(void) {
205 wxThreadGuiInit();
206 p_mainid = (int)getpid();
207 wxMainMutex.Lock();
208 }
209
210 virtual void OnExit(void) {
211 wxMainMutex.Unlock();
212 wxThreadGuiExit();
213 }
214};
215
216IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule)