1  /* Copyright (C) 2003-2023 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 <errno.h>
      19  #include <stdlib.h>
      20  #include <time.h>
      21  #include <sysdep.h>
      22  #include "kernel-posix-timers.h"
      23  #include <pthreadP.h>
      24  #include <shlib-compat.h>
      25  
      26  int
      27  ___timer_delete (timer_t timerid)
      28  {
      29    kernel_timer_t ktimerid = timerid_to_kernel_timer (timerid);
      30    int res = INLINE_SYSCALL_CALL (timer_delete, ktimerid);
      31  
      32    if (res == 0)
      33      {
      34        if (timer_is_sigev_thread (timerid))
      35  	{
      36  	  struct timer *kt = timerid_to_timer (timerid);
      37  
      38  	  /* Remove the timer from the list.  */
      39  	  __pthread_mutex_lock (&__timer_active_sigev_thread_lock);
      40  	  if (__timer_active_sigev_thread == kt)
      41  	    __timer_active_sigev_thread = kt->next;
      42  	  else
      43  	    {
      44  	      struct timer *prevp = __timer_active_sigev_thread;
      45  	      while (prevp->next != NULL)
      46  		if (prevp->next == kt)
      47  		  {
      48  		    prevp->next = kt->next;
      49  		    break;
      50  		  }
      51  		else
      52  		  prevp = prevp->next;
      53  	    }
      54  	  __pthread_mutex_unlock (&__timer_active_sigev_thread_lock);
      55  
      56  	  free (kt);
      57  	}
      58  
      59        return 0;
      60      }
      61  
      62    /* The kernel timer is not known or something else bad happened.
      63       Return the error.  */
      64    return -1;
      65  }
      66  versioned_symbol (libc, ___timer_delete, timer_delete, GLIBC_2_34);
      67  libc_hidden_ver (___timer_delete, __timer_delete)
      68  
      69  #if TIMER_T_WAS_INT_COMPAT
      70  # if OTHER_SHLIB_COMPAT (librt, GLIBC_2_3_3, GLIBC_2_34)
      71  compat_symbol (librt, ___timer_delete, timer_delete, GLIBC_2_3_3);
      72  #endif
      73  
      74  # if OTHER_SHLIB_COMPAT (librt, GLIBC_2_2, GLIBC_2_3_3)
      75  int
      76  __timer_delete_old (int timerid)
      77  {
      78    int res = __timer_delete (__timer_compat_list[timerid]);
      79  
      80    if (res == 0)
      81      /* Successful timer deletion, now free the index.  We only need to
      82         store a word and that better be atomic.  */
      83      __timer_compat_list[timerid] = NULL;
      84  
      85    return res;
      86  }
      87  compat_symbol (librt, __timer_delete_old, timer_delete, GLIBC_2_2);
      88  # endif /* OTHER_SHLIB_COMPAT */
      89  
      90  #else /* !TIMER_T_WAS_INT_COMPAT */
      91  /* The transition from int to timer_t did not change ABI because the
      92     type sizes are the same.  */
      93  # if OTHER_SHLIB_COMPAT (librt, GLIBC_2_2, GLIBC_2_34)
      94  compat_symbol (librt, ___timer_delete, timer_delete, GLIBC_2_2);
      95  # endif
      96  #endif /* !TIMER_T_WAS_INT_COMPAT */