Use spin-locks for short-lived, non-recursive locking sequences

This commit is contained in:
vsonnier
2019-03-03 09:49:27 +01:00
parent 792bf20a9c
commit 5ab44e3104
12 changed files with 78 additions and 48 deletions
+2 -2
View File
@@ -521,7 +521,7 @@ void GLFont::drawString(const std::wstring& str, int pxHeight, float xpos, float
if (cacheable) {
gcCounter++;
std::lock_guard<std::mutex> lock(cache_busy);
std::lock_guard<SpinMutex> lock(cache_busy);
if (gcCounter > GC_DRAW_COUNT_PERIOD) {
@@ -793,7 +793,7 @@ void GLFont::doCacheGC() {
void GLFont::clearCache() {
std::lock_guard<std::mutex> lock(cache_busy);
std::lock_guard<SpinMutex> lock(cache_busy);
std::map<std::wstring, GLFontStringCache * >::iterator cache_iter;
+3 -4
View File
@@ -13,6 +13,8 @@
#include "wx/filename.h"
#include "wx/stdpaths.h"
#include "SpinMutex.h"
class GLFontStringCache {
public:
GLFontStringCache();
@@ -76,9 +78,6 @@ private:
class GLFont {
public:
enum Align {
GLFONT_ALIGN_LEFT, GLFONT_ALIGN_RIGHT, GLFONT_ALIGN_CENTER, GLFONT_ALIGN_TOP, GLFONT_ALIGN_BOTTOM
};
@@ -176,7 +175,7 @@ private:
GLuint texId;
int gcCounter;
std::mutex cache_busy;
SpinMutex cache_busy;
public:
+25
View File
@@ -0,0 +1,25 @@
// Copyright (c) Charles J. Cliffe
// SPDX-License-Identifier: GPL-2.0+
#pragma once
#include <atomic>
// A non-recursive Mutex implemented as a spin-lock.
class SpinMutex {
public:
SpinMutex() = default;
SpinMutex(const SpinMutex&) = delete;
SpinMutex& operator=(const SpinMutex&) = delete;
~SpinMutex() { lock_state.clear(std::memory_order_release); }
void lock() { while (lock_state.test_and_set(std::memory_order_acquire)); }
void unlock() { lock_state.clear(std::memory_order_release); }
private:
std::atomic_flag lock_state = ATOMIC_FLAG_INIT;
};