1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2005,2008 Oracle.  All rights reserved.
5 *
6 * $Id: clock.h,v 12.9 2008/01/08 20:58:17 bostic Exp $
7 */
8/*
9 * Copyright (c) 1982, 1986, 1993
10 *	The Regents of the University of California.  All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	@(#)time.h	8.5 (Berkeley) 5/4/95
37 * FreeBSD: src/sys/sys/time.h,v 1.65 2004/04/07 04:19:49 imp Exp
38 */
39
40#ifndef _DB_CLOCK_H_
41#define	_DB_CLOCK_H_
42
43#if defined(__cplusplus)
44extern "C" {
45#endif
46
47/*
48 * This declaration is POSIX-compatible.  Because there are lots of different
49 * time.h include file patterns out there, it's easier to declare our own name
50 * in all cases than to try and discover if a system has a struct timespec.
51 * For the same reason, and because we'd have to #include <sys/time.h> in db.h,
52 * we don't export any timespec structures in the DB API, even in places where
53 * it would make sense, like the replication statistics information.
54 */
55typedef struct {
56	time_t	tv_sec;				/* seconds */
57	long	tv_nsec;			/* nanoseconds */
58} db_timespec;
59
60/* Operations on timespecs */
61#undef	timespecclear
62#define	timespecclear(tvp)	((tvp)->tv_sec = (tvp)->tv_nsec = 0)
63#undef	timespecisset
64#define	timespecisset(tvp)	((tvp)->tv_sec || (tvp)->tv_nsec)
65#undef	timespeccmp
66#define	timespeccmp(tvp, uvp, cmp)					\
67	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
68	    ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :			\
69	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
70#undef timespecadd
71/*
72 * Note that using timespecadd to add to yourself (i.e. doubling)
73 * must be supported.
74 */
75#define	timespecadd(vvp, uvp)						\
76	do {								\
77		(vvp)->tv_sec += (uvp)->tv_sec;				\
78		(vvp)->tv_nsec += (uvp)->tv_nsec;			\
79		if ((vvp)->tv_nsec >= 1000000000) {			\
80			(vvp)->tv_sec++;				\
81			(vvp)->tv_nsec -= 1000000000;			\
82		}							\
83	} while (0)
84#undef timespecsub
85#define	timespecsub(vvp, uvp)						\
86	do {								\
87		(vvp)->tv_sec -= (uvp)->tv_sec;				\
88		(vvp)->tv_nsec -= (uvp)->tv_nsec;			\
89		if ((vvp)->tv_nsec < 0) {				\
90			(vvp)->tv_sec--;				\
91			(vvp)->tv_nsec += 1000000000;			\
92		}							\
93	} while (0)
94
95#undef timespecset
96#define	timespecset(vvp, sec, nsec)					\
97	do {								\
98		(vvp)->tv_sec = (time_t)(sec);				\
99		(vvp)->tv_nsec = (long)(nsec);				\
100	} while (0)
101
102#define	DB_TIMEOUT_TO_TIMESPEC(t, vvp)					\
103	do {								\
104		(vvp)->tv_sec = (time_t)((t) / 1000000);		\
105		(vvp)->tv_nsec = (long)(((t) % 1000000) * 1000);	\
106	} while (0)
107
108#define	DB_TIMESPEC_TO_TIMEOUT(t, vvp, prec)				\
109	do {								\
110		t = (u_long)((vvp)->tv_sec * 1000000);			\
111		t += (u_long)((vvp)->tv_nsec / 1000);			\
112		/* Add in 1 usec for lost nsec precision if wanted. */	\
113		if (prec)						\
114			t++;						\
115	} while (0)
116
117#define TIMESPEC_ADD_DB_TIMEOUT(vvp, t) 			        \
118	do {							        \
119		db_timespec __tmp;				        \
120		DB_TIMEOUT_TO_TIMESPEC(t, &__tmp);		        \
121		timespecadd((vvp), &__tmp);			        \
122	} while (0)
123
124#if defined(__cplusplus)
125}
126#endif
127#endif /* !_DB_CLOCK_H_ */
128