summaryrefslogtreecommitdiff
path: root/ace/Time_Value.h
blob: eba9f0048c0f70d81e73ab6215c1c0ea5be89d7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* -*- C++ -*- */
// $Id$

// ============================================================================
//
// = LIBRARY
//    ace
// 
// = FILENAME
//    Time_Value.h
//
// = AUTHOR
//    Doug Schmidt 
// 
// ============================================================================

#if !defined (ACE_TIME_VALUE_H)
#define ACE_TIME_VALUE_H

// This file should be a link to the platform/compiler-specific
// configuration file (e.g., config-sunos5-sunc++-4.x.h).  By
// including this here we avoid nasty circular include problems...
#include "ace/config.h"

#if defined (__ACE_INLINE__)
#define ACE_INLINE inline
#else
#define ACE_INLINE
#endif /* __ACE_INLINE__ */

// Nasty macro stuff to account for Microsoft Win32 DLL nonsense.  We
// use these macros so that we don't end up with ACE software
// hard-coded to Microsoft proprietary extensions to C++.

#if defined (ACE_HAS_DLL)
#if defined (ACE_BUILD_DLL)
#if !defined (_MSC_VER) /* Mark classes as exported, Borland. */
#define ACE_Export _export
#else /* Microsoft: */
#define ACE_Export __declspec (dllexport)
#endif /* !_MSC_VER */
#else /* Using the DLL. */
#if !defined _MSC_VER
#define ACE_Export _import
#else
#define ACE_Export  __declspec (dllimport)
#endif /* !_MSC_VER */
#endif /* ACE_BUILD_DLL */
 
#else /* We're not building a DLL! */
#define ACE_Export 
#endif /* ACE_HAS_DLL */

#if defined (ACE_HAS_DLL)
#if defined (ACE_BUILD_SVC_DLL)
#if !defined (_MSC_VER) /* Mark classes as exported, Borland. */
#define ACE_Svc_Export _export
#else /* Microsoft: */
#define ACE_Svc_Export __declspec (dllexport)
#endif /* !_MSC_VER */
#else /* Using the DLL. */
#if !defined _MSC_VER
#define ACE_Svc_Export _import
#else
#define ACE_Svc_Export  __declspec (dllimport)
#endif /* !_MSC_VER */
#endif /* ACE_BUILD_DLL || ACE_BUILD_SVC_DLL */
 
#else /* We're not building a DLL! */
#define ACE_Svc_Export 
#endif /* ACE_HAS_DLL */

// This needs to go here *first* to avoid problems with AIX.
// Just to be safe we'll do it with pthreads, too -- jwr
#if defined (ACE_HAS_DCETHREADS) || defined (ACE_HAS_PTHREADS)
extern "C" {
#include /**/ <pthread.h>
#if defined (DIGITAL_UNIX)
#define pthread_self __pthread_self
extern "C" pthread_t pthread_self (void);
#endif /* DIGITAL_UNIX */
}
#endif /* ACE_HAS_DCETHREADS */

#if (ACE_NTRACE == 1)
#define ACE_TRACE(X)
#else
#define ACE_TRACE(X) ACE_Trace ____ (X, __LINE__, __FILE__)
#endif /* ACE_NTRACE */

#include /**/ <time.h>
#if defined (ACE_NEEDS_SYSTIME_H)
// Some platforms may need to include this, but I suspect that most
// will get it from <time.h>
#if defined (VXWORKS)
#include /**/ <sys/times.h>
#else
#include /**/ <sys/time.h>
#endif /* VXWORKS */
#endif /* ACE_NEEDS_SYSTIME_H */

#if !defined (ACE_HAS_POSIX_TIME)
// Definition per POSIX.
typedef struct timespec 
{		
  time_t tv_sec; // Seconds 
  long tv_nsec;	// Nanoseconds
} timespec_t;
#elif defined (ACE_HAS_BROKEN_POSIX_TIME)
// OSF/1 defines struct timespec in <sys/timers.h> - Tom Marrs
#include /**/ <sys/timers.h>
#endif /* ACE_HAS_POSIX_TIME */

// #if (!defined (timespec) && !defined (m88k))
// #define timestruc_t struct timespec
// #endif /* timespec */

#if !defined (ACE_HAS_SVR4_TIME)
// Definition per SVr4.
typedef struct timespec timestruc_t;	
#endif /* ACE_HAS_SVR4_TIME */

