diff options
Diffstat (limited to 'cpp/src/qpid/sys')
| -rw-r--r-- | cpp/src/qpid/sys/Serializer.cpp | 125 | ||||
| -rw-r--r-- | cpp/src/qpid/sys/Serializer.h | 105 | ||||
| -rw-r--r-- | cpp/src/qpid/sys/apr/Mutex.h | 18 | ||||
| -rw-r--r-- | cpp/src/qpid/sys/posix/Mutex.h | 12 |
4 files changed, 245 insertions, 15 deletions
diff --git a/cpp/src/qpid/sys/Serializer.cpp b/cpp/src/qpid/sys/Serializer.cpp new file mode 100644 index 0000000000..db2b3cab6d --- /dev/null +++ b/cpp/src/qpid/sys/Serializer.cpp @@ -0,0 +1,125 @@ +/* + * + * 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/Serializer.h" +#include "qpid/log/Statement.h" + +#include <boost/bind.hpp> + +#include <assert.h> + +namespace qpid { +namespace sys { + +Serializer::Serializer(bool allowImmediate, Task notifyDispatchFn) + : state(IDLE), immediate(allowImmediate), notifyDispatch(notifyDispatchFn) +{ + if (notifyDispatch.empty()) + notifyDispatch = boost::bind(&Serializer::notifyWorker, this); +} + +Serializer::~Serializer() { + { + Mutex::ScopedLock l(lock); + state = SHUTDOWN; + lock.notify(); + } + if (worker.id() != 0) + worker.join(); +} + +void Serializer::dispatch(Task& task) { + Mutex::ScopedUnlock u(lock); + // Preconditions: lock is held, state is EXECUTING or DISPATCHING + assert(state != IDLE); + assert(state != SHUTDOWN); + assert(state == EXECUTING || state == DISPATCHING); + try { + task(); + } catch (const std::exception& e) { + QPID_LOG(error, "Unexpected exception in Serializer::dispatch" + << e.what()); + assert(0); // Should not happen. + } catch (...) { + QPID_LOG(error, "Unexpected exception in Serializer::dispatch."); + assert(0); // Should not happen. + } +} + +void Serializer::execute(Task task) { + bool needNotify = false; + { + Mutex::ScopedLock l(lock); + assert(state != SHUTDOWN); + if (immediate && state == IDLE) { + state = EXECUTING; + dispatch(task); + if (state != SHUTDOWN) { + assert(state == EXECUTING); + state = IDLE; + } + } + else + queue.push_back(task); + + if (!queue.empty() && state == IDLE) { + state = DISPATCHING; + needNotify = true; + } + } + if (needNotify) + notifyDispatch(); // Not my function, call outside lock. +} + +void Serializer::dispatch() { + Mutex::ScopedLock l(lock); + // TODO aconway 2007-07-16: This loop could be unbounded + // if other threads add work while we're in dispatch(Task&). + // If we need to bound it we could dispatch just the elements + // that were enqueued when dispatch() was first called - save + // begin() iterator and pop only up to that. + while (!queue.empty() && state != SHUTDOWN) { + assert(state == DISPATCHING); + dispatch(queue.front()); + queue.pop_front(); + } + if (state != SHUTDOWN) { + assert(state == DISPATCHING); + state = IDLE; + } +} + +void Serializer::notifyWorker() { + if (!worker.id()) + worker = Thread(*this); + else + lock.notify(); +} + +void Serializer::run() { + Mutex::ScopedLock l(lock); + while (state != SHUTDOWN) { + dispatch(); + lock.wait(); + } +} + +}} // namespace qpid::sys diff --git a/cpp/src/qpid/sys/Serializer.h b/cpp/src/qpid/sys/Serializer.h new file mode 100644 index 0000000000..eba8e48555 --- /dev/null +++ b/cpp/src/qpid/sys/Serializer.h @@ -0,0 +1,105 @@ +#ifndef SERIALIZER_H +#define SERIALIZER_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/Runnable.h" +#include "qpid/sys/Monitor.h" +#include "qpid/sys/Thread.h" + +#include <boost/function.hpp> +#include <boost/noncopyable.hpp> + +#include <deque> + +namespace qpid { +namespace sys { + +/** + * Execute tasks sequentially, queuing tasks when necessary to + * ensure only one thread at a time executes a task and tasks + * are executed in order. + */ +class Serializer : private boost::noncopyable, private Runnable +{ + public: + typedef boost::function<void()> Task; + + /** Start a serializer. + * + * @param notifyDispatch Called when work is pending and there is no + * active dispatch thread. Must arrange for dispatch() to be called + * in some thread other than the calling thread and return. + * By default the Serializer supplies its own dispatch thread. + * + * @param immediate Allow execute() to execute a task immediatly + * in the current thread. + */ + Serializer(bool immediate=true, Task notifyDispatch=Task()); + + ~Serializer(); + + /** + * Task may be executed immediately in the calling thread if there + * are no other tasks pending or executing and the "immediate" + * paramater to the constructor was true. Otherwise task will be + * enqueued for execution by a dispatch thread. + */ + void execute(Task task); + + /** Execute pending tasks sequentially in calling thread. + * Drains the task queue and returns, does not block for more tasks. + * + * @exception ShutdownException if the serializer is being destroyed. + */ + void dispatch(); + + private: + enum State { + IDLE, ///< No threads are active. + EXECUTING, ///< execute() is executing a single task. + DISPATCHING, ///< dispatch() is draining the queue. + SHUTDOWN ///< Serializer is being destroyed. + }; + + void dispatch(Task&); + void notifyWorker(); + void run(); + + Monitor lock; + + State state; + bool immediate; + std::deque<Task> queue; + Thread worker; + Task notifyDispatch; +}; + +}} // namespace qpid::sys + + + + + +#endif /*!SERIALIZER_H*/ diff --git a/cpp/src/qpid/sys/apr/Mutex.h b/cpp/src/qpid/sys/apr/Mutex.h index 6679adeebb..51089c98ff 100644 --- a/cpp/src/qpid/sys/apr/Mutex.h +++ b/cpp/src/qpid/sys/apr/Mutex.h @@ -42,7 +42,7 @@ class Mutex : private boost::noncopyable { inline ~Mutex(); inline void lock(); inline void unlock(); - inline void trylock(); + inline bool trylock(); protected: apr_thread_mutex_t* mutex; @@ -64,8 +64,8 @@ void Mutex::unlock() { CHECK_APR_SUCCESS(apr_thread_mutex_unlock(mutex)); } -void Mutex::trylock() { - CHECK_APR_SUCCESS(apr_thread_mutex_trylock(mutex)); +bool Mutex::trylock() { + return apr_thread_mutex_trylock(mutex) == 0; } @@ -84,8 +84,8 @@ public: inline void wlock(); // will write-lock inline void rlock(); // will read-lock inline void unlock(); - inline void trywlock(); // will write-try - inline void tryrlock(); // will read-try + inline bool trywlock(); // will write-try + inline bool tryrlock(); // will read-try protected: apr_thread_mutex_t* mutex; @@ -111,12 +111,12 @@ void RWlock::unlock() { CHECK_APR_SUCCESS(apr_thread_mutex_unlock(mutex)); } -void RWlock::trywlock() { - CHECK_APR_SUCCESS(apr_thread_mutex_trylock(mutex)); +bool RWlock::trywlock() { + return apr_thread_mutex_trylock(mutex) == 0; } -void RWlock::tryrlock() { - CHECK_APR_SUCCESS(apr_thread_mutex_trylock(mutex)); +bool RWlock::tryrlock() { + return apr_thread_mutex_trylock(mutex) == 0; } diff --git a/cpp/src/qpid/sys/posix/Mutex.h b/cpp/src/qpid/sys/posix/Mutex.h index b29219235d..4cf0c3a3b0 100644 --- a/cpp/src/qpid/sys/posix/Mutex.h +++ b/cpp/src/qpid/sys/posix/Mutex.h @@ -43,7 +43,7 @@ public: inline ~Mutex(); inline void lock(); inline void unlock(); - inline void trylock(); + inline bool trylock(); protected: @@ -127,7 +127,7 @@ struct PODMutex inline void lock(); inline void unlock(); - inline void trylock(); + inline bool trylock(); // Must be public to be a POD: pthread_mutex_t mutex; @@ -143,8 +143,8 @@ void PODMutex::unlock() { QPID_POSIX_THROW_IF(pthread_mutex_unlock(&mutex)); } -void PODMutex::trylock() { - QPID_POSIX_THROW_IF(pthread_mutex_trylock(&mutex)); +bool PODMutex::trylock() { + return pthread_mutex_trylock(&mutex) == 0; } Mutex::Mutex() { @@ -163,8 +163,8 @@ void Mutex::unlock() { QPID_POSIX_THROW_IF(pthread_mutex_unlock(&mutex)); } -void Mutex::trylock() { - QPID_POSIX_THROW_IF(pthread_mutex_trylock(&mutex)); +bool Mutex::trylock() { + return pthread_mutex_trylock(&mutex) == 0; } |
