summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/29_atomics/atomic_flag/test_and_set/explicit-hle.cc
blob: 560eee35a29ffe5df579cb7cd4f08fc2e5c245c8 (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
// { dg-do compile { target i?86-*-* x86_64-*-* } }
// { dg-options "-std=gnu++11 -g0 -fno-exceptions -fno-asynchronous-unwind-tables" }
// { dg-options "-std=gnu++11 -g0 -fno-exceptions -fno-asynchronous-unwind-tables -march=i486" { target ia32 } }
// { dg-final { scan-assembler-times "xacquire\|\.byte\[^\n\r]*0xf2" 14 } }
// { dg-final { scan-assembler-times "xrelease\|\.byte\[^\n\r]*0xf3" 14 } }

// Copyright (C) 2008-2016 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library 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 3, or (at your option)
// any later version.

// This library 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 library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

#include <atomic>

#define ACQ memory_order_acquire | __memory_order_hle_acquire
#define REL memory_order_release | __memory_order_hle_release

int main()
{
  unsigned zero, one;
  using namespace std;
  atomic_flag af = ATOMIC_FLAG_INIT;

  if (!af.test_and_set(ACQ))
    af.clear(REL);

  atomic_uint au = ATOMIC_VAR_INIT(0);

  if (au.exchange(1, ACQ))
    au.store(0, REL);

  if (au.exchange(1, ACQ))
    au.exchange(0, REL);

  zero = 0;
  one = 1;
  if (au.compare_exchange_weak(zero, 1, ACQ, memory_order_consume))
    au.compare_exchange_weak(one, 0, REL, memory_order_consume);

  zero = 0;
  one = 1;
  if (au.compare_exchange_strong(zero, 1, ACQ, memory_order_consume))
    au.compare_exchange_strong(one, 0, REL, memory_order_consume);

  if (!au.fetch_add(1, ACQ))
    au.fetch_add(-1, REL);

  if (!au.fetch_sub(1, ACQ))
    au.fetch_sub(-1, REL);

#if 0 /* broken in underlying target */
  if (!au.fetch_and(1, ACQ))
    au.fetch_and(-1, REL);

  if (!au.fetch_or(1, ACQ))
    au.fetch_or(-1, REL);

  if (!au.fetch_xor(1, ACQ))
    au.fetch_xor(-1, REL);

  if (!au.fetch_nand(1, ACQ))
    au.fetch_nand(-1, REL);
#endif

  volatile atomic_flag vaf = ATOMIC_FLAG_INIT;

  if (!vaf.test_and_set(ACQ))
    vaf.clear(REL);

  volatile atomic_uint vau = ATOMIC_VAR_INIT(0);

  if (!vau.exchange(1, ACQ))
    vau.store(0, REL);

  if (!vau.exchange(1, ACQ))
    vau.exchange(0, REL);

  zero = 0;
  one = 1;
  if (vau.compare_exchange_weak(zero, 1, ACQ, memory_order_consume))
    vau.compare_exchange_weak(one, 0, REL, memory_order_consume);

  zero = 0;
  one = 1;
  if (vau.compare_exchange_strong(zero, 1, ACQ, memory_order_consume))
    vau.compare_exchange_strong(one, 0, REL, memory_order_consume);

  if (!vau.fetch_add(1, ACQ))
    vau.fetch_add(-1, REL);

  if (!vau.fetch_sub(1, ACQ))
    vau.fetch_sub(-1, REL);

#if 0 /* broken in underlying target */

  if (!vau.fetch_and(1, ACQ))
    vau.fetch_and(-1, REL);

  if (!vau.fetch_or(1, ACQ))
    vau.fetch_or(-1, REL);

  if (!vau.fetch_xor(1, ACQ))
    vau.fetch_xor(-1, REL);

  if (!vau.fetch_nand(1, ACQ))
    vau.fetch_nand(-1, REL);
#endif

  return 0;
}