root/trunk/libffado/src/libieee1394/IsoHandlerManager.h

Revision 904, 6.5 kB (checked in by ppalmers, 16 years ago)

simplify threading. Each port now gets two threads: one for transmit and one for receive.

Line 
1 /*
2  * Copyright (C) 2005-2008 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) version 3 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #ifndef __FFADO_ISOHANDLERMANAGER__
25 #define __FFADO_ISOHANDLERMANAGER__
26
27 #include "config.h"
28
29 #include "debugmodule/debugmodule.h"
30
31 #include "libutil/Thread.h"
32
33 #include <sys/poll.h>
34 #include <errno.h>
35
36 #include <vector>
37
38 class Ieee1394Service;
39 class IsoHandler;
40
41 namespace Streaming {
42     class StreamProcessor;
43     class StreamProcessorManager;
44     typedef std::vector<StreamProcessor *> StreamProcessorVector;
45     typedef std::vector<StreamProcessor *>::iterator StreamProcessorVectorIterator;
46 }
47
48 typedef std::vector<IsoHandler *> IsoHandlerVector;
49 typedef std::vector<IsoHandler *>::iterator IsoHandlerVectorIterator;
50
51 class IsoHandlerManager;
52
53 // threads that will handle the packet framing
54 // one thread per direction, as a compromise for one per
55 // channel and one for all
56 class IsoTask : public Util::RunnableInterface
57 {
58     public:
59         enum eTaskType {
60             eTT_Receive,
61             eTT_Transmit,
62         };
63         IsoTask(IsoHandlerManager& manager, enum IsoTask::eTaskType t);
64         virtual ~IsoTask() {};
65
66     public:
67         bool Init();
68         bool Execute();
69
70         /**
71          * requests the thread to sync it's stream map with the manager
72          */
73         bool requestShadowMapUpdate();
74
75         void setVerboseLevel(int i);
76     protected:
77         IsoHandlerManager& m_manager;
78         enum eTaskType m_type;
79
80         // the event request structure
81         SInt32 request_update;
82
83         // static allocation due to RT constraints
84         // this is the map used by the actual thread
85         // it is a shadow of the m_StreamProcessors vector
86         struct pollfd m_poll_fds_shadow[ISOHANDLERMANAGER_MAX_ISO_HANDLERS_PER_PORT];
87         IsoHandler *m_IsoHandler_map_shadow[ISOHANDLERMANAGER_MAX_ISO_HANDLERS_PER_PORT];
88         unsigned int m_poll_nfds_shadow;
89
90         // updates the streams map
91         void updateShadowMapHelper();
92
93         // debug stuff
94         DECLARE_DEBUG_MODULE;
95 };
96
97 /*!
98 \brief The ISO Handler management class
99
100  This class manages the use of ISO handlers by ISO streams.
101  You can register an Streaming::StreamProcessor with an IsoHandlerManager. This
102  manager will assign an IsoHandler to the stream. If nescessary
103  the manager allocates a new handler. If there is already a handler
104  that can handle the Streaming::StreamProcessor (e.g. in case of multichannel receive),
105  it can be assigned.
106
107 */
108
109 class IsoHandlerManager
110 {
111     friend class Streaming::StreamProcessorManager;
112     friend class IsoTask;
113
114     public:
115
116         IsoHandlerManager(Ieee1394Service& service);
117         IsoHandlerManager(Ieee1394Service& service, bool run_rt, int rt_prio);
118         virtual ~IsoHandlerManager();
119
120         bool setThreadParameters(bool rt, int priority);
121
122         void setVerboseLevel(int l); ///< set the verbose level
123
124         void dumpInfo(); ///< print some information about the manager to stdout/stderr
125
126         bool registerStream(Streaming::StreamProcessor *); ///< register an iso stream with the manager
127         bool unregisterStream(Streaming::StreamProcessor *); ///< unregister an iso stream from the manager
128
129         bool startHandlers(); ///< start the managed ISO handlers
130         bool startHandlers(int cycle); ///< start the managed ISO handlers
131         bool stopHandlers(); ///< stop the managed ISO handlers
132
133         bool reset(); ///< reset the ISO manager and all streams
134         bool init();
135
136         bool disable(IsoHandler *); ///< disables a handler
137         bool enable(IsoHandler *); ///< enables a handler
138         ///> disables the handler attached to the stream
139         bool stopHandlerForStream(Streaming::StreamProcessor *);
140         ///> starts the handler attached to the specific stream
141         bool startHandlerForStream(Streaming::StreamProcessor *);
142         ///> starts the handler attached to the specific stream on a specific cycle
143         bool startHandlerForStream(Streaming::StreamProcessor *, int cycle);
144
145         /**
146          * returns the latency of a wake-up for this stream.
147          * The latency is the time it takes for a packet is delivered to the
148          * stream after it has been received (was on the wire).
149          * expressed in cycles
150          */
151         int getPacketLatencyForStream(Streaming::StreamProcessor *);
152
153         void flushHandlerForStream(Streaming::StreamProcessor *stream);
154
155         Ieee1394Service& get1394Service() {return m_service;};
156
157     // the state machine
158     private:
159         enum eHandlerStates {
160             E_Created,
161             E_Prepared,
162             E_Running,
163             E_Error
164         };
165
166         enum eHandlerStates m_State;
167         const char *eHSToString(enum eHandlerStates);
168
169     private:
170         Ieee1394Service&  m_service;
171         // note: there is a disctinction between streams and handlers
172         // because one handler can serve multiple streams (in case of
173         // multichannel receive)
174
175         // only streams are allowed to be registered externally.
176         // we allocate a handler if we need one, otherwise the stream
177         // is assigned to another handler
178
179         // the collection of handlers
180         IsoHandlerVector m_IsoHandlers;
181
182         bool registerHandler(IsoHandler *);
183         bool unregisterHandler(IsoHandler *);
184         void pruneHandlers();
185
186         // the collection of streams
187         Streaming::StreamProcessorVector m_StreamProcessors;
188
189         // thread params for the handler threads
190         bool m_realtime;
191         int m_priority;
192         // handler threads
193         Util::Thread *  m_ReceiveThread;
194         Util::Thread *  m_TransmitThread;
195
196         // actual tasks
197         IsoTask *  m_ReceiveTask;
198         IsoTask *  m_TransmitTask;
199
200         bool updateShadowMapFor(IsoHandler *h);
201
202         // debug stuff
203         DECLARE_DEBUG_MODULE;
204
205 };
206
207 #endif /* __FFADO_ISOHANDLERMANAGER__  */
208
209
210
Note: See TracBrowser for help on using the browser.