]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/Mutex.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / common / Mutex.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14 #include <string>
15
16 #include "common/Mutex.h"
17 #include "common/perf_counters.h"
18 #include "common/ceph_context.h"
19 #include "common/config.h"
20 #include "include/stringify.h"
21 #include "include/utime.h"
22 #include "common/Clock.h"
23 #include "common/valgrind.h"
24
25 Mutex::Mutex(const std::string &n, bool r, bool ld,
26 bool bt,
27 CephContext *cct) :
28 name(n), id(-1), recursive(r), lockdep(ld), backtrace(bt), nlock(0),
29 locked_by(0), cct(cct), logger(0)
30 {
31 ANNOTATE_BENIGN_RACE_SIZED(&id, sizeof(id), "Mutex lockdep id");
32 ANNOTATE_BENIGN_RACE_SIZED(&nlock, sizeof(nlock), "Mutex nlock");
33 ANNOTATE_BENIGN_RACE_SIZED(&locked_by, sizeof(locked_by), "Mutex locked_by");
34 if (cct) {
35 PerfCountersBuilder b(cct, string("mutex-") + name,
36 l_mutex_first, l_mutex_last);
37 b.add_time_avg(l_mutex_wait, "wait", "Average time of mutex in locked state");
38 logger = b.create_perf_counters();
39 cct->get_perfcounters_collection()->add(logger);
40 logger->set(l_mutex_wait, 0);
41 }
42 if (recursive) {
43 // Mutexes of type PTHREAD_MUTEX_RECURSIVE do all the same checks as
44 // mutexes of type PTHREAD_MUTEX_ERRORCHECK.
45 pthread_mutexattr_t attr;
46 pthread_mutexattr_init(&attr);
47 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
48 pthread_mutex_init(&_m,&attr);
49 pthread_mutexattr_destroy(&attr);
50 if (lockdep && g_lockdep)
51 _register();
52 }
53 else if (lockdep) {
54 // If the mutex type is PTHREAD_MUTEX_ERRORCHECK, then error checking
55 // shall be provided. If a thread attempts to relock a mutex that it
56 // has already locked, an error shall be returned. If a thread
57 // attempts to unlock a mutex that it has not locked or a mutex which
58 // is unlocked, an error shall be returned.
59 pthread_mutexattr_t attr;
60 pthread_mutexattr_init(&attr);
61 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
62 pthread_mutex_init(&_m, &attr);
63 pthread_mutexattr_destroy(&attr);
64 if (g_lockdep)
65 _register();
66 }
67 else {
68 // If the mutex type is PTHREAD_MUTEX_NORMAL, deadlock detection
69 // shall not be provided. Attempting to relock the mutex causes
70 // deadlock. If a thread attempts to unlock a mutex that it has not
71 // locked or a mutex which is unlocked, undefined behavior results.
72 pthread_mutex_init(&_m, NULL);
73 }
74 }
75
76 Mutex::~Mutex() {
77 assert(nlock == 0);
78
79 // helgrind gets confused by condition wakeups leading to mutex destruction
80 ANNOTATE_BENIGN_RACE_SIZED(&_m, sizeof(_m), "Mutex primitive");
81 pthread_mutex_destroy(&_m);
82
83 if (cct && logger) {
84 cct->get_perfcounters_collection()->remove(logger);
85 delete logger;
86 }
87 if (lockdep && g_lockdep) {
88 lockdep_unregister(id);
89 }
90 }
91
92 void Mutex::Lock(bool no_lockdep) {
93 int r;
94
95 if (lockdep && g_lockdep && !no_lockdep && !recursive) _will_lock();
96
97 if (logger && cct && cct->_conf->mutex_perf_counter) {
98 utime_t start;
99 // instrumented mutex enabled
100 start = ceph_clock_now();
101 if (TryLock()) {
102 goto out;
103 }
104
105 r = pthread_mutex_lock(&_m);
106
107 logger->tinc(l_mutex_wait,
108 ceph_clock_now() - start);
109 } else {
110 r = pthread_mutex_lock(&_m);
111 }
112
113 assert(r == 0);
114 if (lockdep && g_lockdep) _locked();
115 _post_lock();
116
117 out:
118 ;
119 }
120
121 void Mutex::Unlock() {
122 _pre_unlock();
123 if (lockdep && g_lockdep) _will_unlock();
124 int r = pthread_mutex_unlock(&_m);
125 assert(r == 0);
126 }