File: shared_library_concurrent_load_test.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (119 lines) | stat: -rw-r--r-- 2,971 bytes parent folder | download | duplicates (3)
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
// Copyright Antony Polukhin, 2015-2025
//
// 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)

// For more information, see http://www.boost.org


#include "../example/b2_workarounds.hpp"

#include <boost/dll.hpp>

#include <cctype>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <thread>
#include <vector>

#include <boost/filesystem/path.hpp>

#include <boost/core/lightweight_test.hpp>

namespace {

typedef std::vector<boost::dll::fs::path> paths_t;

class simple_barrier {
    std::mutex mutex_;
    std::condition_variable cv_;
    std::size_t generation_{0};
    const std::size_t initial_count_;
    std::size_t count_{initial_count_};

public:
    explicit simple_barrier(std::size_t count) : initial_count_(count) {}
    simple_barrier(simple_barrier&&) = delete;
    simple_barrier(const simple_barrier&) = delete;

    void wait() {
        std::unique_lock<std::mutex> lock{mutex_};
        const auto gen_snapshot = generation_;

        if (--count_ == 0) {
            ++generation_;
            count_ = initial_count_;
            cv_.notify_all();
        } else {
            cv_.wait(lock, [this, gen_snapshot] {
                return gen_snapshot != generation_;
            });
        }
    }
};

// Disgusting workarounds for b2 on Windows platform
inline paths_t generate_paths(int argc, char* argv[]) {
    paths_t ret;
    ret.reserve(argc - 1);

    for (int i = 1; i < argc; ++i) {
        boost::dll::fs::path p = argv[i];
        if (b2_workarounds::is_shared_library(p)) {
            ret.push_back(p);
        }
    }

    return ret;
}

inline void load_unload(const paths_t& paths, std::size_t count, simple_barrier& b) {
    for (std::size_t j = 0; j < count; j += 2) {
        for (std::size_t i = 0; i < paths.size(); ++i) {
            boost::dll::shared_library lib(paths[i]);
            BOOST_TEST(lib);
        }
        for (std::size_t i = 0; i < paths.size(); ++i) {
            boost::dll::shared_library lib(paths[i]);
            BOOST_TEST(lib.location() != "");
        }

        // Waiting for all threads to unload shared libraries
        b.wait();
    }
}

}  // namespace


int main(int argc, char* argv[]) {
    BOOST_TEST(argc >= 3);
    paths_t paths = generate_paths(argc, argv);
    BOOST_TEST(!paths.empty());

    std::cout << "Libraries:\n\t";
    std::copy(paths.begin(), paths.end(), std::ostream_iterator<boost::dll::fs::path>(std::cout, ", "));
    std::cout << std::endl;

    constexpr std::size_t threads_count =
#ifdef BOOST_TRAVISCI_BUILD
        1
#else
        4
#endif
    ;

    simple_barrier barrier{threads_count};
    std::thread threads[threads_count];
    for (auto& t: threads) {
        t = std::thread(load_unload, paths, 1000, std::ref(barrier));
    }

    for (auto& t: threads) {
        t.join();
    }
    return boost::report_errors();
}