More cleaning
[invirt/third/libt4.git] / rpc / pollmgr.cc
index f73a3a5..023a7aa 100644 (file)
@@ -1,18 +1,15 @@
-#include <sys/time.h>
+#include "types.h"
 #include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
 
-#include "slock.h"
 #include "jsl_log.h"
-#include "method_thread.h"
-#include "lang/verify.h"
 #include "pollmgr.h"
 
 PollMgr *PollMgr::instance = NULL;
-static pthread_once_t pollmgr_is_initialized = PTHREAD_ONCE_INIT;
+static std::once_flag pollmgr_is_initialized;
 
-void
+static void
 PollMgrInit()
 {
        PollMgr::instance = new PollMgr();
@@ -21,7 +18,7 @@ PollMgrInit()
 PollMgr *
 PollMgr::Instance()
 {
-       pthread_once(&pollmgr_is_initialized, PollMgrInit);
+    std::call_once(pollmgr_is_initialized, PollMgrInit);
        return instance;
 }
 
@@ -31,12 +28,10 @@ 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()
+PollMgr::~PollMgr() [[noreturn]]
 {
        //never kill me!!!
        VERIFY(0);
@@ -47,7 +42,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 +55,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 +74,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;
 
@@ -87,7 +82,7 @@ PollMgr::has_callback(int fd, poll_flag flag, aio_callback *c)
 }
 
 void
-PollMgr::wait_loop()
+PollMgr::wait_loop() [[noreturn]]
 {
 
        std::vector<int> readable;
@@ -95,10 +90,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 +132,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 +161,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 +174,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 +213,10 @@ SelectAIO::wait_ready(std::vector<int> *readable, std::vector<int> *writable)
        int high;
 
        {
-               ScopedLock ml(&m_);
+        lock ml(m_);
                trfds = rfds_;
                twfds = wfds_;
                high = highfds_;
-
        }
 
        int ret = select(high+1, &trfds, &twfds, NULL, NULL);