Skip to content

Commit

Permalink
[chore] refine
Browse files Browse the repository at this point in the history
  • Loading branch information
botbw committed Sep 23, 2024
1 parent 2703e93 commit 186e139
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
6 changes: 4 additions & 2 deletions csrc/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ void probe_asyncio(const std::string &backend)
#else
throw std::runtime_error("backend aio is not installed\n");
#endif
} else {
} else if (backend == "pthread") {
#ifndef DISABLE_PTHREAD
aio.reset(new PthreadAsyncIO(2));
#else
throw std::runtime_error("backend pthread is not installed\n");
#endif
} else {
throw std::runtime_error("unknown backend");
}

int fd = fileno(fp);
Expand Down Expand Up @@ -88,7 +90,7 @@ void probe_asyncio(const std::string &backend)
for (int i = 0; i < n_loop; i++)
{
for (int j = 0; j < n_len; j++)
{
{
assert(text[i][j] == new_text[i][j]);
}
}
Expand Down
10 changes: 4 additions & 6 deletions csrc/pthread_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,21 @@ void PthreadAsyncIO::readv(int fd, const iovec *iov, unsigned int iovcnt, unsign
this->read_fut.push_back(std::make_tuple(std::move(fut), callback));
}


void PthreadAsyncIO::get_event(WaitType wt) {
if (wt == NOWAIT) return;
this->sync_write_events();
this->sync_read_events();

}

void PthreadAsyncIO::sync_write_events() {
while (this->write_fut.size() > 0) {
auto front = std::move(this->write_fut.front());
this->write_fut.pop_front();

std::future<ssize_t> fut(std::move(std::get<0>(front)));
auto fut(std::move(std::get<0>(front)));
fut.wait();

callback_t callback = std::get<1>(front);
auto callback = std::get<1>(front);
if (callback != nullptr) {
callback();
}
Expand All @@ -64,10 +62,10 @@ void PthreadAsyncIO::sync_read_events() {
auto front = std::move(this->read_fut.front());
this->read_fut.pop_front();

std::future<ssize_t> fut(std::move(std::get<0>(front)));
auto fut(std::move(std::get<0>(front)));
fut.wait();

callback_t callback = std::get<1>(front);
auto callback = std::get<1>(front);
if (callback != nullptr) {
callback();
}
Expand Down
2 changes: 1 addition & 1 deletion include/asyncio.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using callback_t = std::function<void()>;
enum IOType
{
WRITE,
READ,
READ
};

enum WaitType
Expand Down

0 comments on commit 186e139

Please sign in to comment.