summaryrefslogtreecommitdiffstats
path: root/src/common/RWLock.h
blob: 08c8edc7b6d5e1dcdca8b1b5fa5edd6ea44ac9c9 (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// -*- 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_RWLock_Posix__H
#define CEPH_RWLock_Posix__H

#include <pthread.h>
#include <string>
#include "include/ceph_assert.h"
#include "acconfig.h"
#include "lockdep.h"
#include "common/valgrind.h"

#include <atomic>

class RWLock final
{
  mutable pthread_rwlock_t L;
  std::string name;
  mutable int id;
  mutable std::atomic<unsigned> nrlock = { 0 }, nwlock = { 0 };
  bool track, lockdep;

  std::string unique_name(const char* name) const;

public:
  RWLock(const RWLock& other) = delete;
  const RWLock& operator=(const RWLock& other) = delete;

  RWLock(const std::string &n, bool track_lock=true, bool ld=true, bool prioritize_write=false)
    : name(n), id(-1), track(track_lock),
      lockdep(ld) {
#if defined(HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP)
    if (prioritize_write) {
      pthread_rwlockattr_t attr;
      pthread_rwlockattr_init(&attr);
      // PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
      //   Setting the lock kind to this avoids writer starvation as long as
      //   long as any read locking is not done in a recursive fashion.
      pthread_rwlockattr_setkind_np(&attr,
          PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
      pthread_rwlock_init(&L, &attr);
      pthread_rwlockattr_destroy(&attr);
    } else 
#endif 
    // Next block is in {} to possibly connect to the above if when code is used.
    {
      pthread_rwlock_init(&L, NULL);
    }
    ANNOTATE_BENIGN_RACE_SIZED(&id, sizeof(id), "RWLock lockdep id");
    ANNOTATE_BENIGN_RACE_SIZED(&nrlock, sizeof(nrlock), "RWlock nrlock");
    ANNOTATE_BENIGN_RACE_SIZED(&nwlock, sizeof(nwlock), "RWlock nwlock");
    if (lockdep && g_lockdep) id = lockdep_register(name.c_str());
  }

  bool is_locked() const {
    ceph_assert(track);
    return (nrlock > 0) || (nwlock > 0);
  }

  bool is_wlocked() const {
    ceph_assert(track);
    return (nwlock > 0);
  }
  ~RWLock() {
    // The following check is racy but we are about to destroy
    // the object and we assume that there are no other users.
    if (track)
      ceph_assert(!is_locked());
    pthread_rwlock_destroy(&L);
    if (lockdep && g_lockdep) {
      lockdep_unregister(id);
    }
  }

  void unlock(bool lockdep=true) const {
    if (track) {
      if (nwlock > 0) {
        nwlock--;
      } else {
        ceph_assert(nrlock > 0);
        nrlock--;
      }
    }
    if (lockdep && this->lockdep && g_lockdep)
      id = lockdep_will_unlock(name.c_str(), id);
    int r = pthread_rwlock_unlock(&L);
    ceph_assert(r == 0);
  }

  // read
  void get_read() const {
    if (lockdep && g_lockdep) id = lockdep_will_lock(name.c_str(), id);
    int r = pthread_rwlock_rdlock(&L);
    ceph_assert(r == 0);
    if (lockdep && g_lockdep) id = lockdep_locked(name.c_str(), id);
    if (track)
      nrlock++;
  }
  bool try_get_read() const {
    if (pthread_rwlock_tryrdlock(&L) == 0) {
      if (track)
         nrlock++;
      if (lockdep && g_lockdep) id = lockdep_locked(name.c_str(), id);
      return true;
    }
    return false;
  }
  void put_read() const {
    unlock();
  }
  void lock_shared() {
    get_read();
  }
  void unlock_shared() {
    put_read();
  }
  // write
  void get_write(bool lockdep=true) {
    if (lockdep && this->lockdep && g_lockdep)
      id = lockdep_will_lock(name.c_str(), id);
    int r = pthread_rwlock_wrlock(&L);
    ceph_assert(r == 0);
    if (lockdep && this->lockdep && g_lockdep)
      id = lockdep_locked(name.c_str(), id);
    if (track)
      nwlock++;

  }
  bool try_get_write(bool lockdep=true) {
    if (pthread_rwlock_trywrlock(&L) == 0) {
      if (lockdep && this->lockdep && g_lockdep)
	id = lockdep_locked(name.c_str(), id);
      if (track)
         nwlock++;
      return true;
    }
    return false;
  }
  void put_write() {
    unlock();
  }
  void lock() {
    get_write();
  }
  void get(bool for_write) {
    if (for_write) {
      get_write();
    } else {
      get_read();
    }
  }

public:
  class RLocker {
    const RWLock &m_lock;

    bool locked;

  public:
   explicit  RLocker(const RWLock& lock) : m_lock(lock) {
      m_lock.get_read();
      locked = true;
    }
    void unlock() {
      ceph_assert(locked);
      m_lock.unlock();
      locked = false;
    }
    ~RLocker() {
      if (locked) {
        m_lock.unlock();
      }
    }
  };

  class WLocker {
    RWLock &m_lock;

    bool locked;

  public:
    explicit WLocker(RWLock& lock) : m_lock(lock) {
      m_lock.get_write();
      locked = true;
    }
    void unlock() {
      ceph_assert(locked);
      m_lock.unlock();
      locked = false;
    }
    ~WLocker() {
      if (locked) {
        m_lock.unlock();
      }
    }
  };

  class Context {
    RWLock& lock;

  public:
    enum LockState {
      Untaken = 0,
      TakenForRead = 1,
      TakenForWrite = 2,
    };

  private:
    LockState state;

  public:
    explicit Context(RWLock& l) : lock(l), state(Untaken) {}
    Context(RWLock& l, LockState s) : lock(l), state(s) {}

    void get_write() {
      ceph_assert(state == Untaken);

      lock.get_write();
      state = TakenForWrite;
    }

    void get_read() {
      ceph_assert(state == Untaken);

      lock.get_read();
      state = TakenForRead;
    }

    void unlock() {
      ceph_assert(state != Untaken);
      lock.unlock();
      state = Untaken;
    }

    void promote() {
      ceph_assert(state == TakenForRead);
      unlock();
      get_write();
    }

    LockState get_state() { return state; }
    void set_state(LockState s) {
      state = s;
    }

    bool is_locked() {
      return (state != Untaken);
    }

    bool is_rlocked() {
      return (state == TakenForRead);
    }

    bool is_wlocked() {
      return (state == TakenForWrite);
    }
  };
};

#endif // !CEPH_RWLock_Posix__H