OS_NS_time.inl

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

Generated on Sun Jan 27 12:05:34 2008 for ACE by doxygen 1.3.6