Diffstat (limited to 'noncore/multimedia/opieplayer2/threadutil.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/multimedia/opieplayer2/threadutil.h | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/noncore/multimedia/opieplayer2/threadutil.h b/noncore/multimedia/opieplayer2/threadutil.h new file mode 100644 index 0000000..bcb9db9 --- a/dev/null +++ b/noncore/multimedia/opieplayer2/threadutil.h | |||
@@ -0,0 +1,176 @@ | |||
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 | #ifndef THREADUTIL_H | ||
21 | #define THREADUTIL_H | ||
22 | |||
23 | #include <qvaluelist.h> | ||
24 | #include <qobject.h> | ||
25 | #include <qguardedptr.h> | ||
26 | |||
27 | class QSocketNotifier; | ||
28 | |||
29 | namespace ThreadUtil | ||
30 | { | ||
31 | |||
32 | class Mutex | ||
33 | { | ||
34 | friend class WaitCondition; | ||
35 | public: | ||
36 | Mutex(); | ||
37 | ~Mutex(); | ||
38 | |||
39 | void lock(); | ||
40 | void unlock(); | ||
41 | bool tryLock(); | ||
42 | bool isLocked(); | ||
43 | |||
44 | private: | ||
45 | struct Data; | ||
46 | Data *d; | ||
47 | |||
48 | Mutex( const Mutex & ); | ||
49 | Mutex &operator=( const Mutex & ); | ||
50 | }; | ||
51 | |||
52 | class AutoLock | ||
53 | { | ||
54 | public: | ||
55 | AutoLock( Mutex &mutex ) : m_mutex( mutex ) { m_mutex.lock(); } | ||
56 | ~AutoLock() { m_mutex.unlock(); } | ||
57 | |||
58 | Mutex *operator &() const { return &m_mutex; } | ||
59 | |||
60 | private: | ||
61 | Mutex &m_mutex; | ||
62 | }; | ||
63 | |||
64 | class WaitCondition | ||
65 | { | ||
66 | public: | ||
67 | WaitCondition(); | ||
68 | ~WaitCondition(); | ||
69 | |||
70 | bool wait(); | ||
71 | bool wait( Mutex &mutex ); | ||
72 | |||
73 | void wakeOne(); | ||
74 | void wakeAll(); | ||
75 | |||
76 | private: | ||
77 | struct Data; | ||
78 | Data *d; | ||
79 | |||
80 | WaitCondition( const WaitCondition & ); | ||
81 | WaitCondition &operator=( const WaitCondition & ); | ||
82 | }; | ||
83 | |||
84 | class OnewayNotifier : public QObject | ||
85 | { | ||
86 | Q_OBJECT | ||
87 | public: | ||
88 | OnewayNotifier(); | ||
89 | ~OnewayNotifier(); | ||
90 | |||
91 | void notify(); | ||
92 | |||
93 | signals: | ||
94 | void awake(); | ||
95 | |||
96 | private slots: | ||
97 | void wakeUp(); | ||
98 | |||
99 | private: | ||
100 | int m_readFd; | ||
101 | int m_writeFd; | ||
102 | QSocketNotifier *m_notifier; | ||
103 | }; | ||
104 | |||
105 | |||
106 | class Channel; | ||
107 | |||
108 | class ChannelMessage | ||
109 | { | ||
110 | friend class Channel; | ||
111 | public: | ||
112 | ChannelMessage( int type = -1 ); | ||
113 | virtual ~ChannelMessage(); | ||
114 | |||
115 | int type() const { return m_type; } | ||
116 | |||
117 | void reply(); | ||
118 | |||
119 | private: | ||
120 | ChannelMessage( const ChannelMessage & ); | ||
121 | ChannelMessage &operator=( const ChannelMessage ); | ||
122 | |||
123 | int m_type; | ||
124 | bool m_isCall : 1; | ||
125 | bool m_replied : 1; | ||
126 | bool m_inEventHandler : 1; | ||
127 | Mutex m_guard; | ||
128 | WaitCondition m_condition; | ||
129 | QGuardedPtr<Channel> m_channel; | ||
130 | }; | ||
131 | |||
132 | class Channel : public QObject | ||
133 | { | ||
134 | Q_OBJECT | ||
135 | public: | ||
136 | enum SendType { OneWay, WaitForReply }; | ||
137 | Channel( QObject *parent = 0, const char *name = 0 ); | ||
138 | virtual ~Channel(); | ||
139 | |||
140 | void send( ChannelMessage *message, SendType type ); | ||
141 | |||
142 | protected: | ||
143 | virtual void receiveMessage( ChannelMessage *message, SendType type ) = 0; | ||
144 | |||
145 | private slots: | ||
146 | void deliver(); | ||
147 | |||
148 | private: | ||
149 | OnewayNotifier m_notifier; | ||
150 | |||
151 | struct MsgEnvelope | ||
152 | { | ||
153 | MsgEnvelope() : type( OneWay ), msg( 0 ) {} | ||
154 | MsgEnvelope( SendType _type , ChannelMessage *_msg ) | ||
155 | : type( _type ), msg( _msg ) {} | ||
156 | |||
157 | SendType type; | ||
158 | ChannelMessage *msg; | ||
159 | }; | ||
160 | |||
161 | void deliverOne( const MsgEnvelope &envelope ); | ||
162 | |||
163 | typedef QValueList<MsgEnvelope> MsgEnvelopeList; | ||
164 | |||
165 | MsgEnvelopeList m_pendingMessages; | ||
166 | Mutex m_pendingMessagesGuard; | ||
167 | |||
168 | struct Private; | ||
169 | Private *d; | ||
170 | }; | ||
171 | |||
172 | } | ||
173 | |||
174 | #endif // THREADUTIL_H | ||
175 | /* vim: et sw=4 ts=4 | ||
176 | */ | ||