timer_delete.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* Copyright (C) 2000, 2001 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Kaz Kylheku <kaz@ashi.footprints.net>.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <pthread.h>
  19. #include <time.h>
  20. #include "posix-timer.h"
  21. /* Delete timer TIMERID. */
  22. int
  23. timer_delete (timerid)
  24. timer_t timerid;
  25. {
  26. struct timer_node *timer;
  27. int retval = -1;
  28. pthread_mutex_lock (&__timer_mutex);
  29. timer = timer_id2ptr (timerid);
  30. if (! timer_valid (timer))
  31. /* Invalid timer ID or the timer is not in use. */
  32. __set_errno (EINVAL);
  33. else
  34. {
  35. if (timer->armed && timer->thread != NULL)
  36. {
  37. struct thread_node *thread = timer->thread;
  38. assert (thread != NULL);
  39. /* If thread is cancelled while waiting for handler to terminate,
  40. the mutex is unlocked and timer_delete is aborted. */
  41. pthread_cleanup_push (__timer_mutex_cancel_handler, &__timer_mutex);
  42. /* If timer is currently being serviced, wait for it to finish. */
  43. while (thread->current_timer == timer)
  44. pthread_cond_wait (&thread->cond, &__timer_mutex);
  45. pthread_cleanup_pop (0);
  46. }
  47. /* Remove timer from whatever queue it may be on and deallocate it. */
  48. timer->inuse = TIMER_DELETED;
  49. list_unlink_ip (&timer->links);
  50. timer_delref (timer);
  51. retval = 0;
  52. }
  53. pthread_mutex_unlock (&__timer_mutex);
  54. return retval;
  55. }