mirror of
https://github.com/scorpion-26/gBar.git
synced 2024-11-21 18:52:49 +00:00
Only allow sequential runs of package checking
If the current monitor would quickly change, GetOutdatedPackagesAsync would be called while the previous instance is still running, which caused crashes. Now, GetOutdatedPackagesAsync only uses one instance/callback a time.
This commit is contained in:
parent
2c50bed1d8
commit
5a2f75dd0d
2 changed files with 38 additions and 27 deletions
|
@ -576,17 +576,28 @@ namespace System
|
|||
|
||||
void GetOutdatedPackagesAsync(std::function<void(uint32_t)>&& returnVal)
|
||||
{
|
||||
static bool currentlyRunning = false;
|
||||
static std::function<void(uint32_t)> handlerFunction;
|
||||
static std::mutex configMutex;
|
||||
configMutex.lock();
|
||||
|
||||
{
|
||||
std::scoped_lock<std::mutex> lock(configMutex);
|
||||
if (!RuntimeConfig::Get().hasPackagesScript)
|
||||
{
|
||||
configMutex.unlock();
|
||||
return; // Don't bother
|
||||
}
|
||||
configMutex.unlock();
|
||||
handlerFunction = returnVal;
|
||||
if (currentlyRunning)
|
||||
{
|
||||
// Thread is running, only update handler
|
||||
return;
|
||||
}
|
||||
|
||||
currentlyRunning = true;
|
||||
}
|
||||
|
||||
std::thread(
|
||||
[](std::function<void(uint32_t)> returnVal)
|
||||
[&]()
|
||||
{
|
||||
// We need a pipe, since there is no "libpacman". This should only be called every so often anyways
|
||||
std::string number;
|
||||
|
@ -601,29 +612,28 @@ namespace System
|
|||
ASSERT(feof(pipe), "GetOutdatedPackages: Cannot read to eof!");
|
||||
|
||||
int exitCode = pclose(pipe) / 256;
|
||||
{
|
||||
std::scoped_lock<std::mutex> lock(configMutex);
|
||||
if (exitCode != 0)
|
||||
{
|
||||
configMutex.lock();
|
||||
// Invalid script/error
|
||||
LOG("GetOutdatedPackages: Invalid command. Disabling package widget!");
|
||||
RuntimeConfig::Get().hasPackagesScript = false;
|
||||
configMutex.unlock();
|
||||
currentlyRunning = false;
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
returnVal(std::stoul(buf));
|
||||
handlerFunction(std::stoul(buf));
|
||||
}
|
||||
catch (std::invalid_argument&)
|
||||
{
|
||||
configMutex.lock();
|
||||
LOG("GetOutdatedPackages: Invalid output of the package script. Disabling package widget!");
|
||||
RuntimeConfig::Get().hasPackagesScript = false;
|
||||
configMutex.unlock();
|
||||
return;
|
||||
}
|
||||
},
|
||||
std::move(returnVal))
|
||||
currentlyRunning = false;
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
|
|
|
@ -108,6 +108,7 @@ namespace System
|
|||
// Bytes per second download. dx is time since last call. Will always return 0 on first run
|
||||
double GetNetworkBpsDownload(double dt);
|
||||
|
||||
// This can only be called one at a time. If it is already running it is assumed, that the old handler is no longer valid.
|
||||
void GetOutdatedPackagesAsync(std::function<void(uint32_t)>&& returnVal);
|
||||
|
||||
std::string GetTime();
|
||||
|
|
Loading…
Reference in a new issue