]> code.delx.au - gnu-emacs/blob - src/systime.h
Support higher-resolution time stamps.
[gnu-emacs] / src / systime.h
1 /* systime.h - System-dependent definitions for time manipulations.
2 Copyright (C) 1993-1994, 2002-2012 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
18
19 #ifndef EMACS_SYSTIME_H
20 #define EMACS_SYSTIME_H
21
22 #include <timespec.h>
23
24 #ifdef emacs
25 # ifdef HAVE_X_WINDOWS
26 # include <X11/X.h>
27 # else
28 typedef unsigned long Time;
29 # endif
30 #endif
31
32 /* On some configurations (hpux8.0, X11R4), sys/time.h and X11/Xos.h
33 disagree about the name of the guard symbol. */
34 #ifdef HPUX
35 #ifdef _STRUCT_TIMEVAL
36 #ifndef __TIMEVAL__
37 #define __TIMEVAL__
38 #endif
39 #endif
40 #endif
41 \f
42 /* The type to use to represent temporal intervals. It can be passed
43 as the timeout argument to the pselect system call. */
44 #define EMACS_TIME struct timespec
45
46 /* Resolution of EMACS_TIME time stamps (in units per second), and log
47 base 10 of the resolution. The log must be a positive integer. */
48 #define EMACS_TIME_RESOLUTION 1000000000
49 #define LOG10_EMACS_TIME_RESOLUTION 9
50
51 /* EMACS_SECS (TIME) is an rvalue for the seconds component of TIME.
52 EMACS_SECS_ADDR (time) is the address of the seconds component.
53 EMACS_SET_SECS (TIME, SECONDS) sets that to SECONDS.
54
55 EMACS_NSECS (TIME) is an rvalue for the nanoseconds component of TIME.
56 EMACS_SET_NSECS (TIME, NANOSECONDS) sets that to NANOSECONDS.
57
58 EMACS_SET_SECS_NSECS (TIME, SECS, NSECS) sets both components of TIME. */
59 #define EMACS_SECS(time) ((time).tv_sec + 0)
60 #define EMACS_NSECS(time) ((time).tv_nsec + 0)
61 #define EMACS_SECS_ADDR(time) (&(time).tv_sec)
62 #define EMACS_SET_SECS(time, seconds) ((time).tv_sec = (seconds))
63 #define EMACS_SET_NSECS(time, ns) ((time).tv_nsec = (ns))
64 #define EMACS_SET_SECS_NSECS(time, s, ns) \
65 ((void) (EMACS_SET_SECS (time, s), EMACS_SET_NSECS (time, ns)))
66
67 /* Convenience macros for older code that counts microseconds. */
68 #define EMACS_SET_USECS(time, us) ((void) EMACS_SET_NSECS (time, (us) * 1000))
69 #define EMACS_SET_SECS_USECS(time, secs, usecs) \
70 (EMACS_SET_SECS (time, secs), EMACS_SET_USECS (time, usecs))
71
72 /* Set TIME to an invalid time stamp. */
73 #define EMACS_SET_INVALID_TIME(time) EMACS_SET_SECS_NSECS(time, 0, -1)
74
75 /* Set TIME to the current system time. */
76 #define EMACS_GET_TIME(time) gettime (&(time))
77
78 /* Put into DEST the result of adding SRC1 to SRC2, or of subtracting
79 SRC2 from SRC1. On overflow, store an extremal value. */
80 #define EMACS_ADD_TIME(dest, src1, src2) ((dest) = timespec_add (src1, src2))
81 #define EMACS_SUB_TIME(dest, src1, src2) ((dest) = timespec_sub (src1, src2))
82
83 /* Return the sign of the valid time stamp TIME, either -1, 0, or 1. */
84 #define EMACS_TIME_SIGN(time) timespec_sign (time)
85
86 /* Return 1 if TIME is a valid time stamp. */
87 #define EMACS_TIME_VALID_P(time) (0 <= (time).tv_nsec)
88
89 /* Convert the double D to the greatest EMACS_TIME not greater than D.
90 On overflow, return an extremal value. Return the minimum
91 EMACS_TIME if D is not a number. */
92 #define EMACS_TIME_FROM_DOUBLE(d) dtotimespec (d)
93
94 /* Convert the Emacs time T to an approximate double value D. */
95 #define EMACS_TIME_TO_DOUBLE(t) timespectod (t)
96
97 /* defined in sysdep.c */
98 extern int set_file_times (int, const char *, EMACS_TIME, EMACS_TIME);
99 extern struct timeval make_timeval (EMACS_TIME);
100
101 /* defined in keyboard.c */
102 extern void set_waiting_for_input (EMACS_TIME *);
103
104 /* When lisp.h is not included Lisp_Object is not defined (this can
105 happen when this files is used outside the src directory).
106 Use GCPRO1 to determine if lisp.h was included. */
107 #ifdef GCPRO1
108 /* defined in editfns.c */
109 extern Lisp_Object make_lisp_time (EMACS_TIME);
110 extern int decode_time_components (Lisp_Object, Lisp_Object, Lisp_Object,
111 Lisp_Object, EMACS_TIME *, int *);
112 extern EMACS_TIME lisp_time_argument (Lisp_Object, int *);
113 #endif
114
115 /* Compare times T1 and T2 for equality, inequality etc. */
116
117 #define EMACS_TIME_EQ(T1, T2) (timespec_cmp (T1, T2) == 0)
118 #define EMACS_TIME_NE(T1, T2) (timespec_cmp (T1, T2) != 0)
119 #define EMACS_TIME_GT(T1, T2) (timespec_cmp (T1, T2) > 0)
120 #define EMACS_TIME_GE(T1, T2) (timespec_cmp (T1, T2) >= 0)
121 #define EMACS_TIME_LT(T1, T2) (timespec_cmp (T1, T2) < 0)
122 #define EMACS_TIME_LE(T1, T2) (timespec_cmp (T1, T2) <= 0)
123
124 #endif /* EMACS_SYSTIME_H */