-rw-r--r-- | noncore/multimedia/opieplayer2/threadutil.cpp | 2 | ||||
-rw-r--r-- | noncore/multimedia/opieplayer2/threadutil.h | 2 |
2 files changed, 2 insertions, 2 deletions
diff --git a/noncore/multimedia/opieplayer2/threadutil.cpp b/noncore/multimedia/opieplayer2/threadutil.cpp index 63863b4..4055c74 100644 --- a/noncore/multimedia/opieplayer2/threadutil.cpp +++ b/noncore/multimedia/opieplayer2/threadutil.cpp | |||
@@ -1,284 +1,284 @@ | |||
1 | /* This file is part of the KDE project | 1 | /* This file is part of the KDE project |
2 | Copyright (C) 1999 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 | #include "threadutil.h" | 20 | #include "threadutil.h" |
21 | 21 | ||
22 | #include <qsocketnotifier.h> | 22 | #include <qsocketnotifier.h> |
23 | 23 | ||
24 | #include <pthread.h> | 24 | #include <pthread.h> |
25 | #include <assert.h> | 25 | #include <assert.h> |
26 | #include <unistd.h> | 26 | #include <unistd.h> |
27 | 27 | ||
28 | using namespace ThreadUtil; | 28 | using namespace ThreadUtil; |
29 | 29 | ||
30 | struct Mutex::Data | 30 | struct Mutex::Data |
31 | { | 31 | { |
32 | Data() | 32 | Data() |
33 | { | 33 | { |
34 | pthread_mutex_init( &mutex, 0 ); | 34 | pthread_mutex_init( &mutex, 0 ); |
35 | } | 35 | } |
36 | ~Data() | 36 | ~Data() |
37 | { | 37 | { |
38 | pthread_mutex_destroy( &mutex ); | 38 | pthread_mutex_destroy( &mutex ); |
39 | } | 39 | } |
40 | 40 | ||
41 | pthread_mutex_t mutex; | 41 | pthread_mutex_t mutex; |
42 | }; | 42 | }; |
43 | 43 | ||
44 | Mutex::Mutex() | 44 | Mutex::Mutex() |
45 | : d( new Data ) | 45 | : d( new Data ) |
46 | { | 46 | { |
47 | } | 47 | } |
48 | 48 | ||
49 | Mutex::~Mutex() | 49 | Mutex::~Mutex() |
50 | { | 50 | { |
51 | delete d; | 51 | delete d; |
52 | } | 52 | } |
53 | 53 | ||
54 | void Mutex::lock() | 54 | void Mutex::lock() |
55 | { | 55 | { |
56 | pthread_mutex_lock( &d->mutex ); | 56 | pthread_mutex_lock( &d->mutex ); |
57 | } | 57 | } |
58 | 58 | ||
59 | void Mutex::unlock() | 59 | void Mutex::unlock() |
60 | { | 60 | { |
61 | pthread_mutex_unlock( &d->mutex ); | 61 | pthread_mutex_unlock( &d->mutex ); |
62 | } | 62 | } |
63 | 63 | ||
64 | bool Mutex::tryLock() | 64 | bool Mutex::tryLock() |
65 | { | 65 | { |
66 | return pthread_mutex_trylock( &d->mutex ) == 0; | 66 | return pthread_mutex_trylock( &d->mutex ) == 0; |
67 | } | 67 | } |
68 | 68 | ||
69 | bool Mutex::isLocked() | 69 | bool Mutex::isLocked() |
70 | { | 70 | { |
71 | if ( !tryLock() ) | 71 | if ( !tryLock() ) |
72 | return true; | 72 | return true; |
73 | 73 | ||
74 | unlock(); | 74 | unlock(); |
75 | return false; | 75 | return false; |
76 | } | 76 | } |
77 | 77 | ||
78 | struct WaitCondition::Data | 78 | struct WaitCondition::Data |
79 | { | 79 | { |
80 | Data() | 80 | Data() |
81 | { | 81 | { |
82 | int result = pthread_cond_init( &waitCondition, 0 ); | 82 | int result = pthread_cond_init( &waitCondition, 0 ); |
83 | assert( result == 0 ); | 83 | assert( result == 0 ); |
84 | } | 84 | } |
85 | ~Data() | 85 | ~Data() |
86 | { | 86 | { |
87 | pthread_cond_destroy( &waitCondition ); | 87 | pthread_cond_destroy( &waitCondition ); |
88 | } | 88 | } |
89 | 89 | ||
90 | pthread_cond_t waitCondition; | 90 | pthread_cond_t waitCondition; |
91 | }; | 91 | }; |
92 | 92 | ||
93 | WaitCondition::WaitCondition() | 93 | WaitCondition::WaitCondition() |
94 | : d( new Data ) | 94 | : d( new Data ) |
95 | { | 95 | { |
96 | } | 96 | } |
97 | 97 | ||
98 | WaitCondition::~WaitCondition() | 98 | WaitCondition::~WaitCondition() |
99 | { | 99 | { |
100 | delete d; | 100 | delete d; |
101 | } | 101 | } |
102 | 102 | ||
103 | bool WaitCondition::wait() | 103 | bool WaitCondition::wait() |
104 | { | 104 | { |
105 | Mutex m; | 105 | Mutex m; |
106 | m.lock(); | 106 | m.lock(); |
107 | return wait( m ); | 107 | return wait( m ); |
108 | } | 108 | } |
109 | 109 | ||
110 | bool WaitCondition::wait( Mutex &mutex ) | 110 | bool WaitCondition::wait( Mutex &mutex ) |
111 | { | 111 | { |
112 | return pthread_cond_wait( &d->waitCondition, &mutex.d->mutex ); | 112 | return pthread_cond_wait( &d->waitCondition, &mutex.d->mutex ); |
113 | } | 113 | } |
114 | 114 | ||
115 | void WaitCondition::wakeOne() | 115 | void WaitCondition::wakeOne() |
116 | { | 116 | { |
117 | pthread_cond_signal( &d->waitCondition ); | 117 | pthread_cond_signal( &d->waitCondition ); |
118 | } | 118 | } |
119 | 119 | ||
120 | void WaitCondition::wakeAll() | 120 | void WaitCondition::wakeAll() |
121 | { | 121 | { |
122 | pthread_cond_broadcast( &d->waitCondition ); | 122 | pthread_cond_broadcast( &d->waitCondition ); |
123 | } | 123 | } |
124 | 124 | ||
125 | OnewayNotifier::OnewayNotifier() | 125 | OnewayNotifier::OnewayNotifier() |
126 | { | 126 | { |
127 | int fds[ 2 ]; | 127 | int fds[ 2 ]; |
128 | pipe( fds ); | 128 | pipe( fds ); |
129 | m_readFd = fds[ 0 ]; | 129 | m_readFd = fds[ 0 ]; |
130 | m_writeFd = fds[ 1 ]; | 130 | m_writeFd = fds[ 1 ]; |
131 | 131 | ||
132 | m_notifier = new QSocketNotifier( m_readFd, QSocketNotifier::Read ); | 132 | m_notifier = new QSocketNotifier( m_readFd, QSocketNotifier::Read ); |
133 | connect( m_notifier, SIGNAL( activated( int ) ), | 133 | connect( m_notifier, SIGNAL( activated( int ) ), |
134 | this, SLOT( wakeUp() ) ); | 134 | this, SLOT( wakeUp() ) ); |
135 | } | 135 | } |
136 | 136 | ||
137 | OnewayNotifier::~OnewayNotifier() | 137 | OnewayNotifier::~OnewayNotifier() |
138 | { | 138 | { |
139 | delete m_notifier; | 139 | delete m_notifier; |
140 | 140 | ||
141 | ::close( m_readFd ); | 141 | ::close( m_readFd ); |
142 | ::close( m_writeFd ); | 142 | ::close( m_writeFd ); |
143 | } | 143 | } |
144 | 144 | ||
145 | void OnewayNotifier::notify() | 145 | void OnewayNotifier::notify() |
146 | { | 146 | { |
147 | const char c = 42; | 147 | const char c = 42; |
148 | ::write( m_writeFd, &c, 1 ); | 148 | ::write( m_writeFd, &c, 1 ); |
149 | } | 149 | } |
150 | 150 | ||
151 | void OnewayNotifier::wakeUp() | 151 | void OnewayNotifier::wakeUp() |
152 | { | 152 | { |
153 | char c = 0; | 153 | char c = 0; |
154 | 154 | ||
155 | if ( ::read( m_readFd, &c, 1 ) != 1 ) | 155 | if ( ::read( m_readFd, &c, 1 ) != 1 ) |
156 | return; | 156 | return; |
157 | 157 | ||
158 | emit awake(); | 158 | emit awake(); |
159 | } | 159 | } |
160 | 160 | ||
161 | ChannelMessage::ChannelMessage( int type ) | 161 | ChannelMessage::ChannelMessage( int type ) |
162 | : m_type( type ), m_isCall( false ), m_replied( false ), | 162 | : m_type( type ), m_isCall( false ), m_replied( false ), |
163 | m_inEventHandler( false ) | 163 | m_inEventHandler( false ) |
164 | { | 164 | { |
165 | } | 165 | } |
166 | 166 | ||
167 | ChannelMessage::~ChannelMessage() | 167 | ChannelMessage::~ChannelMessage() |
168 | { | 168 | { |
169 | if ( m_guard.isLocked() ) | 169 | if ( m_guard.isLocked() ) |
170 | m_guard.unlock(); | 170 | m_guard.unlock(); |
171 | } | 171 | } |
172 | 172 | ||
173 | void ChannelMessage::reply() | 173 | void ChannelMessage::reply() |
174 | { | 174 | { |
175 | if ( !m_isCall ) | 175 | if ( !m_isCall ) |
176 | { | 176 | { |
177 | qDebug( "ChannelMessage::reply() - can't reply oneway message!" ); | 177 | qDebug( "ChannelMessage::reply() - can't reply oneway message!" ); |
178 | return; | 178 | return; |
179 | } | 179 | } |
180 | 180 | ||
181 | if ( m_inEventHandler ) | 181 | if ( m_inEventHandler ) |
182 | { | 182 | { |
183 | m_replied = true; | 183 | m_replied = true; |
184 | return; | 184 | return; |
185 | } | 185 | } |
186 | 186 | ||
187 | m_condition.wakeOne(); | 187 | m_condition.wakeOne(); |
188 | m_guard.unlock(); | 188 | m_guard.unlock(); |
189 | } | 189 | } |
190 | 190 | ||
191 | struct Channel::Private | 191 | struct Channel::Private |
192 | { | 192 | { |
193 | Private() | 193 | Private() |
194 | { | 194 | { |
195 | ownerThread = pthread_self(); | 195 | ownerThread = pthread_self(); |
196 | } | 196 | } |
197 | 197 | ||
198 | pthread_t ownerThread; | 198 | pthread_t ownerThread; |
199 | }; | 199 | }; |
200 | 200 | ||
201 | Channel::Channel( QObject *parent, const char *name ) | 201 | Channel::Channel( QObject *parent, const char *name ) |
202 | : QObject( parent, name ), d( new Private ) | 202 | : QObject( parent, name ), d( new Private ) |
203 | { | 203 | { |
204 | connect( &m_notifier, SIGNAL( awake() ), | 204 | connect( &m_notifier, SIGNAL( awake() ), |
205 | this, SLOT( deliver() ) ); | 205 | this, SLOT( deliver() ) ); |
206 | } | 206 | } |
207 | 207 | ||
208 | Channel::~Channel() | 208 | Channel::~Channel() |
209 | { | 209 | { |
210 | delete d; | 210 | delete d; |
211 | } | 211 | } |
212 | 212 | ||
213 | void Channel::send( ChannelMessage *message, SendType type ) | 213 | void Channel::send( ChannelMessage *message, SendType type ) |
214 | { | 214 | { |
215 | if ( type == WaitForReply ) | 215 | if ( type == WaitForReply ) |
216 | { | 216 | { |
217 | message->m_guard.lock(); | 217 | message->m_guard.lock(); |
218 | message->m_isCall = true; | 218 | message->m_isCall = true; |
219 | } | 219 | } |
220 | 220 | ||
221 | m_pendingMessagesGuard.lock(); | 221 | m_pendingMessagesGuard.lock(); |
222 | m_pendingMessages << MsgEnvelope( type, message ); | 222 | m_pendingMessages << MsgEnvelope( type, message ); |
223 | m_pendingMessagesGuard.unlock(); | 223 | m_pendingMessagesGuard.unlock(); |
224 | 224 | ||
225 | if ( d->ownerThread == pthread_self() ) { | 225 | if ( d->ownerThread == pthread_self() ) { |
226 | assert( type != WaitForReply ); | 226 | assert( type != WaitForReply ); |
227 | 227 | ||
228 | deliver(); | 228 | deliver(); |
229 | } | 229 | } |
230 | else | 230 | else |
231 | m_notifier.notify(); | 231 | m_notifier.notify(); |
232 | //QThread::postEvent( this, new QCustomEvent( QEvent::User, envelope ) ); | 232 | //QThread::postEvent( this, new QCustomEvent( QEvent::User, envelope ) ); |
233 | 233 | ||
234 | if ( type == WaitForReply ) | 234 | if ( type == WaitForReply ) |
235 | { | 235 | { |
236 | message->m_condition.wait( message->m_guard ); | 236 | message->m_condition.wait( message->m_guard ); |
237 | message->m_guard.unlock(); | 237 | message->m_guard.unlock(); |
238 | } | 238 | } |
239 | } | 239 | } |
240 | 240 | ||
241 | void Channel::deliver() | 241 | void Channel::deliver() |
242 | { | 242 | { |
243 | AutoLock lock( m_pendingMessagesGuard ); | 243 | AutoLock lock( m_pendingMessagesGuard ); |
244 | 244 | ||
245 | while ( !m_pendingMessages.isEmpty() ) { | 245 | while ( !m_pendingMessages.isEmpty() ) { |
246 | MsgEnvelope envelope = m_pendingMessages.first(); | 246 | MsgEnvelope envelope = m_pendingMessages.first(); |
247 | 247 | ||
248 | m_pendingMessages.remove( m_pendingMessages.begin() ); | 248 | m_pendingMessages.remove( m_pendingMessages.begin() ); |
249 | 249 | ||
250 | m_pendingMessagesGuard.unlock(); | 250 | m_pendingMessagesGuard.unlock(); |
251 | deliverOne( envelope ); | 251 | deliverOne( envelope ); |
252 | m_pendingMessagesGuard.lock(); | 252 | m_pendingMessagesGuard.lock(); |
253 | } | 253 | } |
254 | } | 254 | } |
255 | 255 | ||
256 | void Channel::deliverOne( const MsgEnvelope &envelope ) | 256 | void Channel::deliverOne( const MsgEnvelope &envelope ) |
257 | { | 257 | { |
258 | ChannelMessage *msg = envelope.msg; | 258 | ChannelMessage *msg = envelope.msg; |
259 | 259 | ||
260 | assert( msg ); | 260 | assert( msg ); |
261 | 261 | ||
262 | if ( envelope.type == WaitForReply ) | 262 | if ( envelope.type == WaitForReply ) |
263 | { | 263 | { |
264 | msg->m_guard.lock(); | 264 | msg->m_guard.lock(); |
265 | msg->m_inEventHandler = true; | 265 | msg->m_inEventHandler = true; |
266 | } | 266 | } |
267 | 267 | ||
268 | receiveMessage( msg, envelope.type ); | 268 | receiveMessage( msg, envelope.type ); |
269 | 269 | ||
270 | if ( envelope.type == WaitForReply ) | 270 | if ( envelope.type == WaitForReply ) |
271 | { | 271 | { |
272 | msg->m_inEventHandler = false; | 272 | msg->m_inEventHandler = false; |
273 | if ( msg->m_replied ) | 273 | if ( msg->m_replied ) |
274 | { | 274 | { |
275 | msg->m_condition.wakeOne(); | 275 | msg->m_condition.wakeOne(); |
276 | // this is a bit tricky. we unlock only when we reply. | 276 | // this is a bit tricky. we unlock only when we reply. |
277 | // reply() does an unlock as well. | 277 | // reply() does an unlock as well. |
278 | msg->m_guard.unlock(); | 278 | msg->m_guard.unlock(); |
279 | } | 279 | } |
280 | } | 280 | } |
281 | } | 281 | } |
282 | 282 | ||
283 | /* vim: et sw=4 ts=4 | 283 | /* vim: et sw=4 ts=4 |
284 | */ | 284 | */ |
diff --git a/noncore/multimedia/opieplayer2/threadutil.h b/noncore/multimedia/opieplayer2/threadutil.h index bcb9db9..5cc4cdc 100644 --- a/noncore/multimedia/opieplayer2/threadutil.h +++ b/noncore/multimedia/opieplayer2/threadutil.h | |||
@@ -1,176 +1,176 @@ | |||
1 | /* This file is part of the KDE project | 1 | /* This file is part of the KDE project |
2 | Copyright (C) 1999 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 | namespace ThreadUtil | 29 | namespace ThreadUtil |
30 | { | 30 | { |
31 | 31 | ||
32 | class Mutex | 32 | class Mutex |
33 | { | 33 | { |
34 | friend class WaitCondition; | 34 | friend class WaitCondition; |
35 | public: | 35 | public: |
36 | Mutex(); | 36 | Mutex(); |
37 | ~Mutex(); | 37 | ~Mutex(); |
38 | 38 | ||
39 | void lock(); | 39 | void lock(); |
40 | void unlock(); | 40 | void unlock(); |
41 | bool tryLock(); | 41 | bool tryLock(); |
42 | bool isLocked(); | 42 | bool isLocked(); |
43 | 43 | ||
44 | private: | 44 | private: |
45 | struct Data; | 45 | struct Data; |
46 | Data *d; | 46 | Data *d; |
47 | 47 | ||
48 | Mutex( const Mutex & ); | 48 | Mutex( const Mutex & ); |
49 | Mutex &operator=( const Mutex & ); | 49 | Mutex &operator=( const Mutex & ); |
50 | }; | 50 | }; |
51 | 51 | ||
52 | class AutoLock | 52 | class AutoLock |
53 | { | 53 | { |
54 | public: | 54 | public: |
55 | AutoLock( Mutex &mutex ) : m_mutex( mutex ) { m_mutex.lock(); } | 55 | AutoLock( Mutex &mutex ) : m_mutex( mutex ) { m_mutex.lock(); } |
56 | ~AutoLock() { m_mutex.unlock(); } | 56 | ~AutoLock() { m_mutex.unlock(); } |
57 | 57 | ||
58 | Mutex *operator &() const { return &m_mutex; } | 58 | Mutex *operator &() const { return &m_mutex; } |
59 | 59 | ||
60 | private: | 60 | private: |
61 | Mutex &m_mutex; | 61 | Mutex &m_mutex; |
62 | }; | 62 | }; |
63 | 63 | ||
64 | class WaitCondition | 64 | class WaitCondition |
65 | { | 65 | { |
66 | public: | 66 | public: |
67 | WaitCondition(); | 67 | WaitCondition(); |
68 | ~WaitCondition(); | 68 | ~WaitCondition(); |
69 | 69 | ||
70 | bool wait(); | 70 | bool wait(); |
71 | bool wait( Mutex &mutex ); | 71 | bool wait( Mutex &mutex ); |
72 | 72 | ||
73 | void wakeOne(); | 73 | void wakeOne(); |
74 | void wakeAll(); | 74 | void wakeAll(); |
75 | 75 | ||
76 | private: | 76 | private: |
77 | struct Data; | 77 | struct Data; |
78 | Data *d; | 78 | Data *d; |
79 | 79 | ||
80 | WaitCondition( const WaitCondition & ); | 80 | WaitCondition( const WaitCondition & ); |
81 | WaitCondition &operator=( const WaitCondition & ); | 81 | WaitCondition &operator=( const WaitCondition & ); |
82 | }; | 82 | }; |
83 | 83 | ||
84 | class OnewayNotifier : public QObject | 84 | class OnewayNotifier : public QObject |
85 | { | 85 | { |
86 | Q_OBJECT | 86 | Q_OBJECT |
87 | public: | 87 | public: |
88 | OnewayNotifier(); | 88 | OnewayNotifier(); |
89 | ~OnewayNotifier(); | 89 | ~OnewayNotifier(); |
90 | 90 | ||
91 | void notify(); | 91 | void notify(); |
92 | 92 | ||
93 | signals: | 93 | signals: |
94 | void awake(); | 94 | void awake(); |
95 | 95 | ||
96 | private slots: | 96 | private slots: |
97 | void wakeUp(); | 97 | void wakeUp(); |
98 | 98 | ||
99 | private: | 99 | private: |
100 | int m_readFd; | 100 | int m_readFd; |
101 | int m_writeFd; | 101 | int m_writeFd; |
102 | QSocketNotifier *m_notifier; | 102 | QSocketNotifier *m_notifier; |
103 | }; | 103 | }; |
104 | 104 | ||
105 | 105 | ||
106 | class Channel; | 106 | class Channel; |
107 | 107 | ||
108 | class ChannelMessage | 108 | class ChannelMessage |
109 | { | 109 | { |
110 | friend class Channel; | 110 | friend class Channel; |
111 | public: | 111 | public: |
112 | ChannelMessage( int type = -1 ); | 112 | ChannelMessage( int type = -1 ); |
113 | virtual ~ChannelMessage(); | 113 | virtual ~ChannelMessage(); |
114 | 114 | ||
115 | int type() const { return m_type; } | 115 | int type() const { return m_type; } |
116 | 116 | ||
117 | void reply(); | 117 | void reply(); |
118 | 118 | ||
119 | private: | 119 | private: |
120 | ChannelMessage( const ChannelMessage & ); | 120 | ChannelMessage( const ChannelMessage & ); |
121 | ChannelMessage &operator=( const ChannelMessage ); | 121 | ChannelMessage &operator=( const ChannelMessage ); |
122 | 122 | ||
123 | int m_type; | 123 | int m_type; |
124 | bool m_isCall : 1; | 124 | bool m_isCall : 1; |
125 | bool m_replied : 1; | 125 | bool m_replied : 1; |
126 | bool m_inEventHandler : 1; | 126 | bool m_inEventHandler : 1; |
127 | Mutex m_guard; | 127 | Mutex m_guard; |
128 | WaitCondition m_condition; | 128 | WaitCondition m_condition; |
129 | QGuardedPtr<Channel> m_channel; | 129 | QGuardedPtr<Channel> m_channel; |
130 | }; | 130 | }; |
131 | 131 | ||
132 | class Channel : public QObject | 132 | class Channel : public QObject |
133 | { | 133 | { |
134 | Q_OBJECT | 134 | Q_OBJECT |
135 | public: | 135 | public: |
136 | enum SendType { OneWay, WaitForReply }; | 136 | enum SendType { OneWay, WaitForReply }; |
137 | Channel( QObject *parent = 0, const char *name = 0 ); | 137 | Channel( QObject *parent = 0, const char *name = 0 ); |
138 | virtual ~Channel(); | 138 | virtual ~Channel(); |
139 | 139 | ||
140 | void send( ChannelMessage *message, SendType type ); | 140 | void send( ChannelMessage *message, SendType type ); |
141 | 141 | ||
142 | protected: | 142 | protected: |
143 | virtual void receiveMessage( ChannelMessage *message, SendType type ) = 0; | 143 | virtual void receiveMessage( ChannelMessage *message, SendType type ) = 0; |
144 | 144 | ||
145 | private slots: | 145 | private slots: |
146 | void deliver(); | 146 | void deliver(); |
147 | 147 | ||
148 | private: | 148 | private: |
149 | OnewayNotifier m_notifier; | 149 | OnewayNotifier m_notifier; |
150 | 150 | ||
151 | struct MsgEnvelope | 151 | struct MsgEnvelope |
152 | { | 152 | { |
153 | MsgEnvelope() : type( OneWay ), msg( 0 ) {} | 153 | MsgEnvelope() : type( OneWay ), msg( 0 ) {} |
154 | MsgEnvelope( SendType _type , ChannelMessage *_msg ) | 154 | MsgEnvelope( SendType _type , ChannelMessage *_msg ) |
155 | : type( _type ), msg( _msg ) {} | 155 | : type( _type ), msg( _msg ) {} |
156 | 156 | ||
157 | SendType type; | 157 | SendType type; |
158 | ChannelMessage *msg; | 158 | ChannelMessage *msg; |
159 | }; | 159 | }; |
160 | 160 | ||
161 | void deliverOne( const MsgEnvelope &envelope ); | 161 | void deliverOne( const MsgEnvelope &envelope ); |
162 | 162 | ||
163 | typedef QValueList<MsgEnvelope> MsgEnvelopeList; | 163 | typedef QValueList<MsgEnvelope> MsgEnvelopeList; |
164 | 164 | ||
165 | MsgEnvelopeList m_pendingMessages; | 165 | MsgEnvelopeList m_pendingMessages; |
166 | Mutex m_pendingMessagesGuard; | 166 | Mutex m_pendingMessagesGuard; |
167 | 167 | ||
168 | struct Private; | 168 | struct Private; |
169 | Private *d; | 169 | Private *d; |
170 | }; | 170 | }; |
171 | 171 | ||
172 | } | 172 | } |
173 | 173 | ||
174 | #endif // THREADUTIL_H | 174 | #endif // THREADUTIL_H |
175 | /* vim: et sw=4 ts=4 | 175 | /* vim: et sw=4 ts=4 |
176 | */ | 176 | */ |