[PATCH] atomic operations for ppc

Ingvar Hagelund ingvar at redpill-linpro.com
Tue Mar 13 14:52:30 PDT 2012


This patch should, if it is correct, add the functions atomic_add_uint32 and atomic_sub_uint32 that are missing on some platforms, at least RHEL5/ppc[64]. With this patch, jemalloc may be updated to 2.2.x also in the EPEL5 repositories.

The code was borrowed from the Boost library, see http://www.boost.org/doc/libs/1_49_0/boost/interprocess/detail/atomic.hpp . The Boost library is under the Boost license, which is quite BSD-like, but the license should probably be checked out for compatibility. See http://www.boost.org/LICENSE_1_0.txt 

Since I actually know nothing about ppc assembler myself, and have never used the Boost library, I should thank Federico G. Schwindt for valuable input on the patch.

Jason, could you produce a test case that confirms that the patch works?

Best regards,
Ingvar Hagelund





--- ../jemalloc-2.2.5.orig/include/jemalloc/internal/atomic.h   2011-11-15 02:18:06.000000000 +0100
+++ include/jemalloc/internal/atomic.h  2012-03-09 08:44:45.930652898 +0100
@@ -160,6 +160,33 @@

        return (x);
 }
+#elif (defined(__ppc__) || defined(__PPC__))
+// Code shamelessly stolen from the boost library
+// Please do a license check before distributing
+JEMALLOC_INLINE uint32_t
+atomic_add_uint32(uint32_t *p, uint32_t x)
+{
+       uint32_t prev, temp;
+
+       asm volatile ("0:\n\t"                 // retry local label     
+               "lwarx  %0,0,%2\n\t"       // load prev and reserve 
+               "add    %1,%0,%3\n\t"      // temp = prev + x 
+               "stwcx. %1,0,%2\n\t"       // conditionally store   
+               "bne-   0b"                // start over if we lost
+                                          // the reservation
+               //XXX find a cleaner way to define the temp         
+               //it's not an output
+               : "=&r" (prev), "=&r" (temp)        // output, temp 
+               : "b" (p), "r" (x)                  // inputs       
+               : "memory", "cc");                  // clobbered    
+       return temp;
+}
+
+JEMALLOC_INLINE uint32_t
+atomic_sub_uint32(uint32_t *p, uint32_t x)
+{
+       return atomic_add_uint32(p, -x);
+}
 #else
 #  error "Missing implementation for 32-bit atomic operations"
 #endif



More information about the jemalloc-discuss mailing list