Back to index

glibc  2.9
sem_init.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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 <errno.h>
00021 #include <semaphore.h>
00022 #include <lowlevellock.h>
00023 #include <shlib-compat.h>
00024 #include "semaphoreP.h"
00025 #include <kernel-features.h>
00026 
00027 
00028 int
00029 __new_sem_init (sem, pshared, value)
00030      sem_t *sem;
00031      int pshared;
00032      unsigned int value;
00033 {
00034   /* Parameter sanity check.  */
00035   if (__builtin_expect (value > SEM_VALUE_MAX, 0))
00036     {
00037       __set_errno (EINVAL);
00038       return -1;
00039     }
00040 
00041   /* Map to the internal type.  */
00042   struct new_sem *isem = (struct new_sem *) sem;
00043 
00044   /* Use the values the user provided.  */
00045   isem->value = value;
00046 #ifdef __ASSUME_PRIVATE_FUTEX
00047   isem->private = pshared ? 0 : FUTEX_PRIVATE_FLAG;
00048 #else
00049   isem->private = pshared ? 0 : THREAD_GETMEM (THREAD_SELF,
00050                                           header.private_futex);
00051 #endif
00052 
00053   isem->nwaiters = 0;
00054 
00055   return 0;
00056 }
00057 versioned_symbol (libpthread, __new_sem_init, sem_init, GLIBC_2_1);
00058 
00059 
00060 
00061 #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
00062 int
00063 attribute_compat_text_section
00064 __old_sem_init (sem, pshared, value)
00065      sem_t *sem;
00066      int pshared;
00067      unsigned int value;
00068 {
00069   /* Parameter sanity check.  */
00070   if (__builtin_expect (value > SEM_VALUE_MAX, 0))
00071     {
00072       __set_errno (EINVAL);
00073       return -1;
00074     }
00075 
00076   /* Map to the internal type.  */
00077   struct old_sem *isem = (struct old_sem *) sem;
00078 
00079   /* Use the value the user provided.  */
00080   isem->value = value;
00081 
00082   /* We cannot store the PSHARED attribute.  So we always use the
00083      operations needed for shared semaphores.  */
00084 
00085   return 0;
00086 }
00087 compat_symbol (libpthread, __old_sem_init, sem_init, GLIBC_2_0);
00088 #endif