/[pearpc]/src/tools/thread.cc
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /src/tools/thread.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Wed Sep 5 17:11:21 2007 UTC (11 years, 10 months ago) by dpavlin
File size: 3382 byte(s)
import upstream CVS
1 /*
2 * HT Editor
3 * thread.cc
4 *
5 * Copyright (C) 2003 Sebastian Biallas (sb@biallas.net)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <cstdio>
22 #include <cstdlib>
23
24 #include "debug.h"
25 #include "except.h"
26 #include "thread.h"
27
28 static void *start_thread_routine(void *t)
29 {
30 Thread *thread = ((Thread *)t);
31 thread->setState(threadRunning);
32 void *ret = thread->run();
33 if (thread->getState() == threadRunning) {
34 /*
35 * Thread reached end of run(), and most likely
36 * didn't call terminate(), so we do here
37 */
38 thread->terminate(ret);
39 }
40 return ret;
41 }
42
43 Thread::Thread()
44 {
45 mThreadValid = false;
46 mState = threadInited;
47 mSem = new Semaphore();
48 }
49
50 Thread::~Thread()
51 {
52 if (mState == threadStarting || mState == threadRunning) {
53 // in a perfect world, we'd throw an exception here
54 fprintf(stderr, "Attempt to kill running thread!\n");
55 // but world isn't perfect
56 exit(1);
57 }
58 if (mThreadValid) {
59 sys_destroy_thread(mThread);
60 }
61 }
62
63 void Thread::start(void *param)
64 {
65 setState(threadStarting);
66 mThreadValid = true;
67 sys_create_thread(&mThread, 0, start_thread_routine, this);
68 }
69
70 ThreadState Thread::getState()
71 {
72 return mState;
73 }
74
75 void Thread::setState(ThreadState state)
76 {
77 mSem->lock();
78 mState = state;
79 mSem->signalAll();
80 mSem->unlock();
81 }
82
83 void *Thread::waitForExit()
84 {
85 mSem->lock();
86 while (mState == threadStarting) {
87 mSem->wait();
88 }
89 mSem->unlock();
90 mSem->lock();
91 while (mState == threadRunning) {
92 mSem->wait();
93 }
94 mSem->unlock();
95 return NULL;
96 }
97
98 void Thread::waitForRunning()
99 {
100 mSem->lock();
101 while (mState == threadStarting) {
102 mSem->wait();
103 }
104 mSem->unlock();
105 }
106
107 void Thread::terminate(void *ret)
108 {
109 if (mState == threadRunning) {
110 ASSERT(mThreadValid);
111 setState(threadFinished);
112 sys_exit_thread(ret);
113 } else {
114 throw MsgException("Arrrrrrg. Please read the docu. You musn't call terminate() from outside the thread.");
115 }
116 }
117
118 /*
119 *
120 */
121 ThreadFunction::ThreadFunction(sys_thread_function f, void *aParam)
122 :Thread()
123 {
124 mFunc = f;
125 mParam = aParam;
126 }
127
128 void *ThreadFunction::run()
129 {
130 terminate(mFunc(mParam));
131 // terminate does not return
132 return NULL;
133 }
134
135 /*
136 *
137 */
138 Mutex::Mutex()
139 {
140 sys_create_mutex(&mutex);
141 }
142
143 Mutex::~Mutex()
144 {
145 sys_destroy_mutex(mutex);
146 }
147
148 void Mutex::lock()
149 {
150 sys_lock_mutex(mutex);
151 }
152
153 void Mutex::unlock()
154 {
155 sys_unlock_mutex(mutex);
156 }
157
158 int Mutex::trylock()
159 {
160 return sys_trylock_mutex(mutex);
161 }
162
163 /*
164 *
165 */
166
167 Semaphore::Semaphore()
168 {
169 sys_create_semaphore(&sem);
170 }
171
172 Semaphore::~Semaphore()
173 {
174 sys_destroy_semaphore(sem);
175 }
176
177 void Semaphore::signal()
178 {
179 sys_signal_semaphore(sem);
180 }
181
182 void Semaphore::signalAll()
183 {
184 sys_signal_all_semaphore(sem);
185 }
186
187 void Semaphore::wait()
188 {
189 sys_wait_semaphore(sem);
190 }
191
192 void Semaphore::lock()
193 {
194 sys_lock_semaphore(sem);
195 }
196
197 void Semaphore::unlock()
198 {
199 sys_unlock_semaphore(sem);
200 }
201

  ViewVC Help
Powered by ViewVC 1.1.26