summaryrefslogtreecommitdiff
path: root/atomic/impl/simpleops-atomic-impl-msvc.h
blob: c584bbe7cebc3fcef455b888be8a816c624db690 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
/*
 * Copyright 2007 Chris Wilson
 * Copyright 2010-2011 Andrea Canciani
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Author(s): Andrea Canciani <ranma42@gmail.com>
 *            Chris Wilson <chris@chris-wilson.co.uk>
 */

#ifndef SIMPLEOPS_ATOMIC_IMPL_MSVC_H
#define SIMPLEOPS_ATOMIC_IMPL_MSVC_H

#ifndef SIMPLEOPS_ATOMIC_IMPL_H
#error Private header used directly
#endif

#if ! SIMPLEOPS_HAVE_ATOMIC_IMPL && \
    defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86))

#define SIMPLEOPS_HAVE_ATOMIC_IMPL 1
#define SIMPLEOPS_HAVE_ATOMIC_IMPL_WIN32 1

#include <intrin.h>

#pragma intrinsic (_InterlockedCompareExchange)
#pragma intrinsic (_InterlockedExchange)
#pragma intrinsic (_InterlockedExchangeAdd)
#pragma intrinsic (_ReadWriteBarrier)

#ifdef _M_X64
#pragma intrinsic (_InterlockedCompareExchangePointer)
#pragma intrinsic (_InterlockedExchangePointer)
#endif

static simpleops_always_inline void
_simpleops_atomic_barrier (void)
{
#if SIMPLEOPS_ATOMIC_IMPL_NEEDS_MEMORY_BARRIER
    _ReadWriteBarrier ();
#endif
}

/* Atomic integer functions */
typedef long simpleops_atomic_int_t;

static simpleops_always_inline simpleops_bool_t
simpleops_atomic_int_compare_exchange (simpleops_atomic_int_t volatile *x,
				       simpleops_atomic_int_t oldv,
				       simpleops_atomic_int_t newv)
{
    return _InterlockedCompareExchange (x, newv, oldv) == oldv;
}

static simpleops_always_inline simpleops_atomic_int_t
simpleops_atomic_int_exchange (simpleops_atomic_int_t volatile *x,
			       simpleops_atomic_int_t newv)
{
    return _InterlockedExchange (x, newv);
}

static simpleops_always_inline simpleops_atomic_int_t
simpleops_atomic_int_load (simpleops_atomic_int_t const volatile *x)
{
    _simpleops_atomic_barrier ();
    return *x;
}

static simpleops_always_inline void
simpleops_atomic_int_store (simpleops_atomic_int_t volatile *x,
			    simpleops_atomic_int_t newv)
{
    _simpleops_atomic_barrier ();
    *x = newv;
}

static simpleops_always_inline simpleops_atomic_int_t
simpleops_atomic_int_fetch_add (simpleops_atomic_int_t volatile *x,
				simpleops_atomic_int_t y)
{
    return _InterlockedExchangeAdd (x, y);
}

/* Atomic pointer functions */
static simpleops_always_inline simpleops_bool_t
simpleops_atomic_ptr_compare_exchange (void * volatile *x,
				       void * const oldv,
				       void * const newv)
{
#ifdef _M_X64
    return _InterlockedCompareExchangePointer (x, newv, oldv) == oldv;
#else
    return _InterlockedCompareExchangePointer (x, (int) newv, (int) oldv) == (int) oldv;
#endif
}

static simpleops_always_inline void *
simpleops_atomic_ptr_exchange (void * volatile *x,
			       void * const newv)
{
#ifdef _M_X64
    return _InterlockedExchangePointer (x, newv);
#else
    return (void *) _InterlockedExchangePointer (x, (int) newv);
#endif
}

static simpleops_always_inline void *
simpleops_atomic_ptr_load (void * const volatile *x)
{
    _simpleops_atomic_barrier ();
    return *x;
}

static simpleops_always_inline void
simpleops_atomic_ptr_store (void * volatile *x,
			    void * const newv)
{
    _simpleops_atomic_barrier ();
    *x = newv;
}

#endif

#endif