Back to index

glibc  2.9
pspinlock.c
Go to the documentation of this file.
00001 /* POSIX spinlock implementation.  ia64 version.
00002    Copyright (C) 2000, 2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jes Sorensen <jes@linuxcare.com>
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public License as
00008    published by the Free Software Foundation; either version 2.1 of the
00009    License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00018    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019    Boston, MA 02111-1307, USA.  */
00020 
00021 #include <errno.h>
00022 #include <pthread.h>
00023 #include "internals.h"
00024 #include <ia64intrin.h>
00025 
00026 /* This implementation is inspired by the implementation used in the
00027    Linux kernel. */
00028 
00029 int
00030 __pthread_spin_lock (pthread_spinlock_t *lock)
00031 {
00032   int *p = (int *) lock;
00033   
00034   while (__builtin_expect (__sync_val_compare_and_swap (p, 0, 1), 0))
00035     {
00036       /* Spin without using the atomic instruction.  */
00037       do
00038         __asm __volatile ("" : : : "memory");
00039       while (*p);
00040     }
00041   return 0;
00042 }
00043 weak_alias (__pthread_spin_lock, pthread_spin_lock)
00044 
00045 
00046 int
00047 __pthread_spin_trylock (pthread_spinlock_t *lock)
00048 {
00049   return __sync_val_compare_and_swap ((int *) lock, 0, 1) == 0 ? 0 : EBUSY;
00050 }
00051 weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
00052 
00053 
00054 int
00055 __pthread_spin_unlock (pthread_spinlock_t *lock)
00056 {
00057   return *lock = 0;
00058 }
00059 weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
00060 
00061 
00062 int
00063 __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
00064 {
00065   /* We can ignore the `pshared' parameter.  Since we are busy-waiting
00066      all processes which can access the memory location `lock' points
00067      to can use the spinlock.  */
00068   return *lock = 0;
00069 }
00070 weak_alias (__pthread_spin_init, pthread_spin_init)
00071 
00072 
00073 int
00074 __pthread_spin_destroy (pthread_spinlock_t *lock)
00075 {
00076   /* Nothing to do.  */
00077   return 0;
00078 }
00079 weak_alias (__pthread_spin_destroy, pthread_spin_destroy)