timer_settime.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Check: a unit test framework for C
  3. * Copyright (C) 2001, 2002 Arien Malec
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the
  17. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  18. * MA 02110-1301, USA.
  19. */
  20. #include "libcompat.h"
  21. int timer_settime(timer_t timerid CK_ATTRIBUTE_UNUSED,
  22. int flags CK_ATTRIBUTE_UNUSED,
  23. const struct itimerspec *new_value,
  24. struct itimerspec *old_value CK_ATTRIBUTE_UNUSED)
  25. {
  26. #ifdef HAVE_SETITIMER
  27. /*
  28. * If the system does not have timer_settime() but does have
  29. * setitimer() use that instead of alarm().
  30. */
  31. struct itimerval interval;
  32. interval.it_value.tv_sec = new_value->it_value.tv_sec;
  33. interval.it_value.tv_usec = new_value->it_value.tv_nsec / 1000;
  34. interval.it_interval.tv_sec = new_value->it_interval.tv_sec;
  35. interval.it_interval.tv_usec = new_value->it_interval.tv_nsec / 1000;
  36. return setitimer(ITIMER_REAL, &interval, NULL);
  37. #else
  38. int seconds = new_value->it_value.tv_sec;
  39. /*
  40. * As the alarm() call has only second precision, if the caller
  41. * specifies partial seconds, we round up to the nearest second.
  42. */
  43. if(new_value->it_value.tv_nsec > 0)
  44. {
  45. seconds += 1;
  46. }
  47. alarm(seconds);
  48. return 0;
  49. #endif
  50. }