Back to index

glibc  2.9
pspinlock.c
Go to the documentation of this file.
00001 /* POSIX spinlock implementation.  SPARC v9 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 
00025 /* This implementation is similar to the one used in the Linux kernel.  */
00026 int
00027 __pthread_spin_lock (pthread_spinlock_t *lock)
00028 {
00029   asm volatile
00030     ("1: ldstub  [%0], %%g2\n"
00031      "   brnz,pn %%g2, 2f\n"
00032      "    membar #StoreLoad | #StoreStore\n"
00033      ".subsection 2\n"
00034      "2: ldub    [%0], %%g2\n"
00035      "   brnz,pt %%g2, 2b\n"
00036      "    membar #LoadLoad\n"
00037      "   b,a,pt  %%xcc, 1b\n"
00038      ".previous"
00039      : /* no outputs */
00040      : "r" (lock)
00041      : "g2", "memory");
00042   return 0;
00043 }
00044 weak_alias (__pthread_spin_lock, pthread_spin_lock)
00045 
00046 
00047 int
00048 __pthread_spin_trylock (pthread_spinlock_t *lock)
00049 {
00050   int result;
00051   asm volatile
00052     ("ldstub [%1], %0\n"
00053      "membar #StoreLoad | #StoreStore"
00054      : "=r" (result)
00055      : "r" (lock)
00056      : "memory");
00057   return result == 0 ? 0 : EBUSY;
00058 }
00059 weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
00060 
00061 
00062 int
00063 __pthread_spin_unlock (pthread_spinlock_t *lock)
00064 {
00065   asm volatile
00066     ("membar #StoreStore | #LoadStore\n"
00067      "stb    %%g0, [%0]"
00068      :
00069      : "r" (lock)
00070      : "memory");
00071   return 0;
00072 }
00073 weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
00074 
00075 
00076 int
00077 __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
00078 {
00079   /* We can ignore the `pshared' parameter.  Since we are busy-waiting
00080      all processes which can access the memory location `lock' points
00081      to can use the spinlock.  */
00082   *lock = 0;
00083   return 0;
00084 }
00085 weak_alias (__pthread_spin_init, pthread_spin_init)
00086 
00087 
00088 int
00089 __pthread_spin_destroy (pthread_spinlock_t *lock)
00090 {
00091   /* Nothing to do.  */
00092   return 0;
00093 }
00094 weak_alias (__pthread_spin_destroy, pthread_spin_destroy)