]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/Mutex.cc
6ac022c8af05111c3acf95966bc4a5fad6d03b0a
[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
15 #include "common/Mutex.h"
16 #include "common/perf_counters.h"
17 #include "common/config.h"
18 #include "common/Clock.h"
19 #include "common/valgrind.h"
20
21 Mutex::Mutex(const std::string &n, bool r, bool ld,
22 bool bt,
23 CephContext *cct) :
24 name(n), id(-1), recursive(r), lockdep(ld), backtrace(bt), nlock(0),
25 locked_by(0), cct(cct), logger(0)
26 {
27 ANNOTATE_BENIGN_RACE_SIZED(&id, sizeof(id), "Mutex lockdep id");
28 ANNOTATE_BENIGN_RACE_SIZED(&nlock, sizeof(nlock), "Mutex nlock");
29 ANNOTATE_BENIGN_RACE_SIZED(&locked_by, sizeof(locked_by), "Mutex locked_by");
30 if (cct) {
31 PerfCountersBuilder b(cct, string("mutex-") + name,
32 l_mutex_first, l_mutex_last);
33 b.add_time_avg(l_mutex_wait, "wait", "Average time of mutex in locked state");
34 logger = b.create_perf_counters();
35 cct->get_perfcounters_collection()->add(logger);
36 logger->set(l_mutex_wait, 0);
37 }
38 if (recursive) {
39 // Mutexes of type PTHREAD_MUTEX_RECURSIVE do all the same checks as
40 // mutexes of type PTHREAD_MUTEX_ERRORCHECK.
41 pthread_mutexattr_t attr;
42 pthread_mutexattr_init(&attr);
43 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
44 pthread_mutex_init(&_m,&attr);
45 pthread_mutexattr_destroy(&attr);
46 if (lockdep && g_lockdep)
47 _register();
48 }
49 else if (lockdep) {
50 // If the mutex type is PTHREAD_MUTEX_ERRORCHECK, then error checking
51 // shall be provided. If a thread attempts to relock a mutex that it
52 // has already locked, an error shall be returned. If a thread
53 // attempts to unlock a mutex that it has not locked or a mutex which
54 // is unlocked, an error shall be returned.
55 pthread_mutexattr_t attr;
56 pthread_mutexattr_init(&attr);
57 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
58 pthread_mutex_init(&_m, &attr);
59 pthread_mutexattr_destroy(&attr);
60 if (g_lockdep)
61 _register();
62 }
63 else {
64 // If the mutex type is PTHREAD_MUTEX_NORMAL, deadlock detection
65 // shall not be provided. Attempting to relock the mutex causes
66 // deadlock. If a thread attempts to unlock a mutex that it has not
67 // locked or a mutex which is unlocked, undefined behavior results.
68 pthread_mutex_init(&_m, NULL);
69 }
70 }
71
72 Mutex::~Mutex() {
73 assert(nlock == 0);
74
75 // helgrind gets confused by condition wakeups leading to mutex destruction
76 ANNOTATE_BENIGN_RACE_SIZED(&_m, sizeof(_m), "Mutex primitive");
77 pthread_mutex_destroy(&_m);
78
79 if (cct && logger) {
80 cct->get_perfcounters_collection()->remove(logger);
81 delete logger;
82 }
83 if (lockdep && g_lockdep) {
84 lockdep_unregister(id);
85 }
86 }
87
88 void Mutex::Lock(bool no_lockdep) {
89 int r;
90
91 if (lockdep && g_lockdep && !no_lockdep && !recursive) _will_lock();
92
93 if (logger && cct && cct->_conf->mutex_perf_counter) {
94 utime_t start;
95 // instrumented mutex enabled
96 start = ceph_clock_now();
97 if (TryLock()) {
98 goto out;
99 }
100
101 r = pthread_mutex_lock(&_m);
102
103 logger->tinc(l_mutex_wait,
104 ceph_clock_now() - start);
105 } else {
106 r = pthread_mutex_lock(&_m);
107 }
108
109 assert(r == 0);
110 if (lockdep && g_lockdep) _locked();
111 _post_lock();
112
113 out:
114 ;
115 }
116
117 void Mutex::Unlock() {
118 _pre_unlock();
119 if (lockdep && g_lockdep) _will_unlock();
120 int r = pthread_mutex_unlock(&_m);
121 assert(r == 0);
122 }