Back to index

glibc  2.9
timer_settime.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 <errno.h>
00021 #include <pthread.h>
00022 #include <time.h>
00023 
00024 #include "posix-timer.h"
00025 
00026 
00027 /* Set timer TIMERID to VALUE, returning old value in OVLAUE.  */
00028 int
00029 timer_settime (timerid, flags, value, ovalue)
00030      timer_t timerid;
00031      int flags;
00032      const struct itimerspec *value;
00033      struct itimerspec *ovalue;
00034 {
00035   struct timer_node *timer;
00036   struct thread_node *thread = NULL;
00037   struct timespec now;
00038   int have_now = 0, need_wakeup = 0;
00039   int retval = -1;
00040 
00041   timer = timer_id2ptr (timerid);
00042   if (timer == NULL)
00043     {
00044       __set_errno (EINVAL);
00045       goto bail;
00046     }
00047 
00048   if (value->it_interval.tv_nsec < 0
00049       || value->it_interval.tv_nsec >= 1000000000
00050       || value->it_value.tv_nsec < 0
00051       || value->it_value.tv_nsec >= 1000000000)
00052     {
00053       __set_errno (EINVAL);
00054       goto bail;
00055     }
00056 
00057   /* Will need to know current time since this is a relative timer;
00058      might as well make the system call outside of the lock now! */
00059 
00060   if ((flags & TIMER_ABSTIME) == 0)
00061     {
00062       clock_gettime (timer->clock, &now);
00063       have_now = 1;
00064     }
00065 
00066   pthread_mutex_lock (&__timer_mutex);
00067   timer_addref (timer);
00068 
00069   /* One final check of timer validity; this one is possible only
00070      until we have the mutex, because it accesses the inuse flag. */
00071 
00072   if (! timer_valid(timer))
00073     {
00074       __set_errno (EINVAL);
00075       goto unlock_bail;
00076     }
00077 
00078   if (ovalue != NULL)
00079     {
00080       ovalue->it_interval = timer->value.it_interval;
00081 
00082       if (timer->armed)
00083        {
00084          if (! have_now)
00085            {
00086              pthread_mutex_unlock (&__timer_mutex);
00087              clock_gettime (timer->clock, &now);
00088              have_now = 1;
00089              pthread_mutex_lock (&__timer_mutex);
00090              timer_addref (timer);
00091            }
00092 
00093          timespec_sub (&ovalue->it_value, &timer->expirytime, &now);
00094        }
00095       else
00096        {
00097          ovalue->it_value.tv_sec = 0;
00098          ovalue->it_value.tv_nsec = 0;
00099        }
00100     }
00101 
00102   timer->value = *value;
00103 
00104   list_unlink_ip (&timer->links);
00105   timer->armed = 0;
00106 
00107   thread = timer->thread;
00108 
00109   /* A value of { 0, 0 } causes the timer to be stopped. */
00110   if (value->it_value.tv_sec != 0
00111       || __builtin_expect (value->it_value.tv_nsec != 0, 1))
00112     {
00113       if ((flags & TIMER_ABSTIME) != 0)
00114        /* The user specified the expiration time.  */
00115        timer->expirytime = value->it_value;
00116       else
00117        timespec_add (&timer->expirytime, &now, &value->it_value);
00118 
00119       /* Only need to wake up the thread if timer is inserted
00120         at the head of the queue. */
00121       if (thread != NULL)
00122        need_wakeup = __timer_thread_queue_timer (thread, timer);
00123       timer->armed = 1;
00124     }
00125 
00126   retval = 0;
00127 
00128 unlock_bail:
00129   timer_delref (timer);
00130   pthread_mutex_unlock (&__timer_mutex);
00131 
00132 bail:
00133   if (thread != NULL && need_wakeup)
00134     __timer_thread_wakeup (thread);
00135 
00136   return retval;
00137 }