Amazing the difference a paren can make, including fix my last commit.
[sxemacs] / src / systime.h
1 /* systime.h - System-dependent definitions for time manipulations.
2    Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
3
4 This file is part of SXEmacs
5
6 SXEmacs 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 SXEmacs 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 this program.  If not, see <http://www.gnu.org/licenses/>. */
18
19
20 /* Synched up with: FSF 19.30. */
21
22 #ifndef INCLUDED_systime_h_
23 #define INCLUDED_systime_h_
24
25 #ifdef TIME_WITH_SYS_TIME
26 # include <sys/time.h>
27 # include <time.h>
28 #else
29 # ifdef HAVE_SYS_TIME_H
30 #  include <sys/time.h>
31 # else
32 #  include <time.h>
33 # endif
34 #endif
35
36 /* select() is supposed to be (Unix98) defined in sys/time.h,
37    but FreeBSD and Irix 5 put it in unistd.h instead.
38    If we have it, including it can't hurt. */
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42
43
44 /* struct utimbuf */
45
46 #ifdef HAVE_UTIME
47 # include <utime.h>
48 #endif
49
50 #if defined(HAVE_TZNAME)
51 #ifndef tzname                  /* For SGI.  */
52 extern char *tzname[];          /* RS6000 and others want it this way.  */
53 #endif
54 #endif
55
56 /* On some configurations (hpux8.0, X11R4), sys/time.h and X11/Xos.h
57    disagree about the name of the guard symbol.  */
58 #ifdef HPUX
59 #ifdef _STRUCT_TIMEVAL
60 #ifndef __TIMEVAL__
61 #define __TIMEVAL__
62 #endif
63 #endif
64 #endif
65 \f
66 /* EMACS_TIME is the type to use to represent temporal intervals.
67    At one point this was 'struct timeval' on some systems, int on others.
68    But this is stupid.  Other things than select() code like to
69    manipulate time values, and so microsecond precision should be
70    maintained.  Separate typedefs and conversion functions are provided
71    for select().
72
73    EMACS_SECS (TIME) is an rvalue for the seconds component of TIME.
74    EMACS_SET_SECS (TIME, SECONDS) sets that to SECONDS.
75
76    EMACS_USECS (TIME) is an rvalue for the microseconds component of TIME.
77    EMACS_SET_USECS (TIME, MICROSECONDS) sets that to MICROSECONDS.
78
79    Note that all times are returned in "normalized" format (i.e. the
80    usecs value is in the range 0 <= value < 1000000) and are assumed
81    to be passed in in this format.
82
83    EMACS_SET_SECS_USECS (TIME, SECS, USECS) sets both components of TIME.
84
85    EMACS_GET_TIME (TIME) stores the current system time in TIME, which
86         should be an lvalue.
87
88    set_file_times (PATH, ATIME, MTIME) changes the last-access and
89         last-modification times of the file named PATH to ATIME and
90         MTIME, which are EMACS_TIMEs.
91
92    EMACS_NORMALIZE_TIME (TIME) coerces TIME into normalized format.
93
94    EMACS_ADD_TIME (DEST, SRC1, SRC2) adds SRC1 to SRC2 and stores the
95         result in DEST.  Either or both may be negative.
96
97    EMACS_SUB_TIME (DEST, SRC1, SRC2) subtracts SRC2 from SRC1 and
98         stores the result in DEST.  Either or both may be negative.
99
100    EMACS_TIME_NEG_P (TIME) is true iff TIME is negative.
101
102    EMACS_TIME_EQUAL (TIME1, TIME2) is true iff TIME1 is the same as TIME2.
103    EMACS_TIME_GREATER (TIME1, TIME2) is true iff TIME1 is greater than
104         TIME2.
105    EMACS_TIME_EQUAL_OR_GREATER (TIME1, TIME2) is true iff TIME1 is
106         greater than or equal to TIME2.
107
108 */
109
110 #ifdef HAVE_TIMEVAL
111
112 #define EMACS_SELECT_TIME struct timeval
113 #define EMACS_TIME_TO_SELECT_TIME(time, select_time) ((select_time) = (time))
114
115 #else                           /* not HAVE_TIMEVAL */
116
117 struct timeval {
118         long tv_sec;            /* seconds */
119         long tv_usec;           /* microseconds */
120 };
121
122 #define EMACS_SELECT_TIME int
123 #define EMACS_TIME_TO_SELECT_TIME(time, select_time) \
124   EMACS_TIME_TO_INT (time, select_time)
125
126 #endif                          /* not HAVE_TIMEVAL */
127
128 #define EMACS_TIME_TO_INT(time, intvar)         \
129 do {                                            \
130   EMACS_TIME tmptime = time;                    \
131                                                 \
132   if (tmptime.tv_usec > 0)                      \
133     (intvar) = tmptime.tv_sec + 1;              \
134   else                                          \
135     (intvar) = tmptime.tv_sec;                  \
136 } while (0)
137
138 #define EMACS_TIME struct timeval
139 #define EMACS_SECS(time)                    ((time).tv_sec  + 0)
140 #define EMACS_USECS(time)                   ((time).tv_usec + 0)
141 #define EMACS_SET_SECS(time, seconds)       ((time).tv_sec  = (seconds))
142 #define EMACS_SET_USECS(time, microseconds) ((time).tv_usec = (microseconds))
143
144 #if !defined (HAVE_GETTIMEOFDAY)
145 int gettimeofday(struct timeval *, void *);
146 #endif
147
148 /* On SVR4, the compiler may complain if given this extra BSD arg.  */
149 #ifdef GETTIMEOFDAY_ONE_ARGUMENT
150 #define EMACS_GETTIMEOFDAY(time) gettimeofday(time)
151 #else
152 #define EMACS_GETTIMEOFDAY(time) gettimeofday(time,0)
153 #endif
154
155 /* According to the Xt sources, some NTP daemons on some systems may
156    return non-normalized values. */
157 #define EMACS_GET_TIME(time)                                    \
158 do {                                                            \
159   EMACS_GETTIMEOFDAY (&(time));                                 \
160   EMACS_NORMALIZE_TIME (time);                                  \
161 } while (0)
162
163 #define EMACS_NORMALIZE_TIME(time)                              \
164 do {                                                            \
165   while ((time).tv_usec >= 1000000)                             \
166     {                                                           \
167       (time).tv_usec -= 1000000;                                \
168       (time).tv_sec++;                                          \
169     }                                                           \
170   while ((time).tv_usec < 0)                                    \
171     {                                                           \
172       (time).tv_usec += 1000000;                                \
173       (time).tv_sec--;                                          \
174     }                                                           \
175 } while (0)
176
177 #define EMACS_ADD_TIME(dest, src1, src2)                        \
178 do {                                                            \
179   (dest).tv_sec  = (src1).tv_sec  + (src2).tv_sec;              \
180   (dest).tv_usec = (src1).tv_usec + (src2).tv_usec;             \
181   EMACS_NORMALIZE_TIME (dest);                                  \
182 } while (0)
183
184 #define EMACS_SUB_TIME(dest, src1, src2)                        \
185 do {                                                            \
186   (dest).tv_sec  = (src1).tv_sec  - (src2).tv_sec;              \
187   (dest).tv_usec = (src1).tv_usec - (src2).tv_usec;             \
188   EMACS_NORMALIZE_TIME (dest);                                  \
189 } while (0)
190
191 #define EMACS_TIME_NEG_P(time) ((long)(time).tv_sec < 0)
192
193 #define EMACS_TIME_EQUAL(time1, time2)                          \
194   ((time1).tv_sec == (time2).tv_sec &&                          \
195    (time1).tv_usec == (time2).tv_usec)
196
197 #define EMACS_TIME_GREATER(time1, time2)                        \
198   ((time1).tv_sec > (time2).tv_sec ||                           \
199    ((time1).tv_sec == (time2).tv_sec &&                         \
200     (time1).tv_usec > (time2).tv_usec))
201
202 #define EMACS_TIME_EQUAL_OR_GREATER(time1, time2)               \
203   ((time1).tv_sec > (time2).tv_sec ||                           \
204    ((time1).tv_sec == (time2).tv_sec &&                         \
205     (time1).tv_usec >= (time2).tv_usec))
206
207 #define EMACS_SET_SECS_USECS(time, secs, usecs)         \
208   (EMACS_SET_SECS (time, secs), EMACS_SET_USECS (time, usecs))
209
210 #ifdef emacs
211 int set_file_times(Lisp_Object path, EMACS_TIME atime, EMACS_TIME mtime);
212 #endif
213
214 void get_process_times(double *user_time, double *system_time,
215                        double *real_time);
216
217
218 /* #### Move this comment elsewhere when we figure out the place.
219
220    "qxe" is a unique prefix used to identify encapsulations of standard
221    library functions.  We used to play pre-processing games but in
222    general this leads to nothing but trouble because someone first
223    encountering the code will have no idea that what appears to be a
224    call to a library function has actually been redefined to be a call
225    somewhere else.  This is doubly true when the redefinition occurs
226    in out-of-the way s+m files and only on certainly systems.
227
228    By making the encapsulation explicit we might be making the code
229    that uses is slightly less pretty, but this is more than compensated
230    for by the huge increase in clarity.
231
232    "Standard library function" can refer to any function in any
233    standard library.  If we are explicitly changing the semantics
234    (e.g. Mule-encapsulating), we should use an extended version of
235    the prefix, e.g. perhaps "qxe_xlat_" for functions that Mule-
236    encapsulate, or "qxe_retry_" for functions that automatically
237    retry a system call interrupted by EINTR.  In general, if there
238    is no prefix extension, it means the function is trying to
239    provide (more or less) the same semantics as the standard library
240    function; but be aware that the reimplementation may be incomplete
241    or differ in important respects.  This is especially the case
242    when attempts are made to implement Unix functions on MS Windows.
243 */
244
245 int qxe_setitimer(int kind, const struct itimerval *itnew,
246                   struct itimerval *itold);
247
248 #endif                          /* INCLUDED_systime_h_ */