OS_NS_time.inl

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // OS_NS_time.inl,v 1.27 2006/06/20 20:27:41 shuston Exp
00004 
00005 #include "ace/OS_NS_string.h"
00006 #include "ace/OS_NS_errno.h"
00007 #include "ace/Time_Value.h"
00008 #include "ace/OS_NS_unistd.h"
00009 #include "ace/OS_NS_sys_time.h"
00010 
00011 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00012 
00013 ACE_INLINE char *
00014 ACE_OS::asctime (const struct tm *t)
00015 {
00016   ACE_OS_TRACE ("ACE_OS::asctime");
00017 #if defined (ACE_LACKS_ASCTIME)
00018   ACE_UNUSED_ARG (t);
00019   ACE_NOTSUP_RETURN (0);
00020 #else
00021   ACE_OSCALL_RETURN (ACE_STD_NAMESPACE::asctime (t), char *, 0);
00022 #endif /* ACE_LACKS_ASCTIME */
00023 }
00024 
00025 ACE_INLINE char *
00026 ACE_OS::asctime_r (const struct tm *t, char *buf, int buflen)
00027 {
00028   ACE_OS_TRACE ("ACE_OS::asctime_r");
00029 #if defined (ACE_HAS_REENTRANT_FUNCTIONS)
00030 # if defined (ACE_HAS_2_PARAM_ASCTIME_R_AND_CTIME_R)
00031   char *result;
00032 #   if defined (DIGITAL_UNIX)
00033   ACE_OSCALL (::_Pasctime_r (t, buf), char *, 0, result);
00034 #   else
00035   ACE_OSCALL (::asctime_r (t, buf), char *, 0, result);
00036 #   endif /* DIGITAL_UNIX */
00037   ACE_OS::strsncpy (buf, result, buflen);
00038   return buf;
00039 # else
00040 #   if defined (ACE_HAS_SIZET_PTR_ASCTIME_R_AND_CTIME_R)
00041   ACE_OSCALL_RETURN (::asctime_r (t, buf, reinterpret_cast<size_t*>(&buflen)), char *, 0);
00042 #   else
00043   ACE_OSCALL_RETURN (::asctime_r (t, buf, buflen), char *, 0);
00044 #   endif /* ACE_HAS_SIZET_PTR_ASCTIME_R_AND_CTIME_R */
00045 # endif /* ACE_HAS_2_PARAM_ASCTIME_R_AND_CTIME_R */
00046 #elif defined (ACE_LACKS_ASCTIME_R)
00047   ACE_UNUSED_ARG (t);
00048   ACE_UNUSED_ARG (buf);
00049   ACE_UNUSED_ARG (buflen);
00050   ACE_NOTSUP_RETURN (0);
00051 #else
00052   char *result = 0;
00053   ACE_OSCALL (ACE_STD_NAMESPACE::asctime (t), char *, 0, result);
00054   ACE_OS::strsncpy (buf, result, buflen);
00055   return buf;
00056 #endif /* ACE_HAS_REENTRANT_FUNCTIONS */
00057 }
00058 
00059 ACE_INLINE int
00060 ACE_OS::clock_gettime (clockid_t clockid, struct timespec *ts)
00061 {
00062   ACE_OS_TRACE ("ACE_OS::clock_gettime");
00063 #if defined (ACE_HAS_CLOCK_GETTIME)
00064   ACE_OSCALL_RETURN (::clock_gettime (clockid, ts), int, -1);
00065 #else
00066   ACE_UNUSED_ARG (clockid);
00067   ACE_UNUSED_ARG (ts);
00068   ACE_NOTSUP_RETURN (-1);
00069 #endif /* ACE_HAS_CLOCK_GETTIME */
00070 }
00071 
00072 ACE_INLINE int
00073 ACE_OS::clock_settime (clockid_t clockid, const struct timespec *ts)
00074 {
00075 #if defined (ACE_HAS_CLOCK_SETTIME)
00076 #  if defined (ACE_HAS_NONCONST_CLOCK_SETTIME)
00077   ACE_OSCALL_RETURN (::clock_settime (clockid, const_cast<struct timespec *>(ts)), int, -1);
00078 #  else
00079   ACE_OSCALL_RETURN (::clock_settime (clockid, ts), int, -1);
00080 #  endif /* ACE_HAS_NONCONST_CLOCK_SETTIME */
00081 #else
00082   ACE_UNUSED_ARG (clockid);
00083   ACE_UNUSED_ARG (ts);
00084   ACE_NOTSUP_RETURN (-1);
00085 #endif /* ACE_HAS_CLOCK_SETTIME */
00086 }
00087 
00088 // Magic number declaration and definition for ctime and ctime_r ()
00089 static const int ctime_buf_size = 26;
00090 
00091 ACE_INLINE ACE_TCHAR *
00092 ACE_OS::ctime (const time_t *t)
00093 {
00094   ACE_OS_TRACE ("ACE_OS::ctime");
00095 #if defined (ACE_HAS_BROKEN_CTIME)
00096   ACE_OSCALL_RETURN (::asctime (::localtime (t)), char *, 0);
00097 #elif defined (ACE_HAS_WINCE)
00098   static ACE_TCHAR buf [ctime_buf_size];
00099   return ACE_OS::ctime_r (t,
00100                           buf,
00101                           ctime_buf_size);
00102 #elif defined (ACE_WIN32) && defined (ACE_USES_WCHAR)
00103   ACE_OSCALL_RETURN (::_wctime (t), wchar_t *, 0);
00104 #else
00105 #  if defined (ACE_USES_WCHAR)   /* Not Win32, else it would do the above */
00106   char *narrow_time;
00107   ACE_OSCALL (::ctime (t), char *, 0, narrow_time);
00108   if (narrow_time == 0)
00109     return 0;
00110   // ACE_Ascii_To_Wide::convert allocates (via new []) a wchar_t[]. If
00111   // we've done this before, free the previous one. Yes, this leaves a
00112   // small memory leak (26 characters) but there's no way around this
00113   // that I know of. (Steve Huston, 12-Feb-2003).
00114   static wchar_t *wide_time = 0;
00115   if (wide_time != 0)
00116     delete [] wide_time;
00117   wide_time = ACE_Ascii_To_Wide::convert (narrow_time);
00118   return wide_time;
00119 #  else
00120   ACE_OSCALL_RETURN (::ctime (t), char *, 0);
00121 #  endif /* ACE_USES_WCHAR */
00122 # endif /* ACE_HAS_BROKEN_CTIME */
00123 }
00124 
00125 #if !defined (ACE_HAS_WINCE)  /* CE version in OS.cpp */
00126 ACE_INLINE ACE_TCHAR *
00127 ACE_OS::ctime_r (const time_t *t, ACE_TCHAR *buf, int buflen)
00128 {
00129   ACE_OS_TRACE ("ACE_OS::ctime_r");
00130 
00131 #if defined (ACE_HAS_REENTRANT_FUNCTIONS)
00132 
00133   char *bufp = 0;
00134 #   if defined (ACE_USES_WCHAR)
00135   char narrow_buf[ctime_buf_size];
00136   bufp = narrow_buf;
00137 #   else
00138   bufp = buf;
00139 #   endif /* ACE_USES_WCHAR */
00140 
00141   if (buflen < ctime_buf_size)
00142     {
00143       errno = ERANGE;
00144       return 0;
00145     }
00146 #   if defined (ACE_HAS_2_PARAM_ASCTIME_R_AND_CTIME_R)
00147 #      if defined (DIGITAL_UNIX)
00148   ACE_OSCALL (::_Pctime_r (t, bufp), ACE_TCHAR *, 0, bufp);
00149 #      else /* DIGITAL_UNIX */
00150   ACE_OSCALL (::ctime_r (t, bufp), char *, 0, bufp);
00151 #      endif /* DIGITAL_UNIX */
00152 #   else /* ACE_HAS_2_PARAM_ASCTIME_R_AND_CTIME_R */
00153 
00154 #      if defined (ACE_HAS_SIZET_PTR_ASCTIME_R_AND_CTIME_R)
00155   bufp = ::ctime_r (t, bufp, reinterpret_cast<size_t*>(&buflen));
00156 #      else /* ACE_CTIME_R_RETURNS_INT */
00157   bufp = ::ctime_r (t, bufp, buflen);
00158 #      endif /* ACE_CTIME_R_RETURNS_INT */
00159 
00160 #   endif /* ACE_HAS_2_PARAM_ASCTIME_R_AND_CTIME_R */
00161 
00162   if (bufp == 0)
00163     return 0;
00164 
00165 #   if defined (ACE_USES_WCHAR)
00166   ACE_Ascii_To_Wide wide_buf (bufp);
00167   ACE_OS_String::strcpy (buf, wide_buf.wchar_rep ());
00168   return buf;
00169 #   else
00170   return bufp;
00171 #   endif /* ACE_USES_WCHAR */
00172 
00173 #else /* ACE_HAS_REENTRANT_FUNCTIONS */
00174   if (buflen < ctime_buf_size)
00175     {
00176       errno = ERANGE;
00177       return 0;
00178     }
00179 
00180   ACE_TCHAR *result;
00181 #     if defined (ACE_USES_WCHAR)
00182   ACE_OSCALL (::_wctime (t), wchar_t *, 0, result);
00183 #     else /* ACE_USES_WCHAR */
00184   ACE_OSCALL (::ctime (t), char *, 0, result);
00185 #     endif /* ACE_USES_WCHAR */
00186   if (result != 0)
00187     ACE_OS::strsncpy (buf, result, buflen);
00188   return buf;
00189 #endif /* ACE_HAS_REENTRANT_FUNCTIONS */
00190 }
00191 #endif /* !ACE_HAS_WINCE */
00192 
00193 #if !defined (ACE_LACKS_DIFFTIME)
00194 ACE_INLINE double
00195 ACE_OS::difftime (time_t t1, time_t t0)
00196 {
00197   return ::ace_difftime (t1, t0);
00198 }
00199 #endif /* ! ACE_LACKS_DIFFTIME */
00200 
00201 #if defined (ghs) && defined (ACE_HAS_PENTIUM) && !defined (ACE_WIN32)
00202   extern "C" ACE_hrtime_t ACE_GETHRTIME_NAME ();
00203 #endif /* ghs && ACE_HAS_PENTIUM */
00204 
00205 ACE_INLINE ACE_hrtime_t
00206 ACE_OS::gethrtime (const ACE_HRTimer_Op op)
00207 {
00208   ACE_OS_TRACE ("ACE_OS::gethrtime");
00209 #if defined (ACE_HAS_HI_RES_TIMER)
00210   ACE_UNUSED_ARG (op);
00211   return ::gethrtime ();
00212 #elif defined (ACE_HAS_AIX_HI_RES_TIMER)
00213   ACE_UNUSED_ARG (op);
00214   timebasestruct_t tb;
00215 
00216   ::read_real_time(&tb, TIMEBASE_SZ);
00217   ::time_base_to_time(&tb, TIMEBASE_SZ);
00218 
00219   return ACE_hrtime_t(tb.tb_high) * ACE_ONE_SECOND_IN_NSECS + tb.tb_low;
00220 #elif defined (ghs) && defined (ACE_HAS_PENTIUM) && !defined (ACE_WIN32)
00221   ACE_UNUSED_ARG (op);
00222   // Use .obj/gethrtime.o, which was compiled with g++.
00223   return ACE_GETHRTIME_NAME ();
00224 #elif (defined(__KCC) || defined (__GNUG__) || defined (__INTEL_COMPILER)) && !defined (ACE_WIN32) && !defined(ACE_VXWORKS) && defined (ACE_HAS_PENTIUM)
00225   ACE_UNUSED_ARG (op);
00226 # if defined (ACE_LACKS_LONGLONG_T)
00227   double now;
00228 # else  /* ! ACE_LACKS_LONGLONG_T */
00229   ACE_hrtime_t now;
00230 # endif /* ! ACE_LACKS_LONGLONG_T */
00231 
00232   // See comments about the RDTSC Pentium instruction for the ACE_WIN32
00233   // version of ACE_OS::gethrtime (), below.
00234   //
00235   // Read the high-res tick counter directly into memory variable "now".
00236   // The A constraint signifies a 64-bit int.
00237   asm volatile ("rdtsc" : "=A" (now) : : "memory");
00238 
00239 # if defined (ACE_LACKS_LONGLONG_T)
00240   ACE_UINT32 least, most;
00241   ACE_OS::memcpy (&least, &now, sizeof (ACE_UINT32));
00242   ACE_OS::memcpy (&most, (u_char *) &now + sizeof (ACE_UINT32),
00243                   sizeof (ACE_UINT32));
00244 
00245   ACE_hrtime_t ret (least, most);
00246   return ret;
00247 # else  /* ! ACE_LACKS_LONGLONG_T */
00248   return now;
00249 # endif /* ! ACE_LACKS_LONGLONG_T */
00250 #elif defined (linux) && defined (ACE_HAS_ALPHA_TIMER)
00251   // NOTE:  alphas only have a 32 bit tick (cycle) counter.  The rpcc
00252   // instruction actually reads 64 bits, but the high 32 bits are
00253   // implementation-specific.  Linux and Digital Unix, for example,
00254   // use them for virtual tick counts, i.e., taking into account only
00255   // the time that the process was running.  This information is from
00256   // David Mosberger's article, see comment below.
00257   ACE_UINT32 now;
00258 
00259   // The following statement is based on code published by:
00260   // Mosberger, David, "How to Make Your Applications Fly, Part 1",
00261   // Linux Journal Issue 42, October 1997, page 50.  It reads the
00262   // high-res tick counter directly into the memory variable.
00263   asm volatile ("rpcc %0" : "=r" (now) : : "memory");
00264 
00265   return now;
00266 #elif defined (ACE_WIN32)
00267   ACE_UNUSED_ARG(op);
00268   LARGE_INTEGER freq;
00269 
00270   ::QueryPerformanceCounter (&freq);
00271 
00272 #  if defined (ACE_LACKS_LONGLONG_T)
00273   ACE_UINT64 uint64_freq (freq.u.LowPart,
00274                           static_cast<unsigned int> (freq.u.HighPart));
00275   return uint64_freq;
00276 #  else
00277   return freq.QuadPart;
00278 #  endif //ACE_LACKS_LONGLONG_T
00279 
00280 #elif defined (ACE_HAS_POWERPC_TIMER) && (defined (ghs) || defined (__GNUG__))
00281   // PowerPC w/ GreenHills or g++.
00282 
00283   ACE_UNUSED_ARG (op);
00284   u_long most;
00285   u_long least;
00286 
00287 #if defined (ghs)
00288   ACE_OS::readPPCTimeBase (most, least);
00289 #else
00290   u_long scratch;
00291 
00292   do {
00293     asm volatile ("mftbu %0\n"
00294           "mftb  %1\n"
00295           "mftbu %2"
00296           : "=r" (most), "=r" (least), "=r" (scratch));
00297   } while (most != scratch);
00298 #endif
00299 
00300 #if defined (ACE_LACKS_LONGLONG_T)
00301   return ACE_U_LongLong (least, most);
00302 #else  /* ! ACE_LACKS_LONGLONG_T */
00303   return 0x100000000llu * most  +  least;
00304 #endif /* ! ACE_LACKS_LONGLONG_T */
00305 
00306 #elif defined (ACE_HAS_CLOCK_GETTIME)
00307   // e.g., VxWorks (besides POWERPC && GreenHills) . . .
00308   ACE_UNUSED_ARG (op);
00309   struct timespec ts;
00310 
00311   ACE_OS::clock_gettime (
00312 #if defined (ACE_HAS_CLOCK_GETTIME_MONOTONIC)
00313          CLOCK_MONOTONIC,
00314 #endif /* !ACE_HAS_CLOCK_GETTIME_MONOTONIC */
00315          CLOCK_REALTIME,
00316          &ts);
00317 
00318   // Carefully create the return value to avoid arithmetic overflow
00319   // if ACE_hrtime_t is ACE_U_LongLong.
00320   return static_cast<ACE_hrtime_t> (ts.tv_sec) *
00321     ACE_U_ONE_SECOND_IN_NSECS  +  static_cast<ACE_hrtime_t> (ts.tv_nsec);
00322 #else
00323   ACE_UNUSED_ARG (op);
00324   const ACE_Time_Value now = ACE_OS::gettimeofday ();
00325 
00326   // Carefully create the return value to avoid arithmetic overflow
00327   // if ACE_hrtime_t is ACE_U_LongLong.
00328   return (static_cast<ACE_hrtime_t> (now.sec ()) * (ACE_UINT32) 1000000  +
00329           static_cast<ACE_hrtime_t> (now.usec ())) * (ACE_UINT32) 1000;
00330 #endif /* ACE_HAS_HI_RES_TIMER */
00331 }
00332 
00333 ACE_INLINE struct tm *
00334 ACE_OS::gmtime (const time_t *t)
00335 {
00336   ACE_OS_TRACE ("ACE_OS::gmtime");
00337 #if defined (ACE_LACKS_GMTIME)
00338   ACE_UNUSED_ARG (t);
00339   ACE_NOTSUP_RETURN (0);
00340 #else
00341   ACE_OSCALL_RETURN (::gmtime (t), struct tm *, 0);
00342 #endif /* ACE_LACKS_GMTIME */
00343 }
00344 
00345 ACE_INLINE struct tm *
00346 ACE_OS::gmtime_r (const time_t *t, struct tm *res)
00347 {
00348   ACE_OS_TRACE ("ACE_OS::gmtime_r");
00349 #if defined (ACE_HAS_REENTRANT_FUNCTIONS)
00350 # if defined (DIGITAL_UNIX)
00351   ACE_OSCALL_RETURN (::_Pgmtime_r (t, res), struct tm *, 0);
00352 # else
00353   ACE_OSCALL_RETURN (::gmtime_r (t, res), struct tm *, 0);
00354 # endif /* DIGITAL_UNIX */
00355 #elif defined (ACE_LACKS_GMTIME_R)
00356   ACE_UNUSED_ARG (t);
00357   ACE_UNUSED_ARG (res);
00358   ACE_NOTSUP_RETURN (0);
00359 #else
00360   struct tm *result;
00361   ACE_OSCALL (::gmtime (t), struct tm *, 0, result) ;
00362   if (result != 0)
00363     *res = *result;
00364   return res;
00365 #endif /* ACE_HAS_REENTRANT_FUNCTIONS */
00366 }
00367 
00368 ACE_INLINE struct tm *
00369 ACE_OS::localtime (const time_t *t)
00370 {
00371   ACE_OS_TRACE ("ACE_OS::localtime");
00372 #if defined (ACE_LACKS_LOCALTIME)
00373   ACE_UNUSED_ARG (t);
00374   ACE_NOTSUP_RETURN (0);
00375 #else
00376   ACE_OSCALL_RETURN (::localtime (t), struct tm *, 0);
00377 #endif /* ACE_LACKS_LOCALTIME */
00378 }
00379 
00380 ACE_INLINE int
00381 ACE_OS::nanosleep (const struct timespec *requested,
00382                    struct timespec *remaining)
00383 {
00384   ACE_OS_TRACE ("ACE_OS::nanosleep");
00385 #if defined (ACE_HAS_CLOCK_GETTIME)
00386   // ::nanosleep () is POSIX 1003.1b.  So is ::clock_gettime ().  So,
00387   // if ACE_HAS_CLOCK_GETTIME is defined, then ::nanosleep () should
00388   // be available on the platform.  On Solaris 2.x, both functions
00389   // require linking with -lposix4.
00390   return ::nanosleep ((ACE_TIMESPEC_PTR) requested, remaining);
00391 #else
00392   ACE_UNUSED_ARG (remaining);
00393 
00394   // Convert into seconds and microseconds.
00395   ACE_Time_Value tv (requested->tv_sec,
00396                      requested->tv_nsec / 1000);
00397   return ACE_OS::sleep (tv);
00398 #endif /* ACE_HAS_CLOCK_GETTIME */
00399 }
00400 
00401 ACE_INLINE size_t
00402 ACE_OS::strftime (char *s, size_t maxsize, const char *format,
00403                   const struct tm *timeptr)
00404 {
00405 #if defined (ACE_LACKS_STRFTIME)
00406   ACE_UNUSED_ARG (s);
00407   ACE_UNUSED_ARG (maxsize);
00408   ACE_UNUSED_ARG (format);
00409   ACE_UNUSED_ARG (timeptr);
00410   ACE_NOTSUP_RETURN (0);
00411 #else
00412   return ACE_STD_NAMESPACE::strftime (s, maxsize, format, timeptr);
00413 #endif /* ACE_LACKS_STRFTIME */
00414 }
00415 
00416 ACE_INLINE char *
00417 ACE_OS::strptime (const char *buf, const char *format, struct tm *tm)
00418 {
00419 #if defined (ACE_LACKS_STRPTIME)
00420 #  if defined (ACE_REFUSE_STRPTIME_EMULATION)
00421   ACE_UNUSED_ARG (buf);
00422   ACE_UNUSED_ARG (format);
00423   ACE_UNUSED_ARG (tm);
00424   ACE_NOTSUP_RETURN (0);
00425 #  else
00426   return ACE_OS::strptime_emulation (buf, format, tm);
00427 #  endif /* ACE_REFUSE_STRPTIME_EMULATION */
00428 #else
00429   return ::strptime (buf, format, tm);
00430 #endif /* ACE_LACKS_STRPTIME */
00431 }
00432 
00433 ACE_INLINE time_t
00434 ACE_OS::time (time_t *tloc)
00435 {
00436   ACE_OS_TRACE ("ACE_OS::time");
00437 #if !defined (ACE_HAS_WINCE)
00438   ACE_OSCALL_RETURN (::time (tloc), time_t, (time_t) -1);
00439 #else
00440   time_t retv = ACE_OS::gettimeofday ().sec ();
00441   if (tloc)
00442     *tloc = retv;
00443   return retv;
00444 #endif /* ACE_HAS_WINCE */
00445 }
00446 
00447 // Linux won't compile unless we explicitly use a namespace here.
00448 #if defined (__GNUG__)
00449 namespace ACE_OS {
00450   ACE_INLINE long
00451   timezone (void)
00452   {
00453     return ::ace_timezone ();
00454   }
00455 } /* namespace ACE_OS */
00456 #else
00457 ACE_INLINE long
00458 ACE_OS::timezone (void)
00459 {
00460   return ::ace_timezone ();
00461 }
00462 #endif /* linux */
00463 
00464 ACE_INLINE void
00465 ACE_OS::tzset (void)
00466 {
00467 #if !defined (ACE_HAS_WINCE) && !defined (ACE_VXWORKS) && !defined(__rtems__) && !defined (ACE_HAS_DINKUM_STL)
00468 #   if defined (ACE_WIN32)
00469   ::_tzset ();  // For Win32.
00470 #   else
00471   ::tzset ();   // For UNIX platforms.
00472 #   endif /* ACE_WIN32 */
00473 # else
00474   errno = ENOTSUP;
00475 # endif /* ACE_HAS_WINCE && !VXWORKS && !__rtems__ && !ACE_HAS_DINKUM_STL */
00476 }
00477 
00478 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 09:41:58 2006 for ACE by doxygen 1.3.6