summaryrefslogtreecommitdiff
path: root/include/atomic/x86-msvc.h
blob: 8f3e55aaed765c7f35dfb06d7ac16d87614cc3f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
/* 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 */

/*
  XXX 64-bit atomic operations can be implemented using
  cmpxchg8b, if necessary
*/

// Would it be better to use intrinsics ?
// (InterlockedCompareExchange, InterlockedCompareExchange16
// InterlockedExchangeAdd, InterlockedExchange)

#ifndef _atomic_h_cleanup_
#define _atomic_h_cleanup_ "atomic/x86-msvc.h"

#define MY_ATOMIC_MODE "msvc-x86" LOCK_prefix

#define make_atomic_add_body(S)				\
  _asm {						\
    _asm mov   reg_ ## S, v				\
    _asm LOCK_prefix  xadd *a, reg_ ## S		\
    _asm movzx v, reg_ ## S				\
  }
#define make_atomic_cas_body(S)				\
  _asm {						\
    _asm mov    areg_ ## S, *cmp			\
    _asm mov    reg2_ ## S, set				\
    _asm LOCK_prefix cmpxchg *a, reg2_ ## S		\
    _asm mov    *cmp, areg_ ## S			\
    _asm setz   al					\
    _asm movzx  ret, al					\
  }
#define make_atomic_swap_body(S)			\
  _asm {						\
    _asm mov    reg_ ## S, v				\
    _asm xchg   *a, reg_ ## S				\
    _asm mov    v, reg_ ## S				\
  }

#ifdef MY_ATOMIC_MODE_DUMMY
#define make_atomic_load_body(S)        ret=*a
#define make_atomic_store_body(S)       *a=v
#else
/*
  Actually 32-bit reads/writes are always atomic on x86
  But we add LOCK_prefix here anyway to force memory barriers
*/
#define make_atomic_load_body(S)			\
  _asm {						\
    _asm mov    areg_ ## S, 0				\
    _asm mov    reg2_ ## S, areg_ ## S			\
    _asm LOCK_prefix cmpxchg *a, reg2_ ## S		\
    _asm mov    ret, areg_ ## S				\
  }
#define make_atomic_store_body(S)			\
  _asm {						\
    _asm mov    reg_ ## S, v				\
    _asm xchg   *a, reg_ ## S				\
  }
#endif

#define reg_8           al
#define reg_16          ax
#define reg_32          eax
#define areg_8          al
#define areg_16         ax
#define areg_32         eax
#define reg2_8          bl
#define reg2_16         bx
#define reg2_32         ebx

#else /* cleanup */

#undef reg_8
#undef reg_16
#undef reg_32
#undef areg_8
#undef areg_16
#undef areg_32
#undef reg2_8
#undef reg2_16
#undef reg2_32
#endif