class ACE_Export ACE_Time_Value
  // = TITLE
  //     Operations on "timeval" structures.
  //
  // = DESCRIPTION
  //     This class centralizes all the time-related processing in
  //     ACE.  These timers are typically used in conjunction with
  //     lower-level OS mechanisms like select(), poll(), or
  //     cond_timedwait().  ACE_Time_Value help make the use of these
  //     mechanisms portable across OS platforms,
{
public:
  // = Useful constants.
  static const ACE_Time_Value zero;
  // Constant "0".

  // = Initialization method.
  ACE_Time_Value (long sec = 0, long usec = 0);
  // Default constructor.

  // = Methods for converting to/from various time formats.
  ACE_Time_Value (const struct timeval &t);
  // Construct a Time_Value from a timeval.

  ACE_Time_Value (const timestruc_t &t);
  //  Initializes the ACE_Time_Value object from a timestruc_t. 

  ACE_Time_Value (const ACE_Time_Value &tv);
  // Copy constructor.

#if defined(ACE_WIN32)
  ACE_Time_Value (const FILETIME &ft);
  //  Initializes the ACE_Time_Value object from a Win32 FILETIME
#endif

  void set (long sec = 0, long usec = 0);
  // Construct a Time_Value from a timeval.

  void set (const timeval &t);
  // Construct a Time_Value from a timeval.

  void set (const timestruc_t &t);
  //  Initializes the ACE_Time_Value object from a timestruc_t. 

#if defined(ACE_WIN32)
  void set (const FILETIME &ft);
  //  Initializes the ACE_Time_Value object from a timestruc_t. 
#endif

  long msec (void) const;
  // Converts from Time_Value format into milli-seconds format.

  void msec (long);
  // Converts from milli-seconds format into Time_Value format.

  operator timestruc_t () const;
  // Returns the value of the object as a timestruc_t. 

  operator timeval () const;
  // Returns the value of the object as a timeval.

#if defined(ACE_WIN32)
  operator FILETIME () const;
  // Returns the value of the object as a Win32 FILETIME.
#endif

  // = The following are accessor/mutator methods.

  long sec (void) const;
  // Get seconds.

  void sec (long sec);
  // Set seconds.

  long usec (void) const;
  // Get microseconds.
  
  void usec (long usec);
  // Set microseconds.

  // = The following are arithmetic methods for operating on
  // Time_Values.

  void operator += (const ACE_Time_Value &tv);
  // Add <tv> to this.

  void operator -= (const ACE_Time_Value &tv);
  // Subtract <tv> to this.

  friend ACE_Export ACE_Time_Value operator + (const ACE_Time_Value &tv1, const ACE_Time_Value &tv2);
  // Adds two ACE_Time_Value objects together, returns the sum.

  friend ACE_Export ACE_Time_Value operator - (const ACE_Time_Value &tv1, const ACE_Time_Value &tv2);
  // Subtracts two ACE_Time_Value objects, returns the difference.

  friend ACE_Export int operator < (const ACE_Time_Value &tv1, const ACE_Time_Value &tv2);
  // True if tv1 < tv2.

  friend ACE_Export int operator > (const ACE_Time_Value &tv1, const ACE_Time_Value &tv2);  
  // True if tv1 > tv2.

  friend ACE_Export int operator <= (const ACE_Time_Value &tv1, const ACE_Time_Value &tv2);
  // True if tv1 <= tv2.

  friend ACE_Export int operator >= (const ACE_Time_Value &tv1, const ACE_Time_Value &tv2);  
  // True if tv1 >= tv2.

  friend ACE_Export int operator == (const ACE_Time_Value &tv1, const ACE_Time_Value &tv2);  
  // True if tv1 == tv2.

  friend ACE_Export int operator != (const ACE_Time_Value &tv1, const ACE_Time_Value &tv2);  
  // True if tv1 != tv2.

  void dump (void) const;
  // Dump the state of an object.

private:
  void normalize (void);
  // Put the timevalue into a canonical form.

  long tv_sec_;
  // Seconds.
  
  long tv_usec_;
  // Microseconds.
};

class ACE_Export ACE_Countdown_Time
  // = TITLE
  //     Keeps track of the amount of elapsed time.
  //
  // = DESCRIPTION
  //     This class has a side-effect on the <max_wait_time> -- every
  //     time the <stop> method is called the <max_wait_time> is
  //     updated.
{
public:
  // = Initialization and termination methods.
  ACE_Countdown_Time (ACE_Time_Value *max_wait_time);
  // Cache the <max_wait_time> and call <start>.  

  ~ACE_Countdown_Time (void);
  // Call <stop>.

  int start (void);
  // Cache the current time and enter a start state.

  int stop (void);
  // Subtract the elapsed time from max_wait_time_ and enter a stopped
  // state.

  int update (void);
  // Calls stop and then start.  max_wait_time_ is modified by the
  // call to stop.

private:
  ACE_Time_Value *max_wait_time_;
  // Maximum time we were willing to wait.
  
  ACE_Time_Value start_time_;
  // Beginning of the start time.

  int stopped_;
  // Keeps track of whether we've already been stopped.
};

#if defined (__ACE_INLINE__)
#include "ace/Time_Value.i"
#endif /* __ACE_INLINE__ */

#endif /* ACE_TIME_VALUE */