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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
mod thread_state;

pub use self::thread_state::ThreadStateKind;

use alloc::{boxed::Box, sync::Arc};
use core::task::{Context, Poll, Waker};
use core::time::Duration;
use core::{any::Any, future::Future, pin::Pin};

use bitflags::bitflags;
use cfg_if::cfg_if;
use futures::{channel::oneshot::*, future::FutureExt, pin_mut, select_biased};
use kernel_hal::context::UserContext;
use lock::Mutex;

use self::thread_state::ContextAccessState;
use super::{exception::*, Process, Task};
use crate::object::{KObjectBase, KoID, Signal};
use crate::{define_count_helper, impl_kobject, ZxError, ZxResult};

/// Runnable / computation entity
///
/// ## SYNOPSIS
///
/// TODO
///
/// ## DESCRIPTION
///
/// The thread object is the construct that represents a time-shared CPU execution
/// context. Thread objects live associated to a particular
/// [Process Object](crate::task::Process) which provides the memory and the handles to other
/// objects necessary for I/O and computation.
///
/// ### Lifetime
/// Threads are created by calling [`Thread::create()`], but only start executing
/// when either [`Thread::start()`] or [`Process::start()`] are called. Both syscalls
/// take as an argument the entrypoint of the initial routine to execute.
///
/// The thread passed to [`Process::start()`] should be the first thread to start execution
/// on a process.
///
/// A thread terminates execution:
/// - by calling [`CurrentThread::exit()`]
/// - when the parent process terminates
/// - by calling [`Task::kill()`]
/// - after generating an exception for which there is no handler or the handler
/// decides to terminate the thread.
///
/// Returning from the entrypoint routine does not terminate execution. The last
/// action of the entrypoint should be to call [`CurrentThread::exit()`].
///
/// Closing the last handle to a thread does not terminate execution. In order to
/// forcefully kill a thread for which there is no available handle, use
/// `KernelObject::get_child()` to obtain a handle to the thread. This method is strongly
/// discouraged. Killing a thread that is executing might leave the process in a
/// corrupt state.
///
/// Fuchsia native threads are always *detached*. That is, there is no *join()* operation
/// needed to do a clean termination. However, some runtimes above the kernel, such as
/// C11 or POSIX might require threads to be joined.
///
/// ### Signals
/// Threads provide the following signals:
/// - [`THREAD_TERMINATED`]
/// - [`THREAD_SUSPENDED`]
/// - [`THREAD_RUNNING`]
///
/// When a thread is started [`THREAD_RUNNING`] is asserted. When it is suspended
/// [`THREAD_RUNNING`] is deasserted, and [`THREAD_SUSPENDED`] is asserted. When
/// the thread is resumed [`THREAD_SUSPENDED`] is deasserted and
/// [`THREAD_RUNNING`] is asserted. When a thread terminates both
/// [`THREAD_RUNNING`] and [`THREAD_SUSPENDED`] are deasserted and
/// [`THREAD_TERMINATED`] is asserted.
///
/// Note that signals are OR'd into the state maintained by the
/// `KernelObject::wait_signal()` family of functions thus
/// you may see any combination of requested signals when they return.
///
/// [`Thread::create()`]: Thread::create
/// [`CurrentThread::exit()`]: CurrentThread::exit
/// [`Process::exit()`]: crate::task::Process::exit
/// [`THREAD_TERMINATED`]: crate::object::Signal::THREAD_TERMINATED
/// [`THREAD_SUSPENDED`]: crate::object::Signal::THREAD_SUSPENDED
/// [`THREAD_RUNNING`]: crate::object::Signal::THREAD_RUNNING
pub struct Thread {
    base: KObjectBase,
    _counter: CountHelper,
    proc: Arc<Process>,
    ext: Box<dyn Any + Send + Sync>,
    inner: Mutex<ThreadInner>,
    exceptionate: Arc<Exceptionate>,
}

impl_kobject!(Thread
    fn related_koid(&self) -> KoID {
        self.proc.id()
    }
);
define_count_helper!(Thread);

