testing_clock.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /* Enable POSIX features */
  5. #ifndef _XOPEN_SOURCE
  6. # define _XOPEN_SOURCE 600
  7. #endif
  8. #ifndef _DEFAULT_SOURCE
  9. # define _DEFAULT_SOURCE
  10. #endif
  11. /* On older systems we need to define _BSD_SOURCE.
  12. * _DEFAULT_SOURCE is an alias for that. */
  13. #ifndef _BSD_SOURCE
  14. # define _BSD_SOURCE
  15. #endif
  16. #include <time.h>
  17. #include "testing_clock.h"
  18. #ifdef _WIN32
  19. #include <windows.h> /* WinAPI */
  20. #endif
  21. UA_DateTime testingClock = 0;
  22. UA_DateTime UA_DateTime_now(void) {
  23. return testingClock;
  24. }
  25. UA_DateTime UA_DateTime_nowMonotonic(void) {
  26. return testingClock;
  27. }
  28. void
  29. UA_fakeSleep(UA_UInt32 duration) {
  30. testingClock += duration * UA_MSEC_TO_DATETIME;
  31. }
  32. /* 1 millisecond = 1,000,000 Nanoseconds */
  33. #define NANO_SECOND_MULTIPLIER 1000000
  34. #ifdef _WIN32
  35. void
  36. UA_realSleep(UA_UInt32 duration) {
  37. Sleep(duration);
  38. }
  39. #else
  40. void
  41. UA_realSleep(UA_UInt32 duration) {
  42. UA_UInt32 sec = duration / 1000;
  43. UA_UInt32 ns = (duration % 1000) * NANO_SECOND_MULTIPLIER;
  44. struct timespec sleepValue;
  45. sleepValue.tv_sec = sec;
  46. sleepValue.tv_nsec = ns;
  47. nanosleep(&sleepValue, NULL);
  48. }
  49. #endif