util.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (C) 2010 Michael Buesch <m@bues.ch>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include "util.h"
  15. #include <time.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #include <iostream>
  19. void msleep(unsigned int msecs)
  20. {
  21. int err;
  22. struct timespec time;
  23. time.tv_sec = 0;
  24. while (msecs >= 1000) {
  25. time.tv_sec++;
  26. msecs -= 1000;
  27. }
  28. time.tv_nsec = msecs;
  29. time.tv_nsec *= 1000000;
  30. do {
  31. err = nanosleep(&time, &time);
  32. } while (err && errno == EINTR);
  33. if (err) {
  34. std::cerr << "nanosleep() failed with: "
  35. << strerror(errno) << std::endl;
  36. }
  37. }