File: dsp_thread.hpp

package info (click to toggle)
supercollider 1%3A3.13.0%2Brepack-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 80,296 kB
  • sloc: cpp: 476,363; lisp: 84,680; ansic: 77,685; sh: 25,509; python: 7,909; makefile: 3,440; perl: 1,964; javascript: 974; xml: 826; java: 677; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (233 lines) | stat: -rw-r--r-- 6,703 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
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
//  dsp thread
//  Copyright (C) 2007-2015 Tim Blechmann
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; see the file COPYING.  If not, write to
//  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//  Boston, MA 02111-1307, USA.

#pragma once

#include <cstdint>
#include <iostream>
#include <memory>
#include <thread>
#include <vector>

#include <boost/sync/semaphore.hpp>

#include "dsp_thread_queue.hpp"
#include "malloc_aligned.hpp"
#include "nova-tt/mlock.hpp"

namespace nova {

using std::uint16_t;

struct nop_thread_init {
    nop_thread_init(void) {}

    template <typename Arg> nop_thread_init(Arg const&) {}

    void operator()(int thread_index) {}
};


#if _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
#    define SUPERNOVA_USE_PTHREAD
#endif

/** dsp helper thread
 *
 *  the dsp helper threads are running with a high real-time priority and are
 *  pinned to a specific cpu
 */
template <typename runnable, typename thread_init_functor = nop_thread_init, typename Alloc = std::allocator<void*>>
class dsp_thread : public thread_init_functor {
    typedef nova::dsp_queue_interpreter<runnable, Alloc> dsp_queue_interpreter;

public:
    dsp_thread(dsp_queue_interpreter& interpreter, uint16_t index,
               thread_init_functor const& thread_init = thread_init_functor()):
        thread_init_functor(thread_init),
        interpreter(interpreter),
        index(index) {
#ifdef SUPERNOVA_USE_PTHREAD
        if (stack_size) {
            stack_ = malloc_aligned<char>(stack_size);
            if (stack_ == nullptr)
                throw std::bad_alloc();
            // touch stack to avoid page faults
            for (size_t i = 0; i != stack_size; ++i)
                stack_[i] = 0;
            mlock(stack_, stack_size);
        }
#endif
    }

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

    ~dsp_thread(void) {
#ifdef SUPERNOVA_USE_PTHREAD
        if (stack_)
            free_aligned(stack_);
#endif
    }

    void start() {
        stop = false;

#ifdef SUPERNOVA_USE_PTHREAD
        pthread_attr_t attr;
        pthread_attr_init(&attr);
        int err = pthread_attr_setstack(&attr, stack_, stack_size);
        if (err)
            throw std::logic_error("Cannot set stack of DSP helper thread");

        err = pthread_create(&thread_id, &attr, run_static, this);
        if (err)
            throw std::runtime_error("Cannot create DSP helper thread");
        pthread_attr_destroy(&attr);
#else
        thread = std::thread([this] { this->run(); });
#endif
    }

    void join() {
        stop.store(true, std::memory_order_relaxed);
        wake_thread();

#ifdef SUPERNOVA_USE_PTHREAD
        void* ret;
        int err = pthread_join(thread_id, &ret);
        if (err)
            printf("Error when joining helper thread\n");
#else
        thread.join();
#endif
    }

    void wake_thread(void) { cycle_sem.post(); }

private:
    /** thread function
     * */
    void run(void) {
        thread_init_functor::operator()(index);

        for (;;) {
            cycle_sem.wait();
            if (unlikely(stop.load(std::memory_order_relaxed)))
                return;

            interpreter.tick(index);
        }
    }

    static void* run_static(void* arg) {
        dsp_thread* self = static_cast<dsp_thread*>(arg);
        self->run();
        return nullptr;
    }

private:
    boost::sync::semaphore cycle_sem;
    dsp_queue_interpreter& interpreter;
    std::atomic<bool> stop = { false };
    uint16_t index;

#ifdef SUPERNOVA_USE_PTHREAD
    pthread_t thread_id;

    static const size_t stack_size = 524288;
    char* stack_ = nullptr;

#else
    std::thread thread;
#endif
};

/** \brief container for all dsp threads
 *
 *  - no care is taken, that dsp_thread_pool::run is executed on a valid instance
 *
 * */
template <typename runnable, typename thread_init_functor = nop_thread_init, typename Alloc = std::allocator<void*>>
class dsp_thread_pool {
    typedef nova::dsp_queue_interpreter<runnable, Alloc> dsp_queue_interpreter;
    typedef nova::dsp_thread<runnable, thread_init_functor, Alloc> dsp_thread;

public:
    typedef typename dsp_queue_interpreter::node_count_t node_count_t;
    typedef typename dsp_queue_interpreter::thread_count_t thread_count_t;

    typedef std::unique_ptr<dsp_thread_queue<runnable, Alloc>> dsp_thread_queue_ptr;

    dsp_thread_pool(thread_count_t count, bool yield_if_busy = false,
                    thread_init_functor const& init_functor = thread_init_functor()):
        interpreter(std::min(count, (thread_count_t)std::thread::hardware_concurrency()), yield_if_busy) {
        set_dsp_thread_count(interpreter.get_thread_count(), init_functor);
    }

    void run(void) {
        const bool run_tick = interpreter.init_tick();
        if (likely(run_tick)) {
            wake_threads();
            interpreter.tick_main();
        }
    }

    /** reset queue
     *
     *  don't call, if threads are currently accessing the queue
     * */
    dsp_thread_queue_ptr reset_queue(dsp_thread_queue_ptr&& new_queue) {
        dsp_thread_queue_ptr ret = interpreter.reset_queue(std::move(new_queue));
        return std::move(ret);
    }

    dsp_thread_queue_ptr release_queue(void) { return interpreter.release_queue(); }

public:
    /** thread handling */
    /* @{ */
    void start_threads(void) {
        for (auto& thread : threads)
            thread->start();
    }

    void terminate_threads(void) {
        for (auto& thread : threads)
            thread->join();
    }
    /* @} */

private:
    void set_dsp_thread_count(thread_count_t count, thread_init_functor const& init_functor) {
        for (thread_count_t i = 1; i != count; ++i)
            threads.emplace_back(new dsp_thread(interpreter, i, init_functor));
    }

    /** wake dsp threads */
    void wake_threads(void) {
        for (thread_count_t i = 0; i != interpreter.get_used_helper_threads(); ++i)
            threads[i]->wake_thread();
    }

    dsp_queue_interpreter interpreter;

    std::vector<std::unique_ptr<dsp_thread>> threads;
};

} /* namespace nova */