usermode/library/atomic_ops/x86.h

00001 /*
00002  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
00003  * Copyright (c) 1996-1999 by Silicon Graphics.  All rights reserved.
00004  * Copyright (c) 1999-2003 by Hewlett-Packard Company. All rights reserved.
00005  *
00006  *
00007  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
00008  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
00009  *
00010  * Permission is hereby granted to use or copy this program
00011  * for any purpose,  provided the above notices are retained on all copies.
00012  * Permission to modify the code and to distribute modified code is granted,
00013  * provided the above notices are retained, and a notice that the code was
00014  * modified is included with the above copyright notice.
00015  *
00016  * Some of the machine specific code was borrowed from our GC distribution.
00017  */
00018 
00019 /* The following really assume we have a 486 or better.  Unfortunately  */
00020 /* gcc doesn't define a suitable feature test macro based on command    */
00021 /* line options.                                                        */
00022 /* We should perhaps test dynamically.                                  */
00023 
00024 #include "./aligned_atomic_load_store.h"
00025 
00026 /* Real X86 implementations, except for some old WinChips, appear       */
00027 /* to enforce ordering between memory operations, EXCEPT that a later   */
00028 /* read can pass earlier writes, presumably due to the visible          */
00029 /* presence of store buffers.                                           */
00030 /* We ignore both the WinChips, and the fact that the official specs    */
00031 /* seem to be much weaker (and arguably too weak to be usable).         */
00032 
00033 #include "./ordered_except_wr.h"
00034 
00035 #include "./test_and_set_t_is_char.h"
00036 
00037 #include "./standard_ao_double_t.h"
00038 
00039 #if defined(AO_USE_PENTIUM4_INSTRS)
00040 AO_INLINE void
00041 AO_nop_full(void)
00042 {
00043   __asm__ __volatile__("mfence" : : : "memory");
00044 }
00045 
00046 #define AO_HAVE_nop_full
00047 
00048 #else
00049 
00050 /* We could use the cpuid instruction.  But that seems to be slower     */
00051 /* than the default implementation based on test_and_set_full.  Thus    */
00052 /* we omit that bit of misinformation here.                             */
00053 
00054 #endif
00055 
00056 /* As far as we can tell, the lfence and sfence instructions are not    */
00057 /* currently needed or useful for cached memory accesses.               */
00058 
00059 /* Really only works for 486 and later */
00060 AO_INLINE AO_t
00061 AO_fetch_and_add_full (volatile AO_t *p, AO_t incr)
00062 {
00063   AO_t result;
00064 
00065   __asm__ __volatile__ ("lock; xaddl %0, %1" :
00066                         "=r" (result), "=m" (*p) : "0" (incr), "m" (*p)
00067                         : "memory");
00068   return result;
00069 }
00070 
00071 #define AO_HAVE_fetch_and_add_full
00072 
00073 AO_INLINE unsigned char
00074 AO_char_fetch_and_add_full (volatile unsigned char *p, unsigned char incr)
00075 {
00076   unsigned char result;
00077 
00078   __asm__ __volatile__ ("lock; xaddb %0, %1" :
00079                         "=q" (result), "=m" (*p) : "0" (incr), "m" (*p)
00080                         : "memory");
00081   return result;
00082 }
00083 
00084 #define AO_HAVE_char_fetch_and_add_full
00085 
00086 AO_INLINE unsigned short
00087 AO_short_fetch_and_add_full (volatile unsigned short *p, unsigned short incr)
00088 {
00089   unsigned short result;
00090 
00091   __asm__ __volatile__ ("lock; xaddw %0, %1" :
00092                         "=r" (result), "=m" (*p) : "0" (incr), "m" (*p)
00093                         : "memory");
00094   return result;
00095 }
00096 
00097 #define AO_HAVE_short_fetch_and_add_full
00098 
00099 /* Really only works for 486 and later */
00100 AO_INLINE void
00101 AO_or_full (volatile AO_t *p, AO_t incr)
00102 {
00103   __asm__ __volatile__ ("lock; orl %1, %0" :
00104                         "=m" (*p) : "r" (incr), "m" (*p) : "memory");
00105 }
00106 
00107 #define AO_HAVE_or_full
00108 
00109 AO_INLINE AO_TS_VAL_t
00110 AO_test_and_set_full(volatile AO_TS_t *addr)
00111 {
00112   unsigned char oldval;
00113   /* Note: the "xchg" instruction does not need a "lock" prefix */
00114   __asm__ __volatile__("xchgb %0, %1"
00115                 : "=q"(oldval), "=m"(*addr)
00116                 : "0"(0xff), "m"(*addr) : "memory");
00117   return (AO_TS_VAL_t)oldval;
00118 }
00119 
00120 #define AO_HAVE_test_and_set_full
00121 
00122 /* Returns nonzero if the comparison succeeded. */
00123 AO_INLINE int
00124 AO_compare_and_swap_full(volatile AO_t *addr, AO_t old, AO_t new_val)
00125 {
00126 # ifdef AO_USE_SYNC_CAS_BUILTIN
00127     return (int)__sync_bool_compare_and_swap(addr, old, new_val);
00128 # else
00129     char result;
00130     __asm__ __volatile__("lock; cmpxchgl %3, %0; setz %1"
00131                          : "=m" (*addr), "=a" (result)
00132                          : "m" (*addr), "r" (new_val), "a" (old) : "memory");
00133     return (int)result;
00134 # endif
00135 }
00136 
00137 #define AO_HAVE_compare_and_swap_full
00138 
00139 /* Returns nonzero if the comparison succeeded. */
00140 /* Really requires at least a Pentium.          */
00141 AO_INLINE int
00142 AO_compare_double_and_swap_double_full(volatile AO_double_t *addr,
00143                                        AO_t old_val1, AO_t old_val2,
00144                                        AO_t new_val1, AO_t new_val2)
00145 {
00146   char result;
00147 #if __PIC__
00148   /* If PIC is turned on, we can't use %ebx as it is reserved for the
00149      GOT pointer.  We can save and restore %ebx because GCC won't be
00150      using it for anything else (such as any of the m operands) */
00151   __asm__ __volatile__("pushl %%ebx;"   /* save ebx used for PIC GOT ptr */
00152                        "movl %6,%%ebx;" /* move new_val2 to %ebx */
00153                        "lock; cmpxchg8b %0; setz %1;"
00154                        "pop %%ebx;"     /* restore %ebx */
00155                        : "=m"(*addr), "=a"(result)
00156                        : "m"(*addr), "d" (old_val2), "a" (old_val1),
00157                          "c" (new_val2), "m" (new_val1) : "memory");
00158 #else
00159   /* We can't just do the same thing in non-PIC mode, because GCC
00160    * might be using %ebx as the memory operand.  We could have ifdef'd
00161    * in a clobber, but there's no point doing the push/pop if we don't
00162    * have to. */
00163   __asm__ __volatile__("lock; cmpxchg8b %0; setz %1;"
00164                        : "=m"(*addr), "=a"(result)
00165                        : "m"(*addr), "d" (old_val2), "a" (old_val1),
00166                          "c" (new_val2), "b" (new_val1) : "memory");
00167 #endif
00168   return (int) result;
00169 }
00170 
00171 #define AO_HAVE_compare_double_and_swap_double_full
00172 
00173 #include "./ao_t_is_int.h"

Generated on Sat Apr 23 11:43:34 2011 for Mnemosyne by  doxygen 1.4.7