requestHandler.h

00001 
00002 /* Copyright (c) 2005-2009, 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 EQBASE_REQUESTHANDLER_H
00019 #define EQBASE_REQUESTHANDLER_H
00020 
00021 #include <eq/base/base.h>      // EQ_EXPORT definition
00022 #include <eq/base/hash.h>      // member
00023 #include <eq/base/thread.h>    // thread-safety macros
00024 #include <eq/base/timedLock.h> // member
00025 
00026 #include <list>
00027 
00028 namespace eq
00029 {
00030 namespace base
00031 {
00032     class Lock;
00033 
00047     class RequestHandler : public NonCopyable
00048     {
00049 
00050     public:
00058         EQ_EXPORT RequestHandler( const bool threadSafe = false );
00059 
00061         EQ_EXPORT ~RequestHandler();
00062 
00070         EQ_EXPORT uint32_t registerRequest( void* data = 0 );
00071 
00081         EQ_EXPORT void unregisterRequest( const uint32_t requestID );
00082 
00096         EQ_EXPORT bool waitRequest( const uint32_t requestID, void*& result,
00097                                const uint32_t timeout = EQ_TIMEOUT_INDEFINITE );
00098 
00100         EQ_EXPORT bool waitRequest( const uint32_t requestID, uint32_t& result,
00101                                const uint32_t timeout = EQ_TIMEOUT_INDEFINITE );
00103         EQ_EXPORT bool waitRequest( const uint32_t requestID, bool& result,
00104                                const uint32_t timeout = EQ_TIMEOUT_INDEFINITE );
00106         EQ_EXPORT bool waitRequest( const uint32_t requestID );
00107 
00116         EQ_EXPORT bool isServed( const uint32_t requestID ) const;
00117 
00124         EQ_EXPORT void* getRequestData( const uint32_t requestID );
00125 
00132         EQ_EXPORT void serveRequest( const uint32_t requestID, void* result=0 );
00134         EQ_EXPORT void serveRequest( const uint32_t requestID, uint32_t result);
00136         EQ_EXPORT void serveRequest( const uint32_t requestID, bool result );
00137 
00139         bool isThreadSafe() const { return ( _mutex != 0 ); }
00141         bool isEmpty()      const { return _requests.empty( ); }
00142 
00143     private:
00144         Lock*        _mutex;
00145 
00147         struct Request
00148         {
00149             Request()
00150                 {
00151                     lock.set();
00152                 }
00153             ~Request(){}
00154             
00155             TimedLock lock;
00156             void*     data;
00157 
00158             union Result
00159             {
00160                 void*    rPointer;
00161                 uint32_t rUint32;
00162                 bool     rBool;
00163             } result;
00164         };
00165         // @endcond
00166 
00167         typedef stde::hash_map<uint32_t, Request*> RequestHash;
00168 
00169         uint32_t            _requestID;
00170         RequestHash         _requests;
00171         std::list<Request*> _freeRequests;
00172 
00173         bool _waitRequest( const uint32_t requestID, Request::Result& result,
00174                            const uint32_t timeout );
00175 
00176         CHECK_THREAD_DECLARE( _thread );
00177     };
00178 }
00179 
00180 }
00181 #endif //EQBASE_REQUESTHANDLER_H
Generated on Mon Aug 10 18:58:41 2009 for Equalizer 0.9 by  doxygen 1.5.8