Diffstat (limited to 'noncore/multimedia/opieplayer2/threadutil.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/multimedia/opieplayer2/threadutil.cpp | 284 |
1 files changed, 284 insertions, 0 deletions
diff --git a/noncore/multimedia/opieplayer2/threadutil.cpp b/noncore/multimedia/opieplayer2/threadutil.cpp new file mode 100644 index 0000000..63863b4 --- a/dev/null +++ b/noncore/multimedia/opieplayer2/threadutil.cpp | |||
@@ -0,0 +1,284 @@ | |||
1 | /* This file is part of the KDE project | ||
2 | Copyright (C) 1999 Simon Hausmann <hausmann@kde.org> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Library General Public | ||
6 | License as published by the Free Software Foundation; either | ||
7 | version 2 of the License, or (at your option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | Library General Public License for more details. | ||
13 | |||
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 | ||
16 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
17 | Boston, MA 02111-1307, USA. | ||
18 | */ | ||
19 | |||
20 | #include "threadutil.h" | ||
21 | |||
22 | #include <qsocketnotifier.h> | ||
23 | |||
24 | #include <pthread.h> | ||
25 | #include <assert.h> | ||
26 | #include <unistd.h> | ||
27 | |||
28 | using namespace ThreadUtil; | ||
29 | |||
30 | struct Mutex::Data | ||
31 | { | ||
32 | Data() | ||
33 | { | ||
34 | pthread_mutex_init( &mutex, 0 ); | ||
35 | } | ||
36 | ~Data() | ||
37 | { | ||
38 | pthread_mutex_destroy( &mutex ); | ||
39 | } | ||
40 | |||
41 | pthread_mutex_t mutex; | ||
42 | }; | ||
43 | |||
44 | Mutex::Mutex() | ||
45 | : d( new Data ) | ||
46 | { | ||
47 | } | ||
48 | |||
49 | Mutex::~Mutex() | ||
50 | { | ||
51 | delete d; | ||
52 | } | ||
53 | |||
54 | void Mutex::lock() | ||
55 | { | ||
56 | pthread_mutex_lock( &d->mutex ); | ||
57 | } | ||
58 | |||
59 | void Mutex::unlock() | ||
60 | { | ||
61 | pthread_mutex_unlock( &d->mutex ); | ||
62 | } | ||
63 | |||
64 | bool Mutex::tryLock() | ||
65 | { | ||
66 | return pthread_mutex_trylock( &d->mutex ) == 0; | ||
67 | } | ||
68 | |||
69 | bool Mutex::isLocked() | ||
70 | { | ||
71 | if ( !tryLock() ) | ||
72 | return true; | ||
73 | |||
74 | unlock(); | ||
75 | return false; | ||
76 | } | ||
77 | |||
78 | struct WaitCondition::Data | ||
79 | { | ||
80 | Data() | ||
81 | { | ||
82 | int result = pthread_cond_init( &waitCondition, 0 ); | ||
83 | assert( result == 0 ); | ||
84 | } | ||
85 | ~Data() | ||
86 | { | ||
87 | pthread_cond_destroy( &waitCondition ); | ||
88 | } | ||
89 | |||
90 | pthread_cond_t waitCondition; | ||
91 | }; | ||
92 | |||
93 | WaitCondition::WaitCondition() | ||
94 | : d( new Data ) | ||
95 | { | ||
96 | } | ||
97 | |||
98 | WaitCondition::~WaitCondition() | ||
99 | { | ||
100 | delete d; | ||
101 | } | ||
102 | |||
103 | bool WaitCondition::wait() | ||
104 | { | ||
105 | Mutex m; | ||
106 | m.lock(); | ||
107 | return wait( m ); | ||
108 | } | ||
109 | |||
110 | bool WaitCondition::wait( Mutex &mutex ) | ||
111 | { | ||
112 | return pthread_cond_wait( &d->waitCondition, &mutex.d->mutex ); | ||
113 | } | ||
114 | |||
115 | void WaitCondition::wakeOne() | ||
116 | { | ||
117 | pthread_cond_signal( &d->waitCondition ); | ||
118 | } | ||
119 | |||
120 | void WaitCondition::wakeAll() | ||
121 | { | ||
122 | pthread_cond_broadcast( &d->waitCondition ); | ||
123 | } | ||
124 | |||
125 | OnewayNotifier::OnewayNotifier() | ||
126 | { | ||
127 | int fds[ 2 ]; | ||
128 | pipe( fds ); | ||
129 | m_readFd = fds[ 0 ]; | ||
130 | m_writeFd = fds[ 1 ]; | ||
131 | |||
132 | m_notifier = new QSocketNotifier( m_readFd, QSocketNotifier::Read ); | ||
133 | connect( m_notifier, SIGNAL( activated( int ) ), | ||
134 | this, SLOT( wakeUp() ) ); | ||
135 | } | ||
136 | |||
137 | OnewayNotifier::~OnewayNotifier() | ||
138 | { | ||
139 | delete m_notifier; | ||
140 | |||
141 | ::close( m_readFd ); | ||
142 | ::close( m_writeFd ); | ||
143 | } | ||
144 | |||
145 | void OnewayNotifier::notify() | ||
146 | { | ||
147 | const char c = 42; | ||
148 | ::write( m_writeFd, &c, 1 ); | ||
149 | } | ||
150 | |||
151 | void OnewayNotifier::wakeUp() | ||
152 | { | ||
153 | char c = 0; | ||
154 | |||
155 | if ( ::read( m_readFd, &c, 1 ) != 1 ) | ||
156 | return; | ||
157 | |||
158 | emit awake(); | ||
159 | } | ||
160 | |||
161 | ChannelMessage::ChannelMessage( int type ) | ||
162 | : m_type( type ), m_isCall( false ), m_replied( false ), | ||
163 | m_inEventHandler( false ) | ||
164 | { | ||
165 | } | ||
166 | |||
167 | ChannelMessage::~ChannelMessage() | ||
168 | { | ||
169 | if ( m_guard.isLocked() ) | ||
170 | m_guard.unlock(); | ||
171 | } | ||
172 | |||
173 | void ChannelMessage::reply() | ||
174 | { | ||
175 | if ( !m_isCall ) | ||
176 | { | ||
177 | qDebug( "ChannelMessage::reply() - can't reply oneway message!" ); | ||
178 | return; | ||
179 | } | ||
180 | |||
181 | if ( m_inEventHandler ) | ||
182 | { | ||
183 | m_replied = true; | ||
184 | return; | ||
185 | } | ||
186 | |||
187 | m_condition.wakeOne(); | ||
188 | m_guard.unlock(); | ||
189 | } | ||
190 | |||
191 | struct Channel::Private | ||
192 | { | ||
193 | Private() | ||
194 | { | ||
195 | ownerThread = pthread_self(); | ||
196 | } | ||
197 | |||
198 | pthread_t ownerThread; | ||
199 | }; | ||
200 | |||
201 | Channel::Channel( QObject *parent, const char *name ) | ||
202 | : QObject( parent, name ), d( new Private ) | ||
203 | { | ||
204 | connect( &m_notifier, SIGNAL( awake() ), | ||
205 | this, SLOT( deliver() ) ); | ||
206 | } | ||
207 | |||
208 | Channel::~Channel() | ||
209 | { | ||
210 | delete d; | ||
211 | } | ||
212 | |||
213 | void Channel::send( ChannelMessage *message, SendType type ) | ||
214 | { | ||
215 | if ( type == WaitForReply ) | ||
216 | { | ||
217 | message->m_guard.lock(); | ||
218 | message->m_isCall = true; | ||
219 | } | ||
220 | |||
221 | m_pendingMessagesGuard.lock(); | ||
222 | m_pendingMessages << MsgEnvelope( type, message ); | ||
223 | m_pendingMessagesGuard.unlock(); | ||
224 | |||
225 | if ( d->ownerThread == pthread_self() ) { | ||
226 | assert( type != WaitForReply ); | ||
227 | |||
228 | deliver(); | ||
229 | } | ||
230 | else | ||
231 | m_notifier.notify(); | ||
232 | //QThread::postEvent( this, new QCustomEvent( QEvent::User, envelope ) ); | ||
233 | |||
234 | if ( type == WaitForReply ) | ||
235 | { | ||
236 | message->m_condition.wait( message->m_guard ); | ||
237 | message->m_guard.unlock(); | ||
238 | } | ||
239 | } | ||
240 | |||
241 | void Channel::deliver() | ||
242 | { | ||
243 | AutoLock lock( m_pendingMessagesGuard ); | ||
244 | |||
245 | while ( !m_pendingMessages.isEmpty() ) { | ||
246 | MsgEnvelope envelope = m_pendingMessages.first(); | ||
247 | |||
248 | m_pendingMessages.remove( m_pendingMessages.begin() ); | ||
249 | |||
250 | m_pendingMessagesGuard.unlock(); | ||
251 | deliverOne( envelope ); | ||
252 | m_pendingMessagesGuard.lock(); | ||
253 | } | ||
254 | } | ||
255 | |||
256 | void Channel::deliverOne( const MsgEnvelope &envelope ) | ||
257 | { | ||
258 | ChannelMessage *msg = envelope.msg; | ||
259 | |||
260 | assert( msg ); | ||
261 | |||
262 | if ( envelope.type == WaitForReply ) | ||
263 | { | ||
264 | msg->m_guard.lock(); | ||
265 | msg->m_inEventHandler = true; | ||
266 | } | ||
267 | |||
268 | receiveMessage( msg, envelope.type ); | ||
269 | |||
270 | if ( envelope.type == WaitForReply ) | ||
271 | { | ||
272 | msg->m_inEventHandler = false; | ||
273 | if ( msg->m_replied ) | ||
274 | { | ||
275 | msg->m_condition.wakeOne(); | ||
276 | // this is a bit tricky. we unlock only when we reply. | ||
277 | // reply() does an unlock as well. | ||
278 | msg->m_guard.unlock(); | ||
279 | } | ||
280 | } | ||
281 | } | ||
282 | |||
283 | /* vim: et sw=4 ts=4 | ||
284 | */ | ||