PageRenderTime 37ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/mordor/sleep.cpp

http://github.com/mozy/mordor
C++ | 48 lines | 39 code | 8 blank | 1 comment | 5 complexity | 7d1ed7dbc7173a14d043f09c146b1742 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Copyright (c) 2009 - Mozy, Inc.
  2. #include <boost/bind.hpp>
  3. #include "sleep.h"
  4. #include "assert.h"
  5. #include "fiber.h"
  6. #include "scheduler.h"
  7. #include "timer.h"
  8. namespace Mordor {
  9. static void scheduleMe(Scheduler *scheduler, Fiber::ptr fiber)
  10. {
  11. scheduler->schedule(fiber);
  12. }
  13. void
  14. sleep(TimerManager &timerManager, unsigned long long us)
  15. {
  16. MORDOR_ASSERT(Scheduler::getThis());
  17. timerManager.registerTimer(us,
  18. boost::bind(&scheduleMe, Scheduler::getThis(), Fiber::getThis()));
  19. Scheduler::yieldTo();
  20. }
  21. void
  22. sleep(unsigned long long us)
  23. {
  24. #ifdef WINDOWS
  25. Sleep((DWORD)(us / 1000));
  26. #else
  27. struct timespec ts;
  28. ts.tv_sec = us / 1000000;
  29. ts.tv_nsec = (us % 1000000) * 1000;
  30. while (true) {
  31. if (nanosleep(&ts, &ts) == -1) {
  32. if (errno == EINTR)
  33. continue;
  34. MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API("nanosleep");
  35. }
  36. break;
  37. }
  38. #endif
  39. }
  40. }