2016-01-02 03:02:34 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-01-01 01:05:57 -07:00
|
|
|
/* Copyright (c) FIRST 2016-2017. All Rights Reserved. */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2015-06-25 01:54:20 -07:00
|
|
|
#pragma once
|
|
|
|
|
|
2015-09-01 16:47:57 -07:00
|
|
|
// Allows usage with std::lock_guard without including <mutex> separately
|
2015-06-25 01:54:20 -07:00
|
|
|
#include <mutex>
|
|
|
|
|
|
2016-12-06 19:56:31 -08:00
|
|
|
#if defined(FRC_SIMULATOR) || defined(_WIN32)
|
2017-05-11 21:25:22 -07:00
|
|
|
namespace hal {
|
2015-07-02 14:33:24 -04:00
|
|
|
// We do not want to use pthreads if in the simulator; however, in the
|
|
|
|
|
// simulator, we do not care about priority inversion.
|
2015-07-29 16:48:04 -04:00
|
|
|
typedef std::mutex priority_mutex;
|
|
|
|
|
typedef std::recursive_mutex priority_recursive_mutex;
|
2017-05-11 21:25:22 -07:00
|
|
|
} // namespace hal
|
2016-05-20 17:30:37 -07:00
|
|
|
#else // Covers rest of file.
|
2015-07-02 14:33:24 -04:00
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
2017-05-11 21:25:22 -07:00
|
|
|
namespace hal {
|
|
|
|
|
|
2015-06-25 01:54:20 -07:00
|
|
|
class priority_recursive_mutex {
|
|
|
|
|
public:
|
2016-05-20 17:30:37 -07:00
|
|
|
typedef pthread_mutex_t* native_handle_type;
|
2015-06-25 01:54:20 -07:00
|
|
|
|
|
|
|
|
constexpr priority_recursive_mutex() noexcept = default;
|
2016-05-20 17:30:37 -07:00
|
|
|
priority_recursive_mutex(const priority_recursive_mutex&) = delete;
|
|
|
|
|
priority_recursive_mutex& operator=(const priority_recursive_mutex&) = delete;
|
2015-06-25 01:54:20 -07:00
|
|
|
|
|
|
|
|
// Lock the mutex, blocking until it's available.
|
|
|
|
|
void lock();
|
|
|
|
|
|
|
|
|
|
// Unlock the mutex.
|
|
|
|
|
void unlock();
|
|
|
|
|
|
|
|
|
|
// Tries to lock the mutex.
|
|
|
|
|
bool try_lock() noexcept;
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
pthread_mutex_t* native_handle();
|
2015-06-25 01:54:20 -07:00
|
|
|
|
|
|
|
|
private:
|
2016-05-20 17:30:37 -07:00
|
|
|
// Do the equivalent of setting PTHREAD_PRIO_INHERIT and
|
|
|
|
|
// PTHREAD_MUTEX_RECURSIVE_NP.
|
2015-06-25 01:54:20 -07:00
|
|
|
#if __WORDSIZE == 64
|
|
|
|
|
pthread_mutex_t m_mutex = {
|
|
|
|
|
{0, 0, 0, 0, 0x20 | PTHREAD_MUTEX_RECURSIVE_NP, 0, 0, {0, 0}}};
|
|
|
|
|
#else
|
|
|
|
|
pthread_mutex_t m_mutex = {
|
|
|
|
|
{0, 0, 0, 0x20 | PTHREAD_MUTEX_RECURSIVE_NP, 0, {0}}};
|
|
|
|
|
#endif
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class priority_mutex {
|
|
|
|
|
public:
|
2016-05-20 17:30:37 -07:00
|
|
|
typedef pthread_mutex_t* native_handle_type;
|
2015-06-25 01:54:20 -07:00
|
|
|
|
|
|
|
|
constexpr priority_mutex() noexcept = default;
|
2016-05-20 17:30:37 -07:00
|
|
|
priority_mutex(const priority_mutex&) = delete;
|
|
|
|
|
priority_mutex& operator=(const priority_mutex&) = delete;
|
2015-06-25 01:54:20 -07:00
|
|
|
|
|
|
|
|
// Lock the mutex, blocking until it's available.
|
|
|
|
|
void lock();
|
|
|
|
|
|
|
|
|
|
// Unlock the mutex.
|
|
|
|
|
void unlock();
|
|
|
|
|
|
|
|
|
|
// Tries to lock the mutex.
|
|
|
|
|
bool try_lock() noexcept;
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
pthread_mutex_t* native_handle();
|
2015-06-25 01:54:20 -07:00
|
|
|
|
|
|
|
|
private:
|
2016-05-20 17:30:37 -07:00
|
|
|
// Do the equivalent of setting PTHREAD_PRIO_INHERIT.
|
2015-06-25 01:54:20 -07:00
|
|
|
#if __WORDSIZE == 64
|
2016-05-20 17:30:37 -07:00
|
|
|
pthread_mutex_t m_mutex = {{0, 0, 0, 0, 0x20, 0, 0, {0, 0}}};
|
2015-06-25 01:54:20 -07:00
|
|
|
#else
|
2016-05-20 17:30:37 -07:00
|
|
|
pthread_mutex_t m_mutex = {{0, 0, 0, 0x20, 0, {0}}};
|
2015-06-25 01:54:20 -07:00
|
|
|
#endif
|
|
|
|
|
};
|
2017-05-11 21:25:22 -07:00
|
|
|
} // namespace hal
|
2015-07-02 14:33:24 -04:00
|
|
|
|
|
|
|
|
#endif // FRC_SIMULATOR
|
2017-05-11 21:25:22 -07:00
|
|
|
|
|
|
|
|
// For backwards compatibility
|
|
|
|
|
#ifndef NAMESPACED_PRIORITY
|
|
|
|
|
using priority_mutex = hal::priority_mutex; // NOLINT
|
|
|
|
|
using priority_recursive_mutex = hal::priority_recursive_mutex; // NOLINT
|
|
|
|
|
#endif
|