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_WINCE)
00101 static ACE_TCHAR buf [ctime_buf_size];
00102 return ACE_OS::ctime_r (t,
00103 buf,
00104 ctime_buf_size);
00105 #elif defined (ACE_WIN32) && defined (ACE_USES_WCHAR)
00106 ACE_OSCALL_RETURN (::_wctime (t), wchar_t *, 0);
00107 #else
00108 # if defined (ACE_USES_WCHAR)
00109 char *narrow_time;
00110 ACE_OSCALL (::ctime (t), char *, 0, narrow_time);
00111 if (narrow_time == 0)
00112 return 0;
00113
00114
00115
00116
00117 static wchar_t *wide_time = 0;
00118 if (wide_time != 0)
00119 delete [] wide_time;
00120 wide_time = ACE_Ascii_To_Wide::convert (narrow_time);
00121 return wide_time;
00122 # else
00123 ACE_OSCALL_RETURN (::ctime (t), char *, 0);
00124 # endif
00125 # endif
00126 }
00127
00128 #if !defined (ACE_HAS_WINCE)
00129 ACE_INLINE ACE_TCHAR *
00130 ACE_OS::ctime_r (const time_t *t, ACE_TCHAR *buf, int buflen)
00131 {
00132 ACE_OS_TRACE ("ACE_OS::ctime_r");
00133
00134 #if defined (ACE_HAS_REENTRANT_FUNCTIONS)
00135
00136 char *bufp = 0;
00137 # if defined (ACE_USES_WCHAR)
00138 char narrow_buf[ctime_buf_size];
00139 bufp = narrow_buf;
00140 # else
00141 bufp = buf;
00142 # endif
00143
00144 if (buflen < ctime_buf_size)
00145 {
00146 errno = ERANGE;
00147 return 0;
00148 }
00149 # if defined (ACE_HAS_2_PARAM_ASCTIME_R_AND_CTIME_R)
00150 # if defined (DIGITAL_UNIX)
00151 ACE_OSCALL (::_Pctime_r (t, bufp), ACE_TCHAR *, 0, bufp);
00152 # else
00153 ACE_OSCALL (::ctime_r (t, bufp), char *, 0, bufp);
00154 # endif
00155 # else
00156
00157 # if defined (ACE_HAS_SIZET_PTR_ASCTIME_R_AND_CTIME_R)
00158 bufp = ::ctime_r (t, bufp, reinterpret_cast<size_t*>(&buflen));
00159 # else
00160 bufp = ::ctime_r (t, bufp, buflen);
00161 # endif
00162
00163 # endif
00164
00165 if (bufp == 0)
00166 return 0;
00167
00168 # if defined (ACE_USES_WCHAR)
00169 ACE_Ascii_To_Wide wide_buf (bufp);
00170 ACE_OS_String::strcpy (buf, wide_buf.wchar_rep ());
00171 return buf;
00172 # else
00173 return bufp;
00174 # endif
00175
00176 #elif defined (ACE_HAS_TR24731_2005_CRT)
00177 if (buflen < ctime_buf_size)
00178 {
00179 errno = ERANGE;
00180 return 0;
00181 }
00182 ACE_TCHAR *result = buf;
00183 # if defined (ACE_USES_WCHAR)
00184 ACE_SECURECRTCALL (_wctime_s (buf, buflen, t), wchar_t *, 0, result);
00185 # else
00186 ACE_SECURECRTCALL (ctime_s (buf, buflen, t), char *, 0, result);
00187 # endif
00188 return result;
00189
00190 #else
00191 if (buflen < ctime_buf_size)
00192 {
00193 errno = ERANGE;
00194 return 0;
00195 }
00196
00197 ACE_TCHAR *result = 0;
00198 # if defined (ACE_USES_WCHAR)
00199 ACE_OSCALL (::_wctime (t), wchar_t *, 0, result);
00200 # else
00201 ACE_OSCALL (::ctime (t), char *, 0, result);
00202 # endif
00203 if (result != 0)
00204 ACE_OS::strsncpy (buf, result, buflen);
00205 return buf;
00206 #endif
00207 }
00208 #endif
00209
00210 #if !defined (ACE_LACKS_DIFFTIME)
00211 ACE_INLINE double
00212 ACE_OS::difftime (time_t t1, time_t t0)
00213 {
00214 return ::ace_difftime (t1, t0);
00215 }
00216 #endif
00217
00218 #if defined (ghs) && defined (ACE_HAS_PENTIUM) && !defined (ACE_WIN32)
00219 extern "C" ACE_hrtime_t ACE_GETHRTIME_NAME ();
00220 #endif
00221
00222 ACE_INLINE ACE_hrtime_t
00223 ACE_OS::gethrtime (const ACE_HRTimer_Op op)
00224 {
00225 ACE_OS_TRACE ("ACE_OS::gethrtime");
00226 #if defined (ACE_HAS_HI_RES_TIMER)
00227 ACE_UNUSED_ARG (op);
00228 return ::gethrtime ();
00229 #elif defined (ACE_HAS_AIX_HI_RES_TIMER)
00230 ACE_UNUSED_ARG (op);
00231 timebasestruct_t tb;
00232
00233 ::read_real_time(&tb, TIMEBASE_SZ);
00234 ::time_base_to_time(&tb, TIMEBASE_SZ);
00235
00236 return ACE_hrtime_t(tb.tb_high) * ACE_ONE_SECOND_IN_NSECS + tb.tb_low;
00237 #elif defined (ACE_WIN32)
00238 ACE_UNUSED_ARG(op);
00239 LARGE_INTEGER freq;
00240
00241 ::QueryPerformanceCounter (&freq);
00242
00243 # if defined (ACE_LACKS_LONGLONG_T)
00244 ACE_UINT64 uint64_freq (freq.u.LowPart,
00245 static_cast<unsigned int> (freq.u.HighPart));
00246 return uint64_freq;
00247 # else
00248 return freq.QuadPart;
00249 # endif //ACE_LACKS_LONGLONG_T
00250 #elif defined (ghs) && defined (ACE_HAS_PENTIUM)
00251 ACE_UNUSED_ARG (op);
00252
00253 return ACE_GETHRTIME_NAME ();
00254 #elif (defined (__GNUG__) || defined (__INTEL_COMPILER)) && !defined(ACE_VXWORKS) && defined (ACE_HAS_PENTIUM)
00255 ACE_UNUSED_ARG (op);
00256 # if defined (ACE_LACKS_LONGLONG_T)
00257 double now;
00258 # else
00259 ACE_hrtime_t now;
00260 # endif
00261
00262 #if defined (__amd64__) || defined (__x86_64__)
00263
00264
00265 ACE_UINT32 eax, edx;
00266 asm volatile ("rdtsc" : "=a" (eax), "=d" (edx) : : "memory");
00267 now = (((ACE_UINT64) eax) | (((ACE_UINT64) edx) << 32));
00268 #else
00269
00270
00271 asm volatile ("rdtsc" : "=A" (now) : : "memory");
00272 #endif
00273
00274 # if defined (ACE_LACKS_LONGLONG_T)
00275 ACE_UINT32 least, most;
00276 ACE_OS::memcpy (&least, &now, sizeof (ACE_UINT32));
00277 ACE_OS::memcpy (&most, (u_char *) &now + sizeof (ACE_UINT32),
00278 sizeof (ACE_UINT32));
00279
00280 ACE_hrtime_t ret (least, most);
00281 return ret;
00282 # else
00283 return now;
00284 # endif
00285 #elif defined (linux) && defined (ACE_HAS_ALPHA_TIMER)
00286
00287
00288
00289
00290
00291
00292 ACE_UINT32 now;
00293
00294
00295
00296
00297
00298 asm volatile ("rpcc %0" : "=r" (now) : : "memory");
00299
00300 return now;
00301 #elif defined (ACE_HAS_POWERPC_TIMER) && (defined (ghs) || defined (__GNUG__))
00302
00303
00304 ACE_UNUSED_ARG (op);
00305 u_long most;
00306 u_long least;
00307
00308 #if defined (ghs)
00309 ACE_OS::readPPCTimeBase (most, least);
00310 #else
00311 u_long scratch;
00312
00313 do {
00314 asm volatile ("mftbu %0\n"
00315 "mftb %1\n"
00316 "mftbu %2"
00317 : "=r" (most), "=r" (least), "=r" (scratch));
00318 } while (most != scratch);
00319 #endif
00320
00321 #if defined (ACE_LACKS_LONGLONG_T)
00322 return ACE_U_LongLong (least, most);
00323 #else
00324 return 0x100000000llu * most + least;
00325 #endif
00326
00327 #elif defined (ACE_HAS_CLOCK_GETTIME)
00328
00329 ACE_UNUSED_ARG (op);
00330 struct timespec ts;
00331
00332 ACE_OS::clock_gettime (
00333 #if defined (ACE_HAS_CLOCK_GETTIME_MONOTONIC)
00334 CLOCK_MONOTONIC,
00335 #else
00336 CLOCK_REALTIME,
00337 #endif
00338 &ts);
00339
00340
00341
00342 return static_cast<ACE_hrtime_t> (ts.tv_sec) *
00343 ACE_U_ONE_SECOND_IN_NSECS + static_cast<ACE_hrtime_t> (ts.tv_nsec);
00344 #else
00345 ACE_UNUSED_ARG (op);
00346 ACE_Time_Value const now = ACE_OS::gettimeofday ();
00347
00348
00349
00350 return (static_cast<ACE_hrtime_t> (now.sec ()) * (ACE_UINT32) 1000000 +
00351 static_cast<ACE_hrtime_t> (now.usec ())) * (ACE_UINT32) 1000;
00352 #endif
00353 }
00354
00355 ACE_INLINE struct tm *
00356 ACE_OS::gmtime (const time_t *t)
00357 {
00358 ACE_OS_TRACE ("ACE_OS::gmtime");
00359 #if defined (ACE_LACKS_GMTIME)
00360 ACE_UNUSED_ARG (t);
00361 ACE_NOTSUP_RETURN (0);
00362 #else
00363 ACE_OSCALL_RETURN (::gmtime (t), struct tm *, 0);
00364 #endif
00365 }
00366
00367 ACE_INLINE struct tm *
00368 ACE_OS::gmtime_r (const time_t *t, struct tm *res)
00369 {
00370 ACE_OS_TRACE ("ACE_OS::gmtime_r");
00371 #if defined (ACE_HAS_REENTRANT_FUNCTIONS)
00372 # if defined (DIGITAL_UNIX)
00373 ACE_OSCALL_RETURN (::_Pgmtime_r (t, res), struct tm *, 0);
00374 # else
00375 ACE_OSCALL_RETURN (::gmtime_r (t, res), struct tm *, 0);
00376 # endif
00377 #elif defined (ACE_HAS_TR24731_2005_CRT)
00378 struct tm *tm_p = res;
00379 ACE_SECURECRTCALL (gmtime_s (res, t), struct tm *, 0, tm_p);
00380 return tm_p;
00381 #elif defined (ACE_LACKS_GMTIME_R)
00382 ACE_UNUSED_ARG (t);
00383 ACE_UNUSED_ARG (res);
00384 ACE_NOTSUP_RETURN (0);
00385 #else
00386 struct tm *result;
00387 ACE_OSCALL (::gmtime (t), struct tm *, 0, result) ;
00388 if (result != 0)
00389 *res = *result;
00390 return res;
00391 #endif
00392 }
00393
00394 ACE_INLINE struct tm *
00395 ACE_OS::localtime (const time_t *t)
00396 {
00397 ACE_OS_TRACE ("ACE_OS::localtime");
00398 #if defined (ACE_LACKS_LOCALTIME)
00399 ACE_UNUSED_ARG (t);
00400 ACE_NOTSUP_RETURN (0);
00401 #else
00402 ACE_OSCALL_RETURN (::localtime (t), struct tm *, 0);
00403 #endif
00404 }
00405
00406 ACE_INLINE int
00407 ACE_OS::nanosleep (const struct timespec *requested,
00408 struct timespec *remaining)
00409 {
00410 ACE_OS_TRACE ("ACE_OS::nanosleep");
00411 #if defined (ACE_HAS_CLOCK_GETTIME)
00412
00413
00414
00415
00416 return ::nanosleep ((ACE_TIMESPEC_PTR) requested, remaining);
00417 #else
00418 ACE_UNUSED_ARG (remaining);
00419
00420
00421 ACE_Time_Value tv (requested->tv_sec,
00422 requested->tv_nsec / 1000);
00423 return ACE_OS::sleep (tv);
00424 #endif
00425 }
00426
00427 ACE_INLINE size_t
00428 ACE_OS::strftime (char *s, size_t maxsize, const char *format,
00429 const struct tm *timeptr)
00430 {
00431 #if defined (ACE_LACKS_STRFTIME)
00432 ACE_UNUSED_ARG (s);
00433 ACE_UNUSED_ARG (maxsize);
00434 ACE_UNUSED_ARG (format);
00435 ACE_UNUSED_ARG (timeptr);
00436 ACE_NOTSUP_RETURN (0);
00437 #else
00438 return ACE_STD_NAMESPACE::strftime (s, maxsize, format, timeptr);
00439 #endif
00440 }
00441
00442 ACE_INLINE char *
00443 ACE_OS::strptime (const char *buf, const char *format, struct tm *tm)
00444 {
00445 ACE_OS::memset (tm, 0, sizeof (struct tm));
00446 #if defined (ACE_LACKS_STRPTIME)
00447 return ACE_OS::strptime_emulation (buf, format, tm);
00448 #else
00449 return ACE_STD_NAMESPACE::strptime (buf, format, tm);
00450 #endif
00451 }
00452
00453 ACE_INLINE time_t
00454 ACE_OS::time (time_t *tloc)
00455 {
00456 ACE_OS_TRACE ("ACE_OS::time");
00457 #if defined (ACE_LACKS_TIME)
00458 time_t const retv = ACE_OS::gettimeofday ().sec ();
00459 if (tloc)
00460 *tloc = retv;
00461 return retv;
00462 #else
00463 ACE_OSCALL_RETURN (::time (tloc), time_t, (time_t) -1);
00464 #endif
00465 }
00466
00467
00468 #if defined (__GNUG__)
00469 namespace ACE_OS {
00470 ACE_INLINE long
00471 timezone (void)
00472 {
00473 return ::ace_timezone ();
00474 }
00475 }
00476 #else
00477 ACE_INLINE long
00478 ACE_OS::timezone (void)
00479 {
00480 return ::ace_timezone ();
00481 }
00482 #endif
00483
00484 ACE_INLINE void
00485 ACE_OS::tzset (void)
00486 {
00487 #if defined (ACE_LACKS_TZSET)
00488 errno = ENOTSUP;
00489 #elif defined (ACE_WIN32)
00490 ::_tzset ();
00491 #else
00492 ::tzset ();
00493 #endif
00494 }
00495
00496 ACE_END_VERSIONED_NAMESPACE_DECL