qpid/sys/posix/Condition.h Source File

Apache Qpid - AMQP Messaging for Java JMS, C++, Python, Ruby, and .NET | Apache Qpid Documentation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

qpid/sys/posix/Condition.h
Go to the documentation of this file.

    00001 #ifndef _sys_posix_Condition_h
    00002 #define _sys_posix_Condition_h
    00003
    00004 /*
    00005  *
    00006  * Licensed to the Apache Software Foundation (ASF) under one
    00007  * or more contributor license agreements.  See the NOTICE file
    00008  * distributed with this work for additional information
    00009  * regarding copyright ownership.  The ASF licenses this file
    00010  * to you under the Apache License, Version 2.0 (the
    00011  * "License"); you may not use this file except in compliance
    00012  * with the License.  You may obtain a copy of the License at
    00013  *
    00014  *   http://www.apache.org/licenses/LICENSE-2.0
    00015  *
    00016  * Unless required by applicable law or agreed to in writing,
    00017  * software distributed under the License is distributed on an
    00018  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    00019  * KIND, either express or implied.  See the License for the
    00020  * specific language governing permissions and limitations
    00021  * under the License.
    00022  *
    00023  */
    00024
    00025 #include "qpid/sys/posix/PrivatePosix.h"
    00026
    00027 #include "qpid/sys/Mutex.h"
    00028 #include "qpid/sys/Time.h"
    00029
    00030 #include <time.h>
    00031 #include <sys/errno.h>
    00032 #include <boost/noncopyable.hpp>
    00033
    00034 namespace qpid {
    00035 namespace sys {
    00036
    00040 class Condition
    00041 {
    00042   public:
    00043     inline Condition();
    00044     inline ~Condition();
    00045     inline void wait(Mutex&);
    00046     inline bool wait(Mutex&, const AbsTime& absoluteTime);
    00047     inline void notify();
    00048     inline void notifyAll();
    00049
    00050   private:
    00051     pthread_cond_t condition;
    00052 };
    00053
    00054 Condition::Condition() {
    00055     QPID_POSIX_ASSERT_THROW_IF(pthread_cond_init(&condition, 0));
    00056 }
    00057
    00058 Condition::~Condition() {
    00059     QPID_POSIX_ABORT_IF(pthread_cond_destroy(&condition));
    00060 }
    00061
    00062 void Condition::wait(Mutex& mutex) {
    00063     QPID_POSIX_ASSERT_THROW_IF(pthread_cond_wait(&condition, &mutex.mutex));
    00064 }
    00065
    00066 bool Condition::wait(Mutex& mutex, const AbsTime& absoluteTime){
    00067     struct timespec ts;
    00068     toTimespec(ts, Duration(EPOCH, absoluteTime));
    00069     int status = pthread_cond_timedwait(&condition, &mutex.mutex, &ts);
    00070     if (status != 0) {
    00071         if (status == ETIMEDOUT) return false;
    00072         throw QPID_POSIX_ERROR(status);
    00073     }
    00074     return true;
    00075 }
    00076
    00077 void Condition::notify(){
    00078     QPID_POSIX_ASSERT_THROW_IF(pthread_cond_signal(&condition));
    00079 }
    00080
    00081 void Condition::notifyAll(){
    00082     QPID_POSIX_ASSERT_THROW_IF(pthread_cond_broadcast(&condition));
    00083 }
    00084
    00085 }}
    00086 #endif