mutex.h 9.8 KB
Newer Older
羽飞's avatar
羽飞 已提交
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
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Longda on 2010
//

#ifndef __COMMON_LANG_MUTEX_H__
#define __COMMON_LANG_MUTEX_H__

#include <errno.h>
#include <map>
#include <pthread.h>
#include <set>
#include <sstream>
#include <string.h>
#include <string>
#include <sys/types.h>

#include "common/log/log.h"

namespace common {

#define MUTEX_LOG LOG_DEBUG

class LockTrace {
34 35 36 37 38
public:
  static void check(pthread_mutex_t *mutex, const long long threadId, const char *file, const int line);
  static void lock(pthread_mutex_t *mutex, const long long threadId, const char *file, const int line);
  static void tryLock(pthread_mutex_t *mutex, const long long threadId, const char *file, const int line);
  static void unlock(pthread_mutex_t *mutex, const long long threadId, const char *file, const int line);
羽飞's avatar
羽飞 已提交
39 40 41 42

  static void toString(std::string &result);

  class LockID {
43 44 45 46 47 48 49 50
  public:
    LockID(const long long threadId, const char *file, const int line) : mFile(file), mThreadId(threadId), mLine(line)
    {}
    LockID() : mFile(), mThreadId(0), mLine(0)
    {}

    std::string toString()
    {
羽飞's avatar
羽飞 已提交
51 52
      std::ostringstream oss;

53
      oss << "threaId:" << mThreadId << ",file name:" << mFile << ",line:" << mLine;
羽飞's avatar
羽飞 已提交
54 55 56 57

      return oss.str();
    }

58
  public:
羽飞's avatar
羽飞 已提交
59 60 61 62 63
    std::string mFile;
    const long long mThreadId;
    int mLine;
  };

64
  static void foundDeadLock(LockID &current, LockID &other, pthread_mutex_t *otherWaitMutex);
羽飞's avatar
羽飞 已提交
65

66
  static bool deadlockCheck(LockID &current, std::set<pthread_mutex_t *> &ownMutexs, LockID &other, int recusiveNum);
羽飞's avatar
羽飞 已提交
67

68
  static bool deadlockCheck(pthread_mutex_t *mutex, const long long threadId, const char *file, const int line);
羽飞's avatar
羽飞 已提交
69

70
  static bool checkLockTimes(pthread_mutex_t *mutex, const char *file, const int line);
羽飞's avatar
羽飞 已提交
71

72
  static void insertLock(pthread_mutex_t *mutex, const long long threadId, const char *file, const int line);
羽飞's avatar
羽飞 已提交
73

74 75 76 77
  static void setMaxBlockThreads(int blockNum)
  {
    mMaxBlockTids = blockNum;
  }
羽飞's avatar
羽飞 已提交
78

79
public:
羽飞's avatar
羽飞 已提交
80 81
  static std::set<pthread_mutex_t *> mEnableRecurisives;

82
protected:
羽飞's avatar
羽飞 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  static std::map<pthread_mutex_t *, LockID> mLocks;
  static std::map<pthread_mutex_t *, int> mWaitTimes;
  static std::map<long long, pthread_mutex_t *> mWaitLocks;
  static std::map<long long, std::set<pthread_mutex_t *>> mOwnLocks;