#[derive(Default)]
struct ThreadInner {
    /// Thread context
    ///
    /// It will be taken away when running this thread.
    context: Option<Box<UserContext>>,

    /// Thread context before handling the signal,
    /// (trapframe, siginfo_address, user_signal_ctx_address)
    ///
    /// It only works when executing signal handlers
    context_before: Option<(UserContext, usize, usize)>,

    /// The number of existing `SuspendToken`.
    suspend_count: usize,
    /// The waker of task when suspending.
    waker: Option<Waker>,
    /// A token used to kill blocking thread
    killer: Option<Sender<()>>,
    /// Thread state
    ///
    /// NOTE: This variable will never be `Suspended`. On suspended, the
    /// `suspend_count` is non-zero, and this represents the state before suspended.
    state: ThreadState,
    /// The currently processing exception
    exception: Option<Arc<Exception>>,
    /// Should The ProcessStarting exception generated at start of this thread
    first_thread: bool,
    /// Should The ThreadExiting exception do not block this thread
    killed: bool,
    /// The time this thread has run on cpu
    time: u128,
    flags: ThreadFlag,
}

impl ThreadInner {
    fn state(&self) -> ThreadState {
        // Dying > Exception > Suspend > Blocked
        if self.suspend_count == 0
            || self.context.is_none()
            || self.state == ThreadState::BlockedException
            || self.state == ThreadState::Dying
            || self.state == ThreadState::Dead
        {
            self.state
        } else {
            ThreadState::Suspended
        }
    }

    /// Change state and update signal.
    fn change_state(&mut self, state: ThreadState, base: &KObjectBase) {
        self.state = state;
        match self.state() {
            ThreadState::Dead => base.signal_change(
                Signal::THREAD_RUNNING | Signal::THREAD_SUSPENDED,
                Signal::THREAD_TERMINATED,
            ),
            ThreadState::New | ThreadState::Dying => base.signal_clear(
                Signal::THREAD_RUNNING | Signal::THREAD_SUSPENDED | Signal::THREAD_TERMINATED,
            ),
            ThreadState::Suspended => base.signal_change(
                Signal::THREAD_RUNNING | Signal::THREAD_TERMINATED,
                Signal::THREAD_SUSPENDED,
            ),
            _ => base.signal_change(
                Signal::THREAD_TERMINATED | Signal::THREAD_SUSPENDED,
                Signal::THREAD_RUNNING,
            ),
        }
    }

    /// Backup current context
    fn backup_context(&mut self, context: UserContext, siginfo: usize, uctx: usize) {
        self.context_before = Some((context, siginfo, uctx));
    }
}

bitflags! {
    /// Thread flags.
    #[derive(Default)]
    pub struct ThreadFlag: usize {
        /// The thread currently has a VCPU.
        const VCPU = 1 << 3;
    }
}

type ThreadFuture = dyn Future<Output = ()> + Send;
type ThreadFuturePinned = Pin<Box<ThreadFuture>>;

/// The type of a new thread function.
pub type ThreadFn = fn(thread: CurrentThread) -> ThreadFuturePinned;

impl Thread {
    /// Create a new thread.
    pub fn create(proc: &Arc<Process>, name: &str) -> ZxResult<Arc<Self>> {
        Self::create_with_ext(proc, name, ())
    }

    /// Create a new thread with extension info.
    ///
    /// # Example
    /// ```
    /// # use std::sync::Arc;
    /// # use zircon_object::task::*;
    /// # kernel_hal::init();
    /// let job = Job::root();
    /// let proc = Process::create(&job, "proc").unwrap();
    /// // create a thread with extension info
    /// let thread = Thread::create_with_ext(&proc, "thread", job.clone()).unwrap();
    /// // get the extension info
    /// let ext = thread.ext().downcast_ref::<Arc<Job>>().unwrap();
    /// assert!(Arc::ptr_eq(ext, &job));
    /// ```
    pub fn create_with_ext(
        proc: &Arc<Process>,
        name: &str,
        ext: impl Any + Send + Sync,
    ) -> ZxResult<Arc<Self>> {
        let thread = Arc::new(Thread {
            base: KObjectBase::with_name(name),
            _counter: CountHelper::new(),
            proc: proc.clone(),
            ext: Box::new(ext),
            exceptionate: Exceptionate::new(ExceptionChannelType::Thread),
            inner: Mutex::new(ThreadInner {
                context: Some(Box::new(UserContext::new())),
                ..Default::default()
            }),
        });
        proc.add_thread(thread.clone())?;
        Ok(thread)
    }

