-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrejit_work_offloader.cpp
93 lines (75 loc) · 2.07 KB
/
rejit_work_offloader.cpp
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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
#include "rejit_work_offloader.h"
#include "logger.h"
namespace trace
{
//
// RejitWorkItem
//
RejitWorkItem::RejitWorkItem() : terminating(true), func(nullptr)
{
}
RejitWorkItem::RejitWorkItem(std::function<void()>&& func)
: terminating(false), func(std::forward<std::function<void()>>(func))
{
}
std::unique_ptr<RejitWorkItem> RejitWorkItem::CreateTerminatingWorkItem()
{
return std::make_unique<RejitWorkItem>();
}
//
// RejitWorkOffloader
//
RejitWorkOffloader::RejitWorkOffloader(ICorProfilerInfo7* pInfo)
{
m_profilerInfo = pInfo;
m_offloader_queue = std::make_unique<UniqueBlockingQueue<RejitWorkItem>>();
m_offloader_queue_thread = std::make_unique<std::thread>(EnqueueThreadLoop, this);
}
void RejitWorkOffloader::Enqueue(std::unique_ptr<RejitWorkItem>&& item)
{
m_offloader_queue->push(std::move(item));
}
bool RejitWorkOffloader::WaitForTermination()
{
if (m_offloader_queue_thread->joinable())
{
m_offloader_queue_thread->join();
return true;
}
return false;
}
void RejitWorkOffloader::EnqueueThreadLoop(RejitWorkOffloader* offloader)
{
auto queue = offloader->m_offloader_queue.get();
auto profilerInfo = offloader->m_profilerInfo;
Logger::Info("Initializing ReJIT request thread.");
HRESULT hr = profilerInfo->InitializeCurrentThread();
if (FAILED(hr))
{
Logger::Warn("Call to InitializeCurrentThread fail.");
}
while (true)
{
const auto item = queue->pop();
if (item->terminating)
{
// *************************************
// Exit ReJIT thread
// *************************************
break;
}
else if (item->func != nullptr)
{
// *************************************
// Execute given work
// *************************************
item->func();
}
}
Logger::Info("Exiting ReJIT request thread.");
}
} // namespace trace