Variadic templates for RPCs
[invirt/third/libt4.git] / rpc / pollmgr.cc
index f73a3a5..33aeae2 100644 (file)
@@ -1,16 +1,14 @@
-#include <sys/time.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
 
 #include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
 
-#include "slock.h"
 #include "jsl_log.h"
 #include "jsl_log.h"
-#include "method_thread.h"
 #include "lang/verify.h"
 #include "pollmgr.h"
 #include "lang/verify.h"
 #include "pollmgr.h"
+#include "lock.h"
 
 PollMgr *PollMgr::instance = NULL;
 
 PollMgr *PollMgr::instance = NULL;
-static pthread_once_t pollmgr_is_initialized = PTHREAD_ONCE_INIT;
+static std::once_flag pollmgr_is_initialized;
 
 void
 PollMgrInit()
 
 void
 PollMgrInit()
@@ -21,7 +19,7 @@ PollMgrInit()
 PollMgr *
 PollMgr::Instance()
 {
 PollMgr *
 PollMgr::Instance()
 {
-       pthread_once(&pollmgr_is_initialized, PollMgrInit);
+    std::call_once(pollmgr_is_initialized, PollMgrInit);
        return instance;
 }
 
        return instance;
 }
 
@@ -31,9 +29,7 @@ PollMgr::PollMgr() : pending_change_(false)
        aio_ = new SelectAIO();
        //aio_ = new EPollAIO();
 
        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()
@@ -47,7 +43,7 @@ PollMgr::add_callback(int fd, poll_flag flag, aio_callback *ch)
 {
        VERIFY(fd < MAX_POLL_FDS);
 
 {
        VERIFY(fd < MAX_POLL_FDS);
 
-       ScopedLock ml(&m_);
+    lock ml(m_);
        aio_->watch_fd(fd, flag);
 
        VERIFY(!callbacks_[fd] || callbacks_[fd]==ch);
        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)
 {
 void
 PollMgr::block_remove_fd(int fd)
 {
-       ScopedLock ml(&m_);
+    lock ml(m_);
        aio_->unwatch_fd(fd, CB_RDWR);
        pending_change_ = true;
        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)
 {
        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;
        }
        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)
 {
 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;
 
        if (!callbacks_[fd] || callbacks_[fd]!=c)
                return false;
 
@@ -95,10 +91,10 @@ PollMgr::wait_loop()
 
        while (1) {
                {
 
        while (1) {
                {
-                       ScopedLock ml(&m_);
+            lock ml(m_);
                        if (pending_change_) {
                                pending_change_ = false;
                        if (pending_change_) {
                                pending_change_ = false;
-                               VERIFY(pthread_cond_broadcast(&changedone_c_)==0);
+                changedone_c_.notify_all();
                        }
                }
                readable.clear();
                        }
                }
                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);
        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()
 {
 }
 
 SelectAIO::~SelectAIO()
 {
-       VERIFY(pthread_mutex_destroy(&m_) == 0);
 }
 
 void
 SelectAIO::watch_fd(int fd, poll_flag flag)
 {
 }
 
 void
 SelectAIO::watch_fd(int fd, poll_flag flag)
 {
-       ScopedLock ml(&m_);
+    lock ml(m_);
        if (highfds_ <= fd) 
                highfds_ = fd;
 
        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)
 {
 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) {
        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)
 {
 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) {
        if (flag == CB_RDONLY) {
                FD_CLR(fd, &rfds_);
        }else if (flag == CB_WRONLY) {
@@ -221,11 +214,10 @@ SelectAIO::wait_ready(std::vector<int> *readable, std::vector<int> *writable)
        int high;
 
        {
        int high;
 
        {
-               ScopedLock ml(&m_);
+        lock ml(m_);
                trfds = rfds_;
                twfds = wfds_;
                high = highfds_;
                trfds = rfds_;
                twfds = wfds_;
                high = highfds_;
-
        }
 
        int ret = select(high+1, &trfds, &twfds, NULL, NULL);
        }
 
        int ret = select(high+1, &trfds, &twfds, NULL, NULL);