    /// Get the process.
    pub fn proc(&self) -> &Arc<Process> {
        &self.proc
    }

    /// Get the extension info.
    pub fn ext(&self) -> &Box<dyn Any + Send + Sync> {
        &self.ext
    }

    /// Returns a copy of saved context of current thread, or `Err(ZxError::BAD_STATE)`
    /// if the thread is running.
    pub fn context_cloned(&self) -> ZxResult<UserContext> {
        self.with_context(|ctx| *ctx)
    }

    /// Access saved context of current thread, or `Err(ZxError::BAD_STATE)` if
    /// the thread is running.
    pub fn with_context<T, F>(&self, f: F) -> ZxResult<T>
    where
        F: FnOnce(&mut UserContext) -> T,
    {
        let mut inner = self.inner.lock();
        if let Some(ctx) = inner.context.as_mut() {
            Ok(f(ctx))
        } else {
            Err(ZxError::BAD_STATE)
        }
    }

    /// Backup current user context before calling signal handler
    pub fn backup_context(&self, context: UserContext, siginfo: usize, uctx: usize) {
        let mut inner = self.inner.lock();
        inner.backup_context(context, siginfo, uctx);
    }

    /// Fetch the context backup
    pub fn fetch_backup_context(&self) -> Option<(UserContext, usize, usize)> {
        let mut inner = self.inner.lock();
        inner.context_before.take()
    }

    /// Start execution on the thread.
    pub fn start(self: &Arc<Self>, thread_fn: ThreadFn) -> ZxResult {
        self.inner
            .lock()
            .change_state(ThreadState::Running, &self.base);
        let current = CurrentThread(self.clone());
        let future = thread_fn(current);
        kernel_hal::thread::spawn(ThreadSwitchFuture::new(self.clone(), future));
        Ok(())
    }

    /// Setup the instruction and stack pointer, then tart execution on the thread
    pub fn start_with_entry(
        self: &Arc<Self>,
        entry: usize,
        stack: usize,
        arg1: usize,
        arg2: usize,
        thread_fn: ThreadFn,
    ) -> ZxResult {
        self.with_context(|ctx| ctx.setup_uspace(entry, stack, &[arg1, arg2, 0]))?;
        self.start(thread_fn)
    }

    /// Stop the thread. Internal implementation of `exit` and `kill`.
    ///
    /// The thread do not terminate immediately when stopped. It is just made dying.
    /// It will terminate after some cleanups (when `terminate` are called **explicitly** by upper layer).
    fn stop(&self, killed: bool) {
        let mut inner = self.inner.lock();
        if inner.state == ThreadState::Dead {
            return;
        }
        if killed {
            inner.killed = true;
        }
        if inner.state == ThreadState::Dying {
            if killed {
                if let Some(killer) = inner.killer.take() {
                    // It's ok to ignore the error since the other end could be closed
                    killer.send(()).ok();
                }
            }
            return;
        }
        inner.change_state(ThreadState::Dying, &self.base);
        if let Some(waker) = inner.waker.take() {
            waker.wake_by_ref();
        }
        // For blocking thread, use the killer
        if let Some(killer) = inner.killer.take() {
            // It's ok to ignore the error since the other end could be closed
            killer.send(()).ok();
        }
    }

    /// Read one aspect of thread state.
    pub fn read_state(&self, kind: ThreadStateKind, buf: &mut [u8]) -> ZxResult<usize> {
        let inner = self.inner.lock();
        let state = inner.state();
        if state != ThreadState::BlockedException && state != ThreadState::Suspended {
            if inner.exception.is_some() {
                return Err(ZxError::NOT_SUPPORTED);
            }
            return Err(ZxError::BAD_STATE);
        }
        let context = inner.context.as_ref().ok_or(ZxError::BAD_STATE)?;
        context.read_state(kind, buf)
    }

