• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- PThreadMutex.h ------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  Created by Greg Clayton on 6/16/07.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLDB_TOOLS_DEBUGSERVER_SOURCE_PTHREADMUTEX_H
14 #define LLDB_TOOLS_DEBUGSERVER_SOURCE_PTHREADMUTEX_H
15 
16 #include <assert.h>
17 #include <pthread.h>
18 #include <stdint.h>
19 
20 //#define DEBUG_PTHREAD_MUTEX_DEADLOCKS 1
21 
22 #if defined(DEBUG_PTHREAD_MUTEX_DEADLOCKS)
23 #define PTHREAD_MUTEX_LOCKER(var, mutex)                                       \
24   PThreadMutex::Locker var(mutex, __FUNCTION__, __FILE__, __LINE__)
25 
26 #else
27 #define PTHREAD_MUTEX_LOCKER(var, mutex) PThreadMutex::Locker var(mutex)
28 #endif
29 
30 class PThreadMutex {
31 public:
32   class Locker {
33   public:
34 #if defined(DEBUG_PTHREAD_MUTEX_DEADLOCKS)
35 
36     Locker(PThreadMutex &m, const char *function, const char *file, int line);
37     Locker(PThreadMutex *m, const char *function, const char *file, int line);
38     Locker(pthread_mutex_t *mutex, const char *function, const char *file,
39            int line);
40     ~Locker();
41     void Lock();
42     void Unlock();
43 
44 #else
45     Locker(PThreadMutex &m) : m_pMutex(m.Mutex()) { Lock(); }
46 
47     Locker(PThreadMutex *m) : m_pMutex(m ? m->Mutex() : NULL) { Lock(); }
48 
49     Locker(pthread_mutex_t *mutex) : m_pMutex(mutex) { Lock(); }
50 
51     void Lock() {
52       if (m_pMutex)
53         ::pthread_mutex_lock(m_pMutex);
54     }
55 
56     void Unlock() {
57       if (m_pMutex)
58         ::pthread_mutex_unlock(m_pMutex);
59     }
60 
61     ~Locker() { Unlock(); }
62 
63 #endif
64 
65     // unlock any the current mutex and lock the new one if it is valid
66     void Reset(pthread_mutex_t *pMutex = NULL) {
67       Unlock();
68       m_pMutex = pMutex;
69       Lock();
70     }
71     pthread_mutex_t *m_pMutex;
72 #if defined(DEBUG_PTHREAD_MUTEX_DEADLOCKS)
73     const char *m_function;
74     const char *m_file;
75     int m_line;
76     uint64_t m_lock_time;
77 #endif
78   };
79 
PThreadMutex()80   PThreadMutex() {
81     int err;
82     err = ::pthread_mutex_init(&m_mutex, NULL);
83     assert(err == 0);
84   }
85 
PThreadMutex(int type)86   PThreadMutex(int type) {
87     int err;
88     ::pthread_mutexattr_t attr;
89     err = ::pthread_mutexattr_init(&attr);
90     assert(err == 0);
91     err = ::pthread_mutexattr_settype(&attr, type);
92     assert(err == 0);
93     err = ::pthread_mutex_init(&m_mutex, &attr);
94     assert(err == 0);
95     err = ::pthread_mutexattr_destroy(&attr);
96     assert(err == 0);
97   }
98 
~PThreadMutex()99   ~PThreadMutex() {
100     int err;
101     err = ::pthread_mutex_destroy(&m_mutex);
102     if (err != 0) {
103       err = Unlock();
104       if (err == 0)
105         ::pthread_mutex_destroy(&m_mutex);
106     }
107   }
108 
Mutex()109   pthread_mutex_t *Mutex() { return &m_mutex; }
110 
Lock()111   int Lock() { return ::pthread_mutex_lock(&m_mutex); }
112 
Unlock()113   int Unlock() { return ::pthread_mutex_unlock(&m_mutex); }
114 
115 protected:
116   pthread_mutex_t m_mutex;
117 };
118 
119 #endif
120