Back to index

glibc  2.9
pt-machine.h
Go to the documentation of this file.
00001 /* Machine-dependent pthreads configuration and inline functions.
00002    powerpc version.
00003    Copyright (C) 1996, 1997, 1998, 2000, 2001, 2002, 2003
00004    Free Software Foundation, Inc.
00005    This file is part of the GNU C Library.
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public License as
00009    published by the Free Software Foundation; either version 2.1 of the
00010    License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with the GNU C Library; see the file COPYING.LIB.  If
00019    not, write to the Free Software Foundation, Inc.,
00020    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00021 
00022 /* These routines are from Appendix G of the 'PowerPC 601 RISC Microprocessor
00023    User's Manual', by IBM and Motorola.  */
00024 
00025 #ifndef _PT_MACHINE_H
00026 #define _PT_MACHINE_H   1
00027 
00028 #ifndef PT_EI
00029 # define PT_EI extern inline __attribute__ ((always_inline))
00030 #endif
00031 
00032 extern long int testandset (int *spinlock);
00033 extern int __compare_and_swap (long int *p, long int oldval, long int newval);
00034 
00035 /* For multiprocessor systems, we want to ensure all memory accesses
00036    are completed before we reset a lock.  On other systems, we still
00037    need to make sure that the compiler has flushed everything to memory.  */
00038 #define MEMORY_BARRIER() __asm__ __volatile__ ("sync" : : : "memory")
00039 
00040 /* We want the OS to assign stack addresses.  */
00041 #define FLOATING_STACKS 1
00042 
00043 /* Maximum size of the stack if the rlimit is unlimited.  */
00044 #define ARCH_STACK_MAX_SIZE     8*1024*1024
00045 
00046 /* Get some notion of the current stack.  Need not be exactly the top
00047    of the stack, just something somewhere in the current frame.  */
00048 #define CURRENT_STACK_FRAME  stack_pointer
00049 register char * stack_pointer __asm__ ("r1");
00050 
00051 /* Register r2 (tp) is reserved by the ABI as "thread pointer". */
00052 struct _pthread_descr_struct;
00053 register struct _pthread_descr_struct *__thread_self __asm__("r2");
00054 
00055 /* Return the thread descriptor for the current thread.  */
00056 #define THREAD_SELF  __thread_self
00057 
00058 /* Initialize the thread-unique value.  */
00059 #define INIT_THREAD_SELF(descr, nr)  (__thread_self = (descr))
00060 
00061 /* Access to data in the thread descriptor is easy.  */
00062 #define THREAD_GETMEM(descr, member) \
00063   ((void) (descr), THREAD_SELF->member)
00064 #define THREAD_GETMEM_NC(descr, member) \
00065   ((void) (descr), THREAD_SELF->member)
00066 #define THREAD_SETMEM(descr, member, value) \
00067   ((void) (descr), THREAD_SELF->member = (value))
00068 #define THREAD_SETMEM_NC(descr, member, value) \
00069   ((void) (descr), THREAD_SELF->member = (value))
00070 
00071 /* Compare-and-swap for semaphores. */
00072 /* note that test-and-set(x) is the same as !compare-and-swap(x, 0, 1) */
00073 
00074 #define HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
00075 #define IMPLEMENT_TAS_WITH_CAS
00076 
00077 PT_EI int
00078 __compare_and_swap (long int *p, long int oldval, long int newval)
00079 {
00080   int ret;
00081 
00082   __asm__ __volatile__ (
00083           "0:    lwarx %0,0,%1 ;"
00084           "      xor. %0,%3,%0;"
00085           "      bne 1f;"
00086           "      stwcx. %2,0,%1;"
00087           "      bne- 0b;"
00088           "1:    "
00089        : "=&r"(ret)
00090        : "r"(p), "r"(newval), "r"(oldval)
00091        : "cr0", "memory");
00092   /* This version of __compare_and_swap is to be used when acquiring
00093      a lock, so we don't need to worry about whether other memory
00094      operations have completed, but we do need to be sure that any loads
00095      after this point really occur after we have acquired the lock.  */
00096   __asm__ __volatile__ ("isync" : : : "memory");
00097   return ret == 0;
00098 }
00099 
00100 PT_EI int
00101 __compare_and_swap_with_release_semantics (long int *p,
00102                                       long int oldval, long int newval)
00103 {
00104   int ret;
00105 
00106   MEMORY_BARRIER ();
00107   __asm__ __volatile__ (
00108           "0:    lwarx %0,0,%1 ;"
00109           "      xor. %0,%3,%0;"
00110           "      bne 1f;"
00111           "      stwcx. %2,0,%1;"
00112           "      bne- 0b;"
00113           "1:    "
00114        : "=&r"(ret)
00115        : "r"(p), "r"(newval), "r"(oldval)
00116        : "cr0", "memory");
00117   return ret == 0;
00118 }
00119 
00120 #endif /* pt-machine.h */