-rw-r--r-- | noncore/multimedia/opieplayer2/threadutil.cpp | 14 | ||||
-rw-r--r-- | noncore/multimedia/opieplayer2/threadutil.h | 5 |
2 files changed, 9 insertions, 10 deletions
diff --git a/noncore/multimedia/opieplayer2/threadutil.cpp b/noncore/multimedia/opieplayer2/threadutil.cpp index a5cc92d..7a9e1a4 100644 --- a/noncore/multimedia/opieplayer2/threadutil.cpp +++ b/noncore/multimedia/opieplayer2/threadutil.cpp | |||
@@ -93,112 +93,112 @@ struct WaitCondition::Data | |||
93 | 93 | ||
94 | WaitCondition::WaitCondition() | 94 | WaitCondition::WaitCondition() |
95 | : d( new Data ) | 95 | : d( new Data ) |
96 | { | 96 | { |
97 | } | 97 | } |
98 | 98 | ||
99 | WaitCondition::~WaitCondition() | 99 | WaitCondition::~WaitCondition() |
100 | { | 100 | { |
101 | delete d; | 101 | delete d; |
102 | } | 102 | } |
103 | 103 | ||
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 | }; | 136 | }; |
137 | 137 | ||
138 | extern "C" | 138 | extern "C" |
139 | { | 139 | { |
140 | 140 | ||
141 | void _threadutil_terminate_thread( void *arg ) | 141 | static void terminate_thread( void *arg ) |
142 | { | 142 | { |
143 | Thread *thr = ( Thread* )arg; | 143 | Thread::Data *data = ( Thread::Data* )arg; |
144 | 144 | ||
145 | assert( thr ); | 145 | assert( data ); |
146 | 146 | ||
147 | AutoLock locker( thr->d->guard ); | 147 | AutoLock locker( data->guard ); |
148 | thr->d->isRunning = false; | 148 | data->isRunning = false; |
149 | thr->d->finishCondition.wakeAll(); | 149 | data->finishCondition.wakeAll(); |
150 | } | 150 | } |
151 | 151 | ||
152 | void *_threadutil_start_thread( void *arg ) | 152 | void *_threadutil_start_thread( void *arg ) |
153 | { | 153 | { |
154 | Thread *thr = ( Thread* )arg; | 154 | Thread *thr = ( Thread* )arg; |
155 | 155 | ||
156 | pthread_cleanup_push( _threadutil_terminate_thread, thr ); | 156 | pthread_cleanup_push( terminate_thread, thr->d ); |
157 | 157 | ||
158 | thr->d->isRunning = true; | 158 | thr->d->isRunning = true; |
159 | thr->run(); | 159 | thr->run(); |
160 | 160 | ||
161 | pthread_cleanup_pop( true ); | 161 | pthread_cleanup_pop( true ); |
162 | 162 | ||
163 | Thread::exit(); | 163 | Thread::exit(); |
164 | return 0; // never reached | 164 | return 0; // never reached |
165 | } | 165 | } |
166 | 166 | ||
167 | } | 167 | } |
168 | 168 | ||
169 | Thread::Thread() | 169 | Thread::Thread() |
170 | : d( new Data ) | 170 | : d( new Data ) |
171 | { | 171 | { |
172 | } | 172 | } |
173 | 173 | ||
174 | Thread::~Thread() | 174 | Thread::~Thread() |
175 | { | 175 | { |
176 | assert( d->isRunning == false ); | 176 | assert( d->isRunning == false ); |
177 | delete d; | 177 | delete d; |
178 | } | 178 | } |
179 | 179 | ||
180 | void Thread::start() | 180 | void Thread::start() |
181 | { | 181 | { |
182 | AutoLock lock( d->guard ); | 182 | AutoLock lock( d->guard ); |
183 | 183 | ||
184 | if ( d->isRunning ) { | 184 | if ( d->isRunning ) { |
185 | qDebug( "ThreadUtil::Thread::start() called for running thread." ); | 185 | qDebug( "ThreadUtil::Thread::start() called for running thread." ); |
186 | return; | 186 | return; |
187 | } | 187 | } |
188 | 188 | ||
189 | pthread_attr_t attributes; | 189 | pthread_attr_t attributes; |
190 | pthread_attr_init( &attributes ); | 190 | pthread_attr_init( &attributes ); |
191 | pthread_attr_setscope( &attributes, PTHREAD_SCOPE_SYSTEM ); | 191 | pthread_attr_setscope( &attributes, PTHREAD_SCOPE_SYSTEM ); |
192 | int err = pthread_create( &d->self, &attributes, _threadutil_start_thread, ( void* )this ); | 192 | int err = pthread_create( &d->self, &attributes, _threadutil_start_thread, ( void* )this ); |
193 | if ( err != 0 ) { | 193 | if ( err != 0 ) { |
194 | qDebug( "ThreadUtil::Thread::start() : can't create thread: %s", strerror( err ) ); | 194 | qDebug( "ThreadUtil::Thread::start() : can't create thread: %s", strerror( err ) ); |
195 | pthread_attr_destroy( &attributes ); | 195 | pthread_attr_destroy( &attributes ); |
196 | return; | 196 | return; |
197 | } | 197 | } |
198 | pthread_attr_destroy( &attributes ); | 198 | pthread_attr_destroy( &attributes ); |
199 | } | 199 | } |
200 | 200 | ||
201 | void Thread::terminate() | 201 | void Thread::terminate() |
202 | { | 202 | { |
203 | AutoLock lock( d->guard ); | 203 | AutoLock lock( d->guard ); |
204 | if ( !d->isRunning ) | 204 | if ( !d->isRunning ) |
diff --git a/noncore/multimedia/opieplayer2/threadutil.h b/noncore/multimedia/opieplayer2/threadutil.h index b67b61d..21ae6b2 100644 --- a/noncore/multimedia/opieplayer2/threadutil.h +++ b/noncore/multimedia/opieplayer2/threadutil.h | |||
@@ -1,158 +1,157 @@ | |||
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" | 29 | extern "C" |
30 | { | 30 | { |
31 | void *_threadutil_start_thread( void* ); | 31 | void *_threadutil_start_thread( void* ); |
32 | void _threadutil_terminate_thread( void* ); | ||
33 | } | 32 | } |
34 | 33 | ||
35 | namespace ThreadUtil | 34 | namespace ThreadUtil |
36 | { | 35 | { |
37 | 36 | ||
38 | class Mutex | 37 | class Mutex |
39 | { | 38 | { |
40 | friend class WaitCondition; | 39 | friend class WaitCondition; |
41 | public: | 40 | public: |
42 | Mutex(); | 41 | Mutex(); |
43 | ~Mutex(); | 42 | ~Mutex(); |
44 | 43 | ||
45 | void lock(); | 44 | void lock(); |
46 | void unlock(); | 45 | void unlock(); |
47 | bool tryLock(); | 46 | bool tryLock(); |
48 | bool isLocked(); | 47 | bool isLocked(); |
49 | 48 | ||
50 | private: | 49 | private: |
51 | struct Data; | 50 | struct Data; |
52 | Data *d; | 51 | Data *d; |
53 | 52 | ||
54 | Mutex( const Mutex & ); | 53 | Mutex( const Mutex & ); |
55 | Mutex &operator=( const Mutex & ); | 54 | Mutex &operator=( const Mutex & ); |
56 | }; | 55 | }; |
57 | 56 | ||
58 | class AutoLock | 57 | class AutoLock |
59 | { | 58 | { |
60 | public: | 59 | public: |
61 | AutoLock( Mutex &mutex ) : m_mutex( mutex ) { m_mutex.lock(); } | 60 | AutoLock( Mutex &mutex ) : m_mutex( mutex ) { m_mutex.lock(); } |
62 | ~AutoLock() { m_mutex.unlock(); } | 61 | ~AutoLock() { m_mutex.unlock(); } |
63 | 62 | ||
64 | Mutex *operator &() const { return &m_mutex; } | 63 | Mutex *operator &() const { return &m_mutex; } |
65 | 64 | ||
66 | private: | 65 | private: |
67 | Mutex &m_mutex; | 66 | Mutex &m_mutex; |
68 | }; | 67 | }; |
69 | 68 | ||
70 | class WaitCondition | 69 | class WaitCondition |
71 | { | 70 | { |
72 | public: | 71 | public: |
73 | WaitCondition(); | 72 | WaitCondition(); |
74 | ~WaitCondition(); | 73 | ~WaitCondition(); |
75 | 74 | ||
76 | bool wait(); | 75 | bool wait(); |
77 | bool wait( Mutex &mutex ); | 76 | bool wait( Mutex &mutex ); |
78 | 77 | ||
79 | void wakeOne(); | 78 | void wakeOne(); |
80 | void wakeAll(); | 79 | void wakeAll(); |
81 | 80 | ||
82 | private: | 81 | private: |
83 | struct Data; | 82 | struct Data; |
84 | Data *d; | 83 | Data *d; |
85 | 84 | ||
86 | WaitCondition( const WaitCondition & ); | 85 | WaitCondition( const WaitCondition & ); |
87 | WaitCondition &operator=( const WaitCondition & ); | 86 | WaitCondition &operator=( const WaitCondition & ); |
88 | }; | 87 | }; |
89 | 88 | ||
90 | class Thread | 89 | class Thread |
91 | { | 90 | { |
92 | friend void *::_threadutil_start_thread( void* ); | 91 | friend void *::_threadutil_start_thread( void* ); |
93 | friend void ::_threadutil_terminate_thread( void* ); | ||
94 | public: | 92 | public: |
93 | struct Data; | ||
94 | |||
95 | Thread(); | 95 | Thread(); |
96 | virtual ~Thread(); | 96 | virtual ~Thread(); |
97 | 97 | ||
98 | void start(); | 98 | void start(); |
99 | void terminate(); | 99 | void terminate(); |
100 | 100 | ||
101 | bool wait(); | 101 | bool wait(); |
102 | 102 | ||
103 | bool isRunning() const; | 103 | bool isRunning() const; |
104 | 104 | ||
105 | static void exit(); | 105 | static void exit(); |
106 | protected: | 106 | protected: |
107 | virtual void run() = 0; | 107 | virtual void run() = 0; |
108 | 108 | ||
109 | private: | 109 | private: |
110 | struct Data; | ||
111 | Data *d; | 110 | Data *d; |
112 | }; | 111 | }; |
113 | 112 | ||
114 | class OnewayNotifier : public QObject | 113 | class OnewayNotifier : public QObject |
115 | { | 114 | { |
116 | Q_OBJECT | 115 | Q_OBJECT |
117 | public: | 116 | public: |
118 | OnewayNotifier(); | 117 | OnewayNotifier(); |
119 | ~OnewayNotifier(); | 118 | ~OnewayNotifier(); |
120 | 119 | ||
121 | void notify(); | 120 | void notify(); |
122 | 121 | ||
123 | signals: | 122 | signals: |
124 | void awake(); | 123 | void awake(); |
125 | 124 | ||
126 | private slots: | 125 | private slots: |
127 | void wakeUp(); | 126 | void wakeUp(); |
128 | 127 | ||
129 | private: | 128 | private: |
130 | int m_readFd; | 129 | int m_readFd; |
131 | int m_writeFd; | 130 | int m_writeFd; |
132 | QSocketNotifier *m_notifier; | 131 | QSocketNotifier *m_notifier; |
133 | }; | 132 | }; |
134 | 133 | ||
135 | 134 | ||
136 | class Channel; | 135 | class Channel; |
137 | 136 | ||
138 | class ChannelMessage | 137 | class ChannelMessage |
139 | { | 138 | { |
140 | friend class Channel; | 139 | friend class Channel; |
141 | public: | 140 | public: |
142 | ChannelMessage( int type = -1 ); | 141 | ChannelMessage( int type = -1 ); |
143 | virtual ~ChannelMessage(); | 142 | virtual ~ChannelMessage(); |
144 | 143 | ||
145 | int type() const { return m_type; } | 144 | int type() const { return m_type; } |
146 | 145 | ||
147 | void reply(); | 146 | void reply(); |
148 | 147 | ||
149 | private: | 148 | private: |
150 | ChannelMessage( const ChannelMessage & ); | 149 | ChannelMessage( const ChannelMessage & ); |
151 | ChannelMessage &operator=( const ChannelMessage ); | 150 | ChannelMessage &operator=( const ChannelMessage ); |
152 | 151 | ||
153 | int m_type; | 152 | int m_type; |
154 | bool m_isCall : 1; | 153 | bool m_isCall : 1; |
155 | bool m_replied : 1; | 154 | bool m_replied : 1; |
156 | bool m_inEventHandler : 1; | 155 | bool m_inEventHandler : 1; |
157 | Mutex m_guard; | 156 | Mutex m_guard; |
158 | WaitCondition m_condition; | 157 | WaitCondition m_condition; |