boost_atomic 0.1.0

Boost C++ library boost_atomic packaged using Zanbil
Documentation
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//  Copyright (c) 2020-2025 Andrey Semashev
//
//  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)

#ifndef BOOST_ATOMIC_TEST_WAIT_TEST_HELPERS_HPP_INCLUDED_
#define BOOST_ATOMIC_TEST_WAIT_TEST_HELPERS_HPP_INCLUDED_

#include <boost/atomic/atomic_flag.hpp>
#include <boost/atomic/wait_result.hpp>

#include <cstdlib>
#include <cstring>
#include <chrono>
#include <thread>
#include <memory>
#include <utility>
#include <iostream>
#include <boost/config.hpp>
#include <boost/current_function.hpp>
#include "atomic_wrapper.hpp"
#include "lightweight_test_stream.hpp"
#include "test_thread.hpp"
#include "test_barrier.hpp"

//! Since some of the tests below are allowed to fail, we retry up to this many times to pass the test
constexpr unsigned int test_retry_count = 10u;

//! The test verifies that the wait operation returns immediately if the passed value does not match the atomic value
template< template< typename > class Wrapper, typename T >
inline void test_wait_value_mismatch(T value1, T value2)
{
    Wrapper< T > m_wrapper(value1);

    {
        T received_value = m_wrapper.a.wait(value2);
        BOOST_TEST(received_value == value1);
    }
    {
        boost::atomics::wait_result< T > result = m_wrapper.a.wait_until(value2, std::chrono::steady_clock::now());
        BOOST_TEST(result.value == value1);
        BOOST_TEST(!result.timeout);
    }
    {
        boost::atomics::wait_result< T > result = m_wrapper.a.wait_until(value2, std::chrono::steady_clock::now() + std::chrono::milliseconds(200));
        BOOST_TEST(result.value == value1);
        BOOST_TEST(!result.timeout);
    }
    {
        boost::atomics::wait_result< T > result = m_wrapper.a.wait_for(value2, std::chrono::milliseconds::zero());
        BOOST_TEST(result.value == value1);
        BOOST_TEST(!result.timeout);
    }
    {
        boost::atomics::wait_result< T > result = m_wrapper.a.wait_for(value2, std::chrono::milliseconds(200));
        BOOST_TEST(result.value == value1);
        BOOST_TEST(!result.timeout);
    }
}

/*!
 * The test verifies that notify_one releases one blocked thread and that the released thread receives the modified atomic value.
 *
 * Technically, this test is allowed to fail since wait() is allowed to return spuriously. However, normally this should not happen.
 */
template< template< typename > class Wrapper, typename T >
class notify_one_test
{
private:
    struct thread_state
    {
        T m_received_value;
        std::chrono::steady_clock::time_point m_wakeup_time;

        explicit thread_state(T value) : m_received_value(value)
        {
        }
    };

private:
    Wrapper< T > m_wrapper;

    char m_padding[1024];

    T m_value1, m_value2, m_value3;

    test_barrier m_barrier;

    thread_state m_thread1_state;
    thread_state m_thread2_state;

public:
    explicit notify_one_test(T value1, T value2, T value3) :
        m_wrapper(value1),
        m_value1(value1),
        m_value2(value2),
        m_value3(value3),
        m_barrier(3),
        m_thread1_state(value1),
        m_thread2_state(value1)
    {
    }

