2017-02-09 04:52:55 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_PlatformMutex_h
|
|
|
|
#define mozilla_PlatformMutex_h
|
|
|
|
|
2018-05-02 17:11:53 +03:00
|
|
|
#include "mozilla/Atomics.h"
|
2017-02-09 04:52:55 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/Move.h"
|
2018-07-21 17:28:21 +03:00
|
|
|
#include "mozilla/RecordReplay.h"
|
2017-02-09 04:52:55 +03:00
|
|
|
|
2017-02-23 00:41:35 +03:00
|
|
|
#if !defined(XP_WIN)
|
|
|
|
# include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
2017-02-09 04:52:55 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
class ConditionVariableImpl;
|
|
|
|
|
|
|
|
class MutexImpl {
|
|
|
|
public:
|
|
|
|
struct PlatformData;
|
|
|
|
|
2018-07-21 17:28:21 +03:00
|
|
|
explicit MFBT_API MutexImpl(
|
|
|
|
recordreplay::Behavior aRecorded = recordreplay::Behavior::Preserve);
|
2017-02-09 04:52:55 +03:00
|
|
|
MFBT_API ~MutexImpl();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
MFBT_API void lock();
|
|
|
|
MFBT_API void unlock();
|
2019-01-09 18:09:24 +03:00
|
|
|
// We have a separate, forwarding API so internal uses don't have to go
|
|
|
|
// through the PLT.
|
|
|
|
MFBT_API bool tryLock();
|
2017-02-09 04:52:55 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
MutexImpl(const MutexImpl&) = delete;
|
|
|
|
void operator=(const MutexImpl&) = delete;
|
|
|
|
MutexImpl(MutexImpl&&) = delete;
|
|
|
|
void operator=(MutexImpl&&) = delete;
|
2018-06-18 01:57:18 +03:00
|
|
|
bool operator==(const MutexImpl& rhs) = delete;
|
2017-02-09 04:52:55 +03:00
|
|
|
|
2018-05-02 17:11:53 +03:00
|
|
|
void mutexLock();
|
|
|
|
bool mutexTryLock();
|
|
|
|
|
2017-02-09 04:52:55 +03:00
|
|
|
PlatformData* platformData();
|
|
|
|
|
2017-02-23 00:41:35 +03:00
|
|
|
#if !defined(XP_WIN)
|
|
|
|
void* platformData_[sizeof(pthread_mutex_t) / sizeof(void*)];
|
|
|
|
static_assert(sizeof(pthread_mutex_t) / sizeof(void*) != 0 &&
|
|
|
|
sizeof(pthread_mutex_t) % sizeof(void*) == 0,
|
|
|
|
"pthread_mutex_t must have pointer alignment");
|
2018-05-02 17:11:53 +03:00
|
|
|
# ifdef XP_DARWIN
|
|
|
|
// Moving average of the number of spins it takes to acquire the mutex if we
|
|
|
|
// have to wait. May be accessed by multiple threads concurrently. Getting the
|
|
|
|
// latest value is not essential hence relaxed memory ordering is sufficient.
|
2018-07-21 17:28:21 +03:00
|
|
|
mozilla::Atomic<int32_t, mozilla::MemoryOrdering::Relaxed,
|
|
|
|
recordreplay::Behavior::DontPreserve>
|
|
|
|
averageSpins;
|
2018-05-02 17:11:53 +03:00
|
|
|
# endif
|
2017-02-09 04:52:55 +03:00
|
|
|
#else
|
2017-02-23 00:41:35 +03:00
|
|
|
void* platformData_[6];
|
2017-02-09 04:52:55 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
friend class mozilla::detail::ConditionVariableImpl;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_PlatformMutex_h
|