Drizzled Public API Documentation

gcc_traits.h

00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #pragma once
00021 
00022 namespace drizzled {
00023 namespace internal {
00024 
00025 template<typename T, typename D>
00026 class gcc_traits
00027 {
00028 
00029 public:
00030   typedef T value_type;
00031 
00032   gcc_traits() {}
00033 
00034   inline value_type add_and_fetch(volatile value_type *value, D addend )
00035   {
00036     return __sync_add_and_fetch(value, addend);
00037   }
00038 
00039   inline value_type fetch_and_add(volatile value_type *value, D addend )
00040   {
00041     return __sync_fetch_and_add(value, addend);
00042   }
00043 
00044   inline value_type fetch_and_increment(volatile value_type *value)
00045   {
00046     return __sync_fetch_and_add(value, 1);
00047   }
00048 
00049   inline value_type fetch_and_decrement(volatile value_type *value)
00050   {
00051     return __sync_fetch_and_sub(value, 1);
00052   }
00053 
00054   inline value_type fetch_and_store(volatile value_type *value,
00055                                     value_type new_value)
00056   {
00057     return __sync_lock_test_and_set(value, new_value);
00058   }
00059 
00060   inline bool compare_and_swap(volatile value_type *value,
00061                                      value_type new_value,
00062                                      value_type comparand )
00063   {
00064     return __sync_bool_compare_and_swap(value, comparand, new_value);
00065   }
00066 
00067   inline value_type fetch(const volatile value_type *value) const volatile
00068   {
00069     /* 
00070      * This is necessary to ensure memory barriers are respected when
00071      * simply returning the value pointed at.  However, this does not
00072      * compile on ICC.
00073      *
00074      * @todo
00075      *
00076      * Look at how to rewrite the below to something that ICC feels is
00077      * OK and yet respects memory barriers.
00078      */
00079     return __sync_fetch_and_add(const_cast<value_type *>(value), 0);
00080   }
00081 
00082   inline value_type store_with_release(volatile value_type *value,
00083                                        value_type new_value)
00084   {
00085     *value= new_value;
00086     return *value;
00087   }
00088 
00089 }; /* gcc_traits */
00090 
00091 
00092 } /* namespace internal */
00093 } /* namespace drizzled */
00094