testing_clock.c 864 B

1234567891011121314151617181920212223242526272829303132333435
  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. #include "testing_clock.h"
  5. #ifdef UA_ENABLE_MULTITHREADING
  6. #include <time.h>
  7. #endif
  8. UA_DateTime testingClock = 0;
  9. UA_DateTime UA_DateTime_now(void) {
  10. return testingClock;
  11. }
  12. UA_DateTime UA_DateTime_nowMonotonic(void) {
  13. return testingClock;
  14. }
  15. void
  16. UA_sleep(UA_UInt32 duration) {
  17. testingClock += duration * UA_MSEC_TO_DATETIME;
  18. }
  19. #define NANO_SECOND_MULTIPLIER 1000000 // 1 millisecond = 1,000,000 Nanoseconds
  20. void
  21. UA_realsleep(UA_UInt32 duration) {
  22. #ifdef UA_ENABLE_MULTITHREADING
  23. struct timespec sleepValue;
  24. sleepValue.tv_sec = 0;
  25. sleepValue.tv_nsec = duration * NANO_SECOND_MULTIPLIER;
  26. nanosleep(&sleepValue, NULL);
  27. #endif
  28. }