Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
5.2 KiB

  1. /***
  2. *clock.c - Contains the clock runtime
  3. *
  4. * Copyright (c) 1987-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * The clock runtime returns the processor time used by
  8. * the current process.
  9. *
  10. *Revision History:
  11. * 01-17-87 JCR Module created
  12. * 06-01-87 SKS "itime" must be declared static
  13. * 07-20-87 JCR Changes "inittime" to "_inittime"
  14. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  15. * 03-20-90 GJF Made calling type _CALLTYPE1, added #include
  16. * <cruntime.h> and fixed the copyright. Also, cleaned
  17. * up the formatting a bit.
  18. * 10-04-90 GJF New-style function declarators.
  19. * 01-22-91 GJF ANSI naming.
  20. * 07-25-91 GJF Added _pinittime definition for new initialization
  21. * scheme [_WIN32_].
  22. * 03-13-92 SKS Changed itime from static local to external as
  23. * a part of return to initializer table scheme.
  24. * Changed _inittime to __inittime.
  25. * 05-19-92 DJM POSIX support.
  26. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  27. * 10-29-93 GJF Define entry for initialization section (used to be
  28. * in i386\cinitclk.asm). Also, deleted old Cruiser
  29. * support.
  30. * 04-12-94 GJF Made definition of __itimeb conditional on ndef
  31. * DLL_FOR_WIN32S.
  32. * 02-10-95 GJF Appended Mac version of source file (somewhat cleaned
  33. * up), with appropriate #ifdef-s.
  34. * 07-25-96 RDK Moved PMAC init ptr here from tzset.c.
  35. * 08-26-97 GJF Use GetSystemTimeAsFileTime API.
  36. * 04-28-99 PML Wrap __declspec(allocate()) in _CRTALLOC macro.
  37. * 05-17-99 PML Remove all Macintosh support.
  38. * 03-27-01 PML .CRT$XI routines must now return 0 or _RT_* fatal
  39. * error code (vs7#231220)
  40. *
  41. *******************************************************************************/
  42. #include <sect_attribs.h>
  43. #include <cruntime.h>
  44. #include <windows.h>
  45. #include <stdio.h>
  46. #include <time.h>
  47. #ifdef _POSIX_
  48. #include <posix/sys/times.h>
  49. #else /* ndef _POSIX_ */
  50. #include <internal.h>
  51. #include <sys\timeb.h>
  52. #include <sys\types.h>
  53. #endif /* _POSIX_ */
  54. #ifndef _POSIX_
  55. int __cdecl __inittime(void);
  56. #ifdef _MSC_VER
  57. #pragma data_seg(".CRT$XIC")
  58. _CRTALLOC(".CRT$XIC") static _PIFV pinit = __inittime;
  59. #pragma data_seg()
  60. #endif /* _MSC_VER */
  61. static unsigned __int64 start_tics;
  62. /***
  63. *clock_t clock() - Return the processor time used by this process.
  64. *
  65. *Purpose:
  66. * This routine calculates how much time the calling process
  67. * has used. At startup time, startup calls __inittime which stores
  68. * the initial time. The clock routine calculates the difference
  69. * between the current time and the initial time.
  70. *
  71. * Clock must reference _cinitime so that _cinitim.asm gets linked in.
  72. * That routine, in turn, puts __inittime in the startup initialization
  73. * routine table.
  74. *
  75. *Entry:
  76. * No parameters.
  77. * itime is a static structure of type timeb.
  78. *
  79. *Exit:
  80. * If successful, clock returns the number of CLK_TCKs (milliseconds)
  81. * that have elapsed. If unsuccessful, clock returns -1.
  82. *
  83. *Exceptions:
  84. * None.
  85. *
  86. *******************************************************************************/
  87. clock_t __cdecl clock (
  88. void
  89. )
  90. {
  91. unsigned __int64 current_tics;
  92. FILETIME ct;
  93. GetSystemTimeAsFileTime( &ct );
  94. current_tics = (unsigned __int64)ct.dwLowDateTime +
  95. (((unsigned __int64)ct.dwHighDateTime) << 32);
  96. /* calculate the elapsed number of 100 nanosecond units */
  97. current_tics -= start_tics;
  98. /* return number of elapsed milliseconds */
  99. return (clock_t)(current_tics / 10000);
  100. }
  101. /***
  102. *int __inittime() - Initialize the time location
  103. *
  104. *Purpose:
  105. * This routine stores the time of the process startup.
  106. * It is only linked in if the user issues a clock runtime call.
  107. *
  108. *Entry:
  109. * No arguments.
  110. *
  111. *Exit:
  112. * Returns 0 to indicate no error.
  113. *
  114. *Exceptions:
  115. * None.
  116. *
  117. *******************************************************************************/
  118. int __cdecl __inittime (
  119. void
  120. )
  121. {
  122. FILETIME st;
  123. GetSystemTimeAsFileTime( &st );
  124. start_tics = (unsigned __int64)st.dwLowDateTime +
  125. (((unsigned __int64)st.dwHighDateTime) << 32);
  126. return 0;
  127. }
  128. #else /* _POSIX_ */
  129. /***
  130. *clock_t clock() - Return the processor time used by this process.
  131. *
  132. *Purpose:
  133. * This routine calculates how much time the calling process
  134. * has used. It uses the POSIX system call times().
  135. *
  136. *
  137. *Entry:
  138. * No parameters.
  139. *
  140. *Exit:
  141. * If successful, clock returns the number of CLK_TCKs (milliseconds)
  142. * that have elapsed. If unsuccessful, clock returns -1.
  143. *
  144. *Exceptions:
  145. * None.
  146. *
  147. *******************************************************************************/
  148. clock_t __cdecl clock (
  149. void
  150. )
  151. {
  152. struct tms now;
  153. clock_t elapsed;
  154. elapsed= times(&now);
  155. if (elapsed == (clock_t) -1)
  156. return((clock_t) -1);
  157. else
  158. return(now.tms_utime+now.tms_stime);
  159. }
  160. #endif /* _POSIX_ */