Blame view

RIOT/sys/cpp11-compat/thread.cpp 1.83 KB
fb11e647   vrobic   reseau statique a...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  /*
   * 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 != 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