]>
git.proxmox.com Git - ceph.git/blob - 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
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
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.
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"
25 Mutex::Mutex(const std::string
&n
, bool r
, bool ld
,
28 name(n
), id(-1), recursive(r
), lockdep(ld
), backtrace(bt
), nlock(0),
29 locked_by(0), cct(cct
), logger(0)
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");
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);
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
)
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
);
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
);
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
);
84 cct
->get_perfcounters_collection()->remove(logger
);
87 if (lockdep
&& g_lockdep
) {
88 lockdep_unregister(id
);
92 void Mutex::Lock(bool no_lockdep
) {
95 if (lockdep
&& g_lockdep
&& !no_lockdep
&& !recursive
) _will_lock();
97 if (logger
&& cct
&& cct
->_conf
->mutex_perf_counter
) {
99 // instrumented mutex enabled
100 start
= ceph_clock_now();
105 r
= pthread_mutex_lock(&_m
);
107 logger
->tinc(l_mutex_wait
,
108 ceph_clock_now() - start
);
110 r
= pthread_mutex_lock(&_m
);
114 if (lockdep
&& g_lockdep
) _locked();
121 void Mutex::Unlock() {
123 if (lockdep
&& g_lockdep
) _will_unlock();
124 int r
= pthread_mutex_unlock(&_m
);