blob: 19f8f97bb9ede0485d9f3fdbd58a08fff0ff7ba4 (
plain)
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
|
// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.
#include <gras/thread_pool.hpp>
#include <gras_impl/block_actor.hpp>
#include <boost/thread/thread.hpp>
#include <Theron/Framework.h>
using namespace gras;
ThreadPoolConfig::ThreadPoolConfig(void)
{
thread_count = boost::thread::hardware_concurrency();
thread_count = std::max(size_t(2), thread_count);
node_mask = 0;
processor_mask = 0xffffffff;
yield_strategy = "STRONG";
}
/***********************************************************************
* Thread pool implementation
**********************************************************************/
ThreadPool::ThreadPool(void)
{
//NOP
}
ThreadPool::ThreadPool(boost::weak_ptr<Theron::Framework> p):
boost::shared_ptr<Theron::Framework>(p.lock())
{
//NOP
}
ThreadPool::ThreadPool(const ThreadPoolConfig &config)
{
Theron::Framework::Parameters params(
config.thread_count,
config.node_mask,
config.processor_mask
);
if (config.yield_strategy == "POLITE") params.mYieldStrategy = Theron::YIELD_STRATEGY_POLITE;
if (config.yield_strategy == "STRONG") params.mYieldStrategy = Theron::YIELD_STRATEGY_STRONG;
if (config.yield_strategy == "AGGRESSIVE") params.mYieldStrategy = Theron::YIELD_STRATEGY_AGGRESSIVE;
this->reset(new Theron::Framework(Theron::Framework::Parameters(params)));
}
/***********************************************************************
* Active framework implementation
**********************************************************************/
static boost::weak_ptr<Theron::Framework> weak_framework;
void ThreadPool::set_active(void)
{
weak_framework = *this;
}
static ThreadPool active_thread_pool;
static ThreadPool get_active_thread_pool(void)
{
if (not weak_framework.lock())
{
active_thread_pool = ThreadPool(ThreadPoolConfig());
active_thread_pool.set_active();
std::cout << "Created default thread pool with " << active_thread_pool->GetNumThreads() << " threads." << std::endl;
}
return weak_framework;
}
/***********************************************************************
* Block actor construction - gets active framework
**********************************************************************/
BlockActor::BlockActor(void):
Apology::Worker(*get_active_thread_pool())
{
const char * gras_tpp = getenv("GRAS_TPP");
if (gras_tpp != NULL)
{
ThreadPoolConfig config;
config.thread_count = 1;
this->thread_pool = ThreadPool(config);
}
else
{
this->thread_pool = get_active_thread_pool();
active_thread_pool.reset(); //actors hold this, now its safe to reset, weak_framework only
}
this->register_handlers();
this->handle_task_count = 0;
this->work_count = 0;
}
BlockActor::~BlockActor(void)
{
this->mark_done();
#ifdef WORK_COUNTS
if (work_count == 0) std::cerr << "\n WORK FAIL!!!" << std::endl;
std::cerr << name << " handle_task_count " << handle_task_count << std::endl;
std::cerr << name << " work_count " << work_count << std::endl;
#endif
}
|