moirai 0.8.2

Async/await job system for game development
Documentation
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use crate::{
    coroutine::context,
    job::{Job, JobContext, JobHandle, JobLocation, JobObject, JobOptions, JobPriority, JobTokens},
    jobs::{JobsMeta, JobsRuntime, JobsTags, JobsWakerCommand, Worker, WorkerNotifier},
    third_party::time::{Duration, Instant},
};
use std::{
    collections::LinkedList,
    pin::Pin,
    sync::{
        Arc, LockResult, RwLock, RwLockReadGuard, RwLockWriteGuard,
        atomic::{AtomicBool, Ordering},
    },
    task::{Context, Poll},
};
use typid::ID;

#[derive(Default, Clone)]
pub struct JobQueue {
    queue: Arc<RwLock<LinkedList<JobObject>>>,
    catch_panics: Arc<AtomicBool>,
}

impl JobQueue {
    pub fn catch_panics(self) -> Self {
        self.set_catch_panics(true);
        self
    }

    pub fn does_catch_panics(&self) -> bool {
        self.catch_panics.load(Ordering::Relaxed)
    }

    pub fn set_catch_panics(&self, catch: bool) {
        self.catch_panics.store(catch, Ordering::Relaxed);
    }

    pub fn take_job_objects(&self) -> LinkedList<JobObject> {
        if let Ok(mut queue) = self.inner_mut() {
            std::mem::take(&mut *queue)
        } else {
            LinkedList::new()
        }
    }

    pub fn extract_if(&self, filter: impl Fn(&JobObject) -> bool) -> Vec<JobObject> {
        if let Ok(mut queue) = self.inner_mut() {
            queue.extract_if(|object| filter(object)).collect()
        } else {
            Default::default()
        }
    }

    pub fn take(&self) -> LinkedList<JobObject> {
        if let Ok(mut queue) = self.inner_mut() {
            std::mem::take(&mut *queue)
        } else {
            Default::default()
        }
    }

    pub fn extend(&self, queue: impl IntoIterator<Item = JobObject>) {
        if let Ok(mut current_queue) = self.inner_mut() {
            for object in queue {
                if object.priority == JobPriority::High {
                    current_queue.push_front(object);
                } else {
                    current_queue.push_back(object);
                }
            }
        }
    }

