X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/5fd8cc8409d0efadc07dfe8d6774ad9ff477663d..130f2d53438eb6193accb445aca52fa8e2fe4158:/rpc/pollmgr.cc?ds=sidebyside diff --git a/rpc/pollmgr.cc b/rpc/pollmgr.cc index f73a3a5..33aeae2 100644 --- a/rpc/pollmgr.cc +++ b/rpc/pollmgr.cc @@ -1,16 +1,14 @@ -#include #include #include #include -#include "slock.h" #include "jsl_log.h" -#include "method_thread.h" #include "lang/verify.h" #include "pollmgr.h" +#include "lock.h" PollMgr *PollMgr::instance = NULL; -static pthread_once_t pollmgr_is_initialized = PTHREAD_ONCE_INIT; +static std::once_flag pollmgr_is_initialized; void PollMgrInit() @@ -21,7 +19,7 @@ PollMgrInit() PollMgr * PollMgr::Instance() { - pthread_once(&pollmgr_is_initialized, PollMgrInit); + std::call_once(pollmgr_is_initialized, PollMgrInit); return instance; } @@ -31,9 +29,7 @@ PollMgr::PollMgr() : pending_change_(false) aio_ = new SelectAIO(); //aio_ = new EPollAIO(); - VERIFY(pthread_mutex_init(&m_, NULL) == 0); - VERIFY(pthread_cond_init(&changedone_c_, NULL) == 0); - VERIFY((th_ = method_thread(this, false, &PollMgr::wait_loop)) != 0); + th_ = std::thread(&PollMgr::wait_loop, this); } PollMgr::~PollMgr() @@ -47,7 +43,7 @@ PollMgr::add_callback(int fd, poll_flag flag, aio_callback *ch) { VERIFY(fd < MAX_POLL_FDS); - ScopedLock ml(&m_); + lock ml(m_); aio_->watch_fd(fd, flag); VERIFY(!callbacks_[fd] || callbacks_[fd]==ch); @@ -60,17 +56,17 @@ PollMgr::add_callback(int fd, poll_flag flag, aio_callback *ch) void PollMgr::block_remove_fd(int fd) { - ScopedLock ml(&m_); + lock ml(m_); aio_->unwatch_fd(fd, CB_RDWR); pending_change_ = true; - VERIFY(pthread_cond_wait(&changedone_c_, &m_)==0); + changedone_c_.wait(ml); callbacks_[fd] = NULL; } void PollMgr::del_callback(int fd, poll_flag flag) { - ScopedLock ml(&m_); + lock ml(m_); if (aio_->unwatch_fd(fd, flag)) { callbacks_[fd] = NULL; } @@ -79,7 +75,7 @@ PollMgr::del_callback(int fd, poll_flag flag) bool PollMgr::has_callback(int fd, poll_flag flag, aio_callback *c) { - ScopedLock ml(&m_); + lock ml(m_); if (!callbacks_[fd] || callbacks_[fd]!=c) return false; @@ -95,10 +91,10 @@ PollMgr::wait_loop() while (1) { { - ScopedLock ml(&m_); + lock ml(m_); if (pending_change_) { pending_change_ = false; - VERIFY(pthread_cond_broadcast(&changedone_c_)==0); + changedone_c_.notify_all(); } } readable.clear(); @@ -137,19 +133,16 @@ SelectAIO::SelectAIO() : highfds_(0) int flags = fcntl(pipefd_[0], F_GETFL, NULL); flags |= O_NONBLOCK; fcntl(pipefd_[0], F_SETFL, flags); - - VERIFY(pthread_mutex_init(&m_, NULL) == 0); } SelectAIO::~SelectAIO() { - VERIFY(pthread_mutex_destroy(&m_) == 0); } void SelectAIO::watch_fd(int fd, poll_flag flag) { - ScopedLock ml(&m_); + lock ml(m_); if (highfds_ <= fd) highfds_ = fd; @@ -169,7 +162,7 @@ SelectAIO::watch_fd(int fd, poll_flag flag) bool SelectAIO::is_watched(int fd, poll_flag flag) { - ScopedLock ml(&m_); + lock ml(m_); if (flag == CB_RDONLY) { return FD_ISSET(fd,&rfds_); }else if (flag == CB_WRONLY) { @@ -182,7 +175,7 @@ SelectAIO::is_watched(int fd, poll_flag flag) bool SelectAIO::unwatch_fd(int fd, poll_flag flag) { - ScopedLock ml(&m_); + lock ml(m_); if (flag == CB_RDONLY) { FD_CLR(fd, &rfds_); }else if (flag == CB_WRONLY) { @@ -221,11 +214,10 @@ SelectAIO::wait_ready(std::vector *readable, std::vector *writable) int high; { - ScopedLock ml(&m_); + lock ml(m_); trfds = rfds_; twfds = wfds_; high = highfds_; - } int ret = select(high+1, &trfds, &twfds, NULL, NULL);