1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2001,2008 Oracle.  All rights reserved.
5 *
6 * $Id: os_clock.c,v 12.19 2008/02/06 21:30:36 bostic Exp $
7 */
8
9#include "db_config.h"
10
11#include "db_int.h"
12
13/*
14 * __os_gettime --
15 *	Return the current time-of-day clock in seconds and nanoseconds.
16 *
17 * PUBLIC: void __os_gettime __P((ENV *, db_timespec *, int));
18 */
19void
20__os_gettime(env, tp, monotonic)
21	ENV *env;
22	db_timespec *tp;
23	int monotonic;
24{
25	const char *sc;
26	int ret;
27
28#if defined(HAVE_CLOCK_GETTIME)
29#if defined(HAVE_CLOCK_MONOTONIC)
30	if (monotonic)
31		RETRY_CHK((clock_gettime(
32		    CLOCK_MONOTONIC, (struct timespec *)tp)), ret);
33	else
34#endif
35		RETRY_CHK((clock_gettime(
36		    CLOCK_REALTIME, (struct timespec *)tp)), ret);
37
38	RETRY_CHK((clock_gettime(CLOCK_REALTIME, (struct timespec *)tp)), ret);
39	if (ret != 0) {
40		sc = "clock_gettime";
41		goto err;
42	}
43#elif defined(HAVE_GETTIMEOFDAY)
44	struct timeval v;
45
46	RETRY_CHK((gettimeofday(&v, NULL)), ret);
47	if (ret != 0) {
48		sc = "gettimeofday";
49		goto err;
50	}
51
52	tp->tv_sec = v.tv_sec;
53	tp->tv_nsec = v.tv_usec * NS_PER_US;
54#elif defined(HAVE_TIME)
55	time_t now;
56
57	RETRY_CHK((time(&now) == (time_t)-1 ? 1 : 0), ret);
58	if (ret != 0) {
59		sc = "time";
60		goto err;
61	}
62
63	tp->tv_sec = now;
64	tp->tv_nsec = 0;
65#else
66	NO AVAILABLE CLOCK IMPLEMENTATION
67#endif
68	COMPQUIET(monotonic, 0);
69	return;
70
71err:	__db_syserr(env, ret, "%s", sc);
72	(void)__env_panic(env, __os_posix_err(ret));
73}
74