    /// Write one aspect of thread state.
    pub fn write_state(&self, kind: ThreadStateKind, buf: &[u8]) -> ZxResult {
        let mut inner = self.inner.lock();
        let state = inner.state();
        if state != ThreadState::BlockedException && state != ThreadState::Suspended {
            if inner.exception.is_some() {
                return Err(ZxError::NOT_SUPPORTED);
            }
            return Err(ZxError::BAD_STATE);
        }
        let context = inner.context.as_mut().ok_or(ZxError::BAD_STATE)?;
        context.write_state(kind, buf)
    }

    /// Get the thread's information.
    pub fn get_thread_info(&self) -> ThreadInfo {
        let inner = self.inner.lock();
        ThreadInfo {
            state: inner.state() as u32,
            wait_exception_channel_type: inner
                .exception
                .as_ref()
                .map_or(0, |exception| exception.current_channel_type() as u32),
            cpu_affinity_mask: [0u64; 8],
        }
    }

    /// Get the thread's exception report.
    pub fn get_thread_exception_info(&self) -> ZxResult<ExceptionReport> {
        let inner = self.inner.lock();
        if inner.state() != ThreadState::BlockedException {
            return Err(ZxError::BAD_STATE);
        }
        let report = inner.exception.as_ref().ok_or(ZxError::BAD_STATE)?.report();
        Ok(report)
    }

    /// Get the thread state.
    pub fn state(&self) -> ThreadState {
        self.inner.lock().state()
    }

    /// Add the parameter to the time this thread has run on cpu.
    pub fn time_add(&self, time: u128) {
        self.inner.lock().time += time;
    }

    /// Get the time this thread has run on cpu.
    pub fn get_time(&self) -> u64 {
        self.inner.lock().time as u64
    }

    /// Set this thread as the first thread of a process.
    pub(super) fn set_first_thread(&self) {
        self.inner.lock().first_thread = true;
    }

    /// Whether this thread is the first thread of a process.
    pub fn is_first_thread(&self) -> bool {
        self.inner.lock().first_thread
    }

    /// Get the thread's flags.
    pub fn flags(&self) -> ThreadFlag {
        self.inner.lock().flags
    }

    /// Apply `f` to the thread's flags.
    pub fn update_flags(&self, f: impl FnOnce(&mut ThreadFlag)) {
        f(&mut self.inner.lock().flags)
    }

    /// Terminate the current running thread.
    fn terminate(&self) {
        let mut inner = self.inner.lock();
        self.exceptionate.shutdown();
        inner.change_state(ThreadState::Dead, &self.base);
        self.proc().remove_thread(self.base.id);
    }
}

impl Task for Thread {
    fn kill(&self) {
        self.stop(true)
    }

    fn suspend(&self) {
        let mut inner = self.inner.lock();
        inner.suspend_count += 1;
        let state = inner.state;
        inner.change_state(state, &self.base);
    }

    fn resume(&self) {
        let mut inner = self.inner.lock();
        assert_ne!(inner.suspend_count, 0);
        inner.suspend_count -= 1;
        if inner.suspend_count == 0 {
            let state = inner.state;
            inner.change_state(state, &self.base);
            if let Some(waker) = inner.waker.take() {
                waker.wake_by_ref();
            }
        }
    }

    fn exceptionate(&self) -> Arc<Exceptionate> {
        self.exceptionate.clone()
    }

    fn debug_exceptionate(&self) -> Arc<Exceptionate> {
        panic!("thread do not have debug exceptionate");
    }
}

/// A handle to current thread.
///
/// This is a wrapper of [`Thread`] that provides additional methods for the thread runner.
/// It can only be obtained from the argument of `thread_fn` in a new thread started by [`Thread::start`].
///
/// It will terminate current thread on drop.
///
/// [`Thread`]: crate::task::Thread
/// [`Thread::start`]: crate::task::Thread::start
pub struct CurrentThread(Arc<Thread>);