  static pthread_rwlock_t mMapMutex;
  static int mMaxBlockTids;
};

// Open this macro in Makefile
#ifndef DEBUG_LOCK

#define MUTEXT_STATIC_INIT() PTHREAD_MUTEX_INITIALIZER
#define MUTEX_INIT(lock, attr) pthread_mutex_init(lock, attr)
#define MUTEX_DESTROY(lock) pthread_mutex_destroy(lock)
#define MUTEX_LOCK(lock) pthread_mutex_lock(lock)
#define MUTEX_UNLOCK(lock) pthread_mutex_unlock(lock)
#define MUTEX_TRYLOCK(lock) pthread_mutex_trylock(lock)

#define COND_INIT(cond, attr) pthread_cond_init(cond, attr)
#define COND_DESTROY(cond) pthread_cond_destroy(cond)
#define COND_WAIT(cond, mutex) pthread_cond_wait(cond, mutex)
105
#define COND_WAIT_TIMEOUT(cond, mutex, time, ret) ret = pthread_cond_timedwait(cond, mutex, time)
羽飞's avatar
羽飞 已提交
106 107 108
#define COND_SIGNAL(cond) pthread_cond_signal(cond)
#define COND_BRAODCAST(cond) pthread_cond_broadcast(cond)

109
#else  // DEBUG_LOCK
羽飞's avatar
羽飞 已提交
110

111 112
#define MUTEX_STATIC_INIT()  \
  PTHREAD_MUTEX_INITIALIZER; \
羽飞's avatar
羽飞 已提交
113 114 115 116
  LOG_INFO("PTHREAD_MUTEX_INITIALIZER");

#if defined(__MACH__)

117 118 119 120 121 122 123 124 125 126 127 128
#define MUTEX_INIT(lock, attr)                      \
  ({                                                \
    LOG_INFO("pthread_mutex_init %p", lock);        \
    if (attr != NULL) {                             \
      int type;                                     \
      pthread_mutexattr_gettype(attr, &type);       \
      if (type == PTHREAD_MUTEX_RECURSIVE) {        \
        LockTrace::mEnableRecurisives.insert(lock); \
      }                                             \
    }                                               \
    int result = pthread_mutex_init(lock, attr);    \
    result;                                         \
羽飞's avatar
羽飞 已提交
129 130 131 132
  })

#else

133 134 135 136 137 138 139 140 141 142 143 144
#define MUTEX_INIT(lock, attr)                      \
  ({                                                \
    LOG_INFO("pthread_mutex_init %p", lock);        \
    if (attr != NULL) {                             \
      int type;                                     \
      pthread_mutexattr_gettype(attr, &type);       \
      if (type == PTHREAD_MUTEX_RECURSIVE_NP) {     \
        LockTrace::mEnableRecurisives.insert(lock); \
      }                                             \
    }                                               \
    int result = pthread_mutex_init(lock, attr);    \
    result;                                         \
羽飞's avatar
羽飞 已提交
145 146 147
  })
#endif

148 149 150 151 152 153
#define MUTEX_DESTROY(lock)                     \
  ({                                            \
    LockTrace::mEnableRecurisives.erase(lock);  \
    int result = pthread_mutex_destroy(lock);   \
    LOG_INFO("pthread_mutex_destroy %p", lock); \
    result;                                     \
羽飞's avatar
羽飞 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166
  })

#define MUTEX_LOCK(mutex)                                                      \
  ({                                                                           \
    LockTrace::check(mutex, gettid(), __FILE__, __LINE__);                     \
    int result = pthread_mutex_lock(mutex);                                    \
    LockTrace::lock(mutex, gettid(), __FILE__, __LINE__);                      \
    if (result) {                                                              \
      LOG_ERROR("Failed to lock %p, rc %d:%s", mutex, errno, strerror(errno)); \
    }                                                                          \
    result;                                                                    \
  })

167 168 169 170 171 172 173 174
#define MUTEX_TRYLOCK(mutex)                                \
  ({                                                        \
    LockTrace::check(mutex, gettid(), __FILE__, __LINE__);  \
    int result = pthread_mutex_trylock(mutex);              \
    if (result == 0) {                                      \
      LockTrace::lock(mutex, gettid(), __FILE__, __LINE__); \
    }                                                       \
    result;                                                 \
羽飞's avatar
羽飞 已提交
175 176
  })

177 178 179 180 181 182 183 184 185
#define MUTEX_UNLOCK(lock)                                                      \
  ({                                                                            \
    int result = pthread_mutex_unlock(lock);                                    \
    LockTrace::unlock(lock, gettid(), __FILE__, __LINE__);                      \
    MUTEX_LOG("mutex:%p has been ulocked", lock);                               \
    if (result) {                                                               \
      LOG_ERROR("Failed to unlock %p, rc %d:%s", lock, errno, strerror(errno)); \
    }                                                                           \
    result;                                                                     \
羽飞's avatar
羽飞 已提交
186 187
  })

188 189 190 191 192
#define COND_INIT(cond, attr)                   \
  ({                                            \
    LOG_INFO("pthread_cond_init");              \
    int result = pthread_cond_init(cond, attr); \
    result;                                     \
羽飞's avatar
羽飞 已提交
193 194
  })

195 196 197 198 199
#define COND_DESTROY(cond)                   \
  ({                                         \
    int result = pthread_cond_destroy(cond); \
    LOG_INFO("pthread_cond_destroy");        \
    result;                                  \
羽飞's avatar
羽飞 已提交
200 201
  })

202 203 204 205 206 207 208 209 210
#define COND_WAIT(cond, mutex)                                      \
  ({                                                                \
    MUTEX_LOG("pthread_cond_wait, cond:%p, mutex:%p", cond, mutex); \
    LockTrace::unlock(mutex, gettid(), __FILE__, __LINE__);         \
    int result = pthread_cond_wait(cond, mutex);                    \
    LockTrace::check(mutex, gettid(), __FILE__, __LINE__);          \
    LockTrace::lock(mutex, gettid(), __FILE__, __LINE__);           \
    MUTEX_LOG("Lock %p under pthread_cond_wait", mutex);            \
    result;                                                         \
羽飞's avatar
羽飞 已提交
211 212
  })

213 214 215 216 217 218 219 220 221 222 223
#define COND_WAIT_TIMEOUT(cond, mutex, time, ret)                        \
  ({                                                                     \
    MUTEX_LOG("pthread_cond_timedwait, cond:%p, mutex:%p", cond, mutex); \
    LockTrace::unlock(mutex, gettid(), __FILE__, __LINE__);              \
    int result = pthread_cond_timedwait(cond, mutex, time);              \
    if (result == 0) {                                                   \
      LockTrace::check(mutex, gettid(), __FILE__, __LINE__);             \
      LockTrace::lock(mutex, gettid(), __FILE__, __LINE__);              \
      MUTEX_LOG("Lock %p under pthread_cond_wait", mutex);               \
    }                                                                    \
    result;                                                              \
羽飞's avatar
羽飞 已提交
224 225
  })

226 227 228 229 230
#define COND_SIGNAL(cond)                            \
  ({                                                 \
    int result = pthread_cond_signal(cond);          \
    MUTEX_LOG("pthread_cond_signal, cond:%p", cond); \
    result;                                          \
羽飞's avatar
羽飞 已提交
231 232
  })

233 234 235 236 237
#define COND_BRAODCAST(cond)                            \
  ({                                                    \
    int result = pthread_cond_broadcast(cond);          \
    MUTEX_LOG("pthread_cond_broadcast, cond:%p", cond); \
    result;                                             \
羽飞's avatar
羽飞 已提交
238 239
  })

240
#endif  // DEBUG_LOCK
羽飞's avatar
羽飞 已提交
241

242 243
}  // namespace common
#endif  // __COMMON_LANG_MUTEX_H__