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
|
// templated callback system
// Copyright (C) 2008, 2009 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.
#ifndef UTILITIES_CALLBACK_INTERPRETER_HPP
#define UTILITIES_CALLBACK_INTERPRETER_HPP
#include <thread>
#include "branch_hints.hpp"
#include "callback_system.hpp"
#include "nova-tt/semaphore.hpp"
#include "nova-tt/thread_priority.hpp"
#include <boost/atomic.hpp>
#include <boost/checked_delete.hpp>
namespace nova {
namespace detail {
struct nop_functor
{
void operator()() const {}
};
} /* namespace detail */
template <class callback_type,
bool mpmc = true,
class callback_deleter = boost::checked_deleter<callback_type> >
class callback_interpreter:
callback_system<callback_type, mpmc, callback_deleter>
{
typedef callback_system<callback_type, mpmc, callback_deleter> super_t;
public:
callback_interpreter(void):
sem(0), running(false)
{}
void add_callback(callback_type * cb)
{
super_t::add_callback(cb);
sem.post();
}
void run(void)
{
running.store(true, boost::memory_order_relaxed);
perform();
}
void terminate(void)
{
running.store(false, boost::memory_order_relaxed);
sem.post();
}
protected:
void run(semaphore & sync_sem)
{
running.store(true, boost::memory_order_relaxed);
sync_sem.post();
perform();
}
private:
void perform(void)
{
do
{
sem.wait();
super_t::run_callbacks();
} while(likely(running.load(boost::memory_order_relaxed)));
}
protected:
semaphore sem;
boost::atomic<bool> running;
};
template <class callback_type,
class init_functor = detail::nop_functor,
class callback_deleter = boost::checked_deleter<callback_type> >
class threaded_callback_interpreter:
public callback_interpreter<callback_type, true, callback_deleter>,
init_functor
{
typedef callback_interpreter<callback_type, true, callback_deleter> super;
std::thread callback_thread;
public:
threaded_callback_interpreter(void)
{}
~threaded_callback_interpreter(void)
{
if (super::running.load())
join_thread();
}
void start_thread(void)
{
semaphore sync_sem;
semaphore_sync<semaphore> sync(sync_sem);
std::thread thr( [&]() {
this->run_thread(sync_sem);
});
callback_thread = move(thr);
}
void run_thread(semaphore & sync_sem)
{
init_functor::operator()();
super::run(sync_sem);
}
void join_thread(void)
{
super::terminate();
callback_thread.join();
}
};
template <class callback_type,
class init_functor = detail::nop_functor,
class callback_deleter = boost::checked_deleter<callback_type> >
class callback_interpreter_threadpool:
public callback_interpreter<callback_type, true, callback_deleter>,
init_functor
{
typedef callback_interpreter<callback_type, true, callback_deleter> super;
public:
callback_interpreter_threadpool(uint16_t worker_thread_count, bool rt, uint16_t priority):
worker_thread_count_(worker_thread_count), priority(priority), rt(rt)
{
semaphore sync_sem;
using namespace std;
for (uint16_t i = 0; i != worker_thread_count; ++i)
threads.emplace_back( [&] () {
this->run(sync_sem);
});
for (uint16_t i = 0; i != worker_thread_count; ++i)
sync_sem.wait();
}
~callback_interpreter_threadpool(void)
{
if (super::running.load())
join_threads();
}
void join_threads(void)
{
super::running.store(false);
for (uint16_t i = 0; i != worker_thread_count_; ++i)
super::sem.post();
for (std::thread & thread : threads)
thread.join();
}
private:
void run(semaphore & sync_sem)
{
#ifdef NOVA_TT_PRIORITY_RT
if (rt)
thread_set_priority_rt(priority);
else
#endif
thread_set_priority(priority);
init_functor::operator()();
super::run(sync_sem);
}
std::vector<std::thread> threads;
uint16_t worker_thread_count_;
uint16_t priority;
bool rt;
};
} /* namespace nova */
#endif /* UTILITIES_CALLBACK_INTERPRETER_HPP */
|