llpumpio.h

Go to the documentation of this file.
00001 
00034 #ifndef LL_LLPUMPIO_H
00035 #define LL_LLPUMPIO_H
00036 
00037 #include <set>
00038 #if LL_LINUX  // needed for PATH_MAX in APR.
00039 #include <sys/param.h>
00040 #endif
00041 
00042 #include "apr-1/apr_pools.h"
00043 #include "llbuffer.h"
00044 #include "llframetimer.h"
00045 #include "lliopipe.h"
00046 #include "llrun.h"
00047 
00048 // Define this to enable use with the APR thread library.
00049 //#define LL_THREADS_APR 1
00050 
00051 // some simple constants to help with timeouts
00052 extern const F32 DEFAULT_CHAIN_EXPIRY_SECS;
00053 extern const F32 SHORT_CHAIN_EXPIRY_SECS;
00054 extern const F32 NEVER_CHAIN_EXPIRY_SECS;
00055 
00081 class LLPumpIO
00082 {
00083 public:
00087         LLPumpIO(apr_pool_t* pool);
00088 
00092         ~LLPumpIO();
00093 
00103         bool prime(apr_pool_t* pool);
00104 
00108         typedef std::vector<LLIOPipe::ptr_t> chain_t;
00109 
00121         bool addChain(const chain_t& chain, F32 timeout);
00122         
00126         struct LLLinkInfo
00127         {
00128                 LLIOPipe::ptr_t mPipe;
00129                 LLChannelDescriptors mChannels;
00130         };
00131 
00136         typedef std::vector<LLLinkInfo> links_t;
00137 
00153         bool addChain(
00154                 const links_t& links,
00155                 LLIOPipe::buffer_ptr_t data,
00156                 LLSD context,
00157                 F32 timeout);
00158 
00166         bool setTimeoutSeconds(F32 timeout);
00167 
00191         bool setConditional(LLIOPipe* pipe, const apr_pollfd_t* poll);
00192 
00208         S32 setLock();
00209 
00215         void clearLock(S32 key);
00216 
00228         bool sleepChain(F64 seconds);
00229 
00241         bool copyCurrentLinkInfo(links_t& links) const;
00242 
00253         void pump(const S32& poll_timeout);
00254         void pump();
00255 
00263         //void respond(const chain_t& pipes);
00264 
00276         bool respond(LLIOPipe* pipe);
00277 
00293         bool respond(
00294                 const links_t& links,
00295                 LLIOPipe::buffer_ptr_t data,
00296                 LLSD context);
00297 
00305         void callback();
00306 
00310         enum EControl
00311         {
00312                 PAUSE,
00313                 RESUME,
00314         };
00315         
00321         void control(EControl op);
00322 
00323 protected:
00327         enum EState
00328         {
00329                 NORMAL,
00330                 PAUSING,
00331                 PAUSED
00332         };
00333 
00334         // instance data
00335         EState mState;
00336         bool mRebuildPollset;
00337         apr_pollset_t* mPollset;
00338         S32 mPollsetClientID;
00339         S32 mNextLock;
00340         std::set<S32> mClearLocks;
00341 
00342         // This is the pump's runnable scheduler used for handling
00343         // expiring locks.
00344         LLRunner mRunner;
00345 
00346         // This structure is the stuff we track while running chains.
00347         struct LLChainInfo
00348         {
00349                 // methods
00350                 LLChainInfo();
00351                 void setTimeoutSeconds(F32 timeout);
00352 
00353                 // basic member data
00354                 bool mInit;
00355                 S32 mLock;
00356                 LLFrameTimer mTimer;
00357                 links_t::iterator mHead;
00358                 links_t mChainLinks;
00359                 LLIOPipe::buffer_ptr_t mData;
00360                 bool mEOS;
00361                 LLSD mContext;
00362 
00363                 // tracking inside the pump
00364                 typedef std::pair<LLIOPipe::ptr_t, apr_pollfd_t> pipe_conditional_t;
00365                 typedef std::vector<pipe_conditional_t> conditionals_t;
00366                 conditionals_t mDescriptors;
00367         };
00368 
00369         // All the running chains & info
00370         typedef std::vector<LLChainInfo> pending_chains_t;
00371         pending_chains_t mPendingChains;
00372         typedef std::list<LLChainInfo> running_chains_t;
00373         running_chains_t mRunningChains;
00374 
00375         typedef running_chains_t::iterator current_chain_t;
00376         current_chain_t mCurrentChain;
00377 
00378         // structures necessary for doing callbacks
00379         // since the callbacks only get one chance to run, we do not have
00380         // to maintain a list.
00381         typedef std::vector<LLChainInfo> callbacks_t;
00382         callbacks_t mPendingCallbacks;
00383         callbacks_t mCallbacks;
00384 
00385         // memory allocator for pollsets & mutexes.
00386         apr_pool_t* mPool;
00387         apr_pool_t* mCurrentPool;
00388         S32 mCurrentPoolReallocCount;
00389 
00390 #if LL_THREADS_APR
00391         apr_thread_mutex_t* mChainsMutex;
00392         apr_thread_mutex_t* mCallbackMutex;
00393 #else
00394         int* mChainsMutex;
00395         int* mCallbackMutex;
00396 #endif
00397 
00398 protected:
00399         void initialize(apr_pool_t* pool);
00400         void cleanup();
00401 
00406         void rebuildPollset();
00407 
00416         void processChain(LLChainInfo& chain);
00417 
00426         bool handleChainError(LLChainInfo& chain, LLIOPipe::EStatus error);
00427 };
00428 
00429 
00430 #endif // LL_LLPUMPIO_H

Generated on Thu Jul 1 06:09:03 2010 for Second Life Viewer by  doxygen 1.4.7