EvolvingObjects
eoEvalUserTimeThrowException.h
00001 /*
00002 (c) Thales group, 2010
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Lesser General Public
00006     License as published by the Free Software Foundation;
00007     version 2 of the License.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Lesser General Public License for more details.
00013 
00014     You should have received a copy of the GNU Lesser General Public
00015     License along with this library; if not, write to the Free Software
00016     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 Contact: http://eodev.sourceforge.net
00019 
00020 Authors:
00021 Johann Dréo <johann.dreo@thalesgroup.com>
00022 */
00023 
00024 #if !defined(__unix__) && !defined(_WINDOWS)
00025 #warning "Warning: class 'eoEvalUserTimeThrowException' is only available under UNIX (defining 'rusage' in 'sys/resource.h') or Win32 (defining 'GetProcessTimes' in 'WinBase.h') systems, contributions for other systems are welcomed."
00026 #else //!defined(__unix__) && !defined(_WINDOWS)
00027 
00028 #ifndef __EOEVALUSERTIMETHROWEXCEPTION_H__
00029 #define __EOEVALUSERTIMETHROWEXCEPTION_H__
00030 
00041 #include <eoExceptions.h>
00042 
00043 #ifdef __unix__
00044 
00045 #include <sys/time.h>
00046 #include <sys/resource.h>
00047 
00048 template< class EOT >
00049 class eoEvalUserTimeThrowException : public eoEvalFuncCounter< EOT >
00050 {
00051 public:
00052     eoEvalUserTimeThrowException( eoEvalFunc<EOT> & func, const long max ) : eoEvalFuncCounter<EOT>( func, "CPU-user"), _max(max) {}
00053 
00054     virtual void operator() ( EOT & eo )
00055     {
00056         if( eo.invalid() ) {
00057 
00058             getrusage(RUSAGE_SELF,&_usage);
00059 
00060             long current = _usage.ru_utime.tv_sec;
00061             if( current >= _max ) {
00062                 throw eoMaxTimeException( current );
00063             } else {
00064                 this->func(eo);
00065             }
00066         }
00067     }
00068 
00069 protected:
00070     const long _max;
00071     struct rusage _usage;
00072 };
00073 
00074 #else
00075 #ifdef _WINDOWS
00076 //here _WINDOWS is defined
00077 
00078 #include <WinBase.h>
00079 
00080 template< class EOT >
00081 class eoEvalUserTimeThrowException : public eoEvalFuncCounter< EOT >
00082 {
00083 public:
00084     eoEvalUserTimeThrowException( eoEvalFunc<EOT> & func, const long max ) : eoEvalFuncCounter<EOT>( func, "CPU-user"), _max(max) {}
00085 
00086     virtual void operator() ( EOT & eo )
00087     {
00088         if( eo.invalid() ) {
00089             FILETIME dummy;
00090             GetProcessTimes(GetCurrentProcess(), &dummy, &dummy, &dummy, &_usage);
00091 
00092             ULARGE_INTEGER current;
00093             current.LowPart = _usage.dwLowDateTime;
00094             current.HighPart = _usage.dwHighDateTime;
00095             if( current.QuadPart >= _max ) {
00096                 throw eoMaxTimeException( current.QuadPart );
00097             } else {
00098                 func(eo);
00099             }
00100         }
00101     }
00102 
00103 protected:
00104     const long _max;
00105     FILETIME _usage;
00106 };
00107 
00108 #endif // _WINDOWS
00109 #endif //__unix__
00110 #endif // __EOEVALUSERTIMETHROWEXCEPTION_H__
00111 #endif //!defined(__unix__) && !defined(_WINDOWS)
 All Classes Namespaces Files Functions Variables Typedefs Friends