Change hal sim to use spinlocks (#1291)

This makes callback registration completely thread safe.

This patch also uses templates and macros to dramatically reduce the amount of
manual boilerplate.
This commit is contained in:
Peter Johnson
2018-09-03 16:08:07 -07:00
committed by GitHub
parent 67b1c85315
commit c0ff6198b3
65 changed files with 1305 additions and 7639 deletions

View File

@@ -1,71 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
/* 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#ifndef __FRC_ROBORIO__
#include <memory>
#include "mockdata/NotifyListenerVector.h"
template <typename VectorType, typename CallbackType>
std::shared_ptr<VectorType> RegisterCallbackImpl(
std::shared_ptr<VectorType> currentVector, const char* name,
CallbackType callback, void* param, int32_t* newUid) {
std::shared_ptr<VectorType> newCallbacks;
if (currentVector == nullptr) {
newCallbacks = std::make_shared<VectorType>(
param, callback, reinterpret_cast<unsigned int*>(newUid));
} else {
newCallbacks = currentVector->emplace_back(
param, callback, reinterpret_cast<unsigned int*>(newUid));
}
return newCallbacks;
}
template <typename VectorType, typename CallbackType>
std::shared_ptr<VectorType> CancelCallbackImpl(
std::shared_ptr<VectorType> currentVector, int32_t uid) {
// Create a copy of the callbacks to erase from
auto newCallbacks = currentVector->erase(uid);
return newCallbacks;
}
std::shared_ptr<hal::NotifyListenerVector> RegisterCallback(
std::shared_ptr<hal::NotifyListenerVector> currentVector, const char* name,
HAL_NotifyCallback callback, void* param, int32_t* newUid);
std::shared_ptr<hal::NotifyListenerVector> CancelCallback(
std::shared_ptr<hal::NotifyListenerVector> currentVector, int32_t uid);
void InvokeCallback(std::shared_ptr<hal::NotifyListenerVector> currentVector,
const char* name, const HAL_Value* value);
std::shared_ptr<hal::BufferListenerVector> RegisterCallback(
std::shared_ptr<hal::BufferListenerVector> currentVector, const char* name,
HAL_BufferCallback callback, void* param, int32_t* newUid);
std::shared_ptr<hal::BufferListenerVector> CancelCallback(
std::shared_ptr<hal::BufferListenerVector> currentVector, int32_t uid);
void InvokeCallback(std::shared_ptr<hal::BufferListenerVector> currentVector,
const char* name, uint8_t* buffer, int32_t count);
std::shared_ptr<hal::ConstBufferListenerVector> RegisterCallback(
std::shared_ptr<hal::ConstBufferListenerVector> currentVector,
const char* name, HAL_ConstBufferCallback callback, void* param,
int32_t* newUid);
std::shared_ptr<hal::ConstBufferListenerVector> CancelCallback(
std::shared_ptr<hal::ConstBufferListenerVector> currentVector, int32_t uid);
void InvokeCallback(
std::shared_ptr<hal::ConstBufferListenerVector> currentVector,
const char* name, const uint8_t* buffer, int32_t count);
#endif

View File

@@ -1,142 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
/* 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#ifndef __FRC_ROBORIO__
#include <memory>
#include <wpi/SmallVector.h>
#include "NotifyListener.h"
namespace hal {
// Vector which provides an integrated freelist for removal and reuse of
// individual elements.
template <typename ListenerType>
class HalCallbackListenerVectorImpl {
struct private_init {};
public:
typedef typename wpi::SmallVectorImpl<
HalCallbackListener<ListenerType>>::size_type size_type;
// Constructor for creating copies of the vector
HalCallbackListenerVectorImpl(const HalCallbackListenerVectorImpl* copyFrom,
const private_init&);
// Delete all default constructors so they cannot be used
HalCallbackListenerVectorImpl& operator=(
const HalCallbackListenerVectorImpl&) = delete;
HalCallbackListenerVectorImpl() = delete;
HalCallbackListenerVectorImpl(const HalCallbackListenerVectorImpl&) = delete;
// Create a new vector with a single callback inside of it
HalCallbackListenerVectorImpl(void* param, ListenerType callback,
unsigned int* newUid) {
*newUid = emplace_back_impl(param, callback);
}
size_type size() const { return m_vector.size(); }
HalCallbackListener<ListenerType>& operator[](size_type i) {
return m_vector[i];
}
const HalCallbackListener<ListenerType>& operator[](size_type i) const {
return m_vector[i];
}
// Add a new NotifyListener to a copy of the vector. If there are elements on
// the freelist,
// reuses the last one; otherwise adds to the end of the vector.
// Returns the resulting element index (+1).
std::shared_ptr<HalCallbackListenerVectorImpl<ListenerType>> emplace_back(
void* param, ListenerType callback, unsigned int* newUid);
// Removes the identified element by replacing it with a default-constructed
// one. The element is added to the freelist for later reuse. Returns a copy
std::shared_ptr<HalCallbackListenerVectorImpl<ListenerType>> erase(
unsigned int uid);
private:
wpi::SmallVector<HalCallbackListener<ListenerType>, 4> m_vector;
wpi::SmallVector<unsigned int, 4> m_free;
// Add a new NotifyListener to the vector. If there are elements on the
// freelist,
// reuses the last one; otherwise adds to the end of the vector.
// Returns the resulting element index (+1).
unsigned int emplace_back_impl(void* param, ListenerType callback);
// Removes the identified element by replacing it with a default-constructed
// one. The element is added to the freelist for later reuse.
void erase_impl(unsigned int uid);
};
template <typename ListenerType>
HalCallbackListenerVectorImpl<ListenerType>::HalCallbackListenerVectorImpl(
const HalCallbackListenerVectorImpl<ListenerType>* copyFrom,
const private_init&)
: m_vector(copyFrom->m_vector), m_free(copyFrom->m_free) {}
template <typename ListenerType>
std::shared_ptr<HalCallbackListenerVectorImpl<ListenerType>>
HalCallbackListenerVectorImpl<ListenerType>::emplace_back(
void* param, ListenerType callback, unsigned int* newUid) {
auto newVector =
std::make_shared<HalCallbackListenerVectorImpl<ListenerType>>(
this, private_init());
newVector->m_vector = m_vector;
newVector->m_free = m_free;
*newUid = newVector->emplace_back_impl(param, callback);
return newVector;
}
template <typename ListenerType>
std::shared_ptr<HalCallbackListenerVectorImpl<ListenerType>>
HalCallbackListenerVectorImpl<ListenerType>::erase(unsigned int uid) {
auto newVector =
std::make_shared<HalCallbackListenerVectorImpl<ListenerType>>(
this, private_init());
newVector->m_vector = m_vector;
newVector->m_free = m_free;
newVector->erase_impl(uid);
return newVector;
}
template <typename ListenerType>
unsigned int HalCallbackListenerVectorImpl<ListenerType>::emplace_back_impl(
void* param, ListenerType callback) {
unsigned int uid;
if (m_free.empty()) {
uid = m_vector.size();
m_vector.emplace_back(param, callback);
} else {
uid = m_free.back();
m_free.pop_back();
m_vector[uid] = HalCallbackListener<ListenerType>(param, callback);
}
return uid + 1;
}
template <typename ListenerType>
void HalCallbackListenerVectorImpl<ListenerType>::erase_impl(unsigned int uid) {
--uid;
if (uid >= m_vector.size() || !m_vector[uid]) return;
m_free.push_back(uid);
m_vector[uid] = HalCallbackListener<ListenerType>();
}
typedef HalCallbackListenerVectorImpl<HAL_NotifyCallback> NotifyListenerVector;
typedef HalCallbackListenerVectorImpl<HAL_BufferCallback> BufferListenerVector;
typedef HalCallbackListenerVectorImpl<HAL_ConstBufferCallback>
ConstBufferListenerVector;
} // namespace hal
#endif

View File

@@ -0,0 +1,151 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
/* 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <memory>
#include <utility>
#include <wpi/Compiler.h>
#include <wpi/UidVector.h>
#include <wpi/spinlock.h>
#include "mockdata/NotifyListener.h"
namespace hal {
namespace impl {
class SimCallbackRegistryBase {
public:
using RawFunctor = void (*)();
protected:
using CallbackVector = wpi::UidVector<HalCallbackListener<RawFunctor>, 4>;
public:
void Cancel(int32_t uid) {
std::lock_guard<wpi::recursive_spinlock> lock(m_mutex);
if (m_callbacks) m_callbacks->erase(uid - 1);
}
void Reset() {
std::lock_guard<wpi::recursive_spinlock> lock(m_mutex);
DoReset();
}
wpi::recursive_spinlock& GetMutex() { return m_mutex; }
protected:
int32_t DoRegister(RawFunctor callback, void* param) {
// Must return -1 on a null callback for error handling
if (callback == nullptr) return -1;
if (!m_callbacks) m_callbacks = std::make_unique<CallbackVector>();
return m_callbacks->emplace_back(param, callback) + 1;
}
LLVM_ATTRIBUTE_ALWAYS_INLINE void DoReset() {
if (m_callbacks) m_callbacks->clear();
}
mutable wpi::recursive_spinlock m_mutex;
std::unique_ptr<CallbackVector> m_callbacks;
};
} // namespace impl
/**
* Simulation callback registry. Provides callback functionality.
*
* @tparam CallbackFunction callback function type (e.g. HAL_BufferCallback)
* @tparam GetName function that returns a const char* for the name
*/
template <typename CallbackFunction, const char* (*GetName)()>
class SimCallbackRegistry : public impl::SimCallbackRegistryBase {
public:
int32_t Register(CallbackFunction callback, void* param) {
std::lock_guard<wpi::recursive_spinlock> lock(m_mutex);
return DoRegister(reinterpret_cast<RawFunctor>(callback), param);
}
template <typename... U>
void Invoke(U&&... u) const {
std::lock_guard<wpi::recursive_spinlock> lock(m_mutex);
if (m_callbacks) {
const char* name = GetName();
for (auto&& cb : *m_callbacks)
reinterpret_cast<CallbackFunction>(cb.callback)(name, cb.param,
std::forward<U>(u)...);
}
}
template <typename... U>
LLVM_ATTRIBUTE_ALWAYS_INLINE void operator()(U&&... u) const {
return Invoke(std::forward<U>(u)...);
}
};
/**
* Define a name functor for use with SimCallbackRegistry.
* This creates a function named GetNAMEName() that returns "NAME".
* @param NAME the name to return
*/
#define HAL_SIMCALLBACKREGISTRY_DEFINE_NAME(NAME) \
static LLVM_ATTRIBUTE_ALWAYS_INLINE constexpr const char* \
Get##NAME##Name() { \
return #NAME; \
}
/**
* Define a standard C API for SimCallbackRegistry.
*
* Functions defined:
* - int32 NS_RegisterCAPINAMECallback(
* int32_t index, TYPE callback, void* param)
* - void NS_CancelCAPINAMECallback(int32_t index, int32_t uid)
*
* @param TYPE the underlying callback type (e.g. HAL_BufferCallback)
* @param NS the "namespace" (e.g. HALSIM)
* @param CAPINAME the C API name (usually first letter capitalized)
* @param DATA the backing data array
* @param LOWERNAME the lowercase name of the backing data registry
*/
#define HAL_SIMCALLBACKREGISTRY_DEFINE_CAPI(TYPE, NS, CAPINAME, DATA, \
LOWERNAME) \
int32_t NS##_Register##CAPINAME##Callback(int32_t index, TYPE callback, \
void* param) { \
return DATA[index].LOWERNAME.Register(callback, param); \
} \
\
void NS##_Cancel##CAPINAME##Callback(int32_t index, int32_t uid) { \
DATA[index].LOWERNAME.Cancel(uid); \
}
/**
* Define a standard C API for SimCallbackRegistry (no index variant).
*
* Functions defined:
* - int32 NS_RegisterCAPINAMECallback(TYPE callback, void* param)
* - void NS_CancelCAPINAMECallback(int32_t uid)
*
* @param TYPE the underlying callback type (e.g. HAL_BufferCallback)
* @param NS the "namespace" (e.g. HALSIM)
* @param CAPINAME the C API name (usually first letter capitalized)
* @param DATA the backing data pointer
* @param LOWERNAME the lowercase name of the backing data registry
*/
#define HAL_SIMCALLBACKREGISTRY_DEFINE_CAPI_NOINDEX(TYPE, NS, CAPINAME, DATA, \
LOWERNAME) \
int32_t NS##_Register##CAPINAME##Callback(TYPE callback, void* param) { \
return DATA->LOWERNAME.Register(callback, param); \
} \
\
void NS##_Cancel##CAPINAME##Callback(int32_t uid) { \
DATA->LOWERNAME.Cancel(uid); \
}
} // namespace hal

