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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#![cfg_attr(not(any(feature = "std", test)), no_std)]
#![warn(missing_docs, missing_debug_implementations, unused_import_braces)]
#![cfg_attr(feature = "nightly", feature(option_result_unwrap_unchecked))]
#[cfg(feature = "alloc")]
extern crate alloc;
pub mod mutex;
pub mod queue;
pub mod rw_lock;
pub mod stack;
#[cfg(feature = "alloc")]
mod alloc_impls;
mod impls;
use core::convert::Infallible;
use core::fmt::Debug;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use core::time::Duration;
trait EnsureSend: Send {}
trait EnsureSync: Sync {}
pub trait TimeFunctions {
type InstantType: Add<Duration, Output = Self::InstantType>
+ AddAssign<Duration>
+ Sub<Duration, Output = Self::InstantType>
+ SubAssign<Duration>
+ Sub<Self::InstantType, Output = Duration>
+ Ord
+ Copy;
fn current_time() -> Self::InstantType;
}
pub trait ThreadFunctions {
fn sleep(duration: Duration);
fn yield_now();
}
pub trait TryThreadSpawner<O>
where
O: Send + 'static,
{
type ThreadHandle: ThreadHandle;
type SpawnError;
fn try_spawn(
func: impl FnOnce() -> O + 'static + Send,
) -> Result<Self::ThreadHandle, Self::SpawnError>;
}
pub trait ThreadSpawner<O>: TryThreadSpawner<O, SpawnError = Infallible>
where
O: Send + 'static,
{
fn spawn(func: impl FnOnce() -> O + 'static + Send) -> Self::ThreadHandle {
#[cfg(not(feature = "nightly"))]
{
Self::try_spawn(func).unwrap()
}
#[cfg(feature = "nightly")]
unsafe {
Self::try_spawn(func).unwrap_unchecked()
}
}
}
impl<T, O> ThreadSpawner<O> for T
where
T: TryThreadSpawner<O, SpawnError = Infallible>,
O: Send + 'static,
{
}
pub trait TryResultThreadSpawner<O>: TryThreadSpawner<O>
where
Self::ThreadHandle: TryJoinableHandle<Output = O>,
O: Send + 'static,
{
}
impl<T, O> TryResultThreadSpawner<O> for T
where
T: TryThreadSpawner<O>,
T::ThreadHandle: TryJoinableHandle<Output = O>,
O: Send + 'static,
{
}
pub trait ResultThreadSpawner<O>: ThreadSpawner<O>
where
Self::ThreadHandle: TryJoinableHandle<Output = O>,
O: Send + 'static,
{
}
impl<T, O> ResultThreadSpawner<O> for T
where
T: ThreadSpawner<O>,
T::ThreadHandle: TryJoinableHandle<Output = O>,
O: Send + 'static,
{
}
pub trait ThreadParker {
type ThreadId: Debug;
fn park();
fn unpark(thread: Self::ThreadId);
fn current_thread() -> Self::ThreadId;
}
pub trait ThreadTimeoutParker: ThreadParker {
fn park_timeout(timeout: Duration);
}
pub trait ThreadHandle {
type ThreadId;
fn thread_id(&self) -> &Self::ThreadId;
}
pub trait TryJoinableHandle: Sized + ThreadHandle {
type Output;
type ThreadError;
fn try_join(self) -> Result<Self::Output, Self::ThreadError>;
}
pub trait JoinableHandle: Sized + TryJoinableHandle<ThreadError = Infallible> {
#[inline]
fn join(self) -> Self::Output {
#[cfg(not(feature = "nightly"))]
{
self.try_join().unwrap()
}
#[cfg(feature = "nightly")]
unsafe {
self.try_join().unwrap_unchecked()
}
}
}
impl<T> JoinableHandle for T where T: TryJoinableHandle<ThreadError = Infallible> {}
pub trait ConcurrentSystem<O>: 'static
where
Self: TimeFunctions
+ ThreadFunctions
+ TryThreadSpawner<O>
+ ThreadParker<
ThreadId = <<Self as TryThreadSpawner<O>>::ThreadHandle as ThreadHandle>::ThreadId,
>,
O: Send + 'static,
{
}
#[cfg(feature = "std")]
#[derive(Copy, Clone, Debug)]
pub struct StdThreadFunctions;
#[cfg(feature = "std")]
mod std_thread_impls {
use super::*;
impl TimeFunctions for StdThreadFunctions {
type InstantType = std::time::Instant;
#[inline]
fn current_time() -> Self::InstantType {
std::time::Instant::now()
}
}
impl ThreadFunctions for StdThreadFunctions {
#[inline]
fn sleep(duration: Duration) {
std::thread::sleep(duration)
}
fn yield_now() {
std::thread::yield_now()
}
}
impl<O> TryThreadSpawner<O> for StdThreadFunctions
where
O: Send + 'static,
{
type ThreadHandle = std::thread::JoinHandle<O>;
type SpawnError = Infallible;
fn try_spawn(
func: impl FnOnce() -> O + 'static + Send,
) -> Result<Self::ThreadHandle, Self::SpawnError> {
Ok(std::thread::spawn(func))
}
}
impl ThreadParker for StdThreadFunctions {
type ThreadId = std::thread::Thread;
#[inline]
fn park() {
std::thread::park()
}
#[inline]
fn unpark(thread: Self::ThreadId) {
thread.unpark()
}
#[inline]
fn current_thread() -> Self::ThreadId {
std::thread::current()
}
}
impl ThreadTimeoutParker for StdThreadFunctions {
fn park_timeout(timeout: Duration) {
std::thread::park_timeout(timeout)
}
}
impl<O> ThreadHandle for std::thread::JoinHandle<O> {
type ThreadId = std::thread::Thread;
#[inline]
fn thread_id(&self) -> &Self::ThreadId {
self.thread()
}
}
impl<O> TryJoinableHandle for std::thread::JoinHandle<O> {
type Output = O;
type ThreadError = Box<dyn std::any::Any + Send + 'static>;
#[inline]
fn try_join(self) -> Result<Self::Output, Self::ThreadError> {
self.join()
}
}
impl<O> ConcurrentSystem<O> for StdThreadFunctions where O: Send + 'static {}
}