Apache Qpid - AMQP Messaging for Java JMS, C++, Python, Ruby, and .NET Apache Qpid Documentation
Condition.h
Go to the documentation of this file.
1 #ifndef _sys_posix_Condition_h
2 #define _sys_posix_Condition_h
3 
4 /*
5  *
6  * Licensed to the Apache Software Foundation (ASF) under one
7  * or more contributor license agreements. See the NOTICE file
8  * distributed with this work for additional information
9  * regarding copyright ownership. The ASF licenses this file
10  * to you under the Apache License, Version 2.0 (the
11  * "License"); you may not use this file except in compliance
12  * with the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing,
17  * software distributed under the License is distributed on an
18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19  * KIND, either express or implied. See the License for the
20  * specific language governing permissions and limitations
21  * under the License.
22  *
23  */
24 
26 
27 #include "qpid/sys/Mutex.h"
28 #include "qpid/sys/Time.h"
29 
30 #include <time.h>
31 #include <sys/errno.h>
32 #include <boost/noncopyable.hpp>
33 
34 namespace qpid {
35 namespace sys {
36 
40 class Condition
41 {
42  public:
43  inline Condition();
44  inline ~Condition();
45  inline void wait(Mutex&);
46  inline bool wait(Mutex&, const AbsTime& absoluteTime);
47  inline void notify();
48  inline void notifyAll();
49 
50  private:
51  pthread_cond_t condition;
52 };
53 
55  QPID_POSIX_ASSERT_THROW_IF(pthread_cond_init(&condition, 0));
56 }
57 
59  QPID_POSIX_ABORT_IF(pthread_cond_destroy(&condition));
60 }
61 
62 void Condition::wait(Mutex& mutex) {
63  QPID_POSIX_ASSERT_THROW_IF(pthread_cond_wait(&condition, &mutex.mutex));
64 }
65 
66 bool Condition::wait(Mutex& mutex, const AbsTime& absoluteTime){
67  struct timespec ts;
68  toTimespec(ts, Duration(EPOCH, absoluteTime));
69  int status = pthread_cond_timedwait(&condition, &mutex.mutex, &ts);
70  if (status != 0) {
71  if (status == ETIMEDOUT) return false;
72  throw QPID_POSIX_ERROR(status);
73  }
74  return true;
75 }
76 
78  QPID_POSIX_ASSERT_THROW_IF(pthread_cond_signal(&condition));
79 }
80 
82  QPID_POSIX_ASSERT_THROW_IF(pthread_cond_broadcast(&condition));
83 }
84 
85 }}
86 #endif
Class to represent an instant in time.
Definition: Time.h:80
#define QPID_POSIX_ASSERT_THROW_IF(ERRNO)
Same as _THROW_IF in a release build, but abort a debug build.
Definition: check.h:47
struct timespec & toTimespec(struct timespec &ts, const Duration &t)
Class to represent the duration between instants of time.
Definition: Time.h:115
A condition variable for thread synchronization.
Definition: Condition.h:40
pthread_mutex_t mutex
Definition: Mutex.h:51
This file was automatically generated from the AMQP specification.
Definition: Address.h:27
#define QPID_POSIX_ABORT_IF(ERRNO)
Definition: check.h:51
#define QPID_POSIX_ERROR(ERRNO)
Definition: check.h:33
Mutex lock.
Definition: Mutex.h:35
void wait(Mutex &)
Definition: Condition.h:62
const AbsTime EPOCH
Absolute time point for the Unix epoch: 1970-01-01T00:00:00.
Definition: Time.h:157

Qpid C++ API Reference
Generated on Fri Mar 6 2015 for Qpid C++ Client API by doxygen 1.8.9.1