comparison dbutil.c @ 928:7cd89d4e0335

Add new monotonic_now() wrapper so that timeouts are unaffected by system clock changes
author Matt Johnston <matt@ucc.asn.au>
date Thu, 13 Mar 2014 23:50:09 +0800
parents ff597bf2cfb0
children 8f04e36622c0
comparison
equal deleted inserted replaced
927:122fb3532038 928:7cd89d4e0335
46 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 46 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
47 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 47 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 48 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
49 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 49 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
50 50
51 #include "config.h"
52
53 #ifdef __linux__
54 #define _GNU_SOURCE
55 /* To call clock_gettime() directly */
56 #include <sys/syscall.h>
57 #endif /* __linux */
58
59 #ifdef HAVE_MACH_MACH_TIME_H
60 #include <mach/mach_time.h>
61 #include <mach/mach.h>
62 #endif
63
51 #include "includes.h" 64 #include "includes.h"
52 #include "dbutil.h" 65 #include "dbutil.h"
53 #include "buffer.h" 66 #include "buffer.h"
54 #include "session.h" 67 #include "session.h"
55 #include "atomicio.h" 68 #include "atomicio.h"
930 c |= (xa[i] ^ xb[i]); 943 c |= (xa[i] ^ xb[i]);
931 } 944 }
932 return c; 945 return c;
933 } 946 }
934 947
948 time_t monotonic_now() {
949
950 #if defined(__linux__) && defined(SYS_clock_gettime)
951 /* CLOCK_MONOTONIC_COARSE was added in Linux 2.6.32. Probably cheaper. */
952 #ifndef CLOCK_MONOTONIC_COARSE
953 #define CLOCK_MONOTONIC_COARSE 6
954 #endif
955 static clockid_t clock_source = CLOCK_MONOTONIC_COARSE;
956 struct timespec ts;
957
958 if (syscall(SYS_clock_gettime, clock_source, &ts) == EINVAL) {
959 clock_source = CLOCK_MONOTONIC;
960 syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &ts);
961 }
962 return ts.tv_sec;
963 #elif defined(HAVE_MACH_ABSOLUTE_TIME)
964 /* OS X, see https://developer.apple.com/library/mac/qa/qa1398/_index.html */
965 mach_timebase_info_data_t timebase_info;
966 if (timebase_info.denom == 0) {
967 mach_timebase_info(&timebase_info);
968 }
969 return mach_absolute_time() * timebase_info.numer / timebase_info.denom
970 / 1e9;
971 #else
972 /* Fallback for everything else - this will sometimes go backwards */
973 return time(NULL);
974 #endif
975
976 }
977