    bool run()
    {
        test_thread thread1([this]() { this->thread_func(&this->m_thread1_state); });
        test_thread thread2([this]() { this->thread_func(&this->m_thread2_state); });

        m_barrier.arrive_and_wait();

        std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now();

        std::this_thread::sleep_until(start_time + std::chrono::milliseconds(200));

        m_wrapper.a.store(m_value2);
        m_wrapper.a.notify_one();

        std::this_thread::sleep_until(start_time + std::chrono::milliseconds(500));

        m_wrapper.a.store(m_value3);
        m_wrapper.a.notify_one();

        if (!thread1.try_join_for(std::chrono::seconds(5)))
        {
            BOOST_ERROR("Thread 1 failed to join");
            std::abort();
        }
        if (!thread2.try_join_for(std::chrono::seconds(5)))
        {
            BOOST_ERROR("Thread 2 failed to join");
            std::abort();
        }

        thread_state* first_state = &m_thread1_state;
        thread_state* second_state = &m_thread2_state;
        if (second_state->m_wakeup_time < first_state->m_wakeup_time)
            std::swap(first_state, second_state);

        if ((first_state->m_wakeup_time - start_time) < std::chrono::milliseconds(200))
        {
            std::cout << BOOST_CURRENT_FUNCTION << ": first thread woke up too soon: "
                << std::chrono::duration_cast< std::chrono::milliseconds >(first_state->m_wakeup_time - start_time).count() << " ms" << std::endl;
            return false;
        }

        if ((first_state->m_wakeup_time - start_time) >= std::chrono::milliseconds(500))
        {
            std::cout << BOOST_CURRENT_FUNCTION << ": first thread woke up too late: "
                << std::chrono::duration_cast< std::chrono::milliseconds >(first_state->m_wakeup_time - start_time).count() << " ms" << std::endl;
            return false;
        }

        if ((second_state->m_wakeup_time - start_time) < std::chrono::milliseconds(500))
        {
            std::cout << BOOST_CURRENT_FUNCTION << ": second thread woke up too soon: "
                << std::chrono::duration_cast< std::chrono::milliseconds >(second_state->m_wakeup_time - start_time).count() << " ms" << std::endl;
            return false;
        }

        // Sometimes, even with the time check above, the second thread receives value2. This mostly happens in VMs.
        if (second_state->m_received_value == m_value2)
        {
            std::cout << BOOST_CURRENT_FUNCTION << ": second thread received value2 after waiting for "
                << std::chrono::duration_cast< std::chrono::milliseconds >(second_state->m_wakeup_time - start_time).count() << " ms" << std::endl;
            return false;
        }

        BOOST_TEST_EQ(first_state->m_received_value, m_value2);
        BOOST_TEST_EQ(second_state->m_received_value, m_value3);

        return true;
    }

private:
    void thread_func(thread_state* state)
    {
        m_barrier.arrive_and_wait();

        state->m_received_value = m_wrapper.a.wait(m_value1);
        state->m_wakeup_time = std::chrono::steady_clock::now();
    }
};

