Phil Endecott wrote:
David Daney wrote:
Phil Endecott wrote:
David Daney wrote:
Phil Endecott wrote:
This raises another issue, which is whether there is any way for
code to determine which of the atomic builtins are provided, i.e.
I'd like to write:
#if HAS__sync_lock_test_and_set
.. locking code using __sync_lock_test_and_set
#elseif HAS__sync_something_else
.. locking code using builtins that expand to LDREX and STREX
#else
#warn "no atomic ops, falling back to pthread mutex"
...
#endif
The proper symbol to test would be something like:
__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 or
__GCC_HAVE_SYNC_LOCK_TEST_AND_SET_4 they are automatically set by
GCC when the builtin functions are available.
Really? I don't see anything like that in the output of
gcc -dM -E - < /dev/null|grep -i sync
or
grep -R HAVE_SYNC /usr/include/
on my x86 system, which does have a selection of atomic builtins.
$ /home/daney/gccsvn/mips64-trunk/gcc/xgcc
-B/home/daney/gccsvn/mips64-trunk/gcc/ --version
xgcc (GCC) 4.3.0 20070911 (experimental) [trunk revision 128398]
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There
is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
$ /home/daney/gccsvn/mips64-trunk/gcc/xgcc
-B/home/daney/gccsvn/mips64-trunk/gcc/ -E -dM st.c | grep SYNC
#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 1
#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 1
Mine is
$ gcc --version
gcc (GCC) 4.1.2 20060920 (prerelease) (Debian 4.1.1-14)
Does your st.c do anything?
Not really, but I attach it for your enjoyment.
Does invoking xgcc rather than gcc change anything?
I didn't have that particular GCC build installed, so I issued the
commands necessary to run it from the build directory.
David Daney
void foo();
void a()
{
__sync_synchronize();
}
int b(unsigned long *p, unsigned long o, unsigned long n)
{
return __sync_bool_compare_and_swap(p, o, n);
}
unsigned long v;
int c()
{
return __sync_bool_compare_and_swap(&v, 1UL, 2UL);
}
int d(unsigned long *p)
{
return __sync_bool_compare_and_swap(p, 1UL, v);
}
unsigned long e(unsigned long n)
{
return __sync_add_and_fetch (&v, n);
}
unsigned long f(unsigned long n)
{
return __sync_fetch_and_add (&v, n);
}
unsigned long g()
{
return __sync_fetch_and_add (&v, -7);
}
unsigned long h()
{
return __sync_fetch_and_add (&v, -30000);
}
unsigned long i()
{
return __sync_fetch_and_add (&v, -100000);
}
unsigned long g0()
{
return __sync_fetch_and_add (&v, 7);
}
unsigned long h0()
{
return __sync_fetch_and_add (&v, 30000);
}
unsigned long h00()
{
return __sync_fetch_and_sub (&v, 30000);
}
unsigned long h000()
{
return __sync_fetch_and_sub (&v, 0x7fff);
}
unsigned long h0000()
{
return __sync_fetch_and_sub (&v, 0x8000);
}
unsigned long h00000()
{
return __sync_fetch_and_sub (&v, 0x8001);
}
unsigned long i0()
{
return __sync_fetch_and_add (&v, 100000);
}
unsigned long f1(unsigned long n)
{
return __sync_fetch_and_sub (&v, n);
}
unsigned long f2(unsigned long n)
{
return __sync_fetch_and_or (&v, n);
}
unsigned long f3(unsigned long n)
{
return __sync_fetch_and_xor (&v, n);
}
unsigned long f4(unsigned long n)
{
return __sync_fetch_and_and (&v, n);
}
unsigned long f5(unsigned long n)
{
return __sync_fetch_and_nand (&v, n);
}
unsigned long f51(unsigned long n)
{
return __sync_nand_and_fetch (&v, n);
}
unsigned long j(unsigned long n)
{
__sync_fetch_and_add (&v, n);
return 5;
}