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.
19 #include "include/assert.h"
35 // don't allow copying.
36 void operator=(Mutex &M) {}
37 Mutex( const Mutex &M ) {}
41 id = lockdep_register(name);
43 void _will_lock() { // about to lock
44 id = lockdep_will_lock(name, id);
46 void _locked() { // just locked
47 id = lockdep_locked(name, id);
49 void _unlocked() { // just unlocked
50 id = lockdep_unlocked(name, id);
54 void _will_lock() {} // about to lock
55 void _locked() {} // just locked
56 void _unlocked() {} // just unlocked
60 Mutex(const char *n, bool r = false, bool ld=true) : name(n), id(-1), recursive(r), lockdep(ld), nlock(0) {
62 pthread_mutexattr_t attr;
63 pthread_mutexattr_init(&attr);
64 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
65 pthread_mutex_init(&_m,&attr);
66 pthread_mutexattr_destroy(&attr);
68 pthread_mutex_init(&_m, NULL);
70 if (lockdep && g_lockdep) _register();
74 pthread_mutex_destroy(&_m);
82 int r = pthread_mutex_trylock(&_m);
84 if (lockdep && g_lockdep) _locked();
91 if (lockdep && g_lockdep) _will_lock();
92 int r = pthread_mutex_lock(&_m);
93 if (lockdep && g_lockdep) _locked();
101 int r = pthread_mutex_unlock(&_m);
103 if (lockdep && g_lockdep) _unlocked();
114 Locker(Mutex& m) : mutex(m) {