Use std::atomic for counters

This commit is contained in:
Lovell Fuller 2023-09-30 14:01:04 +01:00
parent 265d70111a
commit 70ac6905c7
6 changed files with 16 additions and 16 deletions

View File

@ -166,10 +166,10 @@ namespace sharp {
}
// How many tasks are in the queue?
volatile int counterQueue = 0;
std::atomic<int> counterQueue{0};
// How many tasks are being processed?
volatile int counterProcess = 0;
std::atomic<int> counterProcess{0};
// Filename extension checkers
static bool EndsWith(std::string const &str, std::string const &end) {

View File

@ -7,6 +7,7 @@
#include <string>
#include <tuple>
#include <vector>
#include <atomic>
#include <napi.h>
#include <vips/vips8>
@ -161,10 +162,10 @@ namespace sharp {
};
// How many tasks are in the queue?
extern volatile int counterQueue;
extern std::atomic<int> counterQueue;
// How many tasks are being processed?
extern volatile int counterProcess;
extern std::atomic<int> counterProcess;
// Filename extension checkers
bool IsJpeg(std::string const &str);

View File

@ -18,7 +18,7 @@ class MetadataWorker : public Napi::AsyncWorker {
void Execute() {
// Decrement queued task counter
g_atomic_int_dec_and_test(&sharp::counterQueue);
sharp::counterQueue--;
vips::VImage image;
sharp::ImageType imageType = sharp::ImageType::UNKNOWN;
@ -281,7 +281,7 @@ Napi::Value metadata(const Napi::CallbackInfo& info) {
worker->Queue();
// Increment queued task counter
g_atomic_int_inc(&sharp::counterQueue);
sharp::counterQueue++;
return info.Env().Undefined();
}

View File

@ -44,9 +44,9 @@ class PipelineWorker : public Napi::AsyncWorker {
// libuv worker
void Execute() {
// Decrement queued task counter
g_atomic_int_dec_and_test(&sharp::counterQueue);
sharp::counterQueue--;
// Increment processing task counter
g_atomic_int_inc(&sharp::counterProcess);
sharp::counterProcess++;
try {
// Open input
@ -1289,8 +1289,8 @@ class PipelineWorker : public Napi::AsyncWorker {
delete baton;
// Decrement processing task counter
g_atomic_int_dec_and_test(&sharp::counterProcess);
Napi::Number queueLength = Napi::Number::New(env, static_cast<double>(sharp::counterQueue));
sharp::counterProcess--;
Napi::Number queueLength = Napi::Number::New(env, static_cast<int>(sharp::counterQueue));
queueListener.MakeCallback(Receiver().Value(), { queueLength });
}
@ -1707,8 +1707,7 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
worker->Queue();
// Increment queued task counter
g_atomic_int_inc(&sharp::counterQueue);
Napi::Number queueLength = Napi::Number::New(info.Env(), static_cast<double>(sharp::counterQueue));
Napi::Number queueLength = Napi::Number::New(info.Env(), static_cast<int>(++sharp::counterQueue));
queueListener.MakeCallback(info.This(), { queueLength });
return info.Env().Undefined();

View File

@ -30,7 +30,7 @@ class StatsWorker : public Napi::AsyncWorker {
void Execute() {
// Decrement queued task counter
g_atomic_int_dec_and_test(&sharp::counterQueue);
sharp::counterQueue--;
vips::VImage image;
sharp::ImageType imageType = sharp::ImageType::UNKNOWN;
@ -177,7 +177,7 @@ Napi::Value stats(const Napi::CallbackInfo& info) {
worker->Queue();
// Increment queued task counter
g_atomic_int_inc(&sharp::counterQueue);
sharp::counterQueue++;
return info.Env().Undefined();
}

View File

@ -70,8 +70,8 @@ Napi::Value concurrency(const Napi::CallbackInfo& info) {
*/
Napi::Value counters(const Napi::CallbackInfo& info) {
Napi::Object counters = Napi::Object::New(info.Env());
counters.Set("queue", sharp::counterQueue);
counters.Set("process", sharp::counterProcess);
counters.Set("queue", static_cast<int>(sharp::counterQueue));
counters.Set("process", static_cast<int>(sharp::counterProcess));
return counters;
}