impl CurrentThread {
    /// Returns the inner structure `Arc<Thread>`.
    pub fn inner(&self) -> Arc<Thread> {
        self.0.clone()
    }

    /// Exit the current thread.
    ///
    /// The thread do not terminate immediately when exited. It is just made dying.
    /// It will terminate after some cleanups on this struct drop.
    pub fn exit(&self) {
        self.stop(false);
    }

    /// Wait until the thread is ready to run (not suspended),
    /// and then take away its context to run the thread.
    pub fn wait_for_run(&self) -> impl Future<Output = Box<UserContext>> {
        #[must_use = "wait_for_run does nothing unless polled/`await`-ed"]
        struct RunnableChecker {
            thread: Arc<Thread>,
        }
        impl Future for RunnableChecker {
            type Output = Box<UserContext>;

            fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
                let mut inner = self.thread.inner.lock();
                if inner.state() != ThreadState::Suspended {
                    // resume:  return the context token from thread object
                    // There is no need to call change_state here
                    // since take away the context of a non-suspended thread won't change it's state
                    Poll::Ready(inner.context.take().unwrap())
                } else {
                    // suspend: put waker into the thread object
                    inner.waker = Some(cx.waker().clone());
                    Poll::Pending
                }
            }
        }
        RunnableChecker {
            thread: self.0.clone(),
        }
    }

    /// The thread ends running and takes back the context.
    pub fn put_context(&self, context: Box<UserContext>) {
        let mut inner = self.inner.lock();
        inner.context = Some(context);
        let state = inner.state;
        inner.change_state(state, &self.base);
    }

    /// Run async future and change state while blocking.
    pub async fn blocking_run<F, T, FT>(
        &self,
        future: F,
        state: ThreadState,
        deadline: Duration,
        cancel_token: Option<Receiver<()>>,
    ) -> ZxResult<T>
    where
        F: Future<Output = FT> + Unpin,
        FT: IntoResult<T>,
    {
        let (old_state, killed) = {
            let mut inner = self.inner.lock();
            if inner.state() == ThreadState::Dying {
                return Err(ZxError::STOP);
            }
            let (sender, receiver) = channel();
            inner.killer = Some(sender);
            let old_state = inner.state;
            inner.change_state(state, &self.base);
            (old_state, receiver)
        };
        let ret = if let Some(cancel_token) = cancel_token {
            select_biased! {
                ret = future.fuse() => ret.into_result(),
                _ = killed.fuse() => Err(ZxError::STOP),
                _ = kernel_hal::thread::sleep_until(deadline).fuse() => Err(ZxError::TIMED_OUT),
                _ = cancel_token.fuse() => Err(ZxError::CANCELED),
            }
        } else {
            select_biased! {
                ret = future.fuse() => ret.into_result(),
                _ = killed.fuse() => Err(ZxError::STOP),
                _ = kernel_hal::thread::sleep_until(deadline).fuse() => Err(ZxError::TIMED_OUT),
            }
        };
        let mut inner = self.inner.lock();
        inner.killer = None;
        if inner.state() == ThreadState::Dying {
            return ret;
        }
        assert_eq!(inner.state, state);
        inner.change_state(old_state, &self.base);
        ret
    }

    /// Create an exception on this thread and wait for the handling.
    pub async fn handle_exception(&self, type_: ExceptionType) {
        let exception = {
            let mut inner = self.inner.lock();
            let cx = if !type_.is_synth() {
                inner.context.as_ref().map(|cx| cx.as_ref())
            } else {
                None
            };
            if !type_.is_synth() {
                error!(
                    "User mode exception: {:?} {:#x?}",
                    type_,
                    cx.expect("Architectural exception should has context")
                );
            }
            let exception = Exception::new(&self.0, type_, cx);
            inner.exception = Some(exception.clone());
            exception
        };
        if type_ == ExceptionType::ThreadExiting {
            let handled = self
                .0
                .proc()
                .debug_exceptionate()
                .send_exception(&exception);
            if let Ok(future) = handled {
                self.dying_run(future).await.ok();
            }
        } else {
            let future = exception.handle();
            pin_mut!(future);
            self.blocking_run(
                future,
                ThreadState::BlockedException,
                Duration::from_nanos(u64::max_value()),
                None,
            )
            .await
            .ok();
        }
        self.inner.lock().exception = None;
    }

    /// Run a blocking task when the thread is exited itself and dying.
    ///
    /// The task will stop running if and once the thread is killed.
    async fn dying_run<F, T, FT>(&self, future: F) -> ZxResult<T>
    where
        F: Future<Output = FT> + Unpin,
        FT: IntoResult<T>,
    {
        let killed = {
            let mut inner = self.inner.lock();
            if inner.killed {
                return Err(ZxError::STOP);
            }
            let (sender, receiver) = channel::<()>();
            inner.killer = Some(sender);
            receiver
        };
        select_biased! {
            ret = future.fuse() => ret.into_result(),
            _ = killed.fuse() => Err(ZxError::STOP),
        }
    }
}

