Replaced instances of std::unique_lock with std::lock_guard where possible

If a lock is used with a mutex that doesn't need to be unlocked again before the lock is destroyed, std::lock_guard can be more efficient than std::unique_lock due to less overhead.

This commit also removes a redundant set of curly braces in PIDController.cpp intended to constrain a lock's scope.

Change-Id: Idd692ce439528ddb319a4c62c40c7351a664eb97
This commit is contained in:
Tyler Veness
2015-09-01 16:47:57 -07:00
committed by Brad Miller (WPI)
parent f64b055499
commit c0ecde302f
20 changed files with 225 additions and 227 deletions

View File

@@ -19,7 +19,7 @@ priority_recursive_mutex Resource::m_createLock;
* have been allocated yet. The indicies of the resources are [0 .. elements - 1].
*/
Resource::Resource(uint32_t elements) {
std::unique_lock<priority_recursive_mutex> sync(m_createLock);
std::lock_guard<priority_recursive_mutex> sync(m_createLock);
m_isAllocated = std::vector<bool>(elements, false);
}
@@ -35,7 +35,7 @@ Resource::Resource(uint32_t elements) {
*/
/*static*/ void Resource::CreateResourceObject(Resource **r, uint32_t elements)
{
std::unique_lock<priority_recursive_mutex> sync(m_createLock);
std::lock_guard<priority_recursive_mutex> sync(m_createLock);
if (*r == NULL)
{
*r = new Resource(elements);
@@ -49,7 +49,7 @@ Resource::Resource(uint32_t elements) {
*/
uint32_t Resource::Allocate(const char *resourceDesc)
{
std::unique_lock<priority_recursive_mutex> sync(m_allocateLock);
std::lock_guard<priority_recursive_mutex> sync(m_allocateLock);
for (uint32_t i=0; i < m_isAllocated.size(); i++)
{
if (!m_isAllocated[i])
@@ -69,7 +69,7 @@ uint32_t Resource::Allocate(const char *resourceDesc)
*/
uint32_t Resource::Allocate(uint32_t index, const char *resourceDesc)
{
std::unique_lock<priority_recursive_mutex> sync(m_allocateLock);
std::lock_guard<priority_recursive_mutex> sync(m_allocateLock);
if (index >= m_isAllocated.size())
{
// TODO: wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, resourceDesc);
@@ -92,7 +92,7 @@ uint32_t Resource::Allocate(uint32_t index, const char *resourceDesc)
*/
void Resource::Free(uint32_t index)
{
std::unique_lock<priority_recursive_mutex> sync(m_allocateLock);
std::lock_guard<priority_recursive_mutex> sync(m_allocateLock);
if (index == ~0ul) return;
if (index >= m_isAllocated.size())
{