PollTimeout.h

Go to the documentation of this file.
00001 
00021 #ifndef HYPERTABLE_POLLTIMEOUT_H
00022 #define HYPERTABLE_POLLTIMEOUT_H
00023 
00024 // remove
00025 #include <iostream>
00026 
00027 #include <boost/thread/xtime.hpp>
00028 
00029 #include <cassert>
00030 
00031 namespace Hypertable {
00032 
00033   class PollTimeout {
00034 
00035   public:
00036 
00037     PollTimeout() : ts_ptr(0), duration_millis(-1) { return; }
00038 
00039     void set(boost::xtime &now, boost::xtime &expire) {
00040       assert((xtime_cmp(now , expire) <= 0));
00041       if (now.sec == expire.sec) {
00042         duration_ts.tv_sec = 0;
00043         duration_ts.tv_nsec = expire.nsec - now.nsec;
00044       }
00045       else {
00046         uint64_t nanos = expire.nsec + (1000000000LL - now.nsec);
00047         duration_ts.tv_sec = ((expire.sec - now.sec) - 1)
00048                               + (nanos / 1000000000LL);
00049         duration_ts.tv_nsec = nanos % 1000000000LL;
00050       }
00051       ts_ptr = &duration_ts;
00052       duration_millis = (int)((duration_ts.tv_sec * 1000)
00053                               + (duration_ts.tv_nsec / 1000000));
00054       assert(duration_millis >= 0);
00055     }
00056 
00057     void set_indefinite() {
00058       ts_ptr = 0;
00059       duration_millis = -1;
00060     }
00061 
00062     int get_millis() { return duration_millis; }
00063 
00064     struct timespec *get_timespec() { return ts_ptr; }
00065 
00066   private:
00067 
00068     struct timespec *ts_ptr;
00069     struct timespec duration_ts;
00070     int duration_millis;
00071   };
00072 
00073 }
00074 
00075 #endif // HYPERTABLE_POLLTIMEOUT_H