impl core::ops::Deref for CurrentThread {
    type Target = Arc<Thread>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Drop for CurrentThread {
    fn drop(&mut self) {
        self.terminate();
    }
}

/// `into_result` returns `Self` if the type parameter is already a `ZxResult`,
/// otherwise wraps the value in an `Ok`.
///
/// Used to implement `Thread::blocking_run`, which takes a future whose `Output` may
/// or may not be a `ZxResult`.
pub trait IntoResult<T> {
    /// Performs the conversion.
    fn into_result(self) -> ZxResult<T>;
}

impl<T> IntoResult<T> for T {
    fn into_result(self) -> ZxResult<T> {
        Ok(self)
    }
}

impl<T> IntoResult<T> for ZxResult<T> {
    fn into_result(self) -> ZxResult<T> {
        self
    }
}

/// The thread state.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ThreadState {
    /// The thread has been created but it has not started running yet.
    New = 0,
    /// The thread is running user code normally.
    Running = 1,
    /// Stopped due to `zx_task_suspend()`.
    Suspended = 2,
    /// In a syscall or handling an exception.
    Blocked = 3,
    /// The thread is in the process of being terminated, but it has not been stopped yet.
    Dying = 4,
    /// The thread has stopped running.
    Dead = 5,
    /// The thread is stopped in an exception.
    BlockedException = 0x103,
    /// The thread is stopped in `zx_nanosleep()`.
    BlockedSleeping = 0x203,
    /// The thread is stopped in `zx_futex_wait()`.
    BlockedFutex = 0x303,
    /// The thread is stopped in `zx_port_wait()`.
    BlockedPort = 0x403,
    /// The thread is stopped in `zx_channel_call()`.
    BlockedChannel = 0x503,
    /// The thread is stopped in `zx_object_wait_one()`.
    BlockedWaitOne = 0x603,
    /// The thread is stopped in `zx_object_wait_many()`.
    BlockedWaitMany = 0x703,
    /// The thread is stopped in `zx_interrupt_wait()`.
    BlockedInterrupt = 0x803,
    /// Pager.
    BlockedPager = 0x903,
}

impl Default for ThreadState {
    fn default() -> Self {
        ThreadState::New
    }
}

/// The thread information.
#[repr(C)]
pub struct ThreadInfo {
    state: u32,
    wait_exception_channel_type: u32,
    cpu_affinity_mask: [u64; 8],
}

struct ThreadSwitchFuture {
    thread: Arc<Thread>,
    future: Mutex<ThreadFuturePinned>,
}

impl ThreadSwitchFuture {
    pub fn new(thread: Arc<Thread>, future: ThreadFuturePinned) -> Self {
        Self {
            future: Mutex::new(future),
            thread,
        }
    }
}

