Equalizer  1.2.1
requestHandler.h
00001 
00002 /* Copyright (c) 2005-2011, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #ifndef COBASE_REQUESTHANDLER_H
00019 #define COBASE_REQUESTHANDLER_H
00020 
00021 #include <co/base/api.h>       // COBASE_API definition
00022 #include <co/base/stdExt.h>    // member
00023 #include <co/base/thread.h>    // thread-safety macros
00024 #include <co/base/spinLock.h>  // member
00025 #include <co/base/timedLock.h> // member
00026 #include <co/base/uint128_t.h> // member
00027 
00028 #include <list>
00029 
00030 namespace co
00031 {
00032 namespace base
00033 {
00047     class RequestHandler : public NonCopyable
00048     {
00049     public:
00051         COBASE_API RequestHandler();
00052 
00054         COBASE_API ~RequestHandler();
00055 
00064         COBASE_API uint32_t registerRequest( void* data = 0 );
00065 
00076         COBASE_API void unregisterRequest( const uint32_t requestID );
00077 
00092         COBASE_API bool waitRequest( const uint32_t requestID, void*& result,
00093                                const uint32_t timeout = EQ_TIMEOUT_INDEFINITE );
00094 
00096         COBASE_API bool waitRequest( const uint32_t requestID, uint32_t& result,
00097                                const uint32_t timeout = EQ_TIMEOUT_INDEFINITE );
00099         COBASE_API bool waitRequest( const uint32_t requestID, bool& result,
00100                                const uint32_t timeout = EQ_TIMEOUT_INDEFINITE );
00102         COBASE_API bool waitRequest(const uint32_t requestID, uint128_t& result,
00103                                const uint32_t timeout = EQ_TIMEOUT_INDEFINITE );
00105         COBASE_API bool waitRequest( const uint32_t requestID );
00106 
00116         COBASE_API bool isRequestServed( const uint32_t requestID ) const;
00117 
00125         COBASE_API void* getRequestData( const uint32_t requestID );
00126 
00134         COBASE_API void serveRequest( const uint32_t requestID,
00135                                         void* result = 0 );
00137         COBASE_API void serveRequest( const uint32_t requestID,
00138                                         uint32_t result );
00140         COBASE_API void serveRequest( const uint32_t requestID, bool result );
00142         COBASE_API void serveRequest( const uint32_t requestID,
00143                                       const uint128_t& result );
00148         bool hasPendingRequests() const { return !_requests.empty( ); }
00149 
00150     private:
00151         mutable SpinLock _mutex;
00152 
00154         struct Request
00155         {
00156             Request() { lock.set(); }
00157             ~Request(){}
00158             
00159             TimedLock lock;
00160             void*     data;
00161 
00162             union Result
00163             {
00164                 void*    rPointer;
00165                 uint32_t rUint32;
00166                 bool     rBool;
00167                 struct
00168                 {
00169                     uint64_t low;
00170                     uint64_t high;
00171                 } rUint128;
00172             } result;
00173         };
00174         // @endcond
00175 
00176         typedef stde::hash_map< uint32_t, Request* > RequestHash;
00177 
00178         uint32_t            _requestID;
00179         RequestHash         _requests;
00180         std::list<Request*> _freeRequests;
00181         friend COBASE_API std::ostream& operator << (std::ostream&,
00182                                                      const RequestHandler&);
00183 
00184         bool _waitRequest( const uint32_t requestID, Request::Result& result,
00185                            const uint32_t timeout );
00186 
00187         EQ_TS_VAR( _thread );
00188     };
00189 
00190     COBASE_API std::ostream& operator << ( std::ostream&,
00191                                            const RequestHandler& );
00192 }
00193 
00194 }
00195 #endif //COBASE_REQUESTHANDLER_H
Generated on Fri Jun 8 2012 15:44:32 for Equalizer 1.2.1 by  doxygen 1.8.0