summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/sys/posix
diff options
context:
space:
mode:
authorStephen D. Huston <shuston@apache.org>2009-02-20 00:04:37 +0000
committerStephen D. Huston <shuston@apache.org>2009-02-20 00:04:37 +0000
commitd4b433f542ab8a506d7dbc53e685770a96ee7958 (patch)
treef0b7d7c643825f279c45f74a8c17f7b0753b53de /cpp/src/qpid/sys/posix
parent44ff0feac9edbf7faffdeed5a3df22313e0543c8 (diff)
downloadqpid-python-d4b433f542ab8a506d7dbc53e685770a96ee7958.tar.gz
Merged win-pollable-condition branch changes 743545:746056 into trunk
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@746061 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src/qpid/sys/posix')
-rw-r--r--cpp/src/qpid/sys/posix/PollableCondition.cpp119
-rw-r--r--cpp/src/qpid/sys/posix/PollableCondition.h56
2 files changed, 105 insertions, 70 deletions
diff --git a/cpp/src/qpid/sys/posix/PollableCondition.cpp b/cpp/src/qpid/sys/posix/PollableCondition.cpp
index 0c55fd3c0d..0991e5fd76 100644
--- a/cpp/src/qpid/sys/posix/PollableCondition.cpp
+++ b/cpp/src/qpid/sys/posix/PollableCondition.cpp
@@ -22,17 +22,46 @@
*
*/
-#include "PollableCondition.h"
+#include "qpid/sys/PollableCondition.h"
+#include "qpid/sys/DispatchHandle.h"
+#include "qpid/sys/IOHandle.h"
#include "qpid/sys/posix/PrivatePosix.h"
#include "qpid/Exception.h"
+#include <boost/bind.hpp>
+
#include <unistd.h>
#include <fcntl.h>
namespace qpid {
namespace sys {
-PollableCondition::PollableCondition() : IOHandle(new sys::IOHandlePrivate) {
+class PollableConditionPrivate : public sys::IOHandle {
+ friend class PollableCondition;
+
+private:
+ PollableConditionPrivate(const sys::PollableCondition::Callback& cb,
+ sys::PollableCondition& parent,
+ const boost::shared_ptr<sys::Poller>& poller);
+ ~PollableConditionPrivate();
+
+ void dispatch(sys::DispatchHandle& h);
+ void rewatch();
+ void unwatch();
+
+private:
+ PollableCondition::Callback cb;
+ PollableCondition& parent;
+ boost::shared_ptr<sys::Poller> poller;
+ int writeFd;
+ std::auto_ptr<DispatchHandleRef> handle;
+};
+
+PollableConditionPrivate::PollableConditionPrivate(const sys::PollableCondition::Callback& cb,
+ sys::PollableCondition& parent,
+ const boost::shared_ptr<sys::Poller>& poller)
+ : IOHandle(new sys::IOHandlePrivate), cb(cb), parent(parent)
+{
int fds[2];
if (::pipe(fds) == -1)
throw ErrnoException(QPID_MSG("Can't create PollableCondition"));
@@ -42,22 +71,71 @@ PollableCondition::PollableCondition() : IOHandle(new sys::IOHandlePrivate) {
throw ErrnoException(QPID_MSG("Can't create PollableCondition"));
if (::fcntl(writeFd, F_SETFL, O_NONBLOCK) == -1)
throw ErrnoException(QPID_MSG("Can't create PollableCondition"));
+ handle.reset (new DispatchHandleRef(*this,
+ boost::bind(&sys::PollableConditionPrivate::dispatch, this, _1),
+ 0, 0));
+ handle->startWatch(poller);
+ handle->unwatch();
+}
+
+PollableConditionPrivate::~PollableConditionPrivate()
+{
+ handle->stopWatch();
+ close(writeFd);
+}
+
+void PollableConditionPrivate::dispatch(sys::DispatchHandle& /*h*/)
+{
+ cb(parent);
+}
+
+void PollableConditionPrivate::rewatch()
+{
+ handle->rewatch();
+}
+
+void PollableConditionPrivate::unwatch()
+{
+ handle->unwatch();
+}
+
+ /* PollableCondition */
+
+PollableCondition::PollableCondition(const Callback& cb,
+ const boost::shared_ptr<sys::Poller>& poller)
+ : impl(new PollableConditionPrivate(cb, *this, poller))
+{
+}
+
+PollableCondition::~PollableCondition()
+{
+ delete impl;
+}
+
+void PollableCondition::set() {
+ static const char dummy=0;
+ ssize_t n = ::write(impl->writeFd, &dummy, 1);
+ if (n == -1 && errno != EAGAIN)
+ throw ErrnoException("Error setting PollableCondition");
}
bool PollableCondition::clear() {
char buf[256];
ssize_t n;
bool wasSet = false;
- while ((n = ::read(impl->fd, buf, sizeof(buf))) > 0)
+ while ((n = ::read(impl->impl->fd, buf, sizeof(buf))) > 0)
wasSet = true;
- if (n == -1 && errno != EAGAIN) throw ErrnoException(QPID_MSG("Error clearing PollableCondition"));
+ if (n == -1 && errno != EAGAIN)
+ throw ErrnoException(QPID_MSG("Error clearing PollableCondition"));
return wasSet;
}
-void PollableCondition::set() {
- static const char dummy=0;
- ssize_t n = ::write(writeFd, &dummy, 1);
- if (n == -1 && errno != EAGAIN) throw ErrnoException("Error setting PollableCondition");
+void PollableCondition::disarm() {
+ impl->unwatch();
+}
+
+void PollableCondition::rearm() {
+ impl->rewatch();
}
@@ -71,22 +149,35 @@ void PollableCondition::set() {
namespace qpid {
namespace sys {
-PollableCondition::PollableCondition() : IOHandle(new sys::IOHandlePrivate) {
+PollableConditionPrivate::PollableConditionPrivate(const PollableCondition::Callback& cb,
+ sys::PollableCondition& parent,
+ const boost::shared_ptr<sys::Poller>& poller)
+ : cb(cb), parent(parent), poller(poller),
+ IOHandle(new sys::IOHandlePrivate) {
impl->fd = ::eventfd(0, 0);
if (impl->fd < 0) throw ErrnoException("conditionfd() failed");
}
+void PollableCondition::set() {
+ static const uint64_t value=1;
+ ssize_t n = ::write(impl->impl->fd,
+ reinterpret_cast<const void*>(&value), 8);
+ if (n != 8) throw ErrnoException("write failed on conditionfd");
+}
+
bool PollableCondition::clear() {
char buf[8];
- ssize_t n = ::read(impl->fd, buf, 8);
+ ssize_t n = ::read(impl->impl->fd, buf, 8);
if (n != 8) throw ErrnoException("read failed on conditionfd");
return *reinterpret_cast<uint64_t*>(buf);
}
-void PollableCondition::set() {
- static const uint64_t value=1;
- ssize_t n = ::write(impl->fd, reinterpret_cast<const void*>(&value), 8);
- if (n != 8) throw ErrnoException("write failed on conditionfd");
+void PollableCondition::disarm() {
+ // ????
+}
+
+void PollableCondition::rearm() {
+ // ????
}
#endif
diff --git a/cpp/src/qpid/sys/posix/PollableCondition.h b/cpp/src/qpid/sys/posix/PollableCondition.h
deleted file mode 100644
index 4ec277b0ec..0000000000
--- a/cpp/src/qpid/sys/posix/PollableCondition.h
+++ /dev/null
@@ -1,56 +0,0 @@
-#ifndef QPID_SYS_POSIX_POLLABLECONDITION_H
-#define QPID_SYS_POSIX_POLLABLECONDITION_H
-
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- */
-
-#include "qpid/sys/IOHandle.h"
-
-namespace qpid {
-namespace sys {
-
-/**
- * A pollable condition to integrate in-process conditions with IO
- * conditions in a polling loop.
- *
- * Setting the condition makes it readable for a poller.
- *
- * Writable/disconnected conditions are undefined and should not be
- * polled for.
- */
-class PollableCondition : public sys::IOHandle {
- public:
- PollableCondition();
-
- /** Set the condition, triggers readable in a poller. */
- void set();
-
- /** Get the current state of the condition, then clear it.
- *@return The state of the condition before it was cleared.
- */
- bool clear();
-
- private:
- int writeFd;
-};
-}} // namespace qpid::sys
-
-#endif /*!QPID_SYS_POSIX_POLLABLECONDITION_H*/