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
use std::sync::Arc;

#[cfg(unix)]
use {mio::unix::pipe, parking_lot::Mutex, std::io::Write};

#[cfg(windows)]
use {
  crate::win,
  std::sync::atomic::{AtomicU32, Ordering},
  windows_sys::Win32::{Foundation::HANDLE, System::Threading::SetEvent}
};

use crate::err::Error;


/// Internal object used to signal receiver thread.
#[derive(Clone)]
pub(crate) struct CmdSignal {
  #[cfg(unix)]
  pub(crate) ctl_tx: Arc<Mutex<pipe::Sender>>,

  #[cfg(windows)]
  pub(crate) cmdreq: Arc<AtomicU32>,

  #[cfg(windows)]
  pub(crate) hev_wakeup: HANDLE
}

impl CmdSignal {
  /// Instruct receiver thread to self-terminate.
  pub(crate) fn kill<E>(&self) -> Result<(), Error<E>> {
    #[cfg(unix)]
    {
      let buf = "t";
      self.ctl_tx.lock().write_all(buf.as_bytes()).map_err(|e| {
        Error::Internal(format!(
          "Unable to send termination signal to receiver thread; {}",
          e
        ))
      })?;
    }

    #[cfg(windows)]
    {
      // ToDo: Is there a more efficient ordering here?
      self
        .cmdreq
        .fetch_or(win::CmdFlags::KILL.bits(), Ordering::SeqCst);
      unsafe {
        if SetEvent(self.hev_wakeup) == 0 {
          Err(Error::Internal(format!(
            "Unable to send termination signal to receiver thread"
          )))?;
        }
      }
    }
    Ok(())
  }

  /// Instruct receiver thread to call inspect call-back.
  #[cfg(feature = "inspect")]
  pub(crate) fn inspect(&self) {
    #[cfg(unix)]
    {
      let buf = "i";
      if let Err(e) = self.ctl_tx.lock().write_all(buf.as_bytes()) {
        eprintln!("Unable to send inspect signal to receiver thread; {}", e);
      }
    }

    #[cfg(windows)]
    {
      // ToDo: Is there a more efficient ordering here?
      self
        .cmdreq
        .fetch_or(win::CmdFlags::INSPECT.bits(), Ordering::SeqCst);
      unsafe {
        if SetEvent(self.hev_wakeup) == 0 {
          eprintln!("Unable to send inspect signal to receiver thread");
        }
      }
    }
  }

  /// Instruct receiver thread to call idle callback.
  #[cfg(feature = "idle")]
  pub(crate) fn idle(&self) {
    #[cfg(unix)]
    {
      let buf = "d";
      if let Err(e) = self.ctl_tx.lock().write_all(buf.as_bytes()) {
        eprintln!("Unable to send idle command to receiver thread; {}", e);
      }
    }

    #[cfg(windows)]
    {
      self
        .cmdreq
        .fetch_or(win::CmdFlags::IDLE.bits(), Ordering::SeqCst);
      unsafe {
        if SetEvent(self.hev_wakeup) == 0 {
          eprintln!("Unable to send idle signal to receiver thread");
        }
      }
    }
  }

  #[allow(dead_code)]
  pub(crate) fn channel(&self) {
    #[cfg(unix)]
    {
      let buf = "c";
      if let Err(e) = self.ctl_tx.lock().write_all(buf.as_bytes()) {
        eprintln!("Unable to send channel command to receiver thread; {}", e);
      }
    }

    #[cfg(windows)]
    {
      self
        .cmdreq
        .fetch_or(win::CmdFlags::CHANNEL.bits(), Ordering::SeqCst);
      unsafe {
        if SetEvent(self.hev_wakeup) == 0 {
          eprintln!("Unable to send channel signal to receiver thread");
        }
      }
    }
  }
}

// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :