summaryrefslogtreecommitdiffstats
path: root/lldb/include/lldb/Host/Mutex.h
blob: b0943a9be2947ce67604108ae795d81b17abf3ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//===-- Mutex.h -------------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_Mutex_h_
#define liblldb_Mutex_h_
#if defined(__cplusplus)

#include <pthread.h>
#include <assert.h>

namespace lldb_private {

//----------------------------------------------------------------------
/// @class Mutex Mutex.h "lldb/Host/Mutex.h"
/// @brief A C++ wrapper class for pthread mutexes.
//----------------------------------------------------------------------
class Mutex
{
public:
    enum Type
    {
        eMutexTypeNormal,       ///< Mutex that can't recursively entered by the same thread
        eMutexTypeRecursive     ///< Mutex can be recursively entered by the same thread
    };

    //------------------------------------------------------------------
    /// @class Mutex::Locker
    ///
    /// A scoped locking class that allows a variety of pthread mutex
    /// objects to have a mutex locked when an Mutex::Locker
    /// object is created, and unlocked when it goes out of scope or
    /// when the Mutex::Locker::Reset(pthread_mutex_t *)
    /// is called. This provides an exception safe way to lock a mutex
    /// in a scope.
    //------------------------------------------------------------------
    class Locker
    {
    public:
        //--------------------------------------------------------------
        /// Default constructor.
        ///
        /// This will create a scoped mutex locking object that doesn't
        /// have a mutex to lock. One will need to be provided using the
        /// Mutex::Locker::Reset(pthread_mutex_t *) method.
        ///
        /// @see Mutex::Locker::Reset(pthread_mutex_t *)
        //--------------------------------------------------------------
        Locker();

        //--------------------------------------------------------------
        /// Constructor with a Mutex object.
        ///
        /// This will create a scoped mutex locking object that extracts
        /// the mutex owned by \a m and locks it.
        ///
        /// @param[in] m
        ///     An instance of a Mutex object that contains a
        ///     valid mutex object.
        //--------------------------------------------------------------
        Locker(Mutex& m);

        //--------------------------------------------------------------
        /// Constructor with a Mutex object pointer.
        ///
        /// This will create a scoped mutex locking object that extracts
        /// the mutex owned by a m and locks it.
        ///
        /// @param[in] m
        ///     A pointer to instance of a Mutex object that
        ///     contains a valid mutex object.
        //--------------------------------------------------------------
        Locker(Mutex* m);

        //--------------------------------------------------------------
        /// Constructor with a raw pthread mutex object pointer.
        ///
        /// This will create a scoped mutex locking object that locks
        /// \a mutex.
        ///
        /// @param[in] mutex
        ///     A pointer to a pthread_mutex_t that will get locked if
        ///     non-NULL.
        //--------------------------------------------------------------
        Locker(pthread_mutex_t *mutex);

        //--------------------------------------------------------------
        /// Desstructor
        ///
        /// Unlocks any valid pthread_mutex_t that this object may
        /// contain.
        //--------------------------------------------------------------
        ~Locker();

        //--------------------------------------------------------------
        /// Change the contained mutex.
        ///
        /// Unlock the current mutex in this object (if it contains a
        /// valid mutex) and lock the new \a mutex object if it is
        /// non-NULL.
        //--------------------------------------------------------------
        void
        Reset(pthread_mutex_t *mutex = NULL);

        //--------------------------------------------------------------
        /// Change the contained mutex only if the mutex can be locked.
        ///
        /// Unlock the current mutex in this object (if it contains a
        /// valid mutex) and try to lock \a mutex. If \a mutex can be 
        /// locked this object will take ownership of the lock and will
        /// unlock it when it goes out of scope or Reset or TryLock are
        /// called again. If the mutex is already locked, this object
        /// will not take ownership of the mutex.
        ///
        /// @return
        ///     Returns \b true if the lock was aquired and the this 
        ///     object will unlock the mutex when it goes out of scope,
        ///     returns \b false otherwise.
        //--------------------------------------------------------------
        bool
        TryLock (pthread_mutex_t *mutex);

    protected:
        //--------------------------------------------------------------
        /// Member variables
        //--------------------------------------------------------------
        pthread_mutex_t *m_mutex_ptr;   ///< A pthread mutex that is locked when
                                        ///< acquired and unlocked when destroyed
                                        ///< or reset.

    private:
        Locker(const Locker&);
        const Locker& operator=(const Locker&);
    };


    //------------------------------------------------------------------
    /// Default constructor.
    ///
    /// Creates a pthread mutex with no attributes.
    //------------------------------------------------------------------
    Mutex();

    //------------------------------------------------------------------
    /// Default constructor.
    ///
    /// Creates a pthread mutex with \a type as the mutex type.
    /// Valid values for \a type include:
    ///     @li Mutex::Type::eMutexTypeNormal
    ///     @li Mutex::Type::eMutexTypeRecursive
    ///
    /// @param[in] type
    ///     The type of the mutex.
    ///
    /// @see ::pthread_mutexattr_settype()
    //------------------------------------------------------------------
    Mutex(Mutex::Type type);

    //------------------------------------------------------------------
    /// Destructor.
    ///
    /// Destroys the mutex owned by this object.
    //------------------------------------------------------------------
    ~Mutex();

    //------------------------------------------------------------------
    /// Mutex get accessor.
    ///
    /// @return
    ///     A pointer to the pthread mutex object owned by this object.
    //------------------------------------------------------------------
    pthread_mutex_t *
    GetMutex();

    //------------------------------------------------------------------
    /// Lock the mutex.
    ///
    /// Locks the mutex owned by this object. If the mutex is already
    /// locked, the calling thread will block until the mutex becomes
    /// available.
    ///
    /// @return
    ///     The error code from \c pthread_mutex_lock().
    //------------------------------------------------------------------
    int
    Lock();

    //------------------------------------------------------------------
    /// Try to lock the mutex.
    ///
    /// Attempts to lock the mutex owned by this object without blocking.
    /// If the mutex is already locked, TryLock() will not block waiting
    /// for the mutex, but will return an error condition.
    ///
    /// @return
    ///     The error code from \c pthread_mutex_trylock().
    //------------------------------------------------------------------
    int
    TryLock();

    //------------------------------------------------------------------
    /// Unlock the mutex.
    ///
    /// If the current thread holds the lock on the owned mutex, then
    /// Unlock() will unlock the mutex. Calling Unlock() on this object
    /// when the calling thread does not hold the lock will result in
    /// undefined behavior.
    ///
    /// @return
    ///     The error code from \c pthread_mutex_unlock().
    //------------------------------------------------------------------
    int
    Unlock();

    static
    int Lock (pthread_mutex_t *mutex_ptr);

    static
    int TryLock (pthread_mutex_t *mutex_ptr);

    static
    int Unlock (pthread_mutex_t *mutex_ptr);

protected:
    //------------------------------------------------------------------
    // Member variables
    //------------------------------------------------------------------
    pthread_mutex_t m_mutex; ///< The pthread mutex object.
private:
    Mutex(const Mutex&);
    const Mutex& operator=(const Mutex&);
};

} // namespace lldb_private

#endif  // #if defined(__cplusplus)
#endif
OpenPOWER on IntegriCloud