Back to index

glibc  2.9
pspinlock.c
Go to the documentation of this file.
00001 /* POSIX spinlock implementation.  hppa version.
00002    Copyright (C) 2000 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
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 "internals.h"
00023 
00024 int
00025 __pthread_spin_lock (pthread_spinlock_t *lock)
00026 {
00027   volatile unsigned int *addr = __ldcw_align (lock);
00028 
00029   while (__ldcw (addr) == 0)
00030     while (*addr == 0) ;
00031 
00032   return 0;
00033 }
00034 weak_alias (__pthread_spin_lock, pthread_spin_lock)
00035 
00036 
00037 int
00038 __pthread_spin_trylock (pthread_spinlock_t *lock)
00039 {
00040   volatile unsigned int *a = __ldcw_align (lock);
00041 
00042   return __ldcw (a) ? 0 : EBUSY;
00043 }
00044 weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
00045 
00046 
00047 int
00048 __pthread_spin_unlock (pthread_spinlock_t *lock)
00049 {
00050   volatile unsigned int *a = __ldcw_align (lock);
00051   int tmp = 1;
00052   /* This should be a memory barrier to newer compilers */
00053   __asm__ __volatile__ ("stw,ma %1,0(%0)"
00054                         : : "r" (a), "r" (tmp) : "memory");           
00055   return 0;
00056 }
00057 weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
00058 
00059 
00060 int
00061 __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
00062 {
00063   /* We can ignore the `pshared' parameter.  Since we are busy-waiting
00064      all processes which can access the memory location `lock' points
00065      to can use the spinlock.  */
00066   volatile unsigned int *a = __ldcw_align (lock);
00067   int tmp = 1;
00068   /* This should be a memory barrier to newer compilers */
00069   __asm__ __volatile__ ("stw,ma %1,0(%0)"
00070                         : : "r" (a), "r" (tmp) : "memory");           
00071   return 0;
00072 }
00073 weak_alias (__pthread_spin_init, pthread_spin_init)
00074 
00075 
00076 int
00077 __pthread_spin_destroy (pthread_spinlock_t *lock)
00078 {
00079   /* Nothing to do.  */
00080   return 0;
00081 }
00082 weak_alias (__pthread_spin_destroy, pthread_spin_destroy)