Back to index

glibc  2.9
timer_delete.c
Go to the documentation of this file.
00001 /* Copyright (C) 2000, 2001 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Kaz Kylheku <kaz@ashi.footprints.net>.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public License as
00007    published by the Free Software Foundation; either version 2.1 of the
00008    License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00017    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.  */
00019 
00020 #include <assert.h>
00021 #include <errno.h>
00022 #include <pthread.h>
00023 #include <time.h>
00024 
00025 #include "posix-timer.h"
00026 
00027 
00028 /* Delete timer TIMERID.  */
00029 int
00030 timer_delete (timerid)
00031      timer_t timerid;
00032 {
00033   struct timer_node *timer;
00034   int retval = -1;
00035 
00036   pthread_mutex_lock (&__timer_mutex);
00037 
00038   timer = timer_id2ptr (timerid);
00039   if (! timer_valid (timer))
00040     /* Invalid timer ID or the timer is not in use.  */
00041     __set_errno (EINVAL);
00042   else
00043     {
00044       if (timer->armed && timer->thread != NULL)
00045        {
00046          struct thread_node *thread = timer->thread;
00047          assert (thread != NULL);
00048 
00049          /* If thread is cancelled while waiting for handler to terminate,
00050             the mutex is unlocked and timer_delete is aborted.  */
00051          pthread_cleanup_push (__timer_mutex_cancel_handler, &__timer_mutex);
00052 
00053          /* If timer is currently being serviced, wait for it to finish.  */
00054          while (thread->current_timer == timer)
00055            pthread_cond_wait (&thread->cond, &__timer_mutex);
00056 
00057          pthread_cleanup_pop (0);
00058         }
00059 
00060       /* Remove timer from whatever queue it may be on and deallocate it.  */
00061       timer->inuse = TIMER_DELETED;
00062       list_unlink_ip (&timer->links);
00063       timer_delref (timer);
00064       retval = 0;
00065     }
00066 
00067   pthread_mutex_unlock (&__timer_mutex);
00068 
00069   return retval;
00070 }