forked from cryfs/cryfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomGeneratorThread.cpp
More file actions
33 lines (27 loc) · 1.27 KB
/
RandomGeneratorThread.cpp
File metadata and controls
33 lines (27 loc) · 1.27 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
#include "RandomGeneratorThread.h"
namespace cpputils {
RandomGeneratorThread::RandomGeneratorThread(ThreadsafeRandomDataBuffer *buffer, size_t minSize, size_t maxSize)
: _randomGenerator(),
_buffer(buffer),
_minSize(minSize),
_maxSize(maxSize),
_thread(std::bind(&RandomGeneratorThread::_loopIteration, this), "RandomGeneratorThread") {
ASSERT(_maxSize >= _minSize, "Invalid parameters");
}
void RandomGeneratorThread::start() {
return _thread.start();
}
bool RandomGeneratorThread::_loopIteration() {
_buffer->waitUntilSizeIsLessThan(_minSize);
size_t neededRandomDataSize = _maxSize - _buffer->size();
ASSERT(_maxSize > _buffer->size(), "This could theoretically fail if another thread refilled the buffer. But we should be the only refilling thread.");
Data randomData = _generateRandomData(neededRandomDataSize);
_buffer->add(randomData);
return true; // Run another iteration (don't terminate thread)
}
Data RandomGeneratorThread::_generateRandomData(size_t size) {
Data newRandom(size);
_randomGenerator.GenerateBlock(static_cast<CryptoPP::byte*>(newRandom.data()), size);
return newRandom;
}
}