-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
POSIX asynchronous I/O is very sub-optimal. They create threads, dynamically allocate memory, and invoke a lot of system calls (which set the signal mask, priority, and more). Additional locks are also possible (if the AIO is using someone else). Moreover, they are not portable.
- Loading branch information
1 parent
137992f
commit bea4aaf
Showing
4 changed files
with
205 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
|
||
#pragma once | ||
#include <condition_variable> | ||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <memory> | ||
#include <mutex> | ||
|
||
#include <unistd.h> | ||
|
||
#include "Config.h" | ||
#include "Portability.h" | ||
|
||
class DoubleBuffer | ||
{ | ||
struct Deleter { | ||
void operator()(char *__ptr) const { free(__ptr); } | ||
}; | ||
|
||
using MemPtrT = std::unique_ptr<char, Deleter>; | ||
|
||
static MemPtrT allocateMemory() | ||
{ | ||
char *tmp; | ||
int err = posix_memalign(reinterpret_cast<void **>(&tmp), 512, | ||
NanoLogConfig::OUTPUT_BUFFER_SIZE); | ||
if (err) { | ||
perror( | ||
"The NanoLog system was not able to allocate enough memory " | ||
"to support its operations. Quitting...\r\n"); | ||
std::exit(-1); | ||
} | ||
return MemPtrT{tmp}; | ||
}; | ||
|
||
static char *accessOnce(const MemPtrT &ptr) | ||
{ | ||
NANOLOG_READ_WRITE_BARRIER; | ||
return ptr.get(); | ||
} | ||
|
||
MemPtrT freeBuffer; | ||
MemPtrT compressingBuffer; | ||
MemPtrT writeBuffer; | ||
unsigned size; | ||
int errorCode; | ||
|
||
std::condition_variable condition; | ||
std::mutex mutex; | ||
|
||
public: | ||
DoubleBuffer() | ||
: freeBuffer(allocateMemory()), | ||
compressingBuffer(allocateMemory()), | ||
writeBuffer(nullptr), | ||
size(0), | ||
errorCode(0), | ||
condition(), | ||
mutex(){}; | ||
|
||
char *getCompressingBuffer() noexcept { return compressingBuffer.get(); } | ||
|
||
bool writeInProgress() const { return accessOnce(freeBuffer) == nullptr; } | ||
|
||
int swapBuffer(unsigned count) noexcept | ||
{ | ||
while (accessOnce(writeBuffer) != nullptr) {} | ||
|
||
{ | ||
std::unique_lock<std::mutex> lock(mutex); | ||
size = count; | ||
std::swap(writeBuffer, compressingBuffer); | ||
if (freeBuffer == nullptr) { | ||
condition.wait(lock, [this]() { return freeBuffer != nullptr; }); | ||
} else { | ||
condition.notify_one(); | ||
} | ||
std::swap(freeBuffer, compressingBuffer); | ||
return errorCode; | ||
} | ||
} | ||
|
||
void writeToFile(int file) noexcept | ||
{ | ||
unsigned tmp_size = 0; | ||
MemPtrT tmp_ptr = nullptr; | ||
|
||
{ | ||
std::unique_lock<std::mutex> lock(mutex); | ||
condition.wait(lock, [this]() { return writeBuffer != nullptr; }); | ||
tmp_size = size; | ||
std::swap(writeBuffer, tmp_ptr); | ||
} | ||
|
||
int res = 0; | ||
if (tmp_size != 0) { | ||
res = write(file, tmp_ptr.get(), tmp_size); | ||
res = (res < 0) ? errno : 0; | ||
} | ||
|
||
while (accessOnce(freeBuffer) != nullptr) {} | ||
|
||
{ | ||
std::unique_lock<std::mutex> lock(mutex); | ||
errorCode = res; | ||
std::swap(freeBuffer, tmp_ptr); | ||
if (compressingBuffer == nullptr) { | ||
condition.notify_one(); | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.