-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[backend] implement pthread backend (#46)
* [backend] init pthread pool * [pthread] make it compilable * [pthread] pass callback f ptr * [pthread] sync api with other backends * [pthread] fetch environ * [pthread] implement sync * [pthread] fix * [pthread] debug hang * [pthread] use c++ 3rd lib thread pool * [pthread] pass test * [chore] remove unintended commit * [chore] refine * [chore] license
- Loading branch information
Showing
8 changed files
with
1,351 additions
and
6 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
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,79 @@ | ||
#include "pthread_backend.h" | ||
|
||
void PthreadAsyncIO::write(int fd, void *buffer, size_t n_bytes, unsigned long long offset, callback_t callback) { | ||
auto fut = this->pool.submit_task( | ||
[fd, buffer, n_bytes, offset] { | ||
return pwrite(fd, buffer, n_bytes, offset); | ||
} | ||
); | ||
this->write_fut.push_back(std::make_tuple(std::move(fut), callback)); | ||
} | ||
|
||
void PthreadAsyncIO::writev(int fd, const iovec *iov, unsigned int iovcnt, unsigned long long offset, callback_t callback) { | ||
auto fut = this->pool.submit_task( | ||
[fd, iov, iovcnt, offset] { | ||
return pwritev(fd, iov, iovcnt, offset); | ||
} | ||
); | ||
this->write_fut.push_back(std::make_tuple(std::move(fut), callback)); | ||
} | ||
|
||
void PthreadAsyncIO::read(int fd, void *buffer, size_t n_bytes, unsigned long long offset, callback_t callback) { | ||
auto fut = this->pool.submit_task( | ||
[fd, buffer, n_bytes, offset] { | ||
return pread(fd, buffer, n_bytes, offset); | ||
} | ||
); | ||
this->read_fut.push_back(std::make_tuple(std::move(fut), callback)); | ||
} | ||
|
||
void PthreadAsyncIO::readv(int fd, const iovec *iov, unsigned int iovcnt, unsigned long long offset, callback_t callback) { | ||
auto fut = this->pool.submit_task( | ||
[fd, iov, iovcnt, offset] { | ||
return preadv(fd, iov, iovcnt, offset); | ||
} | ||
); | ||
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(); | ||
|
||
auto fut(std::move(std::get<0>(front))); | ||
fut.wait(); | ||
|
||
auto callback = std::get<1>(front); | ||
if (callback != nullptr) { | ||
callback(); | ||
} | ||
} | ||
} | ||
|
||
void PthreadAsyncIO::sync_read_events() { | ||
while (this->read_fut.size() > 0) { | ||
auto front = std::move(this->read_fut.front()); | ||
this->read_fut.pop_front(); | ||
|
||
auto fut(std::move(std::get<0>(front))); | ||
fut.wait(); | ||
|
||
auto callback = std::get<1>(front); | ||
if (callback != nullptr) { | ||
callback(); | ||
} | ||
} | ||
} | ||
|
||
void PthreadAsyncIO::synchronize() { | ||
this->get_event(WAIT); | ||
} | ||
|
||
void PthreadAsyncIO::register_file(int fd) {} |
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
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,41 @@ | ||
#pragma once | ||
|
||
#include <stdexcept> | ||
#include <sys/io.h> | ||
#include <sys/uio.h> | ||
#include <unistd.h> | ||
#include <cstdlib> | ||
#include <future> | ||
#include <queue> | ||
#include <tuple> | ||
#include <functional> | ||
|
||
#include "asyncio.h" | ||
#include "threadpool.hpp" | ||
|
||
|
||
class PthreadAsyncIO : public AsyncIO | ||
{ | ||
private: | ||
BS::thread_pool pool; | ||
std::deque<std::tuple<std::future<ssize_t>, callback_t>> write_fut; | ||
std::deque<std::tuple<std::future<ssize_t>, callback_t>> read_fut; | ||
|
||
public: | ||
PthreadAsyncIO(unsigned int n_entries) | ||
: pool(n_entries) {} | ||
|
||
~PthreadAsyncIO() {} | ||
|
||
void write(int fd, void *buffer, size_t n_bytes, unsigned long long offset, callback_t callback); | ||
void read(int fd, void *buffer, size_t n_bytes, unsigned long long offset, callback_t callback); | ||
void writev(int fd, const iovec *iov, unsigned int iovcnt, unsigned long long offset, callback_t callback); | ||
void readv(int fd, const iovec *iov, unsigned int iovcnt, unsigned long long offset, callback_t callback); | ||
|
||
void get_event(WaitType wt); | ||
void sync_write_events(); | ||
void sync_read_events(); | ||
void synchronize(); | ||
|
||
void register_file(int fd); | ||
}; |
Oops, something went wrong.