1 | /*
|
---|
2 | * Copyright (c) 2017 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <cstdlib>
|
---|
30 | #include <thread>
|
---|
31 | #include <utility>
|
---|
32 |
|
---|
33 | namespace std
|
---|
34 | {
|
---|
35 | thread::thread() noexcept
|
---|
36 | : id_{}
|
---|
37 | { /* DUMMY BODY */ }
|
---|
38 |
|
---|
39 | thread::~thread()
|
---|
40 | {
|
---|
41 | // TODO: Change this to std::terminate when implemented.
|
---|
42 | if (joinable())
|
---|
43 | {
|
---|
44 | // TODO: this crashes :(
|
---|
45 | /* fibril_teardown((fibril_t*)id_, false); */
|
---|
46 | /* std::abort(); */
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | thread::thread(thread&& other) noexcept
|
---|
51 | : id_{other.id_}
|
---|
52 | {
|
---|
53 | other.id_ = fid_t{};
|
---|
54 | }
|
---|
55 |
|
---|
56 | thread& thread::operator=(thread&& other) noexcept
|
---|
57 | {
|
---|
58 | id_ = other.id_;
|
---|
59 | other.id_ = fid_t{};
|
---|
60 |
|
---|
61 | return *this;
|
---|
62 | }
|
---|
63 |
|
---|
64 | void thread::swap(thread& other) noexcept
|
---|
65 | {
|
---|
66 | std::swap(id_, other.id_);
|
---|
67 | }
|
---|
68 |
|
---|
69 | bool thread::joinable() const noexcept
|
---|
70 | {
|
---|
71 | return get_id() != id{};
|
---|
72 | }
|
---|
73 |
|
---|
74 | void thread::join()
|
---|
75 | {
|
---|
76 | // TODO:
|
---|
77 | }
|
---|
78 |
|
---|
79 | void thread::detach()
|
---|
80 | {
|
---|
81 | id_ = fid_t{};
|
---|
82 | }
|
---|
83 |
|
---|
84 | thread::id thread::get_id() const noexcept
|
---|
85 | {
|
---|
86 | return id{id_};
|
---|
87 | }
|
---|
88 |
|
---|
89 | thread::native_handle_type thread::native_handle()
|
---|
90 | {
|
---|
91 | /**
|
---|
92 | * For fibrils the fid_t returned from fibril_create
|
---|
93 | * is a fibril_t* casted to fid_t, native handles
|
---|
94 | * are implementation defined so we just recast back.
|
---|
95 | */
|
---|
96 | return (native_handle_type)id_;
|
---|
97 | }
|
---|
98 |
|
---|
99 | unsigned thread::hardware_concurrency() noexcept
|
---|
100 | {
|
---|
101 | // TODO:
|
---|
102 | return 0;
|
---|
103 | }
|
---|
104 |
|
---|
105 | void swap(thread& x, thread& y) noexcept
|
---|
106 | {
|
---|
107 | x.swap(y);
|
---|
108 | }
|
---|
109 |
|
---|
110 | namespace this_thread
|
---|
111 | {
|
---|
112 | thread::id get_id() noexcept
|
---|
113 | {
|
---|
114 | return thread::id{fibril_get_id()};
|
---|
115 | }
|
---|
116 |
|
---|
117 | void yield() noexcept
|
---|
118 | {
|
---|
119 | fibril_yield();
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | bool operator==(thread::id lhs, thread::id rhs) noexcept
|
---|
124 | {
|
---|
125 | return lhs.id_ == rhs.id_;
|
---|
126 | }
|
---|
127 |
|
---|
128 | bool operator!=(thread::id lhs, thread::id rhs) noexcept
|
---|
129 | {
|
---|
130 | return !(lhs == rhs);
|
---|
131 | }
|
---|
132 |
|
---|
133 | bool operator<(thread::id lhs, thread::id rhs) noexcept
|
---|
134 | {
|
---|
135 | return lhs.id_ < rhs.id_;
|
---|
136 | }
|
---|
137 |
|
---|
138 | bool operator<=(thread::id lhs, thread::id rhs) noexcept
|
---|
139 | {
|
---|
140 | return !(rhs < lhs);
|
---|
141 | }
|
---|
142 |
|
---|
143 | bool operator>(thread::id lhs, thread::id rhs) noexcept
|
---|
144 | {
|
---|
145 | return rhs < lhs;
|
---|
146 | }
|
---|
147 |
|
---|
148 | bool operator>=(thread::id lhs, thread::id rhs) noexcept
|
---|
149 | {
|
---|
150 | return !(lhs < rhs);
|
---|
151 | }
|
---|
152 | }
|
---|