Skip to content

Commit c99764d

Browse files
authored
Merge pull request #991 from singnet/senna-982-3
[#982] Add processors for thread pool and dedicated single thread
2 parents c13de1f + 0a8ffb4 commit c99764d

9 files changed

Lines changed: 379 additions & 1 deletion

File tree

‎src/commons/Utils.cc‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ bool Utils::flip_coin(double true_probability) {
3636
return (rand() % f) < lround(true_probability * f);
3737
}
3838

39+
unsigned int Utils::uint_rand(unsigned int closed_lower_bound, unsigned int open_upper_bound) {
40+
if (open_upper_bound <= closed_lower_bound) {
41+
Utils::error("Invalid bounds");
42+
}
43+
return (rand() % (open_upper_bound - closed_lower_bound)) + closed_lower_bound;
44+
}
45+
46+
unsigned int Utils::uint_rand(unsigned int open_upper_bound) {
47+
return Utils::uint_rand(0, open_upper_bound);
48+
}
49+
3950
void Utils::sleep(unsigned int milliseconds) {
4051
this_thread::sleep_for(chrono::milliseconds(milliseconds));
4152
}

‎src/commons/Utils.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class Utils {
5252

5353
static void error(string msg, bool throw_flag = true);
5454
static bool flip_coin(double true_probability = 0.5);
55+
static unsigned int uint_rand(unsigned int open_upper_bound);
56+
static unsigned int uint_rand(unsigned int closed_lower_bound, unsigned int open_upper_bound);
5557
static void sleep(unsigned int milliseconds = 100);
5658
static string get_environment(string const& key);
5759
static map<string, string> parse_config(string const& config_path);

‎src/commons/processor/BUILD‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ package(default_visibility = ["//visibility:public"])
55
cc_library(
66
name = "processor_lib",
77
srcs = [
8+
"DedicatedThread.cc",
89
"Processor.cc",
10+
"ThreadPool.cc",
911
],
1012
hdrs = [
13+
"DedicatedThread.h",
1114
"Processor.h",
15+
"ThreadPool.h",
1216
],
1317
includes = ["."],
1418
deps = [
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#ifndef LOG_LEVEL
2+
#define LOG_LEVEL INFO_LEVEL
3+
#endif
4+
#include "DedicatedThread.h"
5+
6+
#include "Logger.h"
7+
#include "Utils.h"
8+
9+
using namespace processor;
10+
using namespace commons;
11+
12+
// -------------------------------------------------------------------------------------------------
13+
// Public methods
14+
15+
DedicatedThread::DedicatedThread(const string& id, ThreadMethod* job) : Processor(id) {
16+
this->job = job;
17+
this->start_flag = false;
18+
this->stop_flag = false;
19+
}
20+
21+
DedicatedThread::~DedicatedThread() {}
22+
23+
void DedicatedThread::setup() {
24+
this->thread_object = new thread(&DedicatedThread::thread_method, this);
25+
Processor::setup();
26+
}
27+
28+
void DedicatedThread::start() {
29+
if (is_setup()) {
30+
this->api_mutex.lock();
31+
this->start_flag = true;
32+
this->api_mutex.unlock();
33+
}
34+
Processor::start();
35+
}
36+
37+
void DedicatedThread::stop() {
38+
this->api_mutex.lock();
39+
this->stop_flag = true;
40+
this->api_mutex.unlock();
41+
Processor::stop();
42+
LOG_DEBUG("Joining DedicatedThread " + this->to_string() + "...");
43+
this->thread_object->join();
44+
LOG_DEBUG("Joined DedicatedThread " + this->to_string() ". Deleting thread object.");
45+
delete this->thread_object;
46+
}
47+
48+
// -------------------------------------------------------------------------------------------------
49+
// Private methods
50+
51+
bool DedicatedThread::started() {
52+
lock_guard<mutex> semaphore(this->api_mutex);
53+
return this->start_flag;
54+
}
55+
56+
bool DedicatedThread::stopped() {
57+
lock_guard<mutex> semaphore(this->api_mutex);
58+
return this->stop_flag;
59+
}
60+
61+
void DedicatedThread::thread_method() {
62+
while (!started()) {
63+
Utils::sleep();
64+
}
65+
do {
66+
if (!this->job->thread_one_step()) {
67+
Utils::sleep();
68+
};
69+
} while (!stopped());
70+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
3+
#include <mutex>
4+
#include <thread>
5+
6+
#include "Processor.h"
7+
8+
using namespace std;
9+
10+
namespace processor {
11+
12+
class ThreadMethod {
13+
public:
14+
virtual bool thread_one_step() = 0;
15+
};
16+
17+
/**
18+
*
19+
*/
20+
class DedicatedThread : public Processor {
21+
public:
22+
DedicatedThread(const string& id, ThreadMethod* job);
23+
virtual ~DedicatedThread();
24+
25+
virtual void setup();
26+
virtual void start();
27+
virtual void stop();
28+
29+
private:
30+
void thread_method();
31+
bool inline started();
32+
bool inline stopped();
33+
34+
ThreadMethod* job;
35+
thread* thread_object;
36+
bool start_flag;
37+
bool stop_flag;
38+
mutex api_mutex;
39+
};
40+
41+
} // namespace processor

‎src/commons/processor/Processor.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Processor {
1616
enum State { UNDEFINED = 0, WAITING_SETUP, WAITING_START, WAITING_STOP, FINISHED };
1717

1818
Processor(const string& id);
19-
~Processor();
19+
virtual ~Processor();
2020

2121
static void bind_subprocessor(shared_ptr<Processor> root, shared_ptr<Processor> child);
2222

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include "ThreadPool.h"
2+
3+
#include "Utils.h"
4+
5+
using namespace processor;
6+
using namespace commons;
7+
8+
// -------------------------------------------------------------------------------------------------
9+
// Public methods
10+
11+
ThreadPool::ThreadPool(const string& id, unsigned int num_threads) : Processor(id) {
12+
this->num_threads = num_threads;
13+
this->active_tasks = 0;
14+
}
15+
16+
ThreadPool::~ThreadPool() {}
17+
18+
void ThreadPool::setup() { Processor::setup(); }
19+
20+
void ThreadPool::start() {
21+
for (unsigned int i = 0; i < this->num_threads; ++i) {
22+
this->workers.emplace_back([this, i] {
23+
while (true) {
24+
function<void()> task;
25+
{
26+
unique_lock<mutex> lock(this->queue_mutex);
27+
this->condition.wait(lock,
28+
[this] { return this->stop_flag || !this->tasks.empty(); });
29+
if (this->stop_flag && this->tasks.empty()) return;
30+
31+
task = move(this->tasks.front());
32+
this->tasks.pop();
33+
++this->active_tasks;
34+
}
35+
task();
36+
{
37+
unique_lock<mutex> lock(this->queue_mutex);
38+
--this->active_tasks;
39+
if (this->tasks.empty() && this->active_tasks == 0) {
40+
this->done_condition.notify_all();
41+
}
42+
}
43+
}
44+
});
45+
}
46+
Processor::start();
47+
}
48+
49+
void ThreadPool::stop() {
50+
{
51+
unique_lock<mutex> lock(queue_mutex);
52+
this->stop_flag = true;
53+
}
54+
condition.notify_all();
55+
for (thread& worker : workers) worker.join();
56+
Processor::stop();
57+
}
58+
59+
void ThreadPool::enqueue(function<void()> task) {
60+
if (is_running()) {
61+
{
62+
unique_lock<mutex> lock(queue_mutex);
63+
tasks.emplace(move(task));
64+
}
65+
condition.notify_one();
66+
} else {
67+
Utils::error("Attempt to add a job to ThreadPool " + this->to_string() +
68+
" which has not being started");
69+
}
70+
}
71+
72+
int ThreadPool::size() {
73+
unique_lock<mutex> lock(queue_mutex);
74+
return tasks.size();
75+
}
76+
77+
bool ThreadPool::empty() {
78+
unique_lock<mutex> lock(queue_mutex);
79+
return tasks.empty();
80+
}
81+
82+
void ThreadPool::wait() {
83+
unique_lock<mutex> lock(queue_mutex);
84+
done_condition.wait(lock, [this] { return tasks.empty() && active_tasks == 0; });
85+
}

‎src/commons/processor/ThreadPool.h‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#pragma once
2+
3+
#include <condition_variable>
4+
#include <functional>
5+
#include <future>
6+
#include <iostream>
7+
#include <memory>
8+
#include <mutex>
9+
#include <queue>
10+
#include <stdexcept>
11+
#include <thread>
12+
#include <vector>
13+
14+
#include "Processor.h"
15+
16+
using namespace std;
17+
18+
namespace processor {
19+
20+
/**
21+
*
22+
*/
23+
class ThreadPool : public Processor {
24+
public:
25+
ThreadPool(const string& id, unsigned int num_threads);
26+
virtual ~ThreadPool();
27+
28+
virtual void setup();
29+
virtual void start();
30+
virtual void stop();
31+
32+
void enqueue(function<void()> task);
33+
int size();
34+
bool empty();
35+
void wait();
36+
37+
private:
38+
unsigned int num_threads;
39+
40+
vector<thread> workers;
41+
queue<function<void()>> tasks;
42+
mutex queue_mutex;
43+
condition_variable condition;
44+
condition_variable done_condition;
45+
unsigned int active_tasks;
46+
bool stop_flag = false;
47+
};
48+
49+
} // namespace processor

0 commit comments

Comments
 (0)