View File

@@ -0,0 +1,227 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
/* 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <memory>
#include <wpi/Compiler.h>
#include <wpi/UidVector.h>
#include <wpi/spinlock.h>
#include "mockdata/NotifyListener.h"
#include "mockdata/SimCallbackRegistry.h"
namespace hal {
namespace impl {
template <typename T, HAL_Value (*MakeValue)(T)>
class SimDataValueBase : protected SimCallbackRegistryBase {
public:
explicit SimDataValueBase(T value) : m_value(value) {}
LLVM_ATTRIBUTE_ALWAYS_INLINE void CancelCallback(int32_t uid) { Cancel(uid); }
T Get() const {
std::lock_guard<wpi::recursive_spinlock> lock(m_mutex);
return m_value;
}
LLVM_ATTRIBUTE_ALWAYS_INLINE operator T() const { return Get(); }
void Reset(T value) {
std::lock_guard<wpi::recursive_spinlock> lock(m_mutex);
DoReset();
m_value = value;
}
wpi::recursive_spinlock& GetMutex() { return m_mutex; }
protected:
int32_t DoRegisterCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify, const char* name) {
std::unique_lock<wpi::recursive_spinlock> lock(m_mutex);
int32_t newUid = DoRegister(reinterpret_cast<RawFunctor>(callback), param);
if (newUid == -1) return -1;
if (initialNotify) {
// We know that the callback is not null because of earlier null check
HAL_Value value = MakeValue(m_value);
lock.unlock();
callback(name, param, &value);
}
return newUid + 1;
}
void DoSet(T value, const char* name) {
std::lock_guard<wpi::recursive_spinlock> lock(this->m_mutex);
if (m_value != value) {
m_value = value;
if (m_callbacks) {
HAL_Value halValue = MakeValue(value);
for (auto&& cb : *m_callbacks)
reinterpret_cast<HAL_NotifyCallback>(cb.callback)(name, cb.param,
&halValue);
}
}
}
T m_value;
};
} // namespace impl
/**
* Simulation data value wrapper. Provides callback functionality when the
* data value is changed.
*
* @tparam T value type (e.g. double)
* @tparam MakeValue function that takes a T and returns a HAL_Value
* @tparam GetName function that returns a const char* for the name
* @tparam GetDefault function that returns the default T (used only for
* default constructor)
*/
template <typename T, HAL_Value (*MakeValue)(T), const char* (*GetName)(),
T (*GetDefault)() = nullptr>
class SimDataValue final : public impl::SimDataValueBase<T, MakeValue> {
public:
SimDataValue()
: impl::SimDataValueBase<T, MakeValue>(
GetDefault != nullptr ? GetDefault() : T()) {}
explicit SimDataValue(T value)
: impl::SimDataValueBase<T, MakeValue>(value) {}
LLVM_ATTRIBUTE_ALWAYS_INLINE int32_t RegisterCallback(
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) {
return this->DoRegisterCallback(callback, param, initialNotify, GetName());
}
LLVM_ATTRIBUTE_ALWAYS_INLINE void Set(T value) {
this->DoSet(value, GetName());
}
LLVM_ATTRIBUTE_ALWAYS_INLINE SimDataValue& operator=(T value) {
Set(value);
return *this;
}
};
/**
* Define a name functor for use with SimDataValue.
* This creates a function named GetNAMEName() that returns "NAME".
* @param NAME the name to return
*/
#define HAL_SIMDATAVALUE_DEFINE_NAME(NAME) \
static LLVM_ATTRIBUTE_ALWAYS_INLINE constexpr const char* \
Get##NAME##Name() { \
return #NAME; \
}
/**
* Define a standard C API for simulation data.
*
* Functions defined:
* - int32 NS_RegisterCAPINAMECallback(
* int32_t index, HAL_NotifyCallback callback, void* param,
* HAL_Bool initialNotify)
* - void NS_CancelCAPINAMECallback(int32_t index, int32_t uid)
* - TYPE NS_GetCAPINAME(int32_t index)
* - void NS_SetCAPINAME(int32_t index, TYPE value)
*
* @param TYPE the underlying value type (e.g. double)
* @param NS the "namespace" (e.g. HALSIM)
* @param CAPINAME the C API name (usually first letter capitalized)
* @param DATA the backing data array
* @param LOWERNAME the lowercase name of the backing data variable
*/
#define HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, NS, CAPINAME, DATA, LOWERNAME) \
int32_t NS##_Register##CAPINAME##Callback( \
int32_t index, HAL_NotifyCallback callback, void* param, \
HAL_Bool initialNotify) { \
return DATA[index].LOWERNAME.RegisterCallback(callback, param, \
initialNotify); \
} \
\
void NS##_Cancel##CAPINAME##Callback(int32_t index, int32_t uid) { \
DATA[index].LOWERNAME.CancelCallback(uid); \
} \
\
TYPE NS##_Get##CAPINAME(int32_t index) { return DATA[index].LOWERNAME; } \
\
void NS##_Set##CAPINAME(int32_t index, TYPE LOWERNAME) { \
DATA[index].LOWERNAME = LOWERNAME; \
}
/**
* Define a standard C API for simulation data (channel variant).
*
* Functions defined:
* - int32 NS_RegisterCAPINAMECallback(
* int32_t index, int32_t channel, HAL_NotifyCallback callback,
* void* param, HAL_Bool initialNotify)
* - void NS_CancelCAPINAMECallback(int32_t index, int32_t channel, int32_t uid)
* - TYPE NS_GetCAPINAME(int32_t index, int32_t channel)
* - void NS_SetCAPINAME(int32_t index, int32_t channel, TYPE value)
*
* @param TYPE the underlying value type (e.g. double)
* @param NS the "namespace" (e.g. HALSIM)
* @param CAPINAME the C API name (usually first letter capitalized)
* @param DATA the backing data array
* @param LOWERNAME the lowercase name of the backing data variable array
*/
#define HAL_SIMDATAVALUE_DEFINE_CAPI_CHANNEL(TYPE, NS, CAPINAME, DATA, \
LOWERNAME) \
int32_t NS##_Register##CAPINAME##Callback( \
int32_t index, int32_t channel, HAL_NotifyCallback callback, \
void* param, HAL_Bool initialNotify) { \
return DATA[index].LOWERNAME[channel].RegisterCallback(callback, param, \
initialNotify); \
} \
\
void NS##_Cancel##CAPINAME##Callback(int32_t index, int32_t channel, \
int32_t uid) { \
DATA[index].LOWERNAME[channel].CancelCallback(uid); \
} \
\
TYPE NS##_Get##CAPINAME(int32_t index, int32_t channel) { \
return DATA[index].LOWERNAME[channel]; \
} \
\
void NS##_Set##CAPINAME(int32_t index, int32_t channel, TYPE LOWERNAME) { \
DATA[index].LOWERNAME[channel] = LOWERNAME; \
}
/**
* Define a standard C API for simulation data (no index variant).
*
* Functions defined:
* - int32 NS_RegisterCAPINAMECallback(
* HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify)
* - void NS_CancelCAPINAMECallback(int32_t uid)
* - TYPE NS_GetCAPINAME(void)
* - void NS_SetCAPINAME(TYPE value)
*
* @param TYPE the underlying value type (e.g. double)
* @param NS the "namespace" (e.g. HALSIM)
* @param CAPINAME the C API name (usually first letter capitalized)
* @param DATA the backing data pointer
* @param LOWERNAME the lowercase name of the backing data variable
*/
#define HAL_SIMDATAVALUE_DEFINE_CAPI_NOINDEX(TYPE, NS, CAPINAME, DATA, \
LOWERNAME) \
int32_t NS##_Register##CAPINAME##Callback( \
HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) { \
return DATA->LOWERNAME.RegisterCallback(callback, param, initialNotify); \
} \
\
void NS##_Cancel##CAPINAME##Callback(int32_t uid) { \
DATA->LOWERNAME.CancelCallback(uid); \
} \
\
TYPE NS##_Get##CAPINAME(void) { return DATA->LOWERNAME; } \
\
void NS##_Set##CAPINAME(TYPE LOWERNAME) { DATA->LOWERNAME = LOWERNAME; }
} // namespace hal