-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
256 additions
and
10 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
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
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,75 @@ | ||
#include "poll_sync_opt.h" | ||
#include "poller.h" | ||
|
||
#include <sys/eventfd.h> | ||
|
||
poll_sync_opt::~poll_sync_opt() { | ||
if (this->efd != -1) | ||
::close(this->efd); | ||
|
||
if (this->readq != nullptr) | ||
delete this->readq; | ||
if (this->writeq != nullptr) | ||
delete this->writeq; | ||
} | ||
void poll_sync_opt::init(poll_sync_opt::opt_arg &&arg) { | ||
this->do_sync(arg); | ||
} | ||
int poll_sync_opt::open(poller *poll) { | ||
int fd = ::eventfd(0, EFD_NONBLOCK|EFD_CLOEXEC); | ||
if (fd == -1) { | ||
fprintf(stderr, "reactor: create eventfd fail! %s\n", strerror(errno)); | ||
return -1; | ||
} | ||
if (poll->add(this, fd, ev_handler::ev_read) == -1) { | ||
fprintf(stderr, "reactor: add eventfd to poller fail! %s\n", strerror(errno)); | ||
::close(fd); | ||
return -1; | ||
} | ||
this->efd = fd; | ||
return 0; | ||
} | ||
void poll_sync_opt::push(poll_sync_opt::opt_arg &&arg) { | ||
this->mtx.lock(); | ||
this->writeq->push_back(std::move(arg)); | ||
this->mtx.unlock(); | ||
|
||
int expected = 0; | ||
if (!this->notified.compare_exchange_strong(expected, 1)) | ||
return ; | ||
int64_t v = 1; | ||
int ret = 0; | ||
do { | ||
ret = ::write(this->efd, (void *)&v, sizeof(v)); | ||
} while (ret == -1 && errno == EINTR); | ||
} | ||
void poll_sync_opt::do_sync(const poll_sync_opt::opt_arg &arg) { | ||
if (arg.type == poll_sync_opt::sync_cache_t) { | ||
auto p = (poll_sync_opt::sync_cache *)arg.arg; | ||
this->poll->poll_cache_set(p->id, p->value, p->free_func); | ||
delete p; | ||
} | ||
} | ||
bool poll_sync_opt::on_read() { | ||
if (this->readq->empty()) { | ||
this->mtx.lock(); | ||
std::swap(this->readq, this->writeq); | ||
this->mtx.unlock(); | ||
} | ||
for (auto i = 0; i < 8 && !this->readq->empty(); ++i) { | ||
poll_sync_opt::opt_arg &arg = this->readq->front(); | ||
this->do_sync(arg); | ||
this->readq->pop_front(); | ||
} | ||
if (!this->readq->empty()) // Ignore readable eventfd, continue | ||
return true; | ||
|
||
int64_t v = 0; | ||
int ret = 0; | ||
do { | ||
ret = ::read(this->efd, (void *)&v, sizeof(v)); | ||
} while (ret == -1 && errno == EINTR); | ||
|
||
this->notified.store(0); | ||
return true; | ||
} |
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,68 @@ | ||
#ifndef POLL_SYNC_OPT_H_ | ||
#define POLL_SYNC_OPT_H_ | ||
|
||
#include "ev_handler.h" | ||
#include "ringq.h" | ||
|
||
#include <mutex> | ||
#include <atomic> | ||
|
||
// Forward declarations | ||
class poller; | ||
|
||
class poll_sync_opt : public ev_handler { | ||
public: | ||
enum { | ||
sync_cache_t = 1, | ||
}; | ||
class sync_cache { | ||
public: | ||
sync_cache() = default; | ||
int id = 0; | ||
void *value = nullptr; | ||
void (*free_func)(void *) = nullptr; // 负责释放上次一的value | ||
}; | ||
|
||
class opt_arg { | ||
public: | ||
opt_arg() = default; | ||
opt_arg(const int t, void *a) : type(t), arg(a) { } | ||
opt_arg& operator=(opt_arg &&v) { | ||
this->type = v.type; | ||
this->arg = v.arg; | ||
return *this; | ||
} | ||
opt_arg& operator=(const opt_arg &v) { | ||
this->type = v.type; | ||
this->arg = v.arg; | ||
return *this; | ||
} | ||
|
||
int type = 0; | ||
void *arg = nullptr; | ||
}; | ||
|
||
poll_sync_opt() = delete; | ||
poll_sync_opt(const int init_size) { | ||
this->readq = new ringq<poll_sync_opt::opt_arg>(init_size); | ||
this->writeq = new ringq<poll_sync_opt::opt_arg>(init_size); | ||
} | ||
~poll_sync_opt(); | ||
|
||
int open(poller *); | ||
|
||
virtual bool on_read(); | ||
|
||
void init(poll_sync_opt::opt_arg &&arg); | ||
void push(poll_sync_opt::opt_arg &&arg); | ||
private: | ||
void do_sync(const poll_sync_opt::opt_arg &arg); | ||
|
||
int efd = -1; | ||
std::mutex mtx; | ||
std::atomic<int> notified; | ||
ringq<poll_sync_opt::opt_arg> *readq = nullptr; | ||
ringq<poll_sync_opt::opt_arg> *writeq = nullptr; | ||
}; | ||
|
||
#endif // POLL_SYNC_OPT_H_ |
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
Oops, something went wrong.