Skip to content

Commit

Permalink
Merge #19477
Browse files Browse the repository at this point in the history
19477: sys /cpp11-compat: remove pseudo anonymous namespaces r=MrKevinWeiss a=kfessel

### Contribution description

remove pseudo anonymous namespaces in favor of defines that are used thoughout the rest of riot 

### Testing procedure

cpp test do not fail 

### Issues/PRs references



Co-authored-by: Karl Fessel <karl.fessel@ovgu.de>
  • Loading branch information
bors[bot] and kfessel authored May 4, 2023
2 parents 0aceb55 + da17ebb commit 26c61be
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 31 deletions.
8 changes: 2 additions & 6 deletions sys/cpp11-compat/include/riot/chrono.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@

namespace riot {

namespace {
constexpr uint32_t microsecs_in_sec = 1000000;
} // namespace anaonymous

/**
* @brief A time point for timed wait, as clocks from the standard are not
* available on RIOT.
Expand Down Expand Up @@ -94,9 +90,9 @@ class time_point {
private:
timex_t m_handle;
void inline adjust_overhead() {
auto secs = m_handle.microseconds / microsecs_in_sec;
auto secs = m_handle.microseconds / US_PER_SEC;
m_handle.seconds += secs;
m_handle.microseconds -= (secs * microsecs_in_sec);
m_handle.microseconds -= (secs * US_PER_SEC);
}
};

Expand Down
34 changes: 11 additions & 23 deletions sys/cpp11-compat/include/riot/thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,17 @@

namespace riot {

namespace {
/**
* @brief Identify uninitialized threads.
*/
constexpr kernel_pid_t thread_uninitialized = -1;
/**
* @brief The stack size for new threads.
*/
constexpr size_t stack_size = THREAD_STACKSIZE_MAIN;
}

/**
* @brief Holds context data for the thread.
*/
struct thread_data {
thread_data() : ref_count{2}, joining_thread{thread_uninitialized} {
thread_data() : ref_count{2}, joining_thread{KERNEL_PID_UNDEF} {
// nop
}
/** @cond INTERNAL */
std::atomic<unsigned> ref_count;
kernel_pid_t joining_thread;
std::array<char, stack_size> stack;
std::array<char, THREAD_STACKSIZE_MAIN> stack;
/** @endcond */
};

Expand Down Expand Up @@ -103,7 +92,7 @@ class thread_id {
/**
* @brief Creates a uninitialized thread id.
*/
inline thread_id() noexcept : m_handle{thread_uninitialized} {}
inline thread_id() noexcept : m_handle{KERNEL_PID_UNDEF} {}
/**
* @brief Create a thread id from a native handle.
*/
Expand Down Expand Up @@ -236,7 +225,7 @@ class thread {
/**
* @brief Per default, an uninitialized thread is created.
*/
inline thread() noexcept : m_handle{thread_uninitialized} {}
inline thread() noexcept : m_handle{KERNEL_PID_UNDEF} {}
/**
* @brief Create a thread from a functor and arguments for it.
* @param[in] f Functor to run as a thread.
Expand All @@ -254,7 +243,7 @@ class thread {
* @brief Move constructor.
*/
inline thread(thread&& t) noexcept : m_handle{t.m_handle} {
t.m_handle = thread_uninitialized;
t.m_handle = KERNEL_PID_UNDEF;
std::swap(m_data, t.m_data);
}

Expand Down Expand Up @@ -284,7 +273,7 @@ class thread {
* @return `true` if the thread is joinable, `false` otherwise.
*/
inline bool joinable() const noexcept {
return m_handle != thread_uninitialized;
return m_handle != KERNEL_PID_UNDEF;
}
/**
* @brief Block until the thread finishes. Leads to an error if the thread is
Expand Down Expand Up @@ -340,7 +329,7 @@ void* thread_proxy(void* vp) {
catch (...) {
// nop
}
if (data->joining_thread != thread_uninitialized) {
if (data->joining_thread != KERNEL_PID_UNDEF) {
thread_wakeup(data->joining_thread);
}
}
Expand All @@ -351,15 +340,14 @@ void* thread_proxy(void* vp) {
/** @endcond */

template <class F, class... Args>
thread::thread(F&& f, Args&&... args)
: m_data{new thread_data} {
thread::thread(F&& f, Args&&... args) : m_data{new thread_data} {
using namespace std;
using func_and_args = tuple
<thread_data*, typename decay<F>::type, typename decay<Args>::type...>;
unique_ptr<func_and_args> p(
new func_and_args(m_data.get(), forward<F>(f), forward<Args>(args)...));
m_handle = thread_create(
m_data->stack.data(), stack_size, THREAD_PRIORITY_MAIN - 1, 0,
m_data->stack.data(), m_data->stack.size(), THREAD_PRIORITY_MAIN - 1, 0,
&thread_proxy<func_and_args>, p.get(), "riot_cpp_thread");
if (m_handle >= 0) {
p.release();
Expand All @@ -371,11 +359,11 @@ thread::thread(F&& f, Args&&... args)
}

inline thread& thread::operator=(thread&& other) noexcept {
if (m_handle != thread_uninitialized) {
if (m_handle != KERNEL_PID_UNDEF) {
std::terminate();
}
m_handle = other.m_handle;
other.m_handle = thread_uninitialized;
other.m_handle = KERNEL_PID_UNDEF;
std::swap(m_data, other.m_data);
return *this;
}
Expand Down
4 changes: 2 additions & 2 deletions sys/cpp11-compat/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void thread::join() {
m_data->joining_thread = thread_getpid();
thread_sleep();
}
m_handle = thread_uninitialized;
m_handle = KERNEL_PID_UNDEF;
} else {
throw system_error(make_error_code(errc::invalid_argument),
"Can not join an unjoinable thread.");
Expand All @@ -54,7 +54,7 @@ void thread::join() {

void thread::detach() {
if (joinable()) {
m_handle = thread_uninitialized;
m_handle = KERNEL_PID_UNDEF;
} else {
throw system_error(make_error_code(errc::invalid_argument),
"Can not detach an unjoinable thread.");
Expand Down

0 comments on commit 26c61be

Please sign in to comment.