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.

153 lines
4.0 KiB

28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
  1. /*****************************************************************************
  2. * *
  3. * DH_TIME.C *
  4. * *
  5. * Freely redistributable and modifiable. Use at your own risk. *
  6. * *
  7. * Copyright 1994 The Downhill Project *
  8. *
  9. * Modified by Shane Caraveo for use with PHP
  10. *
  11. *****************************************************************************/
  12. /* Include stuff ************************************************************ */
  13. #include "time.h"
  14. #include "unistd.h"
  15. #include "signal.h"
  16. #include <winbase.h>
  17. #include <mmsystem.h>
  18. #include <errno.h>
  19. int gettimeofday(struct timeval *time_Info, struct timezone *timezone_Info)
  20. {
  21. _int64 mstimer, freq;
  22. /* Get the time, if they want it */
  23. if (time_Info != NULL) {
  24. time_Info->tv_sec = time(NULL);
  25. /* get ticks-per-second of the performance counter
  26. Note the necessary typecast to a LARGE_INTEGER structure
  27. */
  28. if (!QueryPerformanceFrequency((LARGE_INTEGER *) & freq)) {
  29. time_Info->tv_usec = 0;
  30. } else {
  31. QueryPerformanceCounter((LARGE_INTEGER *) & mstimer);
  32. mstimer = (__int64) (mstimer * .8);
  33. time_Info->tv_usec = (long) (mstimer % 0x0FFFFFFF);
  34. }
  35. }
  36. /* Get the timezone, if they want it */
  37. if (timezone_Info != NULL) {
  38. _tzset();
  39. timezone_Info->tz_minuteswest = _timezone;
  40. timezone_Info->tz_dsttime = _daylight;
  41. }
  42. /* And return */
  43. return 0;
  44. }
  45. /* this usleep isnt exactly accurate but should do ok */
  46. void usleep(unsigned int useconds)
  47. {
  48. __int64 mstimer, freq;
  49. long now, then;
  50. if (QueryPerformanceFrequency((LARGE_INTEGER *) & freq)) {
  51. QueryPerformanceCounter((LARGE_INTEGER *) & mstimer);
  52. now = (long) (((__int64) (mstimer * .8)) % 0x0FFFFFFF);
  53. then = now + useconds;
  54. while (now < then) {
  55. QueryPerformanceCounter((LARGE_INTEGER *) & mstimer);
  56. now = (long) (((__int64) (mstimer * .8)) % 0x0FFFFFFF);
  57. }
  58. } else {
  59. /*workaround for systems without performance counter
  60. this is actualy a millisecond sleep */
  61. Sleep((int) (useconds / 1000));
  62. }
  63. }
  64. #ifdef HAVE_SETITIMER
  65. #ifndef THREAD_SAFE
  66. unsigned int proftimer, virttimer, realtimer;
  67. extern LPMSG phpmsg;
  68. #endif
  69. struct timer_msg {
  70. int signal;
  71. unsigned int threadid;
  72. };
  73. LPTIMECALLBACK setitimer_timeout(UINT uTimerID, UINT info, DWORD dwUser, DWORD dw1, DWORD dw2)
  74. {
  75. struct timer_msg *msg = (struct timer_msg *) info;
  76. if (msg) {
  77. raise((int) msg->signal);
  78. PostThreadMessage(msg->threadid,
  79. WM_NOTIFY, msg->signal, 0);
  80. free(msg);
  81. }
  82. return 0;
  83. }
  84. int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue)
  85. {
  86. int timeout = value->it_value.tv_sec * 1000 + value->it_value.tv_usec;
  87. int repeat = TIME_ONESHOT;
  88. /*make sure the message queue is initialized */
  89. PeekMessage(phpmsg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
  90. if (timeout > 0) {
  91. struct timer_msg *msg = malloc(sizeof(struct timer_msg));
  92. msg->threadid = GetCurrentThreadId();
  93. if (!ovalue) {
  94. repeat = TIME_PERIODIC;
  95. }
  96. switch (which) {
  97. case ITIMER_REAL:
  98. msg->signal = SIGALRM;
  99. realtimer = timeSetEvent(timeout, 100, (LPTIMECALLBACK) setitimer_timeout, (UINT) msg, repeat);
  100. break;
  101. case ITIMER_VIRT:
  102. msg->signal = SIGVTALRM;
  103. virttimer = timeSetEvent(timeout, 100, (LPTIMECALLBACK) setitimer_timeout, (UINT) msg, repeat);
  104. break;
  105. case ITIMER_PROF:
  106. msg->signal = SIGPROF;
  107. proftimer = timeSetEvent(timeout, 100, (LPTIMECALLBACK) setitimer_timeout, (UINT) msg, repeat);
  108. break;
  109. default:
  110. errno = EINVAL;
  111. return -1;
  112. break;
  113. }
  114. } else {
  115. switch (which) {
  116. case ITIMER_REAL:
  117. timeKillEvent(realtimer);
  118. break;
  119. case ITIMER_VIRT:
  120. timeKillEvent(virttimer);
  121. break;
  122. case ITIMER_PROF:
  123. timeKillEvent(proftimer);
  124. break;
  125. default:
  126. errno = EINVAL;
  127. return -1;
  128. break;
  129. }
  130. }
  131. return 0;
  132. }
  133. #endif