summaryrefslogtreecommitdiffstats
path: root/src/common/mutex_debug.h
blob: c1a4ff2a435015e659815ced6a4b69f24bb55c6b (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#ifndef CEPH_COMMON_MUTEX_DEBUG_H
#define CEPH_COMMON_MUTEX_DEBUG_H

#include <atomic>
#include <system_error>
#include <thread>

#include <pthread.h>

#include "include/ceph_assert.h"
#include "include/common_fwd.h"

#include "ceph_time.h"
#include "likely.h"
#include "lockdep.h"

namespace ceph {
namespace mutex_debug_detail {

class mutex_debugging_base
{
protected:
  std::string group;
  int id = -1;
  bool lockdep;   // track this mutex using lockdep_*
  bool backtrace; // gather backtrace on lock acquisition

  std::atomic<int> nlock = 0;
  std::thread::id locked_by = {};

  bool _enable_lockdep() const {
    return lockdep && g_lockdep;
  }
  void _register();
  void _will_lock(bool recursive=false); // about to lock
  void _locked(); // just locked
  void _will_unlock(); // about to unlock

  mutex_debugging_base(std::string group, bool ld = true, bool bt = false);
  ~mutex_debugging_base();

public:
  bool is_locked() const {
    return (nlock > 0);
  }
  bool is_locked_by_me() const {
    return nlock.load(std::memory_order_acquire) > 0 && locked_by == std::this_thread::get_id();
  }
  operator bool() const {
    return is_locked_by_me();
  }
};

// Since this is a /debugging/ mutex just define it in terms of the
// pthread error check mutex.
template<bool Recursive>
class mutex_debug_impl : public mutex_debugging_base
{
private:
  pthread_mutex_t m;

  void _init() {
    pthread_mutexattr_t a;
    pthread_mutexattr_init(&a);
    int r;
    if (recursive)
      r = pthread_mutexattr_settype(&a, PTHREAD_MUTEX_RECURSIVE);
    else
      r = pthread_mutexattr_settype(&a, PTHREAD_MUTEX_ERRORCHECK);
    ceph_assert(r == 0);
    r = pthread_mutex_init(&m, &a);
    ceph_assert(r == 0);
  }

  bool enable_lockdep(bool no_lockdep) const {
    if (recursive) {
      return false;
    } else if (no_lockdep) {
      return false;
    } else {
      return _enable_lockdep();
    }
  }

public:
  static constexpr bool recursive = Recursive;

  mutex_debug_impl(std::string group, bool ld = true, bool bt = false)
    : mutex_debugging_base(group, ld, bt) {
    _init();
  }

  // Mutex is Destructible
  ~mutex_debug_impl() {
    int r = pthread_mutex_destroy(&m);
    ceph_assert(r == 0);
  }

  // Mutex concept is non-Copyable
  mutex_debug_impl(const mutex_debug_impl&) = delete;
  mutex_debug_impl& operator =(const mutex_debug_impl&) = delete;

  // Mutex concept is non-Movable
  mutex_debug_impl(mutex_debug_impl&&) = delete;
  mutex_debug_impl& operator =(mutex_debug_impl&&) = delete;

  void lock_impl() {
    int r = pthread_mutex_lock(&m);
    // Allowed error codes for Mutex concept
    if (unlikely(r == EPERM ||
		 r == EDEADLK ||
		 r == EBUSY)) {
      throw std::system_error(r, std::generic_category());
    }
    ceph_assert(r == 0);
  }

  void unlock_impl() noexcept {
    int r = pthread_mutex_unlock(&m);
    ceph_assert(r == 0);
  }

  bool try_lock_impl() {
    int r = pthread_mutex_trylock(&m);
    switch (r) {
    case 0:
      return true;
    case EBUSY:
      return false;
    default:
      throw std::system_error(r, std::generic_category());
    }
  }
  pthread_mutex_t* native_handle() {
    return &m;
  }

  void _post_lock() {
    if (!recursive)
      ceph_assert(nlock == 0);
    locked_by = std::this_thread::get_id();
    nlock.fetch_add(1, std::memory_order_release);
  }

  void _pre_unlock() {
    if (recursive) {
      ceph_assert(nlock > 0);
    } else {
      ceph_assert(nlock == 1);
    }
    ceph_assert(locked_by == std::this_thread::get_id());
    if (nlock == 1)
      locked_by = std::thread::id();
    nlock.fetch_sub(1, std::memory_order_release);
  }

  bool try_lock(bool no_lockdep = false) {
    bool locked = try_lock_impl();
    if (locked) {
      if (enable_lockdep(no_lockdep))
	_locked();
      _post_lock();
    }
    return locked;
  }

  void lock(bool no_lockdep = false) {
    if (enable_lockdep(no_lockdep))
      _will_lock(recursive);

    if (try_lock(no_lockdep))
      return;

    lock_impl();
    if (enable_lockdep(no_lockdep))
      _locked();
    _post_lock();
  }

  void unlock(bool no_lockdep = false) {
    _pre_unlock();
    if (enable_lockdep(no_lockdep))
      _will_unlock();
    unlock_impl();
  }

};


} // namespace mutex_debug_detail
typedef mutex_debug_detail::mutex_debug_impl<false> mutex_debug;
typedef mutex_debug_detail::mutex_debug_impl<true> mutex_recursive_debug;
} // namespace ceph

#endif