RakSleep.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #if defined(_WIN32)
  11. #include "WindowsIncludes.h" // Sleep
  12. #else
  13. #include <pthread.h>
  14. #include <time.h>
  15. #include <sys/time.h>
  16. pthread_mutex_t fakeMutex = PTHREAD_MUTEX_INITIALIZER;
  17. pthread_cond_t fakeCond = PTHREAD_COND_INITIALIZER;
  18. #endif
  19. #include "RakSleep.h"
  20. #if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT)
  21. #include "ThreadEmulation.h"
  22. using namespace ThreadEmulation;
  23. #endif
  24. void RakSleep(unsigned int ms)
  25. {
  26. #ifdef _WIN32
  27. Sleep(ms);
  28. #else
  29. //Single thread sleep code thanks to Furquan Shaikh, http://somethingswhichidintknow.blogspot.com/2009/09/sleep-in-pthread.html
  30. //Modified slightly from the original
  31. struct timespec timeToWait;
  32. struct timeval now;
  33. int rt;
  34. gettimeofday(&now,NULL);
  35. long seconds = ms/1000;
  36. long nanoseconds = (ms - seconds * 1000) * 1000000;
  37. timeToWait.tv_sec = now.tv_sec + seconds;
  38. timeToWait.tv_nsec = now.tv_usec*1000 + nanoseconds;
  39. if (timeToWait.tv_nsec >= 1000000000)
  40. {
  41. timeToWait.tv_nsec -= 1000000000;
  42. timeToWait.tv_sec++;
  43. }
  44. pthread_mutex_lock(&fakeMutex);
  45. rt = pthread_cond_timedwait(&fakeCond, &fakeMutex, &timeToWait);
  46. pthread_mutex_unlock(&fakeMutex);
  47. #endif
  48. }
粤ICP备19079148号