my_atomic.h 3.74 KB
Newer Older
unknown's avatar
unknown committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (C) 2006 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

unknown's avatar
unknown committed
17
#ifndef my_atomic_rwlock_init
unknown's avatar
unknown committed
18

unknown's avatar
unknown committed
19
#define intptr         void *
unknown's avatar
unknown committed
20 21 22 23 24

#ifndef MY_ATOMIC_MODE_RWLOCKS
#include "atomic/nolock.h"
#endif

25
#ifndef make_atomic_cas_body
unknown's avatar
unknown committed
26 27 28
#include "atomic/rwlock.h"
#endif

29 30 31 32 33 34 35
#ifndef make_atomic_add_body
#define make_atomic_add_body(S)					\
  int ## S tmp=*a;                                              \
  while (!my_atomic_cas ## S(a, &tmp, tmp+v));                  \
  v=tmp;
#endif

unknown's avatar
unknown committed
36 37 38
#ifdef HAVE_INLINE

#define make_atomic_add(S)					\
unknown's avatar
unknown committed
39
STATIC_INLINE int ## S my_atomic_add ## S(			\
unknown's avatar
unknown committed
40 41 42 43 44 45 46
                        int ## S volatile *a, int ## S v)	\
{								\
  make_atomic_add_body(S);					\
  return v;							\
}

#define make_atomic_swap(S)					\
unknown's avatar
unknown committed
47
STATIC_INLINE int ## S my_atomic_swap ## S(			\
unknown's avatar
unknown committed
48 49 50 51 52 53 54
                         int ## S volatile *a, int ## S v)	\
{								\
  make_atomic_swap_body(S);					\
  return v;							\
}

#define make_atomic_cas(S)					\
unknown's avatar
unknown committed
55
STATIC_INLINE int my_atomic_cas ## S(int ## S volatile *a,	\
unknown's avatar
unknown committed
56 57 58 59 60 61 62 63
                            int ## S *cmp, int ## S set)	\
{								\
  int8 ret;							\
  make_atomic_cas_body(S);					\
  return ret;							\
}

#define make_atomic_load(S)					\
unknown's avatar
unknown committed
64
STATIC_INLINE int ## S my_atomic_load ## S(int ## S volatile *a) \
unknown's avatar
unknown committed
65 66 67 68 69 70 71
{								\
  int ## S ret;						\
  make_atomic_load_body(S);					\
  return ret;							\
}

#define make_atomic_store(S)					\
unknown's avatar
unknown committed
72
STATIC_INLINE void my_atomic_store ## S(			\
unknown's avatar
unknown committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
                     int ## S volatile *a, int ## S v)	\
{								\
  make_atomic_store_body(S);					\
}

#else /* no inline functions */

#define make_atomic_add(S)					\
extern int ## S my_atomic_add ## S(int ## S volatile *a, int ## S v);

#define make_atomic_swap(S)					\
extern int ## S my_atomic_swap ## S(int ## S volatile *a, int ## S v);

#define make_atomic_cas(S)					\
extern int my_atomic_cas ## S(int ## S volatile *a, int ## S *cmp, int ## S set);

#define make_atomic_load(S)					\
extern int ## S my_atomic_load ## S(int ## S volatile *a);

#define make_atomic_store(S)					\
extern void my_atomic_store ## S(int ## S volatile *a, int ## S v);

#endif

make_atomic_cas( 8)
make_atomic_cas(16)
make_atomic_cas(32)
make_atomic_cas(ptr)

102 103 104 105
make_atomic_add( 8)
make_atomic_add(16)
make_atomic_add(32)

unknown's avatar
unknown committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
make_atomic_load( 8)
make_atomic_load(16)
make_atomic_load(32)
make_atomic_load(ptr)

make_atomic_store( 8)
make_atomic_store(16)
make_atomic_store(32)
make_atomic_store(ptr)

make_atomic_swap( 8)
make_atomic_swap(16)
make_atomic_swap(32)
make_atomic_swap(ptr)

unknown's avatar
unknown committed
121 122 123 124 125
#ifdef _atomic_h_cleanup_
#include _atomic_h_cleanup_
#undef _atomic_h_cleanup_
#endif

unknown's avatar
unknown committed
126 127 128 129 130
#undef make_atomic_add
#undef make_atomic_cas
#undef make_atomic_load
#undef make_atomic_store
#undef make_atomic_swap
131 132 133 134 135 136
#undef make_atomic_add_body
#undef make_atomic_cas_body
#undef make_atomic_load_body
#undef make_atomic_store_body
#undef make_atomic_swap_body
#undef intptr
unknown's avatar
unknown committed
137

unknown's avatar
unknown committed
138 139 140 141
#ifndef LF_BACKOFF
#define LF_BACKOFF (1)
#endif

142 143 144 145 146 147 148
#if SIZEOF_CHARP == SIZEOF_INT
typedef int intptr;
#elif SIZEOF_CHARP == SIZEOF_LONG
typedef long intptr;
#else
#error
#endif
149

unknown's avatar
unknown committed
150 151 152 153 154 155
#define MY_ATOMIC_OK       0
#define MY_ATOMIC_NOT_1CPU 1
extern int my_atomic_initialize();

#endif