2 #include "ace/OS_NS_string.h"
3 #include "ace/OS_NS_errno.h"
4 #include "ace/Time_Value.h"
5 #include "ace/OS_NS_unistd.h"
6 #include "ace/OS_NS_sys_time.h"
8 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
11 ACE_OS::asctime (const struct tm *t)
13 ACE_OS_TRACE ("ACE_OS::asctime");
14 #if defined (ACE_LACKS_ASCTIME)
16 ACE_NOTSUP_RETURN (0);
18 ACE_OSCALL_RETURN (ACE_STD_NAMESPACE::asctime (t), char *, 0);
19 #endif /* ACE_LACKS_ASCTIME */
23 ACE_OS::asctime_r (const struct tm *t, char *buf, int buflen)
25 ACE_OS_TRACE ("ACE_OS::asctime_r");
26 #if defined (ACE_HAS_REENTRANT_FUNCTIONS)
27 # if defined (ACE_HAS_2_PARAM_ASCTIME_R_AND_CTIME_R)
29 ace_asctime_r_helper (t, buf);
30 ACE_OS::strsncpy (buf, result, buflen);
33 # if defined (ACE_HAS_SIZET_PTR_ASCTIME_R_AND_CTIME_R)
34 ACE_OSCALL_RETURN (::asctime_r (t, buf, reinterpret_cast<size_t*>(&buflen)), char *, 0);
36 ACE_OSCALL_RETURN (::asctime_r (t, buf, buflen), char *, 0);
37 # endif /* ACE_HAS_SIZET_PTR_ASCTIME_R_AND_CTIME_R */
38 # endif /* ACE_HAS_2_PARAM_ASCTIME_R_AND_CTIME_R */
39 #elif defined (ACE_HAS_TR24731_2005_CRT)
41 ACE_SECURECRTCALL (asctime_s (buf, static_cast<size_t> (buflen), t), \
44 #elif defined (ACE_LACKS_ASCTIME)
47 ACE_UNUSED_ARG (buflen);
48 ACE_NOTSUP_RETURN (0);
51 ACE_OSCALL (ACE_STD_NAMESPACE::asctime (t), char *, 0, result);
52 ACE_OS::strsncpy (buf, result, buflen);
54 #endif /* ACE_HAS_REENTRANT_FUNCTIONS */
58 ACE_OS::clock_gettime (clockid_t clockid, struct timespec *ts)
60 ACE_OS_TRACE ("ACE_OS::clock_gettime");
61 #if defined (ACE_HAS_CLOCK_GETTIME)
62 ACE_OSCALL_RETURN (::clock_gettime (clockid, ts), int, -1);
64 ACE_UNUSED_ARG (clockid);
66 ACE_NOTSUP_RETURN (-1);
67 #endif /* ACE_HAS_CLOCK_GETTIME */
71 ACE_OS::clock_settime (clockid_t clockid, const struct timespec *ts)
73 #if defined (ACE_HAS_CLOCK_SETTIME)
74 # if defined (ACE_HAS_NONCONST_CLOCK_SETTIME)
75 ACE_OSCALL_RETURN (::clock_settime (clockid, const_cast<struct timespec *>(ts)), int, -1);
77 ACE_OSCALL_RETURN (::clock_settime (clockid, ts), int, -1);
78 # endif /* ACE_HAS_NONCONST_CLOCK_SETTIME */
80 ACE_UNUSED_ARG (clockid);
82 ACE_NOTSUP_RETURN (-1);
83 #endif /* ACE_HAS_CLOCK_SETTIME */
86 // Magic number declaration and definition for ctime and ctime_r ()
87 static const int ctime_buf_size = 26;
89 ACE_INLINE ACE_TCHAR *
90 ACE_OS::ctime (const time_t *t)
92 ACE_OS_TRACE ("ACE_OS::ctime");
93 #if defined (ACE_LACKS_CTIME)
95 ACE_NOTSUP_RETURN (0);
96 #elif defined (ACE_HAS_WINCE)
97 static ACE_TCHAR buf [ctime_buf_size];
98 return ACE_OS::ctime_r (t,
101 #elif defined (ACE_WIN32) && defined (ACE_USES_WCHAR)
102 ACE_OSCALL_RETURN (::_wctime (t), wchar_t *, 0);
104 # if defined (ACE_USES_WCHAR) /* Not Win32, else it would do the above */
106 ACE_OSCALL (::ctime (t), char *, 0, narrow_time);
107 if (narrow_time == 0)
109 // ACE_Ascii_To_Wide::convert allocates (via new []) a wchar_t[]. If
110 // we've done this before, free the previous one. Yes, this leaves a
111 // small memory leak (26 characters) but there's no way around this
112 // that I know of. (Steve Huston, 12-Feb-2003).
113 static wchar_t *wide_time = 0;
116 wide_time = ACE_Ascii_To_Wide::convert (narrow_time);
119 ACE_OSCALL_RETURN (::ctime (t), char *, 0);
120 # endif /* ACE_USES_WCHAR */
121 # endif /* ACE_HAS_WINCE */
124 #if !defined (ACE_HAS_WINCE) /* CE version in OS.cpp */
125 ACE_INLINE ACE_TCHAR *
126 ACE_OS::ctime_r (const time_t *t, ACE_TCHAR *buf, int buflen)
128 ACE_OS_TRACE ("ACE_OS::ctime_r");
130 #if defined (ACE_HAS_REENTRANT_FUNCTIONS)
133 # if defined (ACE_USES_WCHAR)
134 char narrow_buf[ctime_buf_size];
138 # endif /* ACE_USES_WCHAR */
140 if (buflen < ctime_buf_size)
145 # if defined (ACE_HAS_2_PARAM_ASCTIME_R_AND_CTIME_R)
146 ACE_OSCALL (::ctime_r (t, bufp), char *, 0, bufp);
147 # else /* ACE_HAS_2_PARAM_ASCTIME_R_AND_CTIME_R */
149 # if defined (ACE_HAS_SIZET_PTR_ASCTIME_R_AND_CTIME_R)
150 bufp = ::ctime_r (t, bufp, reinterpret_cast<size_t*>(&buflen));
151 # else /* ACE_CTIME_R_RETURNS_INT */
152 bufp = ::ctime_r (t, bufp, buflen);
153 # endif /* ACE_CTIME_R_RETURNS_INT */
155 # endif /* ACE_HAS_2_PARAM_ASCTIME_R_AND_CTIME_R */
160 # if defined (ACE_USES_WCHAR)
161 ACE_Ascii_To_Wide wide_buf (bufp);
162 ACE_OS::strcpy (buf, wide_buf.wchar_rep ());
166 # endif /* ACE_USES_WCHAR */
168 #elif defined (ACE_HAS_TR24731_2005_CRT)
169 if (buflen < ctime_buf_size)
174 ACE_TCHAR *result = buf;
175 # if defined (ACE_USES_WCHAR)
176 ACE_SECURECRTCALL (_wctime_s (buf, buflen, t), wchar_t *, 0, result);
178 ACE_SECURECRTCALL (ctime_s (buf, buflen, t), char *, 0, result);
182 #else /* ACE_HAS_REENTRANT_FUNCTIONS */
183 if (buflen < ctime_buf_size)
189 ACE_TCHAR *result = 0;
190 # if defined (ACE_USES_WCHAR)
191 ACE_OSCALL (::_wctime (t), wchar_t *, 0, result);
192 # else /* ACE_USES_WCHAR */
193 ACE_OSCALL (::ctime (t), char *, 0, result);
194 # endif /* ACE_USES_WCHAR */
196 ACE_OS::strsncpy (buf, result, buflen);
198 #endif /* ACE_HAS_REENTRANT_FUNCTIONS */
200 #endif /* !ACE_HAS_WINCE */
202 #if defined (ACE_USES_ULONG_FOR_STAT_TIME)
203 ACE_INLINE ACE_TCHAR *
204 ACE_OS::ctime (const unsigned long *t)
206 return ACE_OS::ctime (reinterpret_cast<const time_t *>(t));
209 ACE_INLINE ACE_TCHAR *
210 ACE_OS::ctime_r (const unsigned long *clock, ACE_TCHAR *buf, int buflen)
212 return ACE_OS::ctime_r (reinterpret_cast<const time_t *>(clock), buf, buflen);
215 ACE_INLINE struct tm *
216 ACE_OS::gmtime (const unsigned long *clock)
218 return ACE_OS::gmtime (reinterpret_cast<const time_t *>(clock));
221 ACE_INLINE struct tm *
222 ACE_OS::gmtime_r (const unsigned long *clock,
225 return ACE_OS::gmtime_r (reinterpret_cast<const time_t *>(clock), res);
228 ACE_INLINE struct tm *
229 ACE_OS::localtime (const unsigned long *clock)
231 return ACE_OS::localtime (reinterpret_cast<const time_t *>(clock));
234 ACE_INLINE struct tm *
235 ACE_OS::localtime_r (const unsigned long *clock,
238 return ACE_OS::localtime_r (reinterpret_cast<const time_t *>(clock), res);
244 #if !defined (ACE_LACKS_DIFFTIME)
246 ACE_OS::difftime (time_t t1, time_t t0)
248 return ::ace_difftime (t1, t0);
250 #endif /* ! ACE_LACKS_DIFFTIME */
252 ACE_INLINE ACE_hrtime_t
253 ACE_OS::gethrtime (const ACE_HRTimer_Op op)
255 ACE_OS_TRACE ("ACE_OS::gethrtime");
256 #if defined (ACE_HAS_HI_RES_TIMER)
258 return ::gethrtime ();
259 #elif defined (ACE_HAS_AIX_HI_RES_TIMER)
263 ::read_real_time(&tb, TIMEBASE_SZ);
264 ::time_base_to_time(&tb, TIMEBASE_SZ);
266 return ACE_hrtime_t(tb.tb_high) * ACE_ONE_SECOND_IN_NSECS + tb.tb_low;
267 #elif defined (ACE_WIN32)
271 ::QueryPerformanceCounter (&freq);
273 return freq.QuadPart;
274 #elif defined (ghs) && defined (ACE_HAS_PENTIUM)
276 // Use .obj/gethrtime.o, which was compiled with g++.
277 return ACE_GETHRTIME_NAME ();
278 #elif (defined (__GNUG__) || defined (__INTEL_COMPILER)) && \
279 !defined (ACE_VXWORKS) && defined (ACE_HAS_PENTIUM) && \
280 !defined (ACE_LACKS_PENTIUM_RDTSC)
284 # if defined (__amd64__) || defined (__x86_64__)
285 // Read the high res tick counter into 32 bit int variables "eax" and
286 // "edx", and then combine them into 64 bit int "now"
288 asm volatile ("rdtsc" : "=a" (eax), "=d" (edx) : : "memory");
289 now = (((ACE_UINT64) eax) | (((ACE_UINT64) edx) << 32));
291 // Read the high-res tick counter directly into memory variable "now".
292 // The A constraint signifies a 64-bit int.
293 asm volatile ("rdtsc" : "=A" (now) : : "memory");
297 #elif defined (ACE_LINUX) && defined (ACE_HAS_ALPHA_TIMER)
298 // NOTE: alphas only have a 32 bit tick (cycle) counter. The rpcc
299 // instruction actually reads 64 bits, but the high 32 bits are
300 // implementation-specific. Linux and Digital Unix, for example,
301 // use them for virtual tick counts, i.e., taking into account only
302 // the time that the process was running. This information is from
303 // David Mosberger's article, see comment below.
306 // The following statement is based on code published by:
307 // Mosberger, David, "How to Make Your Applications Fly, Part 1",
308 // Linux Journal Issue 42, October 1997, page 50. It reads the
309 // high-res tick counter directly into the memory variable.
310 asm volatile ("rpcc %0" : "=r" (now) : : "memory");
313 #elif defined (ACE_HAS_POWERPC_TIMER) && (defined (ghs) || defined (__GNUG__))
314 // PowerPC w/ GreenHills or g++.
321 ACE_OS::readPPCTimeBase (most, least);
326 asm volatile ("mftbu %0\n"
329 : "=r" (most), "=r" (least), "=r" (scratch));
330 } while (most != scratch);
333 return 0x100000000llu * most + least;
335 #elif defined (ACE_HAS_CLOCK_GETTIME)
336 // e.g., VxWorks (besides POWERPC && GreenHills) . . .
340 ACE_OS::clock_gettime (
341 # if defined (ACE_HAS_CLOCK_GETTIME_MONOTONIC)
345 # endif /* !ACE_HAS_CLOCK_GETTIME_MONOTONIC */
348 // Carefully create the return value to avoid arithmetic overflow
349 return static_cast<ACE_hrtime_t> (ts.tv_sec) *
350 ACE_U_ONE_SECOND_IN_NSECS + static_cast<ACE_hrtime_t> (ts.tv_nsec);
353 ACE_Time_Value const now = ACE_OS::gettimeofday ();
355 // Carefully create the return value to avoid arithmetic overflow
356 return (static_cast<ACE_hrtime_t> (now.sec ()) * (ACE_UINT32) 1000000 +
357 static_cast<ACE_hrtime_t> (now.usec ())) * (ACE_UINT32) 1000;
358 #endif /* ACE_HAS_HI_RES_TIMER */
361 ACE_INLINE struct tm *
362 ACE_OS::gmtime (const time_t *t)
364 ACE_OS_TRACE ("ACE_OS::gmtime");
365 #if defined (ACE_LACKS_GMTIME)
367 ACE_NOTSUP_RETURN (0);
369 ACE_OSCALL_RETURN (::gmtime (t), struct tm *, 0);
370 #endif /* ACE_LACKS_GMTIME */
373 ACE_INLINE struct tm *
374 ACE_OS::gmtime_r (const time_t *t, struct tm *res)
376 ACE_OS_TRACE ("ACE_OS::gmtime_r");
377 #if defined (ACE_HAS_REENTRANT_FUNCTIONS)
378 return ace_gmtime_r_helper (t, res);
379 #elif defined (ACE_HAS_TR24731_2005_CRT)
380 struct tm *tm_p = res;
381 ACE_SECURECRTCALL (gmtime_s (res, t), struct tm *, 0, tm_p);
383 #elif defined (ACE_LACKS_GMTIME)
385 ACE_UNUSED_ARG (res);
386 ACE_NOTSUP_RETURN (0);
389 ACE_OSCALL (::gmtime (t), struct tm *, 0, result) ;
393 #endif /* ACE_HAS_REENTRANT_FUNCTIONS */
396 ACE_INLINE struct tm *
397 ACE_OS::localtime (const time_t *t)
399 ACE_OS_TRACE ("ACE_OS::localtime");
400 #if defined (ACE_LACKS_LOCALTIME)
402 ACE_NOTSUP_RETURN (0);
404 ACE_OSCALL_RETURN (::localtime (t), struct tm *, 0);
405 #endif /* ACE_LACKS_LOCALTIME */
409 ACE_OS::nanosleep (const struct timespec *requested,
410 struct timespec *remaining)
412 ACE_OS_TRACE ("ACE_OS::nanosleep");
413 #if defined (ACE_HAS_CLOCK_GETTIME)
414 // ::nanosleep () is POSIX 1003.1b. So is ::clock_gettime (). So,
415 // if ACE_HAS_CLOCK_GETTIME is defined, then ::nanosleep () should
416 // be available on the platform. On Solaris 2.x, both functions
417 // require linking with -lposix4.
418 return ::nanosleep ((ACE_TIMESPEC_PTR) requested, remaining);
420 ACE_UNUSED_ARG (remaining);
422 // Convert into seconds and microseconds.
423 ACE_Time_Value tv (requested->tv_sec,
424 requested->tv_nsec / 1000);
425 return ACE_OS::sleep (tv);
426 #endif /* ACE_HAS_CLOCK_GETTIME */
430 ACE_OS::strftime (char *s, size_t maxsize, const char *format,
431 const struct tm *timeptr)
433 #if defined (ACE_LACKS_STRFTIME)
435 ACE_UNUSED_ARG (maxsize);
436 ACE_UNUSED_ARG (format);
437 ACE_UNUSED_ARG (timeptr);
438 ACE_NOTSUP_RETURN (0);
440 return ACE_STD_NAMESPACE::strftime (s, maxsize, format, timeptr);
441 #endif /* ACE_LACKS_STRFTIME */
445 ACE_OS::strptime (const char *buf, const char *format, struct tm *tm)
447 ACE_OS::memset (tm, 0, sizeof (struct tm));
448 #if defined (ACE_LACKS_STRPTIME)
449 return ACE_OS::strptime_emulation (buf, format, tm);
451 return ACE_STD_NAMESPACE::strptime (buf, format, tm);
452 #endif /* ACE_LACKS_STRPTIME */
456 ACE_OS::time (time_t *tloc)
458 ACE_OS_TRACE ("ACE_OS::time");
459 #if defined (ACE_LACKS_TIME)
460 time_t const retv = ACE_OS::gettimeofday ().sec ();
465 ACE_OSCALL_RETURN (::time (tloc), time_t, (time_t) -1);
466 #endif /* ACE_LACKS_TIME */
469 // Linux won't compile unless we explicitly use a namespace here.
470 #if defined (__GNUG__)
475 return ::ace_timezone ();
477 } /* namespace ACE_OS */
480 ACE_OS::timezone (void)
482 return ::ace_timezone ();
484 #endif /* ACE_LINUX */
489 #if defined (ACE_LACKS_TZSET)
491 #elif defined (ACE_WIN32)
492 ::_tzset (); // For Win32.
494 ::tzset (); // For UNIX platforms.
495 #endif /* ACE_LACKS_TZSET */
498 ACE_END_VERSIONED_NAMESPACE_DECL