From 8b3fdc7bc69739ca69f2511eb75ac36a62f9b48d Mon Sep 17 00:00:00 2001 From: Pedro Maciel Date: Sat, 13 Jan 2024 00:11:53 +0000 Subject: [PATCH] Work on eckit_geo --- src/eccodes/geo/GribSpec.cc | 62 +++++++------------------------------ tests/gribspec.cc | 18 ++++++----- 2 files changed, 23 insertions(+), 57 deletions(-) diff --git a/src/eccodes/geo/GribSpec.cc b/src/eccodes/geo/GribSpec.cc index f1722f647..814fecd2f 100644 --- a/src/eccodes/geo/GribSpec.cc +++ b/src/eccodes/geo/GribSpec.cc @@ -21,17 +21,10 @@ #include #include -#define ECKIT_THREADS -#if defined(ECKIT_THREADS) -#include "eckit/thread/AutoLock.h" -#include "eckit/thread/Mutex.h" -#else -#include -#endif - #include "eckit/config/Resource.h" #include "eckit/exception/Exceptions.h" #include "eckit/geo/PointLonLat.h" +#include "eckit/geo/util/Mutex.h" #include "eckit/log/JSON.h" #include "eckit/types/FloatCompare.h" #include "eckit/types/Fraction.h" @@ -561,46 +554,15 @@ bool get_value(const std::string& name, codes_handle* h, T& value, const Process } -} // namespace - - -namespace util { - +eckit::geo::util::recursive_mutex MUTEX; -#if defined(ECKIT_THREADS) - -using recursive_mutex = eckit::Mutex; - -template -using lock_guard = typename eckit::AutoLock; - -struct once_flag { - pthread_once_t once_ = PTHREAD_ONCE_INIT; +class lock_type { + eckit::geo::util::lock_guard lock_guard_{MUTEX}; }; -template -inline void call_once(once_flag& flag, Callable&& fun) { - pthread_once(&(flag.once_), fun); -} - - -#else - - -using std::call_once; -using std::lock_guard; -using std::once_flag; -using std::recursive_mutex; - -#endif - - -} // namespace util - - -static util::recursive_mutex MUTEX; +} // namespace GribSpec::GribSpec(codes_handle* h) : handle_(h) { @@ -609,7 +571,7 @@ GribSpec::GribSpec(codes_handle* h) : handle_(h) { bool GribSpec::has(const std::string& name) const { - util::lock_guard lock(MUTEX); + lock_type lock; if (cache_.has(name)) { return true; @@ -627,7 +589,7 @@ bool GribSpec::has(const std::string& name) const { bool GribSpec::get(const std::string& name, std::string& value) const { - util::lock_guard lock(MUTEX); + lock_type lock; if (cache_.get(name, value)) { return true; @@ -666,7 +628,7 @@ bool GribSpec::get(const std::string& name, std::string& value) const { bool GribSpec::get(const std::string& name, bool& value) const { - util::lock_guard lock(MUTEX); + lock_type lock; if (cache_.get(name, value)) { return true; @@ -701,7 +663,7 @@ bool GribSpec::get(const std::string& name, int& value) const { bool GribSpec::get(const std::string& name, long& value) const { - util::lock_guard lock(MUTEX); + lock_type lock; if (cache_.get(name, value)) { return true; @@ -750,7 +712,7 @@ bool GribSpec::get(const std::string& name, float& value) const { bool GribSpec::get(const std::string& name, double& value) const { - util::lock_guard lock(MUTEX); + lock_type lock; if (cache_.get(name, value)) { return true; @@ -797,7 +759,7 @@ bool GribSpec::get(const std::string& /*name*/, std::vector& /*value*/) con bool GribSpec::get(const std::string& name, std::vector& value) const { - util::lock_guard lock(MUTEX); + lock_type lock; if (cache_.get(name, value)) { return true; @@ -865,7 +827,7 @@ bool GribSpec::get(const std::string& name, std::vector& value) const { bool GribSpec::get(const std::string& name, std::vector& value) const { - util::lock_guard lock(MUTEX); + lock_type lock; if (cache_.get(name, value)) { return true; diff --git a/tests/gribspec.cc b/tests/gribspec.cc index ad1145f65..d6403dd6a 100644 --- a/tests/gribspec.cc +++ b/tests/gribspec.cc @@ -22,10 +22,11 @@ int main(int argc, const char* argv[]) { - ASSERT_MSG(argc == 3, "ERROR: expected 2 arguments: grib_file json_file"); + // FIXME processes only the first message in the GRIB file + ASSERT_MSG(argc == 2 || argc == 3, "ERROR: expected 1 or 2 arguments: grib_file [json_file]"); - // create a JSON string ("GribSpec") from the GRIB file + // Create a JSON string from the GRIB file ("GribSpec") std::string result_string; auto* grib_file = std::fopen(argv[1], "r"); @@ -37,21 +38,24 @@ int main(int argc, const char* argv[]) { ASSERT(err == CODES_SUCCESS); ASSERT(h != nullptr); - result_string = (std::ostringstream{} << eccodes::geo::GribSpec(h)).str(); - std::cout << result_string << std::endl; + if (argc == 2) { + std::cout << eccodes::geo::GribSpec(h) << std::endl; + return 0; + } - break; // FIXME processes only the first message + result_string = (std::ostringstream{} << eccodes::geo::GribSpec(h)).str(); + break; } std::fclose(grib_file); - // Parse the resulting JSON ("GribSpec") from the GRIB file + // Parse result from the GRIB file ("GribSpec") std::istringstream result_stream(result_string); auto result = eckit::JSONParser(result_stream).parse(); - // Parse the reference JSON from the JSON file + // Parse reference from the JSON file std::ifstream json_stream(argv[2]); auto reference = eckit::JSONParser(json_stream).parse(); json_stream.close();