Condition.h revision 1b584ebc1de8b50fe375cffb5fb33ad13be10046
1//===-- Condition.h ---------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_DBCondition_h_
11#define liblldb_DBCondition_h_
12#if defined(__cplusplus)
13
14
15#include <pthread.h>
16#include "lldb/Host/Mutex.h"
17
18namespace lldb_private {
19
20class TimeValue;
21
22//----------------------------------------------------------------------
23/// @class Condition Condition.h "lldb/Host/Condition.h"
24/// @brief A C++ wrapper class for pthread condition variables.
25///
26/// A class that wraps up a pthread condition (pthread_cond_t). The
27/// class will create a pthread condition when an instance is
28/// constructed, and detroy it when it is destructed. It also provides
29/// access to the standard pthread condition calls.
30//----------------------------------------------------------------------
31class Condition
32{
33public:
34
35    //------------------------------------------------------------------
36    /// Default constructor
37    ///
38    /// The default constructor will initialize a new pthread condition
39    /// and maintain the condition in the object state.
40    //------------------------------------------------------------------
41    Condition ();
42
43    //------------------------------------------------------------------
44    /// Destructor
45    ///
46    /// Destroys the pthread condition that the object owns.
47    //------------------------------------------------------------------
48    ~Condition ();
49
50    //------------------------------------------------------------------
51    /// Unblock all threads waiting for a condition variable
52    ///
53    /// @return
54    ///     The return value from \c pthread_cond_broadcast()
55    //------------------------------------------------------------------
56    int
57    Broadcast ();
58
59    //------------------------------------------------------------------
60    /// Unblocks one thread waiting for the condition variable
61    ///
62    /// @return
63    ///     The return value from \c pthread_cond_signal()
64    //------------------------------------------------------------------
65    int
66    Signal ();
67
68    //------------------------------------------------------------------
69    /// Wait for the condition variable to be signaled.
70    ///
71    /// The Wait() function atomically blocks the current thread
72    /// waiting on this object's condition variable, and unblocks
73    /// \a mutex. The waiting thread unblocks only after another thread
74    /// signals or broadcasts this object's condition variable.
75    ///
76    /// If \a abstime is non-NULL, this function will return when the
77    /// system time reaches the time specified in \a abstime if the
78    /// condition variable doesn't get unblocked. If \a abstime is NULL
79    /// this function will wait for an infinite amount of time for the
80    /// condition variable to be unblocked.
81    ///
82    /// The current thread re-acquires the lock on \a mutex following
83    /// the wait.
84    ///
85    /// @param[in] mutex
86    ///     The mutex to use in the \c pthread_cond_timedwait() or
87    ///     \c pthread_cond_wait() calls.
88    ///
89    /// @param[in] abstime
90    ///     An absolute time at which to stop waiting if non-NULL, else
91    ///     wait an infinite amount of time for the condition variable
92    ///     toget signaled.
93    ///
94    /// @param[out] timed_out
95    ///     If not NULL, will be set to true if the wait timed out, and
96    //      false otherwise.
97    ///
98    /// @see Condition::Broadcast()
99    /// @see Condition::Signal()
100    //------------------------------------------------------------------
101    int
102    Wait (Mutex &mutex, const TimeValue *abstime = NULL, bool *timed_out = NULL);
103
104protected:
105    //------------------------------------------------------------------
106    // Member variables
107    //------------------------------------------------------------------
108    pthread_cond_t m_condition; ///< The condition variable.
109
110    //------------------------------------------------------------------
111    /// Get accessor to the pthread condition object.
112    ///
113    /// @return
114    ///     A pointer to the condition variable owned by this object.
115    //------------------------------------------------------------------
116    pthread_cond_t *
117    GetCondition ();
118};
119
120} // namespace lldb_private
121
122#endif  // #if defined(__cplusplus)
123#endif
124
125