00001
00002
00003
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
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
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
00045 # endif
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
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
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
00086 #else
00087 ACE_UNUSED_ARG (clockid);
00088 ACE_UNUSED_ARG (ts);
00089 ACE_NOTSUP_RETURN (-1);
00090 #endif
00091 }
00092
00093
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)
00111 char *narrow_time;
00112 ACE_OSCALL (::ctime (t), char *, 0, narrow_time);
00113 if (narrow_time == 0)
00114 return 0;
00115
00116
00117
00118
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
00127 # endif
00128 }
00129
00130 #if !defined (ACE_HAS_WINCE)
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
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
00155 ACE_OSCALL (::ctime_r (t, bufp), char *, 0, bufp);
00156 # endif
00157 # else
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
00162 bufp = ::ctime_r (t, bufp, buflen);
00163 # endif
00164
00165 # endif
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
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
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
00203 ACE_OSCALL (::ctime (t), char *, 0, result);
00204 # endif
00205 if (result != 0)
00206 ACE_OS::strsncpy (buf, result, buflen);
00207 return buf;
00208 #endif
00209 }
00210 #endif
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
00219
00220 #if defined (ghs) && defined (ACE_HAS_PENTIUM) && !defined (ACE_WIN32)
00221 extern "C" ACE_hrtime_t ACE_GETHRTIME_NAME ();
00222 #endif
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
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
00261 ACE_hrtime_t now;
00262 # endif
00263
00264
00265
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
00277 return now;
00278 # endif
00279 #elif defined (linux) && defined (ACE_HAS_ALPHA_TIMER)
00280
00281
00282
00283
00284
00285
00286 ACE_UINT32 now;
00287
00288
00289
00290
00291
00292 asm volatile ("rpcc %0" : "=r" (now) : : "memory");
00293
00294 return now;
00295 #elif defined (ACE_HAS_POWERPC_TIMER) && (defined (ghs) || defined (__GNUG__))
00296
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
00318 return 0x100000000llu * most + least;
00319 #endif
00320
00321 #elif defined (ACE_HAS_CLOCK_GETTIME)
00322
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
00330 CLOCK_REALTIME,
00331 &ts);
00332
00333
00334
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
00342
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
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
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
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
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
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
00406
00407
00408
00409 return ::nanosleep ((ACE_TIMESPEC_PTR) requested, remaining);
00410 #else
00411 ACE_UNUSED_ARG (remaining);
00412
00413
00414 ACE_Time_Value tv (requested->tv_sec,
00415 requested->tv_nsec / 1000);
00416 return ACE_OS::sleep (tv);
00417 #endif
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
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
00447 #else
00448 return ::strptime (buf, format, tm);
00449 #endif
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
00464 }
00465
00466
00467 #if defined (__GNUG__)
00468 namespace ACE_OS {
00469 ACE_INLINE long
00470 timezone (void)
00471 {
00472 return ::ace_timezone ();
00473 }
00474 }
00475 #else
00476 ACE_INLINE long
00477 ACE_OS::timezone (void)
00478 {
00479 return ::ace_timezone ();
00480 }
00481 #endif
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 ();
00489 # else
00490 ::tzset ();
00491 # endif
00492 # else
00493 errno = ENOTSUP;
00494 # endif
00495 }
00496
00497 ACE_END_VERSIONED_NAMESPACE_DECL