Windows NT 4.0 source code leak
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.

169 lines
3.7 KiB

4 years ago
  1. /***
  2. *clock.c - Contains the clock runtime
  3. *
  4. * Copyright (c) 1987-1991, 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. *
  27. *******************************************************************************/
  28. #include <cruntime.h>
  29. #include <stdio.h>
  30. #include <time.h>
  31. #ifdef _POSIX_
  32. #include <posix/sys/times.h>
  33. #else
  34. #include <sys\timeb.h>
  35. #include <sys\types.h>
  36. #endif /* _POSIX_ */
  37. #ifndef _POSIX_
  38. void __cdecl __inittime(void);
  39. #ifdef _MSC_VER
  40. #pragma data_seg(".CRT$XIC")
  41. static void (__cdecl *pinit)(void) = __inittime;
  42. #pragma data_seg()
  43. #endif /* _MSC_VER */
  44. struct _timeb __itimeb = { 0, 0, 0, 0 };
  45. /***
  46. *clock_t clock() - Return the processor time used by this process.
  47. *
  48. *Purpose:
  49. * This routine calculates how much time the calling process
  50. * has used. At startup time, startup calls __inittime which stores
  51. * the initial time. The clock routine calculates the difference
  52. * between the current time and the initial time.
  53. *
  54. * Clock must reference _cinitime so that _cinitim.asm gets linked in.
  55. * That routine, in turn, puts __inittime in the startup initialization
  56. * routine table.
  57. *
  58. *Entry:
  59. * No parameters.
  60. * itime is a static structure of type timeb.
  61. *
  62. *Exit:
  63. * If successful, clock returns the number of CLK_TCKs (milliseconds)
  64. * that have elapsed. If unsuccessful, clock returns -1.
  65. *
  66. *Exceptions:
  67. * None.
  68. *
  69. *******************************************************************************/
  70. clock_t _CALLTYPE1 clock (
  71. void
  72. )
  73. {
  74. struct _timeb now;
  75. clock_t elapsed;
  76. #ifdef _CRUISER_
  77. extern int _citime;
  78. /* Reference _citime so __inittime gets executed at startup time.*/
  79. _citime=0;
  80. #endif /* _CRUISER_ */
  81. /* Calculate the difference between the initial time and now. */
  82. _ftime(&now);
  83. elapsed = (now.time - __itimeb.time) * CLOCKS_PER_SEC;
  84. elapsed += (int)now.millitm - (int)__itimeb.millitm;
  85. return(elapsed);
  86. }
  87. /***
  88. *void __inittime() - Initialize the time location
  89. *
  90. *Purpose:
  91. * This routine stores the time of the process startup.
  92. * It is only linked in if the user issues a clock runtime call.
  93. *
  94. *Entry:
  95. * No arguments.
  96. *
  97. *Exit:
  98. * No return value.
  99. *
  100. *Exceptions:
  101. * None.
  102. *
  103. *******************************************************************************/
  104. void _CALLTYPE1 __inittime (
  105. void
  106. )
  107. {
  108. _ftime(&__itimeb);
  109. }
  110. #else /* _POSIX_ */
  111. /***
  112. *clock_t clock() - Return the processor time used by this process.
  113. *
  114. *Purpose:
  115. * This routine calculates how much time the calling process
  116. * has used. It uses the POSIX system call times().
  117. *
  118. *
  119. *Entry:
  120. * No parameters.
  121. *
  122. *Exit:
  123. * If successful, clock returns the number of CLK_TCKs (milliseconds)
  124. * that have elapsed. If unsuccessful, clock returns -1.
  125. *
  126. *Exceptions:
  127. * None.
  128. *
  129. *******************************************************************************/
  130. clock_t _CALLTYPE1 clock (
  131. void
  132. )
  133. {
  134. struct tms now;
  135. clock_t elapsed;
  136. elapsed= times(&now);
  137. if (elapsed == (clock_t) -1)
  138. return((clock_t) -1);
  139. else
  140. return(now.tms_utime+now.tms_stime);
  141. }
  142. #endif /* _POSIX_ */