2015-08-06 17:42:23 +00:00
|
|
|
//
|
|
|
|
|
// Copyright 2015 Ettus Research LLC
|
2018-02-19 23:30:32 +00:00
|
|
|
// Copyright 2018 Ettus Research, a National Instruments Company
|
2015-08-06 17:42:23 +00:00
|
|
|
//
|
2018-02-19 23:30:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2015-08-06 17:42:23 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include <uhd/error.h>
|
|
|
|
|
#include <uhd/exception.hpp>
|
2015-08-12 19:19:20 +00:00
|
|
|
#include <uhd/utils/static.hpp>
|
|
|
|
|
#include <cstring>
|
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-07-06 14:51:55 +00:00
|
|
|
#include <mutex>
|
2015-08-06 17:42:23 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
#define MAP_TO_ERROR(exception_type, error_type) \
|
|
|
|
|
if (dynamic_cast<const uhd::exception_type*>(e)) \
|
|
|
|
|
return error_type;
|
2015-08-06 17:42:23 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
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)
|
2015-08-06 17:42:23 +00:00
|
|
|
MAP_TO_ERROR(not_implemented_error, UHD_ERROR_NOT_IMPLEMENTED)
|
2020-03-02 23:25:13 +00:00
|
|
|
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)
|
2015-08-06 17:42:23 +00:00
|
|
|
|
|
|
|
|
return UHD_ERROR_EXCEPT;
|
|
|
|
|
}
|
2015-08-12 19:19:20 +00:00
|
|
|
|
|
|
|
|
// Store the error string in a single place in library
|
2017-08-01 20:35:58 +00:00
|
|
|
// 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.
|
2015-08-12 19:19:20 +00:00
|
|
|
UHD_SINGLETON_FCN(std::string, _c_global_error_string)
|
|
|
|
|
|
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-07-06 14:51:55 +00:00
|
|
|
static std::mutex _error_c_mutex;
|
2015-08-12 19:19:20 +00:00
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
std::string get_c_global_error_string()
|
|
|
|
|
{
|
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-07-06 14:51:55 +00:00
|
|
|
std::lock_guard<std::mutex> lock(_error_c_mutex);
|
2015-08-12 19:19:20 +00:00
|
|
|
return _c_global_error_string();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
void set_c_global_error_string(const std::string& msg)
|
|
|
|
|
{
|
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-07-06 14:51:55 +00:00
|
|
|
std::lock_guard<std::mutex> lock(_error_c_mutex);
|
2015-08-12 19:19:20 +00:00
|
|
|
_c_global_error_string() = msg;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:25:13 +00:00
|
|
|
uhd_error uhd_get_last_error(char* error_out, size_t strbuffer_len)
|
|
|
|
|
{
|
|
|
|
|
try {
|
2017-08-01 20:35:58 +00:00
|
|
|
auto error_str = get_c_global_error_string();
|
2015-08-12 19:19:20 +00:00
|
|
|
memset(error_out, '\0', strbuffer_len);
|
2017-08-01 20:35:58 +00:00
|
|
|
strncpy(error_out, error_str.c_str(), strbuffer_len);
|
2020-03-02 23:25:13 +00:00
|
|
|
} catch (...) {
|
2015-08-12 19:19:20 +00:00
|
|
|
return UHD_ERROR_UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
return UHD_ERROR_NONE;
|
|
|
|
|
}
|