timer_delete.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 <assert.h>
  15. #include <errno.h>
  16. #include <pthread.h>
  17. #include <time.h>
  18. #include "posix-timer.h"
  19. /* Delete timer TIMERID. */
  20. int
  21. timer_delete (timer_t timerid)
  22. {
  23. struct timer_node *timer;
  24. int retval = -1;
  25. pthread_mutex_lock (&__timer_mutex);
  26. timer = timer_id2ptr (timerid);
  27. if (! timer_valid (timer))
  28. /* Invalid timer ID or the timer is not in use. */
  29. __set_errno (EINVAL);
  30. else
  31. {
  32. if (timer->armed && timer->thread != NULL)
  33. {
  34. struct thread_node *thread = timer->thread;
  35. assert (thread != NULL);
  36. /* If thread is cancelled while waiting for handler to terminate,
  37. the mutex is unlocked and timer_delete is aborted. */
  38. pthread_cleanup_push (__timer_mutex_cancel_handler, &__timer_mutex);
  39. /* If timer is currently being serviced, wait for it to finish. */
  40. while (thread->current_timer == timer)
  41. pthread_cond_wait (&thread->cond, &__timer_mutex);
  42. pthread_cleanup_pop (0);
  43. }
  44. /* Remove timer from whatever queue it may be on and deallocate it. */
  45. timer->inuse = TIMER_DELETED;
  46. list_unlink_ip (&timer->links);
  47. timer_delref (timer);
  48. retval = 0;
  49. }
  50. pthread_mutex_unlock (&__timer_mutex);
  51. return retval;
  52. }