yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* Machine-dependent pthreads configuration and inline functions. |
| 2 | powerpc version. |
| 3 | Copyright (C) 1996, 1997, 1998, 2000, 2001, 2002 Free Software Foundation, Inc. |
| 4 | This file is part of the GNU C Library. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public License as |
| 8 | published by the Free Software Foundation; either version 2.1 of the |
| 9 | License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; see the file COPYING.LIB. If |
| 18 | not, write to the Free Software Foundation, Inc., |
| 19 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | /* These routines are from Appendix G of the 'PowerPC 601 RISC Microprocessor |
| 22 | User's Manual', by IBM and Motorola. */ |
| 23 | |
| 24 | #ifndef _PT_MACHINE_H |
| 25 | #define _PT_MACHINE_H 1 |
| 26 | |
| 27 | #include <features.h> |
| 28 | |
| 29 | #ifndef PT_EI |
| 30 | # define PT_EI __extern_always_inline |
| 31 | #endif |
| 32 | |
| 33 | /* For multiprocessor systems, we want to ensure all memory accesses |
| 34 | are completed before we reset a lock. On other systems, we still |
| 35 | need to make sure that the compiler has flushed everything to memory. */ |
| 36 | #define MEMORY_BARRIER() __asm__ __volatile__ ("sync" : : : "memory") |
| 37 | |
| 38 | /* Get some notion of the current stack. Need not be exactly the top |
| 39 | of the stack, just something somewhere in the current frame. */ |
| 40 | #define CURRENT_STACK_FRAME stack_pointer |
| 41 | register char * stack_pointer __asm__ ("r1"); |
| 42 | |
| 43 | /* Register r2 (tp) is reserved by the ABI as "thread pointer". */ |
| 44 | struct _pthread_descr_struct; |
| 45 | register struct _pthread_descr_struct *__thread_self __asm__("r2"); |
| 46 | |
| 47 | /* Return the thread descriptor for the current thread. */ |
| 48 | #define THREAD_SELF __thread_self |
| 49 | |
| 50 | /* Initialize the thread-unique value. */ |
| 51 | #define INIT_THREAD_SELF(descr, nr) (__thread_self = (descr)) |
| 52 | |
| 53 | /* Compare-and-swap for semaphores. */ |
| 54 | /* note that test-and-set(x) is the same as !compare-and-swap(x, 0, 1) */ |
| 55 | |
| 56 | #define HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS |
| 57 | #define IMPLEMENT_TAS_WITH_CAS |
| 58 | |
| 59 | PT_EI int |
| 60 | __compare_and_swap (long int *p, long int oldval, long int newval) |
| 61 | { |
| 62 | int ret; |
| 63 | |
| 64 | __asm__ __volatile__ ( |
| 65 | "0: lwarx %0,0,%1 ;" |
| 66 | " xor. %0,%3,%0;" |
| 67 | " bne 1f;" |
| 68 | " stwcx. %2,0,%1;" |
| 69 | " bne- 0b;" |
| 70 | "1: " |
| 71 | : "=&r"(ret) |
| 72 | : "r"(p), "r"(newval), "r"(oldval) |
| 73 | : "cr0", "memory"); |
| 74 | /* This version of __compare_and_swap is to be used when acquiring |
| 75 | a lock, so we don't need to worry about whether other memory |
| 76 | operations have completed, but we do need to be sure that any loads |
| 77 | after this point really occur after we have acquired the lock. */ |
| 78 | __asm__ __volatile__ ("isync" : : : "memory"); |
| 79 | return ret == 0; |
| 80 | } |
| 81 | |
| 82 | PT_EI int |
| 83 | __compare_and_swap_with_release_semantics (long int *p, |
| 84 | long int oldval, long int newval) |
| 85 | { |
| 86 | int ret; |
| 87 | |
| 88 | MEMORY_BARRIER (); |
| 89 | __asm__ __volatile__ ( |
| 90 | "0: lwarx %0,0,%1 ;" |
| 91 | " xor. %0,%3,%0;" |
| 92 | " bne 1f;" |
| 93 | " stwcx. %2,0,%1;" |
| 94 | " bne- 0b;" |
| 95 | "1: " |
| 96 | : "=&r"(ret) |
| 97 | : "r"(p), "r"(newval), "r"(oldval) |
| 98 | : "cr0", "memory"); |
| 99 | return ret == 0; |
| 100 | } |
| 101 | |
| 102 | #endif /* pt-machine.h */ |