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

Revision 752, 6.1 kB (checked in by ppalmers, 13 years ago)

- Implement a DLL based mechanism to read the cycle timer. This can potentially be more lightweight for the reader threads since it avoids a the CTR read kernel call. It also has the
side effect that FFADO now works on older kernels that don't implement the cycle timer read call.

Line 
1 /*
2  * Copyright (C) 2005-2007 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 3 of the License, or
12  * (at your option) any later version.
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 "debugmodule/debugmodule.h"
28
29 #include "libutil/Thread.h"
30
31 #include <sys/poll.h>
32 #include <errno.h>
33
34 #include <vector>
35
36 #define FFADO_MAX_ISO_HANDLERS_PER_PORT 16
37
38 #define USLEEP_AFTER_UPDATE_FAILURE 10
39 #define USLEEP_AFTER_UPDATE 100
40 #define MAX_UPDATE_TRIES 10
41 class Ieee1394Service;
42 namespace Util {
43     class PosixThread;
44 }
45
46 class IsoHandler;
47 namespace Streaming {
48     class StreamProcessor;
49     class StreamProcessorManager;
50     typedef std::vector<StreamProcessor *> StreamProcessorVector;
51     typedef std::vector<StreamProcessor *>::iterator StreamProcessorVectorIterator;
52 }
53
54 typedef std::vector<IsoHandler *> IsoHandlerVector;
55 typedef std::vector<IsoHandler *>::iterator IsoHandlerVectorIterator;
56
57 /*!
58 \brief The ISO Handler management class
59
60  This class manages the use of ISO handlers by ISO streams.
61  You can register an Streaming::StreamProcessor with an IsoHandlerManager. This
62  manager will assign an IsoHandler to the stream. If nescessary
63  the manager allocates a new handler. If there is already a handler
64  that can handle the Streaming::StreamProcessor (e.g. in case of multichannel receive),
65  it can be assigned.
66
67 */
68 class IsoHandlerManager : public Util::RunnableInterface
69 {
70     friend class Streaming::StreamProcessorManager;
71
72     public:
73
74         IsoHandlerManager(Ieee1394Service& service);
75         IsoHandlerManager(Ieee1394Service& service, bool run_rt, int rt_prio);
76         virtual ~IsoHandlerManager();
77
78         bool setThreadParameters(bool rt, int priority);
79
80         void setPollTimeout(int t) {m_poll_timeout=t;}; ///< set the timeout used for poll()
81         int getPollTimeout() {return m_poll_timeout;};  ///< get the timeout used for poll()
82
83         void setVerboseLevel(int l); ///< set the verbose level
84
85         void dumpInfo(); ///< print some information about the manager to stdout/stderr
86
87         bool registerStream(Streaming::StreamProcessor *); ///< register an iso stream with the manager
88         bool unregisterStream(Streaming::StreamProcessor *); ///< unregister an iso stream from the manager
89
90         bool startHandlers(); ///< start the managed ISO handlers
91         bool startHandlers(int cycle); ///< start the managed ISO handlers
92         bool stopHandlers(); ///< stop the managed ISO handlers
93
94         bool reset(); ///< reset the ISO manager and all streams
95         bool init();
96
97         bool disable(IsoHandler *); ///< disables a handler
98         bool enable(IsoHandler *); ///< enables a handler
99         ///> disables the handler attached to the stream
100         bool stopHandlerForStream(Streaming::StreamProcessor *);
101         ///> starts the handler attached to the specific stream
102         bool startHandlerForStream(Streaming::StreamProcessor *);
103         ///> starts the handler attached to the specific stream on a specific cycle
104         bool startHandlerForStream(Streaming::StreamProcessor *, int cycle);
105
106         /**
107          * returns the latency of a wake-up for this stream.
108          * The latency is the time it takes for a packet is delivered to the
109          * stream after it has been received (was on the wire).
110          * expressed in cycles
111          */
112         int getPacketLatencyForStream(Streaming::StreamProcessor *);
113
114         void flushHandlerForStream(Streaming::StreamProcessor *stream);
115
116         Ieee1394Service& get1394Service() {return m_service;};
117     // RunnableInterface interface
118     public:
119         bool Execute(); // note that this is called in we while(running) loop
120         bool Init();
121        
122         // protects the operations on the lists
123         // (FIXME: should be changed into a lock-free approach)
124         pthread_mutex_t m_list_lock;
125
126     // the state machine
127     private:
128         enum eHandlerStates {
129             E_Created,
130             E_Prepared,
131             E_Running,
132             E_Error
133         };
134
135         enum eHandlerStates m_State;
136         const char *eHSToString(enum eHandlerStates);
137     private:
138         /// iterate all child handlers
139         bool iterate();
140
141     private:
142         Ieee1394Service&  m_service;
143         // note: there is a disctinction between streams and handlers
144         // because one handler can serve multiple streams (in case of
145         // multichannel receive)
146
147         // only streams are allowed to be registered externally.
148         // we allocate a handler if we need one, otherwise the stream
149         // is assigned to another handler
150
151         // the collection of handlers
152         IsoHandlerVector m_IsoHandlers;
153
154         bool registerHandler(IsoHandler *);
155         bool unregisterHandler(IsoHandler *);
156         void pruneHandlers();
157
158         // the collection of streams
159         Streaming::StreamProcessorVector m_StreamProcessors;
160
161         // poll stuff
162         int m_poll_timeout;
163         // FD map sync requested
164         int32_t m_request_fdmap_update;
165         void updateShadowVars();
166
167         // shadow variables
168         struct pollfd m_poll_fds_shadow[FFADO_MAX_ISO_HANDLERS_PER_PORT];
169         IsoHandler *m_IsoHandler_map_shadow[FFADO_MAX_ISO_HANDLERS_PER_PORT];
170         unsigned int m_poll_nfds_shadow;
171
172         void requestShadowUpdate();
173
174         // threading
175         bool m_realtime;
176         int m_priority;
177         Util::PosixThread *m_isoManagerThread;
178
179         // debug stuff
180         DECLARE_DEBUG_MODULE;
181
182 };
183
184 #endif /* __FFADO_ISOHANDLERMANAGER__  */
185
186
187
Note: See TracBrowser for help on using the browser.