From patchwork Tue Feb 1 20:15:23 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: libgo patch committed: Add __sync_bool_compare_and_swap_4 Date: Tue, 01 Feb 2011 10:15:23 -0000 From: Ian Taylor X-Patchwork-Id: 81370 Message-Id: To: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com The libgo library uses __sync_bool_compare_and_swap_4 (i.e., uses __sync_bool_compare_and_swap on uint32 types). That function is not available for all gcc targets. This patch adds the function to the library for those cases. Ideally this would be handled by libgcc, but that does not currently happen. This function should be harmless if libgcc does get an implementation. This implementation is rather slow but it is better than having the build fail. Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu. Committed to mainline. Ian diff -r 948edbf8367c libgo/runtime/thread.c --- a/libgo/runtime/thread.c Mon Jan 31 15:32:52 2011 -0800 +++ b/libgo/runtime/thread.c Tue Feb 01 12:11:10 2011 -0800 @@ -3,6 +3,7 @@ // license that can be found in the LICENSE file. #include "runtime.h" +#include "go-assert.h" void runtime_initlock(Lock *l) @@ -75,3 +76,38 @@ { sem_destroy(&l->sem); } + +#ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 + +// For targets which don't have the required sync support. Really +// this should be provided by gcc itself. FIXME. + +static pthread_mutex_t sync_lock = PTHREAD_MUTEX_INITIALIZER; + +_Bool +__sync_bool_compare_and_swap_4(uint32*, uint32, uint32) + __attribute__((visibility("hidden"))); + +_Bool +__sync_bool_compare_and_swap_4(uint32* ptr, uint32 old, uint32 new) +{ + int i; + _Bool ret; + + i = pthread_mutex_lock(&sync_lock); + __go_assert(i == 0); + + if(*ptr != old) { + ret = 0; + } else { + *ptr = new; + ret = 1; + } + + i = pthread_mutex_unlock(&sync_lock); + __go_assert(i == 0); + + return ret; +} + +#endif