mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-29 02:21:44 +00:00
Add format script which invokes clang-format on the C++ source code (#41)
On Windows machines, clang-format.exe must be in the PATH environment variable.
This commit is contained in:
committed by
Peter Johnson
parent
68690643d2
commit
e14e45da76
@@ -6,8 +6,8 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "HAL/cpp/Resource.hpp"
|
||||
#include "HAL/Errors.hpp"
|
||||
#include <cstddef>
|
||||
#include "HAL/Errors.hpp"
|
||||
#include "HAL/cpp/priority_mutex.h"
|
||||
|
||||
namespace hal {
|
||||
@@ -16,8 +16,9 @@ priority_recursive_mutex Resource::m_createLock;
|
||||
|
||||
/**
|
||||
* Allocate storage for a new instance of Resource.
|
||||
* Allocate a bool array of values that will get initialized to indicate that no resources
|
||||
* have been allocated yet. The indicies of the resources are [0 .. elements - 1].
|
||||
* Allocate a bool array of values that will get initialized to indicate that no
|
||||
* resources have been allocated yet. The indicies of the resources are [0 ..
|
||||
* elements - 1].
|
||||
*/
|
||||
Resource::Resource(uint32_t elements) {
|
||||
std::lock_guard<priority_recursive_mutex> sync(m_createLock);
|
||||
@@ -34,78 +35,69 @@ Resource::Resource(uint32_t elements) {
|
||||
* track, that is, it will allocate resource numbers in the range
|
||||
* [0 .. elements - 1].
|
||||
*/
|
||||
/*static*/ void Resource::CreateResourceObject(Resource **r, uint32_t elements)
|
||||
{
|
||||
std::lock_guard<priority_recursive_mutex> sync(m_createLock);
|
||||
if (*r == NULL)
|
||||
{
|
||||
*r = new Resource(elements);
|
||||
}
|
||||
/*static*/ void Resource::CreateResourceObject(Resource** r,
|
||||
uint32_t elements) {
|
||||
std::lock_guard<priority_recursive_mutex> sync(m_createLock);
|
||||
if (*r == NULL) {
|
||||
*r = new Resource(elements);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate a resource.
|
||||
* When a resource is requested, mark it allocated. In this case, a free resource value
|
||||
* When a resource is requested, mark it allocated. In this case, a free
|
||||
* resource value
|
||||
* within the range is located and returned after it is marked allocated.
|
||||
*/
|
||||
uint32_t Resource::Allocate(const char *resourceDesc)
|
||||
{
|
||||
std::lock_guard<priority_recursive_mutex> sync(m_allocateLock);
|
||||
for (uint32_t i=0; i < m_isAllocated.size(); i++)
|
||||
{
|
||||
if (!m_isAllocated[i])
|
||||
{
|
||||
m_isAllocated[i] = true;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
// TODO: wpi_setWPIErrorWithContext(NoAvailableResources, resourceDesc);
|
||||
return ~0ul;
|
||||
uint32_t Resource::Allocate(const char* resourceDesc) {
|
||||
std::lock_guard<priority_recursive_mutex> sync(m_allocateLock);
|
||||
for (uint32_t i = 0; i < m_isAllocated.size(); i++) {
|
||||
if (!m_isAllocated[i]) {
|
||||
m_isAllocated[i] = true;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
// TODO: wpi_setWPIErrorWithContext(NoAvailableResources, resourceDesc);
|
||||
return ~0ul;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate a specific resource value.
|
||||
* The user requests a specific resource value, i.e. channel number and it is verified
|
||||
* unallocated, then returned.
|
||||
* The user requests a specific resource value, i.e. channel number and it is
|
||||
* verified unallocated, then returned.
|
||||
*/
|
||||
uint32_t Resource::Allocate(uint32_t index, const char *resourceDesc)
|
||||
{
|
||||
std::lock_guard<priority_recursive_mutex> sync(m_allocateLock);
|
||||
if (index >= m_isAllocated.size())
|
||||
{
|
||||
// TODO: wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, resourceDesc);
|
||||
return ~0ul;
|
||||
}
|
||||
if ( m_isAllocated[index] )
|
||||
{
|
||||
// TODO: wpi_setWPIErrorWithContext(ResourceAlreadyAllocated, resourceDesc);
|
||||
return ~0ul;
|
||||
}
|
||||
m_isAllocated[index] = true;
|
||||
return index;
|
||||
uint32_t Resource::Allocate(uint32_t index, const char* resourceDesc) {
|
||||
std::lock_guard<priority_recursive_mutex> sync(m_allocateLock);
|
||||
if (index >= m_isAllocated.size()) {
|
||||
// TODO: wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, resourceDesc);
|
||||
return ~0ul;
|
||||
}
|
||||
if (m_isAllocated[index]) {
|
||||
// TODO: wpi_setWPIErrorWithContext(ResourceAlreadyAllocated, resourceDesc);
|
||||
return ~0ul;
|
||||
}
|
||||
m_isAllocated[index] = true;
|
||||
return index;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Free an allocated resource.
|
||||
* After a resource is no longer needed, for example a destructor is called for a channel assignment
|
||||
* class, Free will release the resource value so it can be reused somewhere else in the program.
|
||||
* After a resource is no longer needed, for example a destructor is called for
|
||||
* a channel assignment class, Free will release the resource value so it can
|
||||
* be reused somewhere else in the program.
|
||||
*/
|
||||
void Resource::Free(uint32_t index)
|
||||
{
|
||||
std::lock_guard<priority_recursive_mutex> sync(m_allocateLock);
|
||||
if (index == ~0ul) return;
|
||||
if (index >= m_isAllocated.size())
|
||||
{
|
||||
// TODO: wpi_setWPIError(NotAllocated);
|
||||
return;
|
||||
}
|
||||
if (!m_isAllocated[index])
|
||||
{
|
||||
// TODO: wpi_setWPIError(NotAllocated);
|
||||
return;
|
||||
}
|
||||
m_isAllocated[index] = false;
|
||||
void Resource::Free(uint32_t index) {
|
||||
std::lock_guard<priority_recursive_mutex> sync(m_allocateLock);
|
||||
if (index == ~0ul) return;
|
||||
if (index >= m_isAllocated.size()) {
|
||||
// TODO: wpi_setWPIError(NotAllocated);
|
||||
return;
|
||||
}
|
||||
if (!m_isAllocated[index]) {
|
||||
// TODO: wpi_setWPIError(NotAllocated);
|
||||
return;
|
||||
}
|
||||
m_isAllocated[index] = false;
|
||||
}
|
||||
|
||||
} // namespace hal
|
||||
|
||||
@@ -7,9 +7,7 @@
|
||||
|
||||
#include "HAL/cpp/Semaphore.hpp"
|
||||
|
||||
Semaphore::Semaphore(uint32_t count) {
|
||||
m_count = count;
|
||||
}
|
||||
Semaphore::Semaphore(uint32_t count) { m_count = count; }
|
||||
|
||||
void Semaphore::give() {
|
||||
std::lock_guard<priority_mutex> lock(m_mutex);
|
||||
@@ -19,7 +17,7 @@ void Semaphore::give() {
|
||||
|
||||
void Semaphore::take() {
|
||||
std::unique_lock<priority_mutex> lock(m_mutex);
|
||||
m_condition.wait(lock, [this] { return m_count; } );
|
||||
m_condition.wait(lock, [this] { return m_count; });
|
||||
--m_count;
|
||||
}
|
||||
|
||||
@@ -28,8 +26,7 @@ bool Semaphore::tryTake() {
|
||||
if (m_count) {
|
||||
--m_count;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,34 +7,22 @@
|
||||
|
||||
#include "HAL/cpp/priority_mutex.h"
|
||||
|
||||
void priority_recursive_mutex::lock() {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
}
|
||||
void priority_recursive_mutex::lock() { pthread_mutex_lock(&m_mutex); }
|
||||
|
||||
void priority_recursive_mutex::unlock() {
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
}
|
||||
void priority_recursive_mutex::unlock() { pthread_mutex_unlock(&m_mutex); }
|
||||
|
||||
bool priority_recursive_mutex::try_lock() noexcept {
|
||||
return !pthread_mutex_trylock(&m_mutex);
|
||||
}
|
||||
|
||||
pthread_mutex_t* priority_recursive_mutex::native_handle() {
|
||||
return &m_mutex;
|
||||
}
|
||||
pthread_mutex_t* priority_recursive_mutex::native_handle() { return &m_mutex; }
|
||||
|
||||
void priority_mutex::lock() {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
}
|
||||
void priority_mutex::lock() { pthread_mutex_lock(&m_mutex); }
|
||||
|
||||
void priority_mutex::unlock() {
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
}
|
||||
void priority_mutex::unlock() { pthread_mutex_unlock(&m_mutex); }
|
||||
|
||||
bool priority_mutex::try_lock() noexcept {
|
||||
return !pthread_mutex_trylock(&m_mutex);
|
||||
}
|
||||
|
||||
pthread_mutex_t* priority_mutex::native_handle() {
|
||||
return &m_mutex;
|
||||
}
|
||||
pthread_mutex_t* priority_mutex::native_handle() { return &m_mutex; }
|
||||
|
||||
Reference in New Issue
Block a user