summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/thread/test/sync/mutual_exclusion/once/call_once/call_once_pass.cpp
blob: ac7b8076956d2bd7dc6eb4c397e2e8eb9596a913 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// Copyright (C) 2013 Vicente J. Botet Escriba
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// <boost/thread/once.hpp>

// struct once_flag;

// template<class Callable, class ...Args>
//   void call_once(once_flag& flag, Callable&& func, Args&&... args);

//#define BOOST_THREAD_VERSION 4
#define BOOST_THREAD_USES_MOVE
#define BOOST_THREAD_PROVIDES_ONCE_CXX11

#include <boost/thread/once.hpp>
#include <boost/thread/thread.hpp>
#include <boost/detail/lightweight_test.hpp>

#ifdef BOOST_THREAD_PROVIDES_ONCE_CXX11
#define BOOST_INIT_ONCE_INIT
#else
#define BOOST_INIT_ONCE_INIT =BOOST_ONCE_INIT
#endif

typedef boost::chrono::milliseconds ms;

boost::once_flag flg0 BOOST_INIT_ONCE_INIT;

int init0_called = 0;

void init0()
{
    boost::this_thread::sleep_for(ms(250));
    ++init0_called;
}

void f0()
{
    boost::call_once(flg0, init0);
}

boost::once_flag flg3 BOOST_INIT_ONCE_INIT;

int init3_called = 0;
int init3_completed = 0;

void init3()
{
    ++init3_called;
    boost::this_thread::sleep_for(ms(250));
    if (init3_called == 1)
        throw 1;
    ++init3_completed;
}

void f3()
{
    try
    {
        boost::call_once(flg3, init3);
    }
    catch (...)
    {
    }
}

struct init1
{
    static int called;
    typedef void result_type;

    void operator()(int i) {called += i;}
    void operator()(int i) const {called += i;}
};

int init1::called = 0;

boost::once_flag flg1 BOOST_INIT_ONCE_INIT;

void f1()
{
    boost::call_once(flg1, init1(), 1);
}

boost::once_flag flg1_member BOOST_INIT_ONCE_INIT;

struct init1_member
{
    static int called;
    typedef void result_type;
    void call(int i) {
      called += i;
    }
};
int init1_member::called = 0;

void f1_member_l()
{
    init1_member o;
    int i=1;
    boost::call_once(flg1_member, &init1_member::call, o, i);
}
void f1_member_r()
{
    init1_member o;
    boost::call_once(flg1_member, &init1_member::call, o, 1);
}
struct init2
{
    static int called;
    typedef void result_type;

    void operator()(int i, int j) {called += i + j;}
    void operator()(int i, int j) const {called += i + j;}
};

int init2::called = 0;

boost::once_flag flg2 BOOST_INIT_ONCE_INIT;

void f2()
{
    boost::call_once(flg2, init2(), 2, 3);
    boost::call_once(flg2, init2(), 4, 5);
}

boost::once_flag flg41 BOOST_INIT_ONCE_INIT;
boost::once_flag flg42 BOOST_INIT_ONCE_INIT;

int init41_called = 0;
int init42_called = 0;

void init42();

void init41()
{
    boost::this_thread::sleep_for(ms(250));
    ++init41_called;
}

void init42()
{
    boost::this_thread::sleep_for(ms(250));
    ++init42_called;
}

void f41()
{
    boost::call_once(flg41, init41);
    boost::call_once(flg42, init42);
}

void f42()
{
    boost::call_once(flg42, init42);
    boost::call_once(flg41, init41);
}

class MoveOnly
{
public:
  typedef void result_type;

  BOOST_THREAD_MOVABLE_ONLY(MoveOnly)
  MoveOnly()
  {
  }
  MoveOnly(BOOST_THREAD_RV_REF(MoveOnly))
  {}

  void operator()(BOOST_THREAD_RV_REF(MoveOnly))
  {
  }
  void operator()(int)
  {
  }
  void operator()()
  {
  }
};


struct id_string
{
    static boost::once_flag flag;
    static void do_init(id_string & )
    {}
    void operator()()
    {
      boost::call_once(flag, &id_string::do_init, boost::ref(*this));
    }
//    void operator()(int,int)
//    {
//      // This should fail but works with gcc-4.6.3
//      //std::bind(&id_string::do_init, *this)();
//      std::bind(&id_string::do_init, std::ref(*this))();
//    }
//    void operator()(int) const
//    {
//      //std::bind(&id_string::do_init, *this)();
//    }
};


boost::once_flag id_string::flag BOOST_INIT_ONCE_INIT;

int main()
{

  //
  {
    id_string id;
    id();
    //id(1,1);
  }
    // check basic functionality
    {
        boost::thread t0(f0);
        boost::thread t1(f0);
        t0.join();
        t1.join();
        BOOST_TEST(init0_called == 1);
    }
    // check basic exception safety
    {
        boost::thread t0(f3);
        boost::thread t1(f3);
        t0.join();
        t1.join();
        BOOST_TEST(init3_called == 2);
        BOOST_TEST(init3_completed == 1);
    }
    // check deadlock avoidance
    {
        boost::thread t0(f41);
        boost::thread t1(f42);
        t0.join();
        t1.join();
        BOOST_TEST(init41_called == 1);
        BOOST_TEST(init42_called == 1);
    }
    // check functors with 1 arg
    {
        boost::thread t0(f1);
        boost::thread t1(f1);
        t0.join();
        t1.join();
        BOOST_TEST(init1::called == 1);
    }
    // check functors with 2 args
    {
        boost::thread t0(f2);
        boost::thread t1(f2);
        t0.join();
        t1.join();
        BOOST_TEST(init2::called == 5);
    }

    // check member function with 1 arg
    {
        boost::thread t0(f1_member_l);
        boost::thread t1(f1_member_r);
        t0.join();
        t1.join();
        BOOST_TEST(init1_member::called == 1);
    }
#if defined BOOST_THREAD_PLATFORM_PTHREAD || (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__ > 40600)
    {
        boost::once_flag f BOOST_INIT_ONCE_INIT;
        boost::call_once(f, MoveOnly());
    }
#endif
#if defined BOOST_THREAD_PROVIDES_INVOKE
    {
        boost::once_flag f BOOST_INIT_ONCE_INIT;
        boost::call_once(f, MoveOnly(), 1);
    }
    {
        boost::once_flag f BOOST_INIT_ONCE_INIT;
        boost::call_once(f, MoveOnly(), MoveOnly());
    }
#endif  // BOOST_THREAD_PLATFORM_PTHREAD
    return boost::report_errors();
}