timer_gettime.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Copyright (C) 2000-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If
  13. not, see <https://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <pthread.h>
  16. #include <time.h>
  17. #include "posix-timer.h"
  18. /* Get current value of timer TIMERID and store it in VALUE. */
  19. int
  20. timer_gettime (timer_t timerid, struct itimerspec *value)
  21. {
  22. struct timer_node *timer;
  23. struct timespec now, expiry;
  24. int retval = -1, armed = 0, valid;
  25. clock_t clock = 0;
  26. pthread_mutex_lock (&__timer_mutex);
  27. timer = timer_id2ptr (timerid);
  28. valid = timer_valid (timer);
  29. if (valid) {
  30. armed = timer->armed;
  31. expiry = timer->expirytime;
  32. clock = timer->clock;
  33. value->it_interval = timer->value.it_interval;
  34. }
  35. pthread_mutex_unlock (&__timer_mutex);
  36. if (valid)
  37. {
  38. if (armed)
  39. {
  40. __clock_gettime (clock, &now);
  41. if (timespec_compare (&now, &expiry) < 0)
  42. timespec_sub (&value->it_value, &expiry, &now);
  43. else
  44. {
  45. value->it_value.tv_sec = 0;
  46. value->it_value.tv_nsec = 0;
  47. }
  48. }
  49. else
  50. {
  51. value->it_value.tv_sec = 0;
  52. value->it_value.tv_nsec = 0;
  53. }
  54. retval = 0;
  55. }
  56. else
  57. __set_errno (EINVAL);
  58. return retval;
  59. }