Back to index

glibc  2.9
lock-intern.h
Go to the documentation of this file.
00001 /* Copyright (C) 1994, 1996, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #ifndef _LOCK_INTERN_H
00020 #define       _LOCK_INTERN_H
00021 
00022 #include <sys/cdefs.h>
00023 #include <machine-lock.h>
00024 
00025 #ifndef _EXTERN_INLINE
00026 #define _EXTERN_INLINE __extern_inline
00027 #endif
00028 
00029 
00030 /* Initialize LOCK.  */
00031 
00032 _EXTERN_INLINE void
00033 __spin_lock_init (__spin_lock_t *__lock)
00034 {
00035   *__lock = __SPIN_LOCK_INITIALIZER;
00036 }
00037 
00038 
00039 /* Lock LOCK, blocking if we can't get it.  */
00040 extern void __spin_lock_solid (__spin_lock_t *__lock);
00041 
00042 /* Lock the spin lock LOCK.  */
00043 
00044 _EXTERN_INLINE void
00045 __spin_lock (__spin_lock_t *__lock)
00046 {
00047   if (! __spin_try_lock (__lock))
00048     __spin_lock_solid (__lock);
00049 }
00050 
00051 /* Name space-clean internal interface to mutex locks.
00052 
00053    Code internal to the C library uses these functions to lock and unlock
00054    mutex locks.  These locks are of type `struct mutex', defined in
00055    <cthreads.h>.  The functions here are name space-clean.  If the program
00056    is linked with the cthreads library, `__mutex_lock_solid' and
00057    `__mutex_unlock_solid' will invoke the corresponding cthreads functions
00058    to implement real mutex locks.  If not, simple stub versions just use
00059    spin locks.  */
00060 
00061 
00062 /* Initialize the newly allocated mutex lock LOCK for further use.  */
00063 extern void __mutex_init (void *__lock);
00064 
00065 /* Lock LOCK, blocking if we can't get it.  */
00066 extern void __mutex_lock_solid (void *__lock);
00067 
00068 /* Finish unlocking LOCK, after the spin lock LOCK->held has already been
00069    unlocked.  This function will wake up any thread waiting on LOCK.  */
00070 extern void __mutex_unlock_solid (void *__lock);
00071 
00072 /* Lock the mutex lock LOCK.  */
00073 
00074 _EXTERN_INLINE void
00075 __mutex_lock (void *__lock)
00076 {
00077   if (! __spin_try_lock ((__spin_lock_t *) __lock))
00078     __mutex_lock_solid (__lock);
00079 }
00080 
00081 /* Unlock the mutex lock LOCK.  */
00082 
00083 _EXTERN_INLINE void
00084 __mutex_unlock (void *__lock)
00085 {
00086   __spin_unlock ((__spin_lock_t *) __lock);
00087   __mutex_unlock_solid (__lock);
00088 }
00089 
00090 
00091 _EXTERN_INLINE int
00092 __mutex_trylock (void *__lock)
00093 {
00094   return __spin_try_lock ((__spin_lock_t *) __lock);
00095 }
00096 
00097 #endif /* lock-intern.h */