diff --git a/src/common.cc b/src/common.cc index b7b78ceb..9e2eacde 100644 --- a/src/common.cc +++ b/src/common.cc @@ -166,10 +166,10 @@ namespace sharp { } // How many tasks are in the queue? - volatile int counterQueue = 0; + std::atomic counterQueue{0}; // How many tasks are being processed? - volatile int counterProcess = 0; + std::atomic counterProcess{0}; // Filename extension checkers static bool EndsWith(std::string const &str, std::string const &end) { diff --git a/src/common.h b/src/common.h index a9425945..f4a59c58 100644 --- a/src/common.h +++ b/src/common.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -161,10 +162,10 @@ namespace sharp { }; // How many tasks are in the queue? - extern volatile int counterQueue; + extern std::atomic counterQueue; // How many tasks are being processed? - extern volatile int counterProcess; + extern std::atomic counterProcess; // Filename extension checkers bool IsJpeg(std::string const &str); diff --git a/src/metadata.cc b/src/metadata.cc index 6bb861f4..83173c66 100644 --- a/src/metadata.cc +++ b/src/metadata.cc @@ -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(); } diff --git a/src/pipeline.cc b/src/pipeline.cc index 5596d86f..4bef6642 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -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(sharp::counterQueue)); + sharp::counterProcess--; + Napi::Number queueLength = Napi::Number::New(env, static_cast(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(sharp::counterQueue)); + Napi::Number queueLength = Napi::Number::New(info.Env(), static_cast(++sharp::counterQueue)); queueListener.MakeCallback(info.This(), { queueLength }); return info.Env().Undefined(); diff --git a/src/stats.cc b/src/stats.cc index c6c0ccc0..74454190 100644 --- a/src/stats.cc +++ b/src/stats.cc @@ -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(); } diff --git a/src/utilities.cc b/src/utilities.cc index 2ff4e53b..369d744a 100644 --- a/src/utilities.cc +++ b/src/utilities.cc @@ -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(sharp::counterQueue)); + counters.Set("process", static_cast(sharp::counterProcess)); return counters; }