/usr/include/hphp/util/mutex.h is in hhvm-dev 3.11.1+dfsg-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | /*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2015 Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#ifndef incl_HPHP_MUTEX_H_
#define incl_HPHP_MUTEX_H_
#include <pthread.h>
#include <time.h>
// This fixes a bug in tbb headers
// make sure these aren't defined on cygwin
#ifdef __CYGWIN__
# ifdef _WIN32
# undef _WIN32
# endif
# ifdef _WIN64
# undef _WIN64
# endif
#endif
#include <tbb/concurrent_hash_map.h>
#include "hphp/util/portability.h"
#include "hphp/util/assertions.h"
#include "hphp/util/rank.h"
namespace HPHP {
///////////////////////////////////////////////////////////////////////////////
template <bool enableAssertions>
class BaseMutex {
private:
#ifdef DEBUG
static const int kMagic = 0xba5eba11;
int m_magic;
Rank m_rank;
// m_owner/m_hasOwner for keeping track of lock ownership, useful for debugging
pthread_t m_owner;
unsigned int m_acquires;
bool m_recursive;
bool m_hasOwner;
#endif
inline void recordAcquisition() {
#ifdef DEBUG
if (enableAssertions) {
assert(!m_hasOwner ||
pthread_equal(m_owner, pthread_self()));
assert(m_acquires == 0 ||
pthread_equal(m_owner, pthread_self()));
pushRank(m_rank);
m_hasOwner = true;
m_owner = pthread_self();
m_acquires++;
assert(m_recursive || m_acquires == 1);
}
#endif
}
inline void invalidateOwner() {
#ifdef DEBUG
if (enableAssertions) {
m_hasOwner = false;
m_acquires = 0;
}
#endif
}
inline void recordRelease() {
#ifdef DEBUG
if (enableAssertions) {
popRank(m_rank);
assertOwnedBySelf();
assert(m_acquires > 0);
if (--m_acquires == 0) {
m_hasOwner = false;
}
}
#endif
}
public:
inline void assertNotOwned() const {
#ifdef DEBUG
if (enableAssertions) {
assert(!m_hasOwner);
assert(m_acquires == 0);
}
#endif
}
inline void assertOwnedBySelf() const {
#ifdef DEBUG
if (enableAssertions) {
assert(m_hasOwner);
assert(pthread_equal(m_owner, pthread_self()));
assert(m_acquires > 0);
}
#endif
}
public:
explicit BaseMutex(bool recursive = true, Rank r = RankUnranked) {
pthread_mutexattr_init(&m_mutexattr);
if (recursive) {
pthread_mutexattr_settype(&m_mutexattr, PTHREAD_MUTEX_RECURSIVE);
} else {
#if (defined(__APPLE__) || defined(__CYGWIN__) || defined(__MINGW__) || \
defined(_MSC_VER))
pthread_mutexattr_settype(&m_mutexattr, PTHREAD_MUTEX_DEFAULT);
#else
pthread_mutexattr_settype(&m_mutexattr, PTHREAD_MUTEX_ADAPTIVE_NP);
#endif
}
pthread_mutex_init(&m_mutex, &m_mutexattr);
#ifdef DEBUG
m_rank = r;
m_magic = kMagic;
invalidateOwner();
m_recursive = recursive;
#endif
}
~BaseMutex() {
#ifdef DEBUG
assert(m_magic == kMagic);
#endif
assertNotOwned();
pthread_mutex_destroy(&m_mutex);
pthread_mutexattr_destroy(&m_mutexattr);
#ifdef DEBUG
m_magic = ~m_magic;
#endif
}
bool tryLock() {
#ifdef DEBUG
assert(m_magic == kMagic);
#endif
bool success = !pthread_mutex_trylock(&m_mutex);
if (success) {
recordAcquisition();
assertOwnedBySelf();
}
return success;
}
void lock() {
#ifdef DEBUG
assert(m_magic == kMagic);
checkRank(m_rank);
#endif
UNUSED int ret = pthread_mutex_lock(&m_mutex);
assert(ret == 0);
recordAcquisition();
assertOwnedBySelf();
}
void unlock() {
#ifdef DEBUG
assert(m_magic == kMagic);
#endif
recordRelease();
UNUSED int ret = pthread_mutex_unlock(&m_mutex);
assert(ret == 0);
}
private:
BaseMutex(const BaseMutex &); // suppress
BaseMutex &operator=(const BaseMutex &); // suppress
protected:
pthread_mutexattr_t m_mutexattr;
pthread_mutex_t m_mutex;
};
/**
* Standard recursive mutex, which can be used for condition variables.
* This mutex does not support enabling assertions
*/
class Mutex : public BaseMutex<false> {
public:
explicit Mutex(bool recursive = true, Rank rank = RankUnranked) :
BaseMutex<false>(recursive, rank) {}
explicit Mutex(Rank rank, bool recursive = true) :
BaseMutex<false>(recursive, rank) {}
pthread_mutex_t &getRaw() { return m_mutex; }
};
/**
* Simple recursive mutex, which does not expose the underlying raw
* pthread_mutex_t. This allows this mutex to support enabling assertions
*/
class SimpleMutex : public BaseMutex<true> {
public:
explicit SimpleMutex(bool recursive = true, Rank rank = RankUnranked) :
BaseMutex<true>(recursive, rank) {}
};
///////////////////////////////////////////////////////////////////////////////
/**
* Read-write lock wrapper.
*/
class ReadWriteMutex {
#ifdef DEBUG
/*
* We have a track record of self-deadlocking on these, and our pthread
* implementation tends to do crazy things when a rwlock is double-wlocked,
* so check and assert early in debug builds.
*/
static constexpr pthread_t InvalidThread = (pthread_t)0;
pthread_t m_writeOwner;
Rank m_rank;
#endif
void invalidateWriteOwner() {
#ifdef DEBUG
m_writeOwner = InvalidThread;
#endif
}
void recordWriteAcquire() {
#ifdef DEBUG
assert(m_writeOwner == InvalidThread);
m_writeOwner = pthread_self();
#endif
}
void assertNotWriteOwner() {
#ifdef DEBUG
assert(m_writeOwner != pthread_self());
#endif
}
void assertNotWriteOwned() {
#ifdef DEBUG
assert(m_writeOwner == InvalidThread);
#endif
}
public:
explicit ReadWriteMutex(Rank rank = RankUnranked)
#ifdef DEBUG
: m_rank(rank)
#endif
{
invalidateWriteOwner();
pthread_rwlock_init(&m_rwlock, nullptr);
}
~ReadWriteMutex() {
assertNotWriteOwned();
pthread_rwlock_destroy(&m_rwlock);
}
void acquireRead() {
/*
* Atomically downgrading a write lock to a read lock is not part of the
* pthreads standard. See task #528421.
*/
assertNotWriteOwner();
pushRank(m_rank);
pthread_rwlock_rdlock(&m_rwlock);
/*
* Again, see task #528421.
*/
assertNotWriteOwned();
}
void acquireWrite() {
assertNotWriteOwner();
pushRank(m_rank);
pthread_rwlock_wrlock(&m_rwlock);
assertNotWriteOwned();
recordWriteAcquire();
}
bool attemptRead() { return !pthread_rwlock_tryrdlock(&m_rwlock); }
bool attemptWrite() { return !pthread_rwlock_trywrlock(&m_rwlock); }
void release() {
#ifdef DEBUG
popRank(m_rank);
if (m_writeOwner == pthread_self()) {
invalidateWriteOwner();
}
#endif
pthread_rwlock_unlock(&m_rwlock);
}
private:
ReadWriteMutex(const ReadWriteMutex &); // suppress
ReadWriteMutex &operator=(const ReadWriteMutex &); // suppress
pthread_rwlock_t m_rwlock;
};
/*
* A ranked wrapper around tbb::concurrent_hash_map.
*/
template<typename K, typename V, typename H=K, Rank R=RankUnranked>
class RankedCHM : public tbb::concurrent_hash_map<K, V, H> {
typedef tbb::concurrent_hash_map<K, V, H> RawCHM;
public:
class accessor : public RawCHM::accessor {
bool freed;
public:
accessor() : freed(false) { pushRank(R); }
~accessor() { if (!freed) popRank(R); }
void release() {
RawCHM::accessor::release();
popRank(R);
freed = true;
}
};
class const_accessor : public RawCHM::const_accessor {
bool freed;
public:
const_accessor() : freed(false) { pushRank(R); }
~const_accessor() { if (!freed) popRank(R); }
void release() {
RawCHM::const_accessor::release();
popRank(R);
freed = true;
}
};
bool find(const_accessor& a, const K& k) const { return RawCHM::find(a, k); }
bool find(accessor& a, const K& k) { return RawCHM::find(a, k); }
bool insert(accessor& a, const K& k) { return RawCHM::insert(a, k); }
bool insert(const_accessor& a, const K& k) { return RawCHM::insert(a, k); }
bool erase(accessor& a) { return RawCHM::erase(a); }
bool erase(const_accessor& a) { return RawCHM::erase(a); }
bool erase(const K& k) { return RawCHM::erase(k); }
};
///////////////////////////////////////////////////////////////////////////////
}
#endif // incl_HPHP_MUTEX_H_
|