summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/30_threads/stop_token/stop_token.cc
blob: 9f4cb39471318cde60eee2c5fa72edb36545a46e (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (C) 2019-2021 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/>.

// { dg-options "-std=gnu++2a" }
// { dg-add-options libatomic }
// { dg-do run { target c++2a } }

#include <stop_token>
#include <testsuite_hooks.h>

void
test01()
{
  std::stop_source ssrc;
  std::stop_token tok = ssrc.get_token();
  VERIFY(tok.stop_possible());
  VERIFY(!tok.stop_requested());

  std::stop_token copy(tok);
  VERIFY(copy == tok);
  VERIFY(copy.stop_possible());
  VERIFY(!copy.stop_requested());
  VERIFY(tok.stop_possible());
  VERIFY(!tok.stop_requested());

  std::stop_token move(std::move(tok));
  VERIFY(move != tok);
  VERIFY(move == copy);
  VERIFY(move.stop_possible());
  VERIFY(!move.stop_requested());
  VERIFY(!tok.stop_possible());
  VERIFY(!tok.stop_requested());
  VERIFY(copy.stop_possible());
  VERIFY(!copy.stop_requested());

  ssrc.request_stop();
  VERIFY(move.stop_possible());
  VERIFY(move.stop_requested());
  VERIFY(!tok.stop_possible());
  VERIFY(!tok.stop_requested());
  VERIFY(copy.stop_possible());
  VERIFY(copy.stop_requested());

  tok.swap(move);
  VERIFY(tok == copy);
  VERIFY(!move.stop_possible());
  VERIFY(!move.stop_requested());
  VERIFY(tok.stop_possible());
  VERIFY(tok.stop_requested());
  VERIFY(copy.stop_possible());
  VERIFY(copy.stop_requested());

  swap(move, copy);
  VERIFY(tok == move);
  VERIFY(move.stop_possible());
  VERIFY(move.stop_requested());
  VERIFY(tok.stop_possible());
  VERIFY(tok.stop_requested());
  VERIFY(!copy.stop_possible());
  VERIFY(!copy.stop_requested());
}

void
test02()
{
  std::stop_source src1, src2;
  std::stop_token tok = src1.get_token();
  VERIFY(tok.stop_possible());
  VERIFY(!tok.stop_requested());

  std::stop_token copy = src2.get_token();
  VERIFY(copy != tok);
  copy = tok;
  VERIFY(copy == tok);
  copy = src2.get_token();
  VERIFY(copy != tok);
}

void
test03()
{
  // create stop_source
  std::stop_source ssrc;
  VERIFY(ssrc.stop_possible());
  VERIFY(!ssrc.stop_requested());

  // create stop_token from stop_source
  std::stop_token stok{ssrc.get_token()};
  VERIFY(ssrc.stop_possible());
  VERIFY(!ssrc.stop_requested());
  VERIFY(stok.stop_possible());
  VERIFY(!stok.stop_requested());

  // register callback
  bool cb1called{false};
  auto cb1 = [&]{
               cb1called = true;
             };
  {
    std::stop_callback scb1{stok, cb1};
    VERIFY(ssrc.stop_possible());
    VERIFY(!ssrc.stop_requested());
    VERIFY(stok.stop_possible());
    VERIFY(!stok.stop_requested());
    VERIFY(!cb1called);
  } // unregister callback

  // register another callback
  bool cb2called{false};
  auto cb2 = [&]{
               VERIFY(stok.stop_requested());
               cb2called = true;
             };
  std::stop_callback scb2a{stok, cb2}; // copies cb2
  //  std::stop_callback scb2b{stok, std::move(cb2)};
  VERIFY(ssrc.stop_possible());
  VERIFY(!ssrc.stop_requested());
  VERIFY(stok.stop_possible());
  VERIFY(!stok.stop_requested());
  VERIFY(!cb1called);
  VERIFY(!cb2called);

  // request stop
  auto b = ssrc.request_stop();
  VERIFY(b);
  VERIFY(ssrc.stop_possible());
  VERIFY(ssrc.stop_requested());
  VERIFY(stok.stop_possible());
  VERIFY(stok.stop_requested());
  VERIFY(!cb1called);
  VERIFY(cb2called);

  b = ssrc.request_stop();
  VERIFY(!b);

  // register another callback
  bool cb3called{false};
  std::stop_callback scb3{stok, [&]
                                {
                                  cb3called = true;
                                }};
  VERIFY(ssrc.stop_possible());
  VERIFY(ssrc.stop_requested());
  VERIFY(stok.stop_possible());
  VERIFY(stok.stop_requested());
  VERIFY(!cb1called);
  VERIFY(cb2called);
  VERIFY(cb3called);
}

int main()
{
  test01();
  test02();
  test03();
}