diff options
author | Eliot Horowitz <eliot@10gen.com> | 2009-12-02 12:21:19 -0500 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2009-12-02 12:21:19 -0500 |
commit | 83cac7fcb08a6f14ce060be9414348e756511fa6 (patch) | |
tree | 6f27c4b1da8e06b09a16d0f19cf6d922e98e42fb | |
parent | 66f7357c3bd4663f49cdb94ec91a36c469f2b3c3 (diff) | |
parent | f5c9a86b92343fd718129e6caceefc67e00a0ff5 (diff) | |
download | mongo-83cac7fcb08a6f14ce060be9414348e756511fa6.tar.gz |
Merge branch 'master' of git@github.com:mongodb/mongo
-rw-r--r-- | util/goodies.h | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/util/goodies.h b/util/goodies.h index 3d34715a430..21ade5f85b8 100644 --- a/util/goodies.h +++ b/util/goodies.h @@ -19,6 +19,10 @@ #pragma once +#if defined(__MSCV__) +# include <windows.h> +#endif + namespace mongo { #if !defined(_WIN32) && !defined(NOEXECINFO) @@ -117,9 +121,30 @@ namespace mongo { return x; } - // TODO: make atomic + // returns original value (like x++) WrappingInt atomicIncrement(){ - return x++; +#if defined(__MSCV__) + // InterlockedIncrement returns the new value + return InterlockedIncrement((volatile long*)&x)-1; //long is 32bits in Win64 +#elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) + // this is in GCC >= 4.1 + return __sync_fetch_and_add(&x, 1); +#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) + // from boost 1.39 interprocess/detail/atomic.hpp + int r; + int val = 1; + asm volatile + ( + "lock\n\t" + "xadd %1, %0": + "+m"( x ), "=r"( r ): // outputs (%0, %1) + "1"( val ): // inputs (%2 == %1) + "memory", "cc" // clobbers + ); + return r; +#else +# error "unsupported compiler or platform" +#endif } static int diff(unsigned a, unsigned b) { |