stdlib: Simplify getenv
[glibc.git] / rt / timer_delete.c
blob49136f8979b98f886cdba5517d0f5179d236dd22
1 /* Copyright (C) 2000-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
9 The GNU C Library 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 GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, see <https://www.gnu.org/licenses/>. */
18 #include <assert.h>
19 #include <errno.h>
20 #include <pthread.h>
21 #include <time.h>
23 #include "posix-timer.h"
26 /* Delete timer TIMERID. */
27 int
28 timer_delete (timer_t timerid)
30 struct timer_node *timer;
31 int retval = -1;
33 pthread_mutex_lock (&__timer_mutex);
35 timer = timer_id2ptr (timerid);
36 if (! timer_valid (timer))
37 /* Invalid timer ID or the timer is not in use. */
38 __set_errno (EINVAL);
39 else
41 if (timer->armed && timer->thread != NULL)
43 struct thread_node *thread = timer->thread;
44 assert (thread != NULL);
46 /* If thread is cancelled while waiting for handler to terminate,
47 the mutex is unlocked and timer_delete is aborted. */
48 pthread_cleanup_push (__timer_mutex_cancel_handler, &__timer_mutex);
50 /* If timer is currently being serviced, wait for it to finish. */
51 while (thread->current_timer == timer)
52 pthread_cond_wait (&thread->cond, &__timer_mutex);
54 pthread_cleanup_pop (0);
57 /* Remove timer from whatever queue it may be on and deallocate it. */
58 timer->inuse = TIMER_DELETED;
59 list_unlink_ip (&timer->links);
60 timer_delref (timer);
61 retval = 0;
64 pthread_mutex_unlock (&__timer_mutex);
66 return retval;