-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathThreadsManager.cpp
More file actions
83 lines (69 loc) · 2.19 KB
/
ThreadsManager.cpp
File metadata and controls
83 lines (69 loc) · 2.19 KB
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
#include "ThreadsManager.h"
#include <stdexcept>
#include <string>
#include <map>
#include <memory>
#include <thread>
#include <sstream>
extern "C" {
void async_threads_execute_fcall(const int index_id);
}
using namespace std;
namespace AWI
{
FortranCallback::FortranCallback(const int index_id_)
: index_id(index_id_)
{
}
void FortranCallback::executeCallback()
{
int idx = index_id;
async_threads_execute_fcall(idx);
}
void ThreadsManager::addThread(const int index_id)
{
string name = std::to_string(index_id); // todo: we do not seem to need a string here, use int in the map
map<string, unique_ptr<FortranCallback> >::iterator it = callbacks.find(name);
if(it != callbacks.end())
{
std::stringstream exceptionMessage;
exceptionMessage << __FILE__ << ":" << __LINE__ <<" thread already exists: "<<name;
throw std::runtime_error(exceptionMessage.str());
}
callbacks[name] = unique_ptr<FortranCallback>(new FortranCallback(index_id));
}
void ThreadsManager::begin(const int index_id)
{
string name = std::to_string(index_id);
map<string, unique_ptr<FortranCallback> >::iterator it = callbacks.find(name);
if(it != callbacks.end())
{
unique_ptr<FortranCallback> &w = it->second;
worker_threads[name] = new thread(&FortranCallback::executeCallback, w.get());
}
else
{
std::stringstream exceptionMessage;
exceptionMessage << __FILE__ << ":" << __LINE__ <<" unknown thread: "<<name;
throw std::runtime_error(exceptionMessage.str());
}
}
void ThreadsManager::end(const int index_id)
{
string name = std::to_string(index_id);
map<string, thread*>::iterator it = worker_threads.find(name);
if(it != worker_threads.end())
{
thread *t = it->second;
t->join();
worker_threads.erase(it);
delete t;
}
else
{
std::stringstream exceptionMessage;
exceptionMessage << __FILE__ << ":" << __LINE__ <<" unknown thread: "<<name;
throw std::runtime_error(exceptionMessage.str());
}
}
}