File size: 5,640 Bytes
2492322
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Copyright (c) 2019, QuantStack and Mamba Contributors
//
// Distributed under the terms of the BSD 3-Clause License.
//
// The full license is in the file LICENSE, distributed with this software.

#ifndef MAMBA_CORE_THREAD_UTILS_HPP
#define MAMBA_CORE_THREAD_UTILS_HPP

#include <condition_variable>
#include <csignal>
#include <exception>
#include <functional>
#include <mutex>
#include <thread>
#include <utility>

namespace mamba
{

    /***********************
     * thread interruption *
     ***********************/


    using signal_handler_t = void (*)(int);

#ifndef _WIN32
    void set_signal_handler(const std::function<void(sigset_t)>& handler);

    int stop_receiver_thread();
    int kill_receiver_thread();
    void reset_sig_interrupted();
#endif

    void set_default_signal_handler();
    void restore_previous_signal_handler();
    signal_handler_t previous_signal_handler();
    bool is_sig_interrupted() noexcept;
    void set_sig_interrupted() noexcept;

    void interruption_point();

    class thread_interrupted : public std::exception
    {
    public:

        thread_interrupted() = default;

        const char* what() const throw()
        {
            return "Thread interrupted";
        }
    };

    /****************
     * thread count *
     ****************/

    void increase_thread_count();
    void decrease_thread_count();
    int get_thread_count();

    // Waits until all other threads have finished
    // Must be called by the cleaning thread to ensure
    // it won't free resources that could be required
    // by threads still active.
    void wait_for_all_threads();

    /**********
     * thread *
     **********/

    // Thread that increases the threads count upon
    // creation and decreases it upon deletion. Use it
    // when you need to ensure all threads have exited
    class thread
    {
    public:

        thread() = default;
        ~thread() = default;

        thread(const thread&) = delete;
        thread& operator=(const thread&) = delete;

        thread(thread&&) noexcept = default;
        thread& operator=(thread&&) = default;

        template <class Function, class... Args>
        explicit thread(Function&& func, Args&&... args);

        bool joinable() const noexcept;
        std::thread::id get_id() const noexcept;

        void join();
        void detach();
        std::thread::native_handle_type native_handle();

        std::thread extract()
        {
            return std::move(m_thread);
        }

    private:

        std::thread m_thread;
    };

    template <class Function, class... Args>
    inline thread::thread(Function&& func, Args&&... args)
    {
        increase_thread_count();
        auto f = std::bind(std::forward<Function>(func), std::forward<Args>(args)...);
        m_thread = std::thread(
            [f]()
            {
                try
                {
                    f();
                }
                catch (thread_interrupted&)
                {
                    errno = EINTR;
                }
                decrease_thread_count();
            }
        );
    }

    /**********************
     * interruption_guard *
     **********************/

    class interruption_guard
    {
    public:

        template <class Function, class... Args>
        interruption_guard(Function&& func, Args&&... args);
        ~interruption_guard();

        interruption_guard(const interruption_guard&) = delete;
        interruption_guard& operator=(const interruption_guard&) = delete;

        interruption_guard(interruption_guard&&) = delete;
        interruption_guard& operator=(interruption_guard&&) = delete;

    private:

        static std::function<void()> m_cleanup_function;
    };

    template <class Function, class... Args>
    inline interruption_guard::interruption_guard(Function&& func, Args&&... args)
    {
        m_cleanup_function = std::bind(std::forward<Function>(func), std::forward<Args>(args)...);
    }

    class counting_semaphore
    {
    public:

        inline counting_semaphore(std::ptrdiff_t max = 0);
        inline void lock();
        inline void unlock();
        inline std::ptrdiff_t get_max();
        inline void set_max(std::ptrdiff_t value);

    private:

        std::ptrdiff_t m_value, m_max;
        std::mutex m_access_mutex;
        std::condition_variable m_cv;
    };

    /*************************************
     * counting_semaphore implementation *
     *************************************/

    inline counting_semaphore::counting_semaphore(std::ptrdiff_t max)
    {
        set_max(max);
        m_value = m_max;
    }

    inline void counting_semaphore::lock()
    {
        std::unique_lock lock{ m_access_mutex };
        m_cv.wait(lock, [&]() { return m_value > 0; });
        --m_value;
    }

    inline void counting_semaphore::unlock()
    {
        {
            std::unique_lock lock{ m_access_mutex };
            if (++m_value <= 0)
            {
                return;
            }
        }
        m_cv.notify_all();
    }

    inline std::ptrdiff_t counting_semaphore::get_max()
    {
        return m_max;
    }

    inline void counting_semaphore::set_max(std::ptrdiff_t value)
    {
        std::ptrdiff_t new_max;
        if (value == 0)
        {
            new_max = std::thread::hardware_concurrency();
        }
        else if (value < 0)
        {
            new_max = std::thread::hardware_concurrency() + value;
        }
        else
        {
            new_max = value;
        }

        m_value += new_max - m_max;
        m_max = new_max;
    }
}  // namespace mamba

#endif