Diffstat (limited to 'noncore/multimedia/opieplayer2/threadutil.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/multimedia/opieplayer2/threadutil.cpp | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/noncore/multimedia/opieplayer2/threadutil.cpp b/noncore/multimedia/opieplayer2/threadutil.cpp index 4055c74..5687f42 100644 --- a/noncore/multimedia/opieplayer2/threadutil.cpp +++ b/noncore/multimedia/opieplayer2/threadutil.cpp | |||
@@ -25,4 +25,5 @@ | |||
25 | #include <assert.h> | 25 | #include <assert.h> |
26 | #include <unistd.h> | 26 | #include <unistd.h> |
27 | #include <errno.h> | ||
27 | 28 | ||
28 | using namespace ThreadUtil; | 29 | using namespace ThreadUtil; |
@@ -123,4 +124,102 @@ void WaitCondition::wakeAll() | |||
123 | } | 124 | } |
124 | 125 | ||
126 | struct Thread::Data | ||
127 | { | ||
128 | Data() : isRunning( false ) | ||
129 | {} | ||
130 | |||
131 | pthread_t self; | ||
132 | Mutex guard; | ||
133 | bool isRunning; | ||
134 | |||
135 | WaitCondition finishCondition; | ||
136 | }; | ||
137 | |||
138 | extern "C" | ||
139 | { | ||
140 | |||
141 | void _threadutil_terminate_thread( void *arg ) | ||
142 | { | ||
143 | Thread *thr = ( Thread* )arg; | ||
144 | |||
145 | assert( thr ); | ||
146 | |||
147 | AutoLock locker( thr->d->guard ); | ||
148 | thr->d->isRunning = false; | ||
149 | thr->d->finishCondition.wakeAll(); | ||
150 | } | ||
151 | |||
152 | void *_threadutil_start_thread( void *arg ) | ||
153 | { | ||
154 | Thread *thr = ( Thread* )arg; | ||
155 | |||
156 | pthread_cleanup_push( _threadutil_terminate_thread, thr ); | ||
157 | |||
158 | thr->d->isRunning = true; | ||
159 | thr->run(); | ||
160 | |||
161 | pthread_cleanup_pop( true ); | ||
162 | |||
163 | Thread::exit(); | ||
164 | return 0; // never reached | ||
165 | } | ||
166 | |||
167 | } | ||
168 | |||
169 | Thread::Thread() | ||
170 | : d( new Data ) | ||
171 | { | ||
172 | } | ||
173 | |||
174 | Thread::~Thread() | ||
175 | { | ||
176 | assert( d->isRunning == false ); | ||
177 | delete d; | ||
178 | } | ||
179 | |||
180 | void Thread::start() | ||
181 | { | ||
182 | AutoLock lock( d->guard ); | ||
183 | |||
184 | if ( d->isRunning ) { | ||
185 | qDebug( "ThreadUtil::Thread::start() called for running thread." ); | ||
186 | return; | ||
187 | } | ||
188 | |||
189 | pthread_attr_t attributes; | ||
190 | pthread_attr_init( &attributes ); | ||
191 | pthread_attr_setscope( &attributes, PTHREAD_SCOPE_SYSTEM ); | ||
192 | int err = pthread_create( &d->self, &attributes, _threadutil_start_thread, ( void* )this ); | ||
193 | if ( err != 0 ) { | ||
194 | qDebug( "ThreadUtil::Thread::start() : can't create thread: %s", strerror( err ) ); | ||
195 | pthread_attr_destroy( &attributes ); | ||
196 | return; | ||
197 | } | ||
198 | pthread_attr_destroy( &attributes ); | ||
199 | } | ||
200 | |||
201 | void Thread::terminate() | ||
202 | { | ||
203 | AutoLock lock( d->guard ); | ||
204 | if ( !d->isRunning ) | ||
205 | return; | ||
206 | |||
207 | pthread_cancel( d->self ); | ||
208 | } | ||
209 | |||
210 | bool Thread::wait() | ||
211 | { | ||
212 | AutoLock lock( d->guard ); | ||
213 | if ( !d->isRunning ) | ||
214 | return true; | ||
215 | |||
216 | return d->finishCondition.wait( d->guard ); | ||
217 | } | ||
218 | |||
219 | void Thread::exit() | ||
220 | { | ||
221 | pthread_exit( 0 ); | ||
222 | } | ||
223 | |||
125 | OnewayNotifier::OnewayNotifier() | 224 | OnewayNotifier::OnewayNotifier() |
126 | { | 225 | { |