-rw-r--r-- | noncore/multimedia/opieplayer2/threadutil.cpp | 17 | ||||
-rw-r--r-- | noncore/multimedia/opieplayer2/threadutil.h | 6 |
2 files changed, 11 insertions, 12 deletions
diff --git a/noncore/multimedia/opieplayer2/threadutil.cpp b/noncore/multimedia/opieplayer2/threadutil.cpp index 7a9e1a4..ff38b1e 100644 --- a/noncore/multimedia/opieplayer2/threadutil.cpp +++ b/noncore/multimedia/opieplayer2/threadutil.cpp | |||
@@ -104,121 +104,126 @@ WaitCondition::~WaitCondition() | |||
104 | bool WaitCondition::wait() | 104 | bool WaitCondition::wait() |
105 | { | 105 | { |
106 | Mutex m; | 106 | Mutex m; |
107 | m.lock(); | 107 | m.lock(); |
108 | return wait( m ); | 108 | return wait( m ); |
109 | } | 109 | } |
110 | 110 | ||
111 | bool WaitCondition::wait( Mutex &mutex ) | 111 | bool WaitCondition::wait( Mutex &mutex ) |
112 | { | 112 | { |
113 | return pthread_cond_wait( &d->waitCondition, &mutex.d->mutex ); | 113 | return pthread_cond_wait( &d->waitCondition, &mutex.d->mutex ); |
114 | } | 114 | } |
115 | 115 | ||
116 | void WaitCondition::wakeOne() | 116 | void WaitCondition::wakeOne() |
117 | { | 117 | { |
118 | pthread_cond_signal( &d->waitCondition ); | 118 | pthread_cond_signal( &d->waitCondition ); |
119 | } | 119 | } |
120 | 120 | ||
121 | void WaitCondition::wakeAll() | 121 | void WaitCondition::wakeAll() |
122 | { | 122 | { |
123 | pthread_cond_broadcast( &d->waitCondition ); | 123 | pthread_cond_broadcast( &d->waitCondition ); |
124 | } | 124 | } |
125 | 125 | ||
126 | struct Thread::Data | 126 | struct Thread::Data |
127 | { | 127 | { |
128 | Data() : isRunning( false ) | 128 | Data() : isRunning( false ) |
129 | {} | 129 | {} |
130 | 130 | ||
131 | pthread_t self; | 131 | pthread_t self; |
132 | Mutex guard; | 132 | Mutex guard; |
133 | bool isRunning; | 133 | bool isRunning; |
134 | 134 | ||
135 | WaitCondition finishCondition; | 135 | WaitCondition finishCondition; |
136 | |||
137 | Thread *thr; | ||
138 | |||
139 | void run() { thr->run(); } | ||
136 | }; | 140 | }; |
137 | 141 | ||
138 | extern "C" | 142 | extern "C" |
139 | { | 143 | { |
140 | 144 | ||
141 | static void terminate_thread( void *arg ) | 145 | static void terminate_thread( void *arg ) |
142 | { | 146 | { |
143 | Thread::Data *data = ( Thread::Data* )arg; | 147 | Thread::Data *data = ( Thread::Data* )arg; |
144 | 148 | ||
145 | assert( data ); | 149 | assert( data ); |
146 | 150 | ||
147 | AutoLock locker( data->guard ); | 151 | AutoLock locker( data->guard ); |
148 | data->isRunning = false; | 152 | data->isRunning = false; |
149 | data->finishCondition.wakeAll(); | 153 | data->finishCondition.wakeAll(); |
150 | } | 154 | } |
151 | 155 | ||
152 | void *_threadutil_start_thread( void *arg ) | 156 | static void *start_thread( void *arg ) |
153 | { | 157 | { |
154 | Thread *thr = ( Thread* )arg; | 158 | Thread::Data *data = ( Thread::Data* )arg; |
155 | 159 | ||
156 | pthread_cleanup_push( terminate_thread, thr->d ); | 160 | pthread_cleanup_push( terminate_thread, data ); |
157 | 161 | ||
158 | thr->d->isRunning = true; | 162 | data->isRunning = true; |
159 | thr->run(); | 163 | data->run(); |
160 | 164 | ||
161 | pthread_cleanup_pop( true ); | 165 | pthread_cleanup_pop( true ); |
162 | 166 | ||
163 | Thread::exit(); | 167 | Thread::exit(); |
164 | return 0; // never reached | 168 | return 0; // never reached |
165 | } | 169 | } |
166 | 170 | ||
167 | } | 171 | } |
168 | 172 | ||
169 | Thread::Thread() | 173 | Thread::Thread() |
170 | : d( new Data ) | 174 | : d( new Data ) |
171 | { | 175 | { |
176 | d->thr = this; | ||
172 | } | 177 | } |
173 | 178 | ||
174 | Thread::~Thread() | 179 | Thread::~Thread() |
175 | { | 180 | { |
176 | assert( d->isRunning == false ); | 181 | assert( d->isRunning == false ); |
177 | delete d; | 182 | delete d; |
178 | } | 183 | } |
179 | 184 | ||
180 | void Thread::start() | 185 | void Thread::start() |
181 | { | 186 | { |
182 | AutoLock lock( d->guard ); | 187 | AutoLock lock( d->guard ); |
183 | 188 | ||
184 | if ( d->isRunning ) { | 189 | if ( d->isRunning ) { |
185 | qDebug( "ThreadUtil::Thread::start() called for running thread." ); | 190 | qDebug( "ThreadUtil::Thread::start() called for running thread." ); |
186 | return; | 191 | return; |
187 | } | 192 | } |
188 | 193 | ||
189 | pthread_attr_t attributes; | 194 | pthread_attr_t attributes; |
190 | pthread_attr_init( &attributes ); | 195 | pthread_attr_init( &attributes ); |
191 | pthread_attr_setscope( &attributes, PTHREAD_SCOPE_SYSTEM ); | 196 | pthread_attr_setscope( &attributes, PTHREAD_SCOPE_SYSTEM ); |
192 | int err = pthread_create( &d->self, &attributes, _threadutil_start_thread, ( void* )this ); | 197 | int err = pthread_create( &d->self, &attributes, start_thread, ( void* )d ); |
193 | if ( err != 0 ) { | 198 | if ( err != 0 ) { |
194 | qDebug( "ThreadUtil::Thread::start() : can't create thread: %s", strerror( err ) ); | 199 | qDebug( "ThreadUtil::Thread::start() : can't create thread: %s", strerror( err ) ); |
195 | pthread_attr_destroy( &attributes ); | 200 | pthread_attr_destroy( &attributes ); |
196 | return; | 201 | return; |
197 | } | 202 | } |
198 | pthread_attr_destroy( &attributes ); | 203 | pthread_attr_destroy( &attributes ); |
199 | } | 204 | } |
200 | 205 | ||
201 | void Thread::terminate() | 206 | void Thread::terminate() |
202 | { | 207 | { |
203 | AutoLock lock( d->guard ); | 208 | AutoLock lock( d->guard ); |
204 | if ( !d->isRunning ) | 209 | if ( !d->isRunning ) |
205 | return; | 210 | return; |
206 | 211 | ||
207 | pthread_cancel( d->self ); | 212 | pthread_cancel( d->self ); |
208 | } | 213 | } |
209 | 214 | ||
210 | bool Thread::wait() | 215 | bool Thread::wait() |
211 | { | 216 | { |
212 | AutoLock lock( d->guard ); | 217 | AutoLock lock( d->guard ); |
213 | if ( !d->isRunning ) | 218 | if ( !d->isRunning ) |
214 | return true; | 219 | return true; |
215 | 220 | ||
216 | return d->finishCondition.wait( d->guard ); | 221 | return d->finishCondition.wait( d->guard ); |
217 | } | 222 | } |
218 | 223 | ||
219 | bool Thread::isRunning() const | 224 | bool Thread::isRunning() const |
220 | { | 225 | { |
221 | AutoLock lock( d->guard ); | 226 | AutoLock lock( d->guard ); |
222 | return d->isRunning; | 227 | return d->isRunning; |
223 | } | 228 | } |
224 | 229 | ||
diff --git a/noncore/multimedia/opieplayer2/threadutil.h b/noncore/multimedia/opieplayer2/threadutil.h index 21ae6b2..2fd0c68 100644 --- a/noncore/multimedia/opieplayer2/threadutil.h +++ b/noncore/multimedia/opieplayer2/threadutil.h | |||
@@ -1,123 +1,117 @@ | |||
1 | /* This file is part of the KDE project | 1 | /* This file is part of the KDE project |
2 | Copyright (C) 2002 Simon Hausmann <hausmann@kde.org> | 2 | Copyright (C) 2002 Simon Hausmann <hausmann@kde.org> |
3 | 3 | ||
4 | This library is free software; you can redistribute it and/or | 4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Library General Public | 5 | modify it under the terms of the GNU Library General Public |
6 | License as published by the Free Software Foundation; either | 6 | License as published by the Free Software Foundation; either |
7 | version 2 of the License, or (at your option) any later version. | 7 | version 2 of the License, or (at your option) any later version. |
8 | 8 | ||
9 | This library is distributed in the hope that it will be useful, | 9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Library General Public License for more details. | 12 | Library General Public License for more details. |
13 | 13 | ||
14 | You should have received a copy of the GNU Library General Public License | 14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to | 15 | along with this library; see the file COPYING.LIB. If not, write to |
16 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 16 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
17 | Boston, MA 02111-1307, USA. | 17 | Boston, MA 02111-1307, USA. |
18 | */ | 18 | */ |
19 | 19 | ||
20 | #ifndef THREADUTIL_H | 20 | #ifndef THREADUTIL_H |
21 | #define THREADUTIL_H | 21 | #define THREADUTIL_H |
22 | 22 | ||
23 | #include <qvaluelist.h> | 23 | #include <qvaluelist.h> |
24 | #include <qobject.h> | 24 | #include <qobject.h> |
25 | #include <qguardedptr.h> | 25 | #include <qguardedptr.h> |
26 | 26 | ||
27 | class QSocketNotifier; | 27 | class QSocketNotifier; |
28 | 28 | ||
29 | extern "C" | ||
30 | { | ||
31 | void *_threadutil_start_thread( void* ); | ||
32 | } | ||
33 | |||
34 | namespace ThreadUtil | 29 | namespace ThreadUtil |
35 | { | 30 | { |
36 | 31 | ||
37 | class Mutex | 32 | class Mutex |
38 | { | 33 | { |
39 | friend class WaitCondition; | 34 | friend class WaitCondition; |
40 | public: | 35 | public: |
41 | Mutex(); | 36 | Mutex(); |
42 | ~Mutex(); | 37 | ~Mutex(); |
43 | 38 | ||
44 | void lock(); | 39 | void lock(); |
45 | void unlock(); | 40 | void unlock(); |
46 | bool tryLock(); | 41 | bool tryLock(); |
47 | bool isLocked(); | 42 | bool isLocked(); |
48 | 43 | ||
49 | private: | 44 | private: |
50 | struct Data; | 45 | struct Data; |
51 | Data *d; | 46 | Data *d; |
52 | 47 | ||
53 | Mutex( const Mutex & ); | 48 | Mutex( const Mutex & ); |
54 | Mutex &operator=( const Mutex & ); | 49 | Mutex &operator=( const Mutex & ); |
55 | }; | 50 | }; |
56 | 51 | ||
57 | class AutoLock | 52 | class AutoLock |
58 | { | 53 | { |
59 | public: | 54 | public: |
60 | AutoLock( Mutex &mutex ) : m_mutex( mutex ) { m_mutex.lock(); } | 55 | AutoLock( Mutex &mutex ) : m_mutex( mutex ) { m_mutex.lock(); } |
61 | ~AutoLock() { m_mutex.unlock(); } | 56 | ~AutoLock() { m_mutex.unlock(); } |
62 | 57 | ||
63 | Mutex *operator &() const { return &m_mutex; } | 58 | Mutex *operator &() const { return &m_mutex; } |
64 | 59 | ||
65 | private: | 60 | private: |
66 | Mutex &m_mutex; | 61 | Mutex &m_mutex; |
67 | }; | 62 | }; |
68 | 63 | ||
69 | class WaitCondition | 64 | class WaitCondition |
70 | { | 65 | { |
71 | public: | 66 | public: |
72 | WaitCondition(); | 67 | WaitCondition(); |
73 | ~WaitCondition(); | 68 | ~WaitCondition(); |
74 | 69 | ||
75 | bool wait(); | 70 | bool wait(); |
76 | bool wait( Mutex &mutex ); | 71 | bool wait( Mutex &mutex ); |
77 | 72 | ||
78 | void wakeOne(); | 73 | void wakeOne(); |
79 | void wakeAll(); | 74 | void wakeAll(); |
80 | 75 | ||
81 | private: | 76 | private: |
82 | struct Data; | 77 | struct Data; |
83 | Data *d; | 78 | Data *d; |
84 | 79 | ||
85 | WaitCondition( const WaitCondition & ); | 80 | WaitCondition( const WaitCondition & ); |
86 | WaitCondition &operator=( const WaitCondition & ); | 81 | WaitCondition &operator=( const WaitCondition & ); |
87 | }; | 82 | }; |
88 | 83 | ||
89 | class Thread | 84 | class Thread |
90 | { | 85 | { |
91 | friend void *::_threadutil_start_thread( void* ); | ||
92 | public: | 86 | public: |
93 | struct Data; | 87 | struct Data; |
94 | 88 | ||
95 | Thread(); | 89 | Thread(); |
96 | virtual ~Thread(); | 90 | virtual ~Thread(); |
97 | 91 | ||
98 | void start(); | 92 | void start(); |
99 | void terminate(); | 93 | void terminate(); |
100 | 94 | ||
101 | bool wait(); | 95 | bool wait(); |
102 | 96 | ||
103 | bool isRunning() const; | 97 | bool isRunning() const; |
104 | 98 | ||
105 | static void exit(); | 99 | static void exit(); |
106 | protected: | 100 | protected: |
107 | virtual void run() = 0; | 101 | virtual void run() = 0; |
108 | 102 | ||
109 | private: | 103 | private: |
110 | Data *d; | 104 | Data *d; |
111 | }; | 105 | }; |
112 | 106 | ||
113 | class OnewayNotifier : public QObject | 107 | class OnewayNotifier : public QObject |
114 | { | 108 | { |
115 | Q_OBJECT | 109 | Q_OBJECT |
116 | public: | 110 | public: |
117 | OnewayNotifier(); | 111 | OnewayNotifier(); |
118 | ~OnewayNotifier(); | 112 | ~OnewayNotifier(); |
119 | 113 | ||
120 | void notify(); | 114 | void notify(); |
121 | 115 | ||
122 | signals: | 116 | signals: |
123 | void awake(); | 117 | void awake(); |