uhd/host/lib/error_c.cpp
Martin Braun b6119e581e uhd: Replace Boost mutexes and locks with standard options
This is a very mechanical task that could almost have been done with
sed. Boost versions of mutexes and locks were removed, and replaced with
std:: versions. The replacement tables are as follows:

== Mutexes ==
- boost::mutex -> std::mutex
- boost::recursive_mutex -> std::recursive_mutex

Mutexes behave identically between Boost and std:: and have the same
API.

== Locks ==
C++11 has only two types of lock that we use/need in UHD:
- std::lock_guard: Identical to boost::lock_guard
- std::unique_lock: Identical to boost::unique_lock

Boost also has boost::mutex::scoped_lock, which is a typedef for
boost::unique_lock<>. However, we often have used scoped_lock where we
meant to use lock_guard<>. The name is a bit misleading, "scoped lock"
sounding a bit like an RAII mechanism. Therefore, some previous
boost::mutex::scoped_lock are now std::lock_guard<>.

std::unique_lock is required when doing more than RAII locking (i.e.,
unlocking, relocking, usage with condition variables, etc.).

== Condition Variables ==
Condition variables were out of the scope of this lock/mutex change, but
in UHD, we inconsistently use boost::condition vs.
boost::condition_variable. The former is a templated version of the
latter, and thus works fine with std::mutex'es. Therefore, some
boost::condition_variable where changed to boost::condition.

All locks and mutexes use `#include <mutex>`. The corresponding Boost
includes were removed. In some cases, this exposed issues with implicit
Boost includes elsewhere. The missing explicit includes were added.
2021-10-19 12:21:33 -07:00

67 lines
2 KiB
C++

//
// Copyright 2015 Ettus Research LLC
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
#include <uhd/error.h>
#include <uhd/exception.hpp>
#include <uhd/utils/static.hpp>
#include <cstring>
#include <mutex>
#define MAP_TO_ERROR(exception_type, error_type) \
if (dynamic_cast<const uhd::exception_type*>(e)) \
return error_type;
uhd_error error_from_uhd_exception(const uhd::exception* e)
{
MAP_TO_ERROR(index_error, UHD_ERROR_INDEX)
MAP_TO_ERROR(key_error, UHD_ERROR_KEY)
MAP_TO_ERROR(not_implemented_error, UHD_ERROR_NOT_IMPLEMENTED)
MAP_TO_ERROR(usb_error, UHD_ERROR_USB)
MAP_TO_ERROR(io_error, UHD_ERROR_IO)
MAP_TO_ERROR(os_error, UHD_ERROR_OS)
MAP_TO_ERROR(assertion_error, UHD_ERROR_ASSERTION)
MAP_TO_ERROR(lookup_error, UHD_ERROR_LOOKUP)
MAP_TO_ERROR(type_error, UHD_ERROR_TYPE)
MAP_TO_ERROR(value_error, UHD_ERROR_VALUE)
MAP_TO_ERROR(runtime_error, UHD_ERROR_RUNTIME)
MAP_TO_ERROR(environment_error, UHD_ERROR_ENVIRONMENT)
MAP_TO_ERROR(system_error, UHD_ERROR_SYSTEM)
return UHD_ERROR_EXCEPT;
}
// Store the error string in a single place in library
// Note: Don't call _c_global_error_string() directly, it needs to be locked
// for thread-safety. Use set_c_global_error_string() and
// get_c_global_error_string() instead.
UHD_SINGLETON_FCN(std::string, _c_global_error_string)
static std::mutex _error_c_mutex;
std::string get_c_global_error_string()
{
std::lock_guard<std::mutex> lock(_error_c_mutex);
return _c_global_error_string();
}
void set_c_global_error_string(const std::string& msg)
{
std::lock_guard<std::mutex> lock(_error_c_mutex);
_c_global_error_string() = msg;
}
uhd_error uhd_get_last_error(char* error_out, size_t strbuffer_len)
{
try {
auto error_str = get_c_global_error_string();
memset(error_out, '\0', strbuffer_len);
strncpy(error_out, error_str.c_str(), strbuffer_len);
} catch (...) {
return UHD_ERROR_UNKNOWN;
}
return UHD_ERROR_NONE;
}