aboutsummaryrefslogtreecommitdiffstats
path: root/components/util/thread.rs
blob: 85a7c52adf8f0ee6c0653988bece59fa4f297085 (plain) (blame)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use ipc_channel::ipc::IpcSender;
use panicking;
use serde::Serialize;
use std::any::Any;
use std::borrow::ToOwned;
use std::sync::mpsc::Sender;
use std::thread;
use thread_state;

pub type PanicReason = Option<String>;

pub fn spawn_named<F>(name: String, f: F)
    where F: FnOnce() + Send + 'static
{
    spawn_named_with_send_on_failure_maybe(name, None, f, |_| {});
}

pub trait AddFailureDetails {
    fn add_panic_message(&mut self, message: String);
    fn add_panic_object(&mut self, object: &Any) {
        if let Some(message) = object.downcast_ref::<String>() {
            self.add_panic_message(message.to_owned());
        } else if let Some(&message) = object.downcast_ref::<&'static str>() {
            self.add_panic_message(message.to_owned());
        }
    }
}

/// An abstraction over `Sender<T>` and `IpcSender<T>`, for use in
/// `spawn_named_with_send_on_failure`.
pub trait SendOnFailure {
    type Value;
    fn send_on_failure(&mut self, value: Self::Value);
}

impl<T> SendOnFailure for Sender<T> where T: Send + 'static {
    type Value = T;
    fn send_on_failure(&mut self, value: T) {
        // Discard any errors to avoid double-panic
        let _ = self.send(value);
    }
}

impl<T> SendOnFailure for IpcSender<T> where T: Send + Serialize + 'static {
    type Value = T;
    fn send_on_failure(&mut self, value: T) {
        // Discard any errors to avoid double-panic
        let _ = self.send(value);
    }
}

/// Arrange to send a particular message to a channel if the thread fails.
pub fn spawn_named_with_send_on_failure<F, T, S>(name: String,
                                                 state: thread_state::ThreadState,
                                                 f: F,
                                                 mut msg: T,
                                                 mut dest: S)
    where F: FnOnce() + Send + 'static,
          T: Send + AddFailureDetails + 'static,
          S: Send + SendOnFailure + 'static,
          S::Value: From<T>,
{
    spawn_named_with_send_on_failure_maybe(name, Some(state), f,
                                           move |err| {
                                               msg.add_panic_object(err);
                                               dest.send_on_failure(S::Value::from(msg));
                                           });
}

fn spawn_named_with_send_on_failure_maybe<F, G>(name: String,
                                                 state: Option<thread_state::ThreadState>,
                                                 f: F,
                                                 fail: G)
                                                 where F: FnOnce() + Send + 'static,
                                                       G: FnOnce(&(Any + Send)) + Send + 'static {


    let builder = thread::Builder::new().name(name.clone());

    let local = panicking::PanicHandlerLocal {
        fail: Box::new(fail),
    };

    let f_with_state = move || {
        if let Some(state) = state {
            thread_state::initialize(state);
        }

        // set the handler
        panicking::LOCAL_INFO.with(|i| {
            *i.borrow_mut() = Some(local);
        });
        f();
    };


    builder.spawn(f_with_state).unwrap();
}