Increase the background thread priority
This commit is contained in:
parent
b0c8495f6c
commit
0f10991dd9
3 changed files with 53 additions and 1 deletions
|
|
@ -99,6 +99,10 @@ namespace config {
|
||||||
static constexpr double amplitudeTriangle = 0.625;
|
static constexpr double amplitudeTriangle = 0.625;
|
||||||
static constexpr double amplitudeSaw = 0.515;
|
static constexpr double amplitudeSaw = 0.515;
|
||||||
static constexpr double amplitudeSquare = 0.515;
|
static constexpr double amplitudeSquare = 0.515;
|
||||||
|
/**
|
||||||
|
Background file loading
|
||||||
|
*/
|
||||||
|
static constexpr int backgroundLoaderPthreadPriority = 50; // expressed in %
|
||||||
} // namespace config
|
} // namespace config
|
||||||
|
|
||||||
} // namespace sfz
|
} // namespace sfz
|
||||||
|
|
|
||||||
|
|
@ -37,8 +37,14 @@
|
||||||
#include "absl/memory/memory.h"
|
#include "absl/memory/memory.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <sndfile.hh>
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <system_error>
|
||||||
|
#include <sndfile.hh>
|
||||||
|
#if defined(_WIN32)
|
||||||
|
#include <windows.h>
|
||||||
|
#else
|
||||||
|
#include <pthread.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
void readBaseFile(SndfileHandle& sndFile, sfz::FileAudioBuffer& output, uint32_t numFrames, bool reverse)
|
void readBaseFile(SndfileHandle& sndFile, sfz::FileAudioBuffer& output, uint32_t numFrames, bool reverse)
|
||||||
{
|
{
|
||||||
|
|
@ -365,6 +371,8 @@ void sfz::FilePool::tryToClearPromises()
|
||||||
|
|
||||||
void sfz::FilePool::clearingThread()
|
void sfz::FilePool::clearingThread()
|
||||||
{
|
{
|
||||||
|
raiseCurrentThreadPriority();
|
||||||
|
|
||||||
RTSemaphore& request = semClearingRequest;
|
RTSemaphore& request = semClearingRequest;
|
||||||
do {
|
do {
|
||||||
request.wait();
|
request.wait();
|
||||||
|
|
@ -376,6 +384,8 @@ void sfz::FilePool::clearingThread()
|
||||||
|
|
||||||
void sfz::FilePool::loadingThread() noexcept
|
void sfz::FilePool::loadingThread() noexcept
|
||||||
{
|
{
|
||||||
|
raiseCurrentThreadPriority();
|
||||||
|
|
||||||
FilePromisePtr promise;
|
FilePromisePtr promise;
|
||||||
do {
|
do {
|
||||||
workerBarrier.wait();
|
workerBarrier.wait();
|
||||||
|
|
@ -502,3 +512,36 @@ void sfz::FilePool::waitForBackgroundLoading() noexcept
|
||||||
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void sfz::FilePool::raiseCurrentThreadPriority() noexcept
|
||||||
|
{
|
||||||
|
#if defined(_WIN32)
|
||||||
|
#pragma message("Implement Win32 thread background priority")
|
||||||
|
HANDLE thread = GetCurrentThread();
|
||||||
|
const int priority = THREAD_PRIORITY_ABOVE_NORMAL; /*THREAD_PRIORITY_HIGHEST*/
|
||||||
|
if (!SetThreadPriority(thread, priority)) {
|
||||||
|
std::system_error error(GetLastError(), std::system_category());
|
||||||
|
DBG("[sfizz] Cannot set current thread priority: " << error.what());
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
pthread_t thread = pthread_self();
|
||||||
|
int policy;
|
||||||
|
sched_param param;
|
||||||
|
|
||||||
|
if (pthread_getschedparam(thread, &policy, ¶m) != 0) {
|
||||||
|
DBG("[sfizz] Cannot get current thread scheduling parameters");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
policy = SCHED_RR;
|
||||||
|
const int minprio = sched_get_priority_min(policy);
|
||||||
|
const int maxprio = sched_get_priority_max(policy);
|
||||||
|
param.sched_priority = minprio +
|
||||||
|
config::backgroundLoaderPthreadPriority * (maxprio - minprio) / 100;
|
||||||
|
|
||||||
|
if (pthread_setschedparam(thread, policy, ¶m) != 0) {
|
||||||
|
DBG("[sfizz] Cannot set current thread scheduling parameters");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -259,6 +259,11 @@ public:
|
||||||
* in the queue.
|
* in the queue.
|
||||||
*/
|
*/
|
||||||
void waitForBackgroundLoading() noexcept;
|
void waitForBackgroundLoading() noexcept;
|
||||||
|
/**
|
||||||
|
* @brief Assign the current thread a priority which is appropriate
|
||||||
|
* for background sample file processing.
|
||||||
|
*/
|
||||||
|
static void raiseCurrentThreadPriority() noexcept;
|
||||||
private:
|
private:
|
||||||
Logger& logger;
|
Logger& logger;
|
||||||
fs::path rootDirectory;
|
fs::path rootDirectory;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue