S390: Fix building with --disable-mutli-arch [BZ #31196]
[platform/upstream/glibc.git] / rt / timer_delete.c
1 /* Copyright (C) 2000-2024 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
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
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.
13
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/>.  */
17
18 #include <assert.h>
19 #include <errno.h>
20 #include <pthread.h>
21 #include <time.h>
22
23 #include "posix-timer.h"
24
25
26 /* Delete timer TIMERID.  */
27 int
28 timer_delete (timer_t timerid)
29 {
30   struct timer_node *timer;
31   int retval = -1;
32
33   pthread_mutex_lock (&__timer_mutex);
34
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
40     {
41       if (timer->armed && timer->thread != NULL)
42         {
43           struct thread_node *thread = timer->thread;
44           assert (thread != NULL);
45
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);
49
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);
53
54           pthread_cleanup_pop (0);
55         }
56
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;
62     }
63
64   pthread_mutex_unlock (&__timer_mutex);
65
66   return retval;
67 }