Skip to content
This repository was archived by the owner on Apr 24, 2022. It is now read-only.

Commit f4a5396

Browse files
committedJan 29, 2018
Fix data race in mining progress
1 parent de00189 commit f4a5396

File tree

1 file changed

+60
-64
lines changed

1 file changed

+60
-64
lines changed
 

‎libethcore/Farm.h

+60-64
Original file line numberDiff line numberDiff line change
@@ -160,39 +160,39 @@ class Farm: public FarmFace
160160
}
161161
}
162162

163-
void collectHashRate()
164-
{
165-
WorkingProgress p;
166-
Guard l2(x_minerWork);
167-
p.ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - m_lastStart).count();
168-
//Collect
169-
for (auto const& i : m_miners)
170-
{
171-
uint64_t minerHashCount = i->hashCount();
172-
p.hashes += minerHashCount;
173-
p.minersHashes.push_back(minerHashCount);
174-
}
163+
void collectHashRate()
164+
{
165+
auto now = std::chrono::steady_clock::now();
175166

176-
//Reset
177-
for (auto const& i : m_miners)
178-
{
179-
i->resetHashCount();
180-
}
181-
m_lastStart = std::chrono::steady_clock::now();
167+
std::lock_guard<std::mutex> lock(x_minerWork);
182168

183-
if (p.hashes > 0) {
184-
m_lastProgresses.push_back(p);
185-
}
169+
WorkingProgress p;
170+
p.ms = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_lastStart).count();
171+
m_lastStart = now;
186172

187-
// We smooth the hashrate over the last x seconds
188-
int allMs = 0;
189-
for (auto const& cp : m_lastProgresses) {
190-
allMs += cp.ms;
191-
}
192-
if (allMs > m_hashrateSmoothInterval) {
193-
m_lastProgresses.erase(m_lastProgresses.begin());
194-
}
195-
}
173+
// Collect
174+
for (auto const& i : m_miners)
175+
{
176+
uint64_t minerHashCount = i->hashCount();
177+
p.hashes += minerHashCount;
178+
p.minersHashes.push_back(minerHashCount);
179+
}
180+
181+
// Reset
182+
for (auto const& i : m_miners)
183+
i->resetHashCount();
184+
185+
if (p.hashes > 0)
186+
m_lastProgresses.push_back(p);
187+
188+
// We smooth the hashrate over the last x seconds
189+
int allMs = 0;
190+
for (auto const& cp : m_lastProgresses)
191+
allMs += cp.ms;
192+
193+
if (allMs > m_hashrateSmoothInterval)
194+
m_lastProgresses.erase(m_lastProgresses.begin());
195+
}
196196

197197
void processHashRate(const boost::system::error_code& ec) {
198198

@@ -223,37 +223,36 @@ class Farm: public FarmFace
223223
return m_isMining;
224224
}
225225

226-
/**
227-
* @brief Get information on the progress of mining this work package.
228-
* @return The progress with mining so far.
229-
*/
230-
WorkingProgress const& miningProgress(bool hwmon = false) const
231-
{
232-
WorkingProgress p;
233-
p.ms = 0;
234-
p.hashes = 0;
235-
{
236-
Guard l2(x_minerWork);
237-
for (auto const& i : m_miners) {
238-
p.minersHashes.push_back(0);
239-
if (hwmon)
240-
p.minerMonitors.push_back(i->hwmon());
241-
}
242-
}
243-
244-
for (auto const& cp : m_lastProgresses) {
245-
p.ms += cp.ms;
246-
p.hashes += cp.hashes;
247-
for (unsigned int i = 0; i < cp.minersHashes.size(); i++)
248-
{
249-
p.minersHashes.at(i) += cp.minersHashes.at(i);
250-
}
251-
}
252-
253-
Guard l(x_progress);
254-
m_progress = p;
255-
return m_progress;
256-
}
226+
/**
227+
* @brief Get information on the progress of mining this work package.
228+
* @return The progress with mining so far.
229+
*/
230+
WorkingProgress const& miningProgress(bool hwmon = false) const
231+
{
232+
std::lock_guard<std::mutex> lock(x_minerWork);
233+
WorkingProgress p;
234+
p.ms = 0;
235+
p.hashes = 0;
236+
for (auto const& i : m_miners)
237+
{
238+
p.minersHashes.push_back(0);
239+
if (hwmon)
240+
p.minerMonitors.push_back(i->hwmon());
241+
}
242+
243+
for (auto const& cp : m_lastProgresses)
244+
{
245+
p.ms += cp.ms;
246+
p.hashes += cp.hashes;
247+
for (unsigned int i = 0; i < cp.minersHashes.size(); i++)
248+
{
249+
p.minersHashes.at(i) += cp.minersHashes.at(i);
250+
}
251+
}
252+
253+
m_progress = p;
254+
return m_progress;
255+
}
257256

258257
SolutionStats getSolutionStats() {
259258
return m_solutionStats;
@@ -350,11 +349,8 @@ class Farm: public FarmFace
350349

351350
std::atomic<bool> m_isMining = {false};
352351

353-
mutable Mutex x_progress;
354352
mutable WorkingProgress m_progress;
355353

356-
mutable Mutex x_hwmons;
357-
358354
SolutionFound m_onSolutionFound;
359355
MinerRestart m_onMinerRestart;
360356

0 commit comments

Comments
 (0)
This repository has been archived.