template< template< typename > class Wrapper, typename T >
inline void test_notify_one(T value1, T value2, T value3)
{
    for (unsigned int i = 0u; i < test_retry_count; ++i)
    {
        notify_one_test< Wrapper, T > test(value1, value2, value3);
        if (test.run())
            return;

        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    BOOST_ERROR("notify_one_test could not complete because of the timing issues");
}

/*!
 * The test verifies that notify_all releases all blocked threads and that the released threads receive the modified atomic value.
 *
 * Technically, this test is allowed to fail since wait() is allowed to return spuriously. However, normally this should not happen.
 */
template< template< typename > class Wrapper, typename T >
class notify_all_test
{
private:
    struct thread_state
    {
        T m_received_value;
        std::chrono::steady_clock::time_point m_wakeup_time;

        explicit thread_state(T value) : m_received_value(value)
        {
        }
    };

private:
    Wrapper< T > m_wrapper;

    char m_padding[1024];

    T m_value1, m_value2;

    test_barrier m_barrier;

    thread_state m_thread1_state;
    thread_state m_thread2_state;

public:
    explicit notify_all_test(T value1, T value2) :
        m_wrapper(value1),
        m_value1(value1),
        m_value2(value2),
        m_barrier(3),
        m_thread1_state(value1),
        m_thread2_state(value1)
    {
    }

    bool run()
    {
        test_thread thread1([this]() { this->thread_func(&this->m_thread1_state); });
        test_thread thread2([this]() { this->thread_func(&this->m_thread2_state); });

        m_barrier.arrive_and_wait();

        std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now();

        std::this_thread::sleep_until(start_time + std::chrono::milliseconds(200));

        m_wrapper.a.store(m_value2);
        m_wrapper.a.notify_all();

        if (!thread1.try_join_for(std::chrono::seconds(5)))
        {
            BOOST_ERROR("Thread 1 failed to join");
            std::abort();
        }
        if (!thread2.try_join_for(std::chrono::seconds(5)))
        {
            BOOST_ERROR("Thread 2 failed to join");
            std::abort();
        }

        if ((m_thread1_state.m_wakeup_time - start_time) < std::chrono::milliseconds(200))
        {
            std::cout << BOOST_CURRENT_FUNCTION << ": first thread woke up too soon: "
                << std::chrono::duration_cast< std::chrono::milliseconds >(m_thread1_state.m_wakeup_time - start_time).count() << " ms" << std::endl;
            return false;
        }

        if ((m_thread2_state.m_wakeup_time - start_time) < std::chrono::milliseconds(200))
        {
            std::cout << BOOST_CURRENT_FUNCTION << ": second thread woke up too soon: "
                << std::chrono::duration_cast< std::chrono::milliseconds >(m_thread2_state.m_wakeup_time - start_time).count() << " ms" << std::endl;
            return false;
        }

        BOOST_TEST_EQ(m_thread1_state.m_received_value, m_value2);
        BOOST_TEST_EQ(m_thread2_state.m_received_value, m_value2);

        return true;
    }

private:
    void thread_func(thread_state* state)
    {
        m_barrier.arrive_and_wait();

        state->m_received_value = m_wrapper.a.wait(m_value1);
        state->m_wakeup_time = std::chrono::steady_clock::now();
    }
};

template< template< typename > class Wrapper, typename T >
inline void test_notify_all(T value1, T value2)
{
    for (unsigned int i = 0u; i < test_retry_count; ++i)
    {
        notify_all_test< Wrapper, T > test(value1, value2);
        if (test.run())
            return;

        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    BOOST_ERROR("notify_all_test could not complete because blocked thread wake up too soon");
}

/*!
 * The test verifies that absolute timeout expiry is correctly registered.
 */
template< template< typename > class Wrapper, typename T, typename Clock >
class abs_timeout_test
{
private:
    Wrapper< T > m_wrapper;
    T m_value1;

public:
    explicit abs_timeout_test(T value1) :
        m_wrapper(value1),
        m_value1(value1)
    {
    }

    bool run()
    {
        typename Clock::time_point start_time = Clock::now();

        boost::atomics::wait_result< T > result = m_wrapper.a.wait_until(m_value1, start_time + std::chrono::milliseconds(200));

        typename Clock::time_point wakeup_time = Clock::now();

        if ((wakeup_time - start_time) < std::chrono::milliseconds(200))
        {
            std::cout << BOOST_CURRENT_FUNCTION << ": thread woke up too soon: "
                << std::chrono::duration_cast< std::chrono::milliseconds >(wakeup_time - start_time).count() << " ms" << std::endl;
            return false;
        }

        BOOST_TEST_EQ(result.value, m_value1);
        BOOST_TEST(result.timeout);

        return true;
    }
};

template< template< typename > class Wrapper, typename Clock, typename T >
inline void test_abs_timeout(T value1)
{
    for (unsigned int i = 0u; i < test_retry_count; ++i)
    {
        abs_timeout_test< Wrapper, T, Clock > test(value1);
        if (test.run())
            return;

        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    BOOST_ERROR("abs_timeout_test could not complete because blocked thread wake up too soon");
}

/*!
 * The test verifies that relative timeout expiry is correctly registered.
 */
template< template< typename > class Wrapper, typename T >
class rel_timeout_test
{
private:
    Wrapper< T > m_wrapper;
    T m_value1;

public:
    explicit rel_timeout_test(T value1) :
        m_wrapper(value1),
        m_value1(value1)
    {
    }

    bool run()
    {
        std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now();

        boost::atomics::wait_result< T > result = m_wrapper.a.wait_for(m_value1, std::chrono::milliseconds(200));

        std::chrono::steady_clock::time_point wakeup_time = std::chrono::steady_clock::now();

        if ((wakeup_time - start_time) < std::chrono::milliseconds(200))
        {
            std::cout << BOOST_CURRENT_FUNCTION << ": thread woke up too soon: "
                << std::chrono::duration_cast< std::chrono::milliseconds >(wakeup_time - start_time).count() << " ms" << std::endl;
            return false;
        }

        BOOST_TEST_EQ(result.value, m_value1);
        BOOST_TEST(result.timeout);

        return true;
    }
};

template< template< typename > class Wrapper, typename T >
inline void test_rel_timeout(T value1)
{
    for (unsigned int i = 0u; i < test_retry_count; ++i)
    {
        rel_timeout_test< Wrapper, T > test(value1);
        if (test.run())
            return;

        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    BOOST_ERROR("rel_timeout_test could not complete because blocked thread wake up too soon");
}

/*!
 * The test verifies that notifying interrupts a waiting operation with an absolute timeout.
 */
template< template< typename > class Wrapper, typename T, typename Clock >
class notify_abs_timeout_test
{
private:
    struct thread_state
    {
        boost::atomics::wait_result< T > m_received_result;
        typename Clock::time_point m_wakeup_time;

        explicit thread_state(T value)
        {
            m_received_result.value = value;
            m_received_result.timeout = true;
        }
    };

private:
    Wrapper< T > m_wrapper;

    char m_padding[1024];

    T m_value1, m_value2;

    test_barrier m_barrier;

    thread_state m_thread1_state;

public:
    explicit notify_abs_timeout_test(T value1, T value2) :
        m_wrapper(value1),
        m_value1(value1),
        m_value2(value2),
        m_barrier(2),
        m_thread1_state(value1)
    {
    }

    bool run()
    {
        test_thread thread1([this]() { this->thread_func(&this->m_thread1_state); });

        m_barrier.arrive_and_wait();

        typename Clock::time_point start_time = Clock::now();

        std::this_thread::sleep_until(start_time + std::chrono::milliseconds(200));

        m_wrapper.a.store(m_value2);
        m_wrapper.a.notify_all();

        if (!thread1.try_join_for(std::chrono::seconds(5)))
        {
            BOOST_ERROR("Thread 1 failed to join");
            std::abort();
        }

        if ((m_thread1_state.m_wakeup_time - start_time) < std::chrono::milliseconds(200))
        {
            std::cout << BOOST_CURRENT_FUNCTION << ": first thread woke up too soon: "
                << std::chrono::duration_cast< std::chrono::milliseconds >(m_thread1_state.m_wakeup_time - start_time).count() << " ms" << std::endl;
            return false;
        }

        BOOST_TEST_EQ(m_thread1_state.m_received_result.value, m_value2);
        BOOST_TEST(!m_thread1_state.m_received_result.timeout);

        return true;
    }

private:
    void thread_func(thread_state* state)
    {
        m_barrier.arrive_and_wait();

        state->m_received_result = m_wrapper.a.wait_until(m_value1, Clock::now() + std::chrono::seconds(2));
        state->m_wakeup_time = Clock::now();
    }
};

template< template< typename > class Wrapper, typename Clock, typename T >
inline void test_notify_abs_timeout(T value1, T value2)
{
    for (unsigned int i = 0u; i < test_retry_count; ++i)
    {
        notify_abs_timeout_test< Wrapper, T, Clock > test(value1, value2);
        if (test.run())
            return;

        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    BOOST_ERROR("notify_abs_timeout_test could not complete because blocked thread wake up too soon");
}

/*!
 * The test verifies that notifying interrupts a waiting operation with a relative timeout.
 */
template< template< typename > class Wrapper, typename T >
class notify_rel_timeout_test
{
private:
    struct thread_state
    {
        boost::atomics::wait_result< T > m_received_result;
        std::chrono::steady_clock::time_point m_wakeup_time;

        explicit thread_state(T value)
        {
            m_received_result.value = value;
            m_received_result.timeout = true;
        }
    };

private:
    Wrapper< T > m_wrapper;

    char m_padding[1024];

    T m_value1, m_value2;

    test_barrier m_barrier;

    thread_state m_thread1_state;

public:
    explicit notify_rel_timeout_test(T value1, T value2) :
        m_wrapper(value1),
        m_value1(value1),
        m_value2(value2),
        m_barrier(2),
        m_thread1_state(value1)
    {
    }

    bool run()
    {
        test_thread thread1([this]() { this->thread_func(&this->m_thread1_state); });

        m_barrier.arrive_and_wait();

        std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now();

        std::this_thread::sleep_until(start_time + std::chrono::milliseconds(200));

        m_wrapper.a.store(m_value2);
        m_wrapper.a.notify_all();

        if (!thread1.try_join_for(std::chrono::seconds(5)))
        {
            BOOST_ERROR("Thread 1 failed to join");
            std::abort();
        }

        if ((m_thread1_state.m_wakeup_time - start_time) < std::chrono::milliseconds(200))
        {
            std::cout << BOOST_CURRENT_FUNCTION << ": first thread woke up too soon: "
                << std::chrono::duration_cast< std::chrono::milliseconds >(m_thread1_state.m_wakeup_time - start_time).count() << " ms" << std::endl;
            return false;
        }

        BOOST_TEST_EQ(m_thread1_state.m_received_result.value, m_value2);
        BOOST_TEST(!m_thread1_state.m_received_result.timeout);

        return true;
    }

private:
    void thread_func(thread_state* state)
    {
        m_barrier.arrive_and_wait();

        state->m_received_result = m_wrapper.a.wait_for(m_value1, std::chrono::seconds(2));
        state->m_wakeup_time = std::chrono::steady_clock::now();
    }
};

template< template< typename > class Wrapper, typename T >
inline void test_notify_rel_timeout(T value1, T value2)
{
    for (unsigned int i = 0u; i < test_retry_count; ++i)
    {
        notify_rel_timeout_test< Wrapper, T > test(value1, value2);
        if (test.run())
            return;

        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    BOOST_ERROR("notify_rel_timeout_test could not complete because blocked thread wake up too soon");
}

//! Invokes all wait/notify tests
template< template< typename > class Wrapper, typename T >
void test_wait_notify_api(T value1, T value2, T value3)
{
    test_wait_value_mismatch< Wrapper >(value1, value2);
    test_notify_one< Wrapper >(value1, value2, value3);
    test_notify_all< Wrapper >(value1, value2);
    test_abs_timeout< Wrapper, std::chrono::system_clock >(value1);
    test_abs_timeout< Wrapper, std::chrono::steady_clock >(value1);
    test_rel_timeout< Wrapper >(value1);
    test_notify_abs_timeout< Wrapper, std::chrono::system_clock >(value1, value2);
    test_notify_abs_timeout< Wrapper, std::chrono::steady_clock >(value1, value2);
    test_notify_rel_timeout< Wrapper >(value1, value2);
}

//! Invokes all wait/notify tests
template< template< typename > class Wrapper, typename T >
void test_wait_notify_api(T value1, T value2, T value3, int has_native_wait_notify_macro)
{
    BOOST_TEST_EQ(Wrapper< T >::atomic_type::always_has_native_wait_notify, (has_native_wait_notify_macro == 2));
    test_wait_notify_api< Wrapper >(value1, value2, value3);
}


inline void test_flag_wait_notify_api()
{
    boost::atomic_flag f = BOOST_ATOMIC_FLAG_INIT;

    bool received_value = f.wait(true);
    BOOST_TEST(!received_value);
    f.notify_one();
    f.notify_all();
}

struct struct_3_bytes
{
    unsigned char data[3u];

    inline bool operator==(struct_3_bytes const& c) const
    {
        return std::memcmp(data, &c.data, sizeof(data)) == 0;
    }
    inline bool operator!=(struct_3_bytes const& c) const
    {
        return std::memcmp(data, &c.data, sizeof(data)) != 0;
    }
};

template< typename Char, typename Traits >
inline std::basic_ostream< Char, Traits >& operator<< (std::basic_ostream< Char, Traits >& strm, struct_3_bytes const& val)
{
    strm << "[struct_3_bytes{ " << static_cast< unsigned int >(val.data[0])
        << ", " << static_cast< unsigned int >(val.data[1]) << ", " << static_cast< unsigned int >(val.data[2]) << " }]";
    return strm;
}

struct large_struct
{
    unsigned char data[256u];

    inline bool operator==(large_struct const& c) const
    {
        return std::memcmp(data, &c.data, sizeof(data)) == 0;
    }
    inline bool operator!=(large_struct const& c) const
    {
        return std::memcmp(data, &c.data, sizeof(data)) != 0;
    }
};

template< typename Char, typename Traits >
inline std::basic_ostream< Char, Traits >& operator<< (std::basic_ostream< Char, Traits >& strm, large_struct const&)
{
    strm << "[large_struct]";
    return strm;
}

#endif // BOOST_ATOMIC_TEST_WAIT_TEST_HELPERS_HPP_INCLUDED_