$search
00001 00008 /***************************************************************************** 00009 ** Ifdefs 00010 *****************************************************************************/ 00011 00012 #ifndef ECL_TIME_SLEEP_POS_HPP_ 00013 #define ECL_TIME_SLEEP_POS_HPP_ 00014 00015 /***************************************************************************** 00016 ** Platform Check 00017 *****************************************************************************/ 00018 00019 #include <ecl/config.hpp> 00020 #if defined(ECL_IS_POSIX) 00021 00022 /***************************************************************************** 00023 ** Includes 00024 *****************************************************************************/ 00025 00026 #include <sstream> 00027 #include <time.h> 00028 #include <errno.h> 00029 #include <ecl/config/macros.hpp> 00030 #include <ecl/exceptions/macros.hpp> 00031 #include <ecl/exceptions/standard_exception.hpp> 00032 #include "duration.hpp" 00033 00034 /***************************************************************************** 00035 ** Namespaces 00036 *****************************************************************************/ 00037 00038 namespace ecl { 00039 00040 /***************************************************************************** 00041 ** Interface [Sleep] 00042 *****************************************************************************/ 00060 class ECL_PUBLIC Sleep { 00061 public: 00071 Sleep(const Duration &duration); 00081 Sleep(const unsigned long &seconds = 0); 00082 virtual ~Sleep() {} 00083 00091 void operator()() ecl_assert_throw_decl(StandardException); 00099 Duration duration() { return Duration(required.tv_sec,required.tv_nsec); } 00100 00110 void operator()(const unsigned long &seconds) ecl_assert_throw_decl(StandardException); 00120 void operator()(const Duration &duration) ecl_assert_throw_decl(StandardException); 00121 00122 private: 00123 timespec required, remaining; 00124 }; 00125 00141 class ECL_PUBLIC MilliSleep { 00142 public: 00152 MilliSleep(const unsigned long &milliseconds = 0); 00153 00154 virtual ~MilliSleep() {} 00155 00163 void operator()() ecl_assert_throw_decl(StandardException); 00171 Duration duration() { return Duration(required.tv_sec,required.tv_nsec); } 00172 00182 void operator()(const unsigned long &milliseconds) ecl_assert_throw_decl(StandardException); 00183 private: 00184 timespec required, remaining; 00185 }; 00186 00202 class ECL_PUBLIC MicroSleep { 00203 public: 00213 MicroSleep(const unsigned long µseconds = 0); 00214 00215 virtual ~MicroSleep() {} 00216 00217 00225 void operator()() ecl_assert_throw_decl(StandardException); 00233 Duration duration() { return Duration(required.tv_sec,required.tv_nsec); } 00243 void operator()(const unsigned long µ_seconds) ecl_assert_throw_decl(StandardException); 00244 private: 00245 timespec required, remaining; 00246 }; 00247 00263 class ECL_PUBLIC NanoSleep { 00264 public: 00274 NanoSleep(const unsigned long &nanoseconds = 0); 00275 00276 virtual ~NanoSleep() {} 00277 00278 00286 void operator()() ecl_assert_throw_decl(StandardException); 00294 Duration duration() { return Duration(required.tv_sec,required.tv_nsec); } 00304 void operator()(const unsigned long &nanoseconds) ecl_assert_throw_decl(StandardException); 00305 private: 00306 timespec required, remaining; 00307 }; 00308 00309 00310 } // namespace ecl 00311 00312 /***************************************************************************** 00313 ** Interface [Exceptions][Sleeper Classes] 00314 *****************************************************************************/ 00315 00316 #if defined(ECL_HAS_EXCEPTIONS) 00317 namespace ecl { 00318 namespace time { 00319 00320 00321 /***************************************************************************** 00322 ** Interface [Sleep Exceptions] 00323 *****************************************************************************/ 00330 inline ecl::StandardException throwSleepException(const char* loc ) { 00331 int error_result = errno; 00332 switch (error_result) { 00333 case ( EINTR ) : return StandardException(loc, ecl::InterruptedError, "A posix signal interrupted the sleep."); 00334 case ( EINVAL ) : return StandardException(loc, ecl::InvalidInputError, "Specified value was negative or exceeded resolution range.\n\n Sleep: [N/A]\n MilliSleep: [0-1000]\n MicroSleep: [0-1x10^6]\n NanoSleep: [0-1x10^9]\n"); 00335 case ( EFAULT ) : return StandardException(loc, ecl::MemoryError, "Internal posix issue copying information from user space."); 00336 default : 00337 { 00338 std::ostringstream ostream; 00339 ostream << "Unknown posix error " << error_result << ": " << strerror(error_result) << "."; 00340 return StandardException(loc, UnknownError, ostream.str()); 00341 } 00342 } 00343 00344 } 00345 00346 00347 }; // namespace time 00348 } // namespace ecl 00349 #endif 00350 00351 #endif /* ECL_IS_POSIX */ 00352 #endif /* ECL_TIME_SLEEP_POS_HPP_ */