1
/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Stewart Smith
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
#include <gtest/gtest.h>
25
# if defined(__SUNPRO_CC)
26
# include <drizzled/atomic/sun_studio.h>
29
# if !defined(__ICC) && (defined(HAVE_GCC_ATOMIC_BUILTINS) || defined(__SUNPRO_CC))
30
# include <drizzled/atomic/gcc_traits.h>
31
# define ATOMIC_TRAITS internal::gcc_traits
32
# else /* use pthread impl */
33
# define ATOMIC_TRAITS internal::pthread_traits
37
#include <drizzled/atomic/pthread_traits.h>
39
#include <drizzled/atomics.h>
41
using namespace drizzled;
44
struct atomic_pthread {
47
# define __DRIZZLE_DECL_ATOMIC_PTHREAD(T) \
48
template<> struct atomic_pthread<T> \
49
: internal::atomic_impl<T,T, internal::pthread_traits<T,T> > { \
51
: internal::atomic_impl<T,T, internal::pthread_traits<T,T> >() {} \
52
T operator=( T rhs ) { return store_with_release(rhs); } \
55
__DRIZZLE_DECL_ATOMIC_PTHREAD(unsigned int)
57
TEST(pthread_atomic_operations, fetch_and_store)
59
atomic_pthread<uint32_t> u235;
61
EXPECT_EQ(0, u235.fetch_and_store(1));
63
u235.fetch_and_store(15);
65
EXPECT_EQ(15, u235.fetch_and_store(100));
69
TEST(pthread_atomic_operations, fetch_and_increment)
71
atomic_pthread<uint32_t> u235;
73
EXPECT_EQ(0, u235.fetch_and_increment());
77
TEST(pthread_atomic_operations, fetch_and_add)
79
atomic_pthread<uint32_t> u235;
81
EXPECT_EQ(0, u235.fetch_and_add(2));
85
TEST(pthread_atomic_operations, add_and_fetch)
87
atomic_pthread<uint32_t> u235;
89
EXPECT_EQ(10, u235.add_and_fetch(10));
93
TEST(pthread_atomic_operations, fetch_and_decrement)
95
atomic_pthread<uint32_t> u235;
97
u235.fetch_and_store(15);
99
EXPECT_EQ(15, u235.fetch_and_decrement());
103
TEST(pthread_atomic_operations, compare_and_swap)
105
atomic_pthread<uint32_t> u235;
107
u235.fetch_and_store(100);
109
EXPECT_EQ(false, u235.compare_and_swap(42, 200));
110
EXPECT_EQ(true, u235.compare_and_swap(200, 100));
111
EXPECT_EQ(200, u235);
114
TEST(pthread_atomic_operations, increment)
116
atomic_pthread<uint32_t> u235;
117
u235.fetch_and_store(200);
118
EXPECT_EQ(201, u235.increment());
121
TEST(pthread_atomic_operations, decrement)
123
atomic_pthread<uint32_t> u235;
124
u235.fetch_and_store(200);
126
EXPECT_EQ(199, u235.decrement());
129
// these don't build for whatever reason
130
// EXPECT_EQ(242, u235+=42);
131
// EXPECT_EQ(200, u235-=42);