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

Revision 978, 6.4 kB (checked in by ppalmers, 13 years ago)

try to detect and survive ISO thread hangup

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         IsoTask(IsoHandlerManager& manager);
60         virtual ~IsoTask() {};
61
62     public:
63         bool Init();
64         bool Execute();
65
66         /**
67          * requests the thread to sync it's stream map with the manager
68          */
69         bool requestShadowMapUpdate();
70
71         void setVerboseLevel(int i);
72     protected:
73         IsoHandlerManager& m_manager;
74
75         // the event request structure
76         SInt32 request_update;
77
78         // static allocation due to RT constraints
79         // this is the map used by the actual thread
80         // it is a shadow of the m_StreamProcessors vector
81         struct pollfd   m_poll_fds_shadow[ISOHANDLERMANAGER_MAX_ISO_HANDLERS_PER_PORT];
82         IsoHandler *    m_IsoHandler_map_shadow[ISOHANDLERMANAGER_MAX_ISO_HANDLERS_PER_PORT];
83         unsigned int    m_poll_nfds_shadow;
84         IsoHandler *    m_SyncIsoHandler;
85
86         // updates the streams map
87         void updateShadowMapHelper();
88
89 #ifdef DEBUG
90         uint64_t m_last_loop_entry;
91         int m_successive_short_loops;
92 #endif
93
94         // debug stuff
95         DECLARE_DEBUG_MODULE;
96 };
97
98 /*!
99 \brief The ISO Handler management class
100
101  This class manages the use of ISO handlers by ISO streams.
102  You can register an Streaming::StreamProcessor with an IsoHandlerManager. This
103  manager will assign an IsoHandler to the stream. If nescessary
104  the manager allocates a new handler. If there is already a handler
105  that can handle the Streaming::StreamProcessor (e.g. in case of multichannel receive),
106  it can be assigned.
107
108 */
109
110 class IsoHandlerManager
111 {
112     friend class Streaming::StreamProcessorManager;
113     friend class IsoTask;
114     friend class IsoHandler;
115
116     public:
117
118         IsoHandlerManager(Ieee1394Service& service);
119         IsoHandlerManager(Ieee1394Service& service, bool run_rt, int rt_prio);
120         virtual ~IsoHandlerManager();
121
122         bool setThreadParameters(bool rt, int priority);
123
124         void setVerboseLevel(int l); ///< set the verbose level
125
126         void dumpInfo(); ///< print some information about the manager to stdout/stderr
127
128         bool registerStream(Streaming::StreamProcessor *); ///< register an iso stream with the manager
129         bool unregisterStream(Streaming::StreamProcessor *); ///< unregister an iso stream from the manager
130
131         bool startHandlers(); ///< start the managed ISO handlers
132         bool startHandlers(int cycle); ///< start the managed ISO handlers
133         bool stopHandlers(); ///< stop the managed ISO handlers
134
135         bool reset(); ///< reset the ISO manager and all streams
136         bool init();
137
138         bool disable(IsoHandler *); ///< disables a handler
139         bool enable(IsoHandler *); ///< enables a handler
140         ///> disables the handler attached to the stream
141         bool stopHandlerForStream(Streaming::StreamProcessor *);
142         ///> starts the handler attached to the specific stream
143         bool startHandlerForStream(Streaming::StreamProcessor *);
144         ///> starts the handler attached to the specific stream on a specific cycle
145         bool startHandlerForStream(Streaming::StreamProcessor *, int cycle);
146
147         /**
148          * returns the latency of a wake-up for this stream.
149          * The latency is the time it takes for a packet is delivered to the
150          * stream after it has been received (was on the wire).
151          * expressed in cycles
152          */
153         int getPacketLatencyForStream(Streaming::StreamProcessor *);
154
155         void flushHandlerForStream(Streaming::StreamProcessor *stream);
156
157         Ieee1394Service& get1394Service() {return m_service;};
158
159     protected:
160         void requestShadowMapUpdate();
161
162     // the state machine
163     private:
164         enum eHandlerStates {
165             E_Created,
166             E_Prepared,
167             E_Running,
168             E_Error
169         };
170
171         enum eHandlerStates m_State;
172         const char *eHSToString(enum eHandlerStates);
173
174     private:
175         Ieee1394Service&  m_service;
176         // note: there is a disctinction between streams and handlers
177         // because one handler can serve multiple streams (in case of
178         // multichannel receive)
179
180         // only streams are allowed to be registered externally.
181         // we allocate a handler if we need one, otherwise the stream
182         // is assigned to another handler
183
184         // the collection of handlers
185         IsoHandlerVector m_IsoHandlers;
186
187         bool registerHandler(IsoHandler *);
188         bool unregisterHandler(IsoHandler *);
189         void pruneHandlers();
190
191         // the collection of streams
192         Streaming::StreamProcessorVector m_StreamProcessors;
193
194         // handler thread/task
195         bool            m_realtime;
196         int             m_priority;
197         Util::Thread *  m_IsoThread;
198         IsoTask *       m_IsoTask;
199
200         // debug stuff
201         DECLARE_DEBUG_MODULE;
202
203 };
204
205 #endif /* __FFADO_ISOHANDLERMANAGER__  */
206
207
208
Note: See TracBrowser for help on using the browser.