    pub fn inner<'a>(&'a self) -> LockResult<RwLockReadGuard<'a, LinkedList<JobObject>>> {
        self.queue.read()
    }

    pub fn inner_mut<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, LinkedList<JobObject>>> {
        self.queue.write()
    }

    pub fn is_empty(&self) -> bool {
        self.inner().map_or(true, |queue| queue.is_empty())
    }

    pub fn len(&self) -> usize {
        self.inner().map_or(0, |queue| queue.len())
    }

    pub fn filter_count(&self, f: impl Fn(&JobObject) -> bool) -> usize {
        self.inner()
            .map(|queue| queue.iter().filter(|object| f(object)).count())
            .unwrap_or_default()
    }

    pub fn clear(&self) {
        if let Ok(mut queue) = self.inner_mut() {
            queue.clear();
        }
    }

    pub fn append(&self, other: &Self) {
        if let Ok(mut other_queue) = other.inner_mut() {
            self.extend(std::mem::take(&mut *other_queue));
        }
    }

    #[allow(clippy::too_many_arguments)]
    pub fn run(
        &self,
        worker_location: JobLocation,
        ignore_location: bool,
        timeout: Duration,
        global_meta: JobsMeta,
        worker_meta: JobsMeta,
        hash_tokens: JobTokens,
        notifier: WorkerNotifier,
    ) {
        let timer = Instant::now();
        let mut pending = vec![];
        while let Some(object) = self.dequeue(&worker_location, ignore_location) {
            if let Some(object) = self.poll_once(
                object,
                global_meta.clone(),
                worker_meta.clone(),
                hash_tokens.clone(),
                notifier.clone(),
                true,
            ) {
                pending.push(object);
            }
            if timer.elapsed() >= timeout {
                break;
            }
        }
        self.extend(pending);
    }

    pub fn poll_once(
        &self,
        object: JobObject,
        global_meta: JobsMeta,
        worker_meta: JobsMeta,
        hash_tokens: JobTokens,
        notifier: WorkerNotifier,
        use_exclusive_worker: bool,
    ) -> Option<JobObject> {
        if use_exclusive_worker && object.location == JobLocation::Exclusive {
            Worker::exclusive(
                object,
                self.clone(),
                global_meta.clone(),
                Default::default(),
                hash_tokens.clone(),
                notifier.clone(),
            );
            return None;
        }
        let JobObject {
            id,
            job,
            context,
            location,
            mut priority,
            cancel,
            suspend,
            meta,
            tags,
            #[cfg(debug_assertions)]
            creation_backtrace,
            #[cfg(feature = "tracing")]
            mut tracing_span,
        } = object;
        let (poll_result, receiver) = if cancel.load(Ordering::Relaxed) {
            let (_, rx) = std::sync::mpsc::channel();
            (None, rx)
        } else if suspend.load(Ordering::Relaxed) {
            let (_, rx) = std::sync::mpsc::channel();
            (Some(job), rx)
        } else {
            let (waker, receiver) = JobsRuntime {
                queue: self.clone(),
                location: location.clone(),
                context,
                priority,
                global_meta: global_meta.clone(),
                worker_meta: worker_meta.clone(),
                local_meta: meta.clone(),
                tags: tags.clone(),
                hash_tokens: hash_tokens.clone(),
                notify: notifier.clone(),
                cancel: cancel.clone(),
                suspend: suspend.clone(),
            }
            .into_waker();
            let mut cx = Context::from_waker(&waker);
            #[cfg(feature = "tracing")]
            let _span = {
                let span = tracing::span!(
                    tracing::Level::TRACE,
                    "Job poll",
                    id = id.to_string(),
                    location = location.to_string(),
                    context = context.to_string(),
                    priority = priority.to_string(),
                    thread_id = format!("{:?}", std::thread::current().id()),
                );
                if let Some(last) = tracing_span.take() {
                    span.follows_from(last);
                }
                span.entered()
            };
            let poll_result = job.poll(
                &mut cx,
                self.does_catch_panics(),
                #[cfg(debug_assertions)]
                &creation_backtrace,
            );
            (poll_result, receiver)
        };
        let mut notify_workers = false;
        let result = if let Some(job) = poll_result {
            let mut move_to = None;
            for command in receiver.try_iter() {
                notify_workers = true;
                match command {
                    JobsWakerCommand::MoveTo(location) => move_to = Some(location),
                    JobsWakerCommand::ChangePriority(new_priority) => {
                        priority = new_priority;
                    }
                }
            }
            match move_to {
                Some(JobLocation::Queue(q)) => {
                    q.enqueue(JobObject {
                        id,
                        job,
                        context,
                        location,
                        priority,
                        cancel,
                        suspend,
                        meta,
                        tags,
                        #[cfg(debug_assertions)]
                        creation_backtrace,
                        #[cfg(feature = "tracing")]
                        tracing_span,
                    });
                    None
                }
                Some(location) => {
                    self.enqueue(JobObject {
                        id,
                        job,
                        context,
                        location,
                        priority,
                        cancel,
                        suspend,
                        meta,
                        tags,
                        #[cfg(debug_assertions)]
                        creation_backtrace,
                        #[cfg(feature = "tracing")]
                        tracing_span,
                    });
                    None
                }
                None => Some(JobObject {
                    id,
                    job,
                    context,
                    location,
                    priority,
                    cancel,
                    suspend,
                    meta,
                    tags,
                    #[cfg(debug_assertions)]
                    creation_backtrace,
                    #[cfg(feature = "tracing")]
                    tracing_span,
                }),
            }
        } else {
            None
        };
        if notify_workers {
            notifier.notify();
        }
        result
    }

    pub fn spawn<T: Send + 'static>(
        &self,
        options: impl Into<JobOptions>,
        job: impl Future<Output = T> + Send + Sync + 'static,
    ) -> JobHandle<T> {
        let options = options.into();
        let handle = JobHandle::<T>::default().with_meta(options.meta);
        let handle2 = handle.clone();
        let job = Job(Box::pin(async move {
            handle2.put(job.await);
        }));
        self.schedule(
            options.location,
            options.priority,
            options.tags,
            handle,
            job,
        )
    }

    pub fn spawn_closure<T: Send + 'static>(
        &self,
        options: impl Into<JobOptions>,
        job: impl FnOnce(JobContext) -> T + Send + Sync + 'static,
    ) -> JobHandle<T> {
        let options = options.into();
        let handle = JobHandle::<T>::default().with_meta(options.meta);
        let handle2 = handle.clone();
        let job = Job(Box::pin(async move {
            handle2.put(job(context().await));
        }));
        self.schedule(
            options.location,
            options.priority,
            options.tags,
            handle,
            job,
        )
    }

    fn schedule<T: Send + 'static>(
        &self,
        location: JobLocation,
        priority: JobPriority,
        tags: JobsTags,
        handle: JobHandle<T>,
        job: Job,
    ) -> JobHandle<T> {
        #[cfg(debug_assertions)]
        let creation_backtrace = std::backtrace::Backtrace::capture().to_string();
        self.enqueue(JobObject {
            id: ID::new(),
            job,
            context: Default::default(),
            location,
            priority,
            cancel: handle.cancel.clone(),
            suspend: handle.suspend.clone(),
            meta: handle.meta.clone(),
            tags,
            #[cfg(debug_assertions)]
            creation_backtrace,
            #[cfg(feature = "tracing")]
            tracing_span: None,
        });
        handle
    }

    pub fn enqueue(&self, object: JobObject) {
        if let Ok(mut queue) = self.inner_mut() {
            if object.priority == JobPriority::High {
                queue.push_front(object);
            } else {
                queue.push_back(object);
            }
        }
    }

    pub fn dequeue(
        &self,
        target_location: &JobLocation,
        ignore_location: bool,
    ) -> Option<JobObject> {
        let mut queue = self.inner_mut().ok()?;
        let mut extract = queue.extract_if(|object| {
            if ignore_location || object.location == JobLocation::Exclusive {
                true
            } else {
                match (&object.location, target_location) {
                    (JobLocation::Local, JobLocation::Local)
                    | (JobLocation::UnnamedWorker, JobLocation::UnnamedWorker) => true,
                    (JobLocation::NamedWorker(a), JobLocation::NamedWorker(b)) if a == b => true,
                    (JobLocation::ExactThread(a), _) => *a == std::thread::current().id(),
                    (JobLocation::OtherThanThread(a), _) => *a != std::thread::current().id(),
                    (JobLocation::NonLocal, b) => b != &JobLocation::Local,
                    (JobLocation::Queue(q), _) => q == self,
                    (JobLocation::Unknown, _) => true,
                    _ => false,
                }
            }
        });
        extract.next()
    }
}

impl Future for JobQueue {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        if self.is_empty() {
            cx.waker().wake_by_ref();
            Poll::Pending
        } else {
            cx.waker().wake_by_ref();
            Poll::Ready(())
        }
    }
}

impl PartialEq for JobQueue {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.queue, &other.queue)
    }
}

impl std::fmt::Debug for JobQueue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("JobQueue")
            .field("len", &self.len())
            .finish()
    }
}