mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
4c3e92f183
- Introduced enum type `thread_state_t` to replace preprocessor macros - Moved thread states to `sched.h` for two reasons: a) Because of the interdependencies of `sched.h` and `thread.h` keeping it in `thread.h` would result in ugly code. b) Theses thread states are defined from the schedulers point of view, so it actually makes senses to have it defined there
83 lines
1.8 KiB
C++
83 lines
1.8 KiB
C++
/*
|
|
* Copyright (C) 2015 Hamburg University of Applied Sciences (HAW)
|
|
*
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
* directory for more details.
|
|
*/
|
|
|
|
/**
|
|
* @ingroup cpp11-compat
|
|
* @{
|
|
*
|
|
* @file
|
|
* @brief C++11 thread drop in replacement
|
|
*
|
|
* @author Raphael Hiesgen <raphael.hiesgen (at) haw-hamburg.de>
|
|
*
|
|
* @}
|
|
*/
|
|
|
|
#include "xtimer.h"
|
|
|
|
#include <cerrno>
|
|
#include <system_error>
|
|
|
|
#include "riot/thread.hpp"
|
|
|
|
using namespace std;
|
|
|
|
namespace riot {
|
|
|
|
thread::~thread() {
|
|
if (joinable()) {
|
|
terminate();
|
|
}
|
|
}
|
|
|
|
void thread::join() {
|
|
if (this->get_id() == this_thread::get_id()) {
|
|
throw system_error(make_error_code(errc::resource_deadlock_would_occur),
|
|
"Joining this leads to a deadlock.");
|
|
}
|
|
if (joinable()) {
|
|
auto status = thread_getstatus(m_handle);
|
|
if (status != (int)STATUS_NOT_FOUND && status != STATUS_STOPPED) {
|
|
m_data->joining_thread = sched_active_pid;
|
|
thread_sleep();
|
|
}
|
|
m_handle = thread_uninitialized;
|
|
} else {
|
|
throw system_error(make_error_code(errc::invalid_argument),
|
|
"Can not join an unjoinable thread.");
|
|
}
|
|
// missing: no_such_process system error
|
|
}
|
|
|
|
void thread::detach() {
|
|
if (joinable()) {
|
|
m_handle = thread_uninitialized;
|
|
} else {
|
|
throw system_error(make_error_code(errc::invalid_argument),
|
|
"Can not detach an unjoinable thread.");
|
|
}
|
|
}
|
|
|
|
unsigned thread::hardware_concurrency() noexcept {
|
|
// there is currently no API for this
|
|
return 1;
|
|
}
|
|
|
|
namespace this_thread {
|
|
|
|
void sleep_for(const chrono::nanoseconds& ns) {
|
|
using namespace chrono;
|
|
if (ns > nanoseconds::zero()) {
|
|
xtimer_usleep64(static_cast<uint64_t>(duration_cast<microseconds>(ns).count()));
|
|
}
|
|
}
|
|
|
|
} // namespace this_thread
|
|
|
|
} // namespace riot
|