impl Future for ThreadSwitchFuture {
    type Output = ();
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        cfg_if! {
            if #[cfg(all(target_os = "none", target_arch = "aarch64"))] {
                use kernel_hal::arch::config::USER_TABLE_FLAG;
                kernel_hal::vm::activate_paging(self.thread.proc().vmar().table_phys() | USER_TABLE_FLAG);
            } else {
                kernel_hal::vm::activate_paging(self.thread.proc().vmar().table_phys());
            }
        }
        kernel_hal::thread::set_current_thread(Some(self.thread.clone()));
        let ret = self.future.lock().as_mut().poll(cx);
        kernel_hal::thread::set_current_thread(None);
        ret
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::object::*;
    use crate::task::*;
    use kernel_hal::timer::timer_now;

    #[test]
    fn create() {
        let root_job = Job::root();
        let proc = Process::create(&root_job, "proc").expect("failed to create process");
        let thread = Thread::create(&proc, "thread").expect("failed to create thread");
        assert_eq!(thread.flags(), ThreadFlag::empty());

        assert_eq!(thread.related_koid(), proc.id());
        let child = proc.get_child(thread.id()).unwrap().downcast_arc().unwrap();
        assert!(Arc::ptr_eq(&child, &thread));
    }

    #[async_std::test]
    async fn start() {
        kernel_hal::init();
        let root_job = Job::root();
        let proc = Process::create(&root_job, "proc").expect("failed to create process");
        let thread = Thread::create(&proc, "thread").expect("failed to create thread");
        let thread1 = Thread::create(&proc, "thread1").expect("failed to create thread");

        // function for new thread
        async fn new_thread(thread: CurrentThread) {
            let cx = thread.wait_for_run().await;
            assert_eq!(cx.general().rip, 1);
            assert_eq!(cx.general().rsp, 4);
            assert_eq!(cx.general().rdi, 3);
            assert_eq!(cx.general().rsi, 2);
            async_std::task::sleep(Duration::from_millis(10)).await;
            thread.put_context(cx);
        }

        // start a new thread
        let handle = Handle::new(proc.clone(), Rights::DEFAULT_PROCESS);
        proc.start(&thread, 1, 4, Some(handle.clone()), 2, |thread| {
            Box::pin(new_thread(thread))
        })
        .expect("failed to start thread");

        // check info and state
        let info = proc.get_info();
        assert!(info.started && !info.has_exited && info.return_code == 0);
        assert_eq!(proc.status(), Status::Running);
        assert_eq!(thread.state(), ThreadState::Running);

        // start again should fail
        assert_eq!(
            proc.start(&thread, 1, 4, Some(handle.clone()), 2, |thread| Box::pin(
                new_thread(thread)
            )),
            Err(ZxError::BAD_STATE)
        );

        // start another thread should fail
        assert_eq!(
            proc.start(&thread1, 1, 4, Some(handle.clone()), 2, |thread| Box::pin(
                new_thread(thread)
            )),
            Err(ZxError::BAD_STATE)
        );

        // wait 100ms for the new thread to exit
        async_std::task::sleep(core::time::Duration::from_millis(100)).await;

        // no other references to `Thread`
        assert_eq!(Arc::strong_count(&thread), 1);
        assert_eq!(thread.state(), ThreadState::Dead);
    }

    #[async_std::test]
    async fn blocking_run() {
        let root_job = Job::root();
        let proc = Process::create(&root_job, "proc").expect("failed to create process");
        let thread = Thread::create(&proc, "thread").expect("failed to create thread");
        let thread = CurrentThread(thread);

        let handle = Handle::new(proc.clone(), Rights::DEFAULT_PROCESS);
        let handle_value = proc.add_handle(handle);
        let object = proc
            .get_dyn_object_with_rights(handle_value, Rights::WAIT)
            .unwrap();

        let cancel_token = proc.get_cancel_token(handle_value).unwrap();
        let future = object.wait_signal(Signal::READABLE);
        let deadline = timer_now() + Duration::from_millis(20);
        let result = thread
            .blocking_run(
                future,
                ThreadState::BlockedWaitOne,
                deadline.into(),
                Some(cancel_token),
            )
            .await;
        assert_eq!(result.err(), Some(ZxError::TIMED_OUT));

        let cancel_token = proc.get_cancel_token(handle_value).unwrap();
        let future = object.wait_signal(Signal::READABLE);
        let deadline = timer_now() + Duration::from_millis(20);
        async_std::task::spawn({
            let proc = proc.clone();
            async move {
                async_std::task::sleep(Duration::from_millis(10)).await;
                proc.remove_handle(handle_value).unwrap();
            }
        });
        let result = thread
            .blocking_run(
                future,
                ThreadState::BlockedWaitOne,
                deadline.into(),
                Some(cancel_token),
            )
            .await;
        assert_eq!(result.err(), Some(ZxError::CANCELED));
    }

    #[test]
    fn info() {
        let root_job = Job::root();
        let proc = Process::create(&root_job, "proc").expect("failed to create process");
        let thread = Thread::create(&proc, "thread").expect("failed to create thread");

        let info = thread.get_thread_info();
        assert!(info.state == thread.state() as u32 && info.wait_exception_channel_type == 0);
        assert_eq!(
            thread.get_thread_exception_info().err(),
            Some(ZxError::BAD_STATE)
        );
    }

    #[test]
    fn read_write_state() {
        let root_job = Job::root();
        let proc = Process::create(&root_job, "proc").expect("failed to create process");
        let thread = Thread::create(&proc, "thread").expect("failed to create thread");

        const SIZE: usize = core::mem::size_of::<kernel_hal::context::GeneralRegs>();
        let mut buf = [0; 10];
        assert_eq!(
            thread.read_state(ThreadStateKind::General, &mut buf).err(),
            Some(ZxError::BAD_STATE)
        );
        assert_eq!(
            thread.write_state(ThreadStateKind::General, &buf).err(),
            Some(ZxError::BAD_STATE)
        );

        thread.suspend();

        assert_eq!(
            thread.read_state(ThreadStateKind::General, &mut buf).err(),
            Some(ZxError::BUFFER_TOO_SMALL)
        );
        assert_eq!(
            thread.write_state(ThreadStateKind::General, &buf).err(),
            Some(ZxError::BUFFER_TOO_SMALL)
        );

        let mut buf = [0; SIZE];
        assert!(thread
            .read_state(ThreadStateKind::General, &mut buf)
            .is_ok());
        assert!(thread.write_state(ThreadStateKind::General, &buf).is_ok());
        // TODO
    }

    #[test]
    fn ext() {
        let root_job = Job::root();
        let proc = Process::create(&root_job, "proc").expect("failed to create process");
        let thread = Thread::create(&proc, "thread").expect("failed to create thread");

        let _ext = thread.ext();
        // TODO
    }

    #[async_std::test]
    async fn wait_for_run() {
        let root_job = Job::root();
        let proc = Process::create(&root_job, "proc").expect("failed to create process");
        let thread = Thread::create(&proc, "thread").expect("failed to create thread");

        assert_eq!(thread.state(), ThreadState::New);

        thread.start(|thread| Box::pin(new_thread(thread))).unwrap();
        async fn new_thread(thread: CurrentThread) {
            assert_eq!(thread.state(), ThreadState::Running);

            // without suspend
            let context = thread.wait_for_run().await;
            thread.put_context(context);

            // with suspend
            thread.suspend();
            thread.suspend();
            assert_eq!(thread.state(), ThreadState::Suspended);
            async_std::task::spawn({
                let thread = (*thread).clone();
                async move {
                    async_std::task::sleep(Duration::from_millis(10)).await;
                    thread.resume();
                    async_std::task::sleep(Duration::from_millis(10)).await;
                    thread.resume();
                }
            });
            let time = timer_now();
            let _context = thread.wait_for_run().await;
            assert!(timer_now() - time >= Duration::from_millis(20));
        }
        let thread: Arc<dyn KernelObject> = thread;
        thread.wait_signal(Signal::THREAD_TERMINATED).await;
    }

    #[test]
    fn time() {
        let root_job = Job::root();
        let proc = Process::create(&root_job, "proc").expect("failed to create process");
        let thread = Thread::create(&proc, "thread").expect("failed to create thread");

        assert_eq!(thread.get_time(), 0);
        thread.time_add(10);
        assert_eq!(thread.get_time(), 10);
    }
}