Back to index

glibc  2.9
pthread_condattr_setclock.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003, 2004, 2007, 2008 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the 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; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <assert.h>
00021 #include <errno.h>
00022 #include <stdbool.h>
00023 #include <time.h>
00024 #include <sysdep.h>
00025 #include "pthreadP.h"
00026 #include <kernel-features.h>
00027 
00028 
00029 int
00030 pthread_condattr_setclock (attr, clock_id)
00031      pthread_condattr_t *attr;
00032      clockid_t clock_id;
00033 {
00034   /* Only a few clocks are allowed.  CLOCK_REALTIME is always allowed.
00035      CLOCK_MONOTONIC only if the kernel has the necessary support.  */
00036   if (clock_id == CLOCK_MONOTONIC)
00037     {
00038 #ifndef __ASSUME_POSIX_TIMERS
00039 # ifdef __NR_clock_getres
00040       /* Check whether the clock is available.  */
00041       static int avail;
00042 
00043       if (avail == 0)
00044        {
00045          struct timespec ts;
00046 
00047          INTERNAL_SYSCALL_DECL (err);
00048          int val;
00049          val = INTERNAL_SYSCALL (clock_getres, err, 2, CLOCK_MONOTONIC, &ts);
00050          avail = INTERNAL_SYSCALL_ERROR_P (val, err) ? -1 : 1;
00051        }
00052 
00053       if (avail < 0)
00054 # endif
00055        /* Not available.  */
00056        return EINVAL;
00057 #endif
00058     }
00059   else if (clock_id != CLOCK_REALTIME)
00060     /* If more clocks are allowed some day the storing of the clock ID
00061        in the pthread_cond_t structure needs to be adjusted.  */
00062     return EINVAL;
00063 
00064   /* Make sure the value fits in the bits we reserved.  */
00065   assert (clock_id < (1 << COND_NWAITERS_SHIFT));
00066 
00067   int *valuep = &((struct pthread_condattr *) attr)->value;
00068 
00069   *valuep = ((*valuep & ~(((1 << COND_NWAITERS_SHIFT) - 1) << 1))
00070             | (clock_id << 1));
00071 
00072   return 0;
00073 }