File: wait_fuzz.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (78 lines) | stat: -rw-r--r-- 2,399 bytes parent folder | download | duplicates (7)
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
//  Copyright (c) 2020 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)

// This is a fuzzing test for waiting and notifying operations.
// The test creates a number of threads exceeding the number of hardware threads, each of which
// blocks on the atomic object. The main thread then notifies one or all threads repeatedly,
// while incrementing the atomic object. The test ends when the atomic counter reaches the predefined limit.
// The goal of the test is to verify that (a) it doesn't crash and (b) all threads get unblocked in the end.

#include <boost/memory_order.hpp>
#include <boost/atomic/atomic.hpp>

#include <iostream>
#include <boost/config.hpp>
#include <boost/bind/bind.hpp>
#include <boost/chrono/chrono.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/barrier.hpp>
#include <boost/smart_ptr/scoped_array.hpp>

namespace chrono = boost::chrono;

boost::atomic< unsigned int > g_atomic(0u);

BOOST_CONSTEXPR_OR_CONST unsigned int loop_count = 4096u;

void thread_func(boost::barrier* barrier)
{
    barrier->wait();

    unsigned int old_count = 0u;
    while (true)
    {
        unsigned int new_count = g_atomic.wait(old_count, boost::memory_order_relaxed);
        if (new_count >= loop_count)
            break;

        old_count = new_count;
    }
}

int main()
{
    const unsigned int thread_count = boost::thread::hardware_concurrency() + 4u;
    boost::barrier barrier(thread_count + 1u);
    boost::scoped_array< boost::thread > threads(new boost::thread[thread_count]);

    for (unsigned int i = 0u; i < thread_count; ++i)
        boost::thread(boost::bind(&thread_func, &barrier)).swap(threads[i]);

    barrier.wait();

    // Let the threads block on the atomic counter
    boost::this_thread::sleep_for(chrono::milliseconds(100));

    while (true)
    {
        for (unsigned int i = 0u; i < thread_count; ++i)
        {
            g_atomic.opaque_add(1u, boost::memory_order_relaxed);
            g_atomic.notify_one();
        }

        unsigned int old_count = g_atomic.fetch_add(1u, boost::memory_order_relaxed);
        g_atomic.notify_all();

        if ((old_count + 1u) >= loop_count)
            break;
    }

    for (unsigned int i = 0u; i < thread_count; ++i)
        threads[i].join();

    return 0u;
}