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
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
use std::{
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc,
    },
    thread::JoinHandle,
};

use parking_lot::Mutex;
use tracing::info;

use crate::common::thread::{Runnable, ServiceThread};

pub struct ServiceThreadStd {
    name: String,
    runnable: Arc<Mutex<dyn Runnable>>,
    thread: Option<JoinHandle<()>>,
    stopped: Arc<AtomicBool>,
    started: Arc<AtomicBool>,
    notified: (parking_lot::Mutex<()>, parking_lot::Condvar),
}

impl ServiceThreadStd {
    pub fn new<T: Runnable>(name: String, runnable: T) -> Self {
        ServiceThreadStd {
            name,
            runnable: Arc::new(Mutex::new(runnable)),
            thread: None,
            stopped: Arc::new(AtomicBool::new(false)),
            started: Arc::new(AtomicBool::new(false)),
            notified: (Default::default(), Default::default()),
        }
    }
}

impl ServiceThreadStd {
    pub fn start(&mut self) {
        if let Ok(value) =
            self.started
                .compare_exchange(false, true, Ordering::SeqCst, Ordering::Relaxed)
        {
            if value {
                return;
            }
        } else {
            return;
        }
        let name = self.name.clone();
        let runnable = self.runnable.clone();
        let stopped = self.stopped.clone();
        let thread = std::thread::Builder::new()
            .name(name.clone())
            .spawn(move || {
                log::info!("Starting service thread: {}", name);
                if stopped.load(std::sync::atomic::Ordering::Relaxed) {
                    log::info!("Service thread stopped: {}", name);
                    return;
                }
                runnable.lock().run();
            })
            .expect("Failed to start service thread");
        self.thread = Some(thread);
    }

    pub fn shutdown(&mut self) {
        self.shutdown_interrupt(false);
    }

    pub fn shutdown_interrupt(&mut self, interrupt: bool) {
        if let Ok(value) =
            self.started
                .compare_exchange(true, false, Ordering::SeqCst, Ordering::Relaxed)
        {
            if !value {
                return;
            }
        } else {
            return;
        }
        self.stopped.store(true, Ordering::Relaxed);
        self.wakeup();
        if let Some(thread) = self.thread.take() {
            if interrupt {
                drop(thread);
            } else {
                thread.join().expect("Failed to join service thread");
            }
        }
    }

    pub fn make_stop(&mut self) {
        if !self.started.load(Ordering::Acquire) {
            return;
        }
        self.stopped.store(true, Ordering::Release);
    }

    pub fn wakeup(&mut self) {
        self.notified.1.notify_all();
    }

    pub fn wait_for_running(&mut self, interval: i64) {
        let mut guard = self.notified.0.lock();
        self.notified.1.wait_for(
            &mut guard,
            std::time::Duration::from_millis(interval as u64),
        );
    }

    pub fn is_stopped(&self) -> bool {
        self.stopped.load(Ordering::Acquire)
    }

    pub fn get_service_name(&self) -> String {
        self.name.clone()
    }
}

#[cfg(test)]
mod tests {
    /*use mockall::{automock, predicate::*};

    use super::*;

    struct MockTestRunnable;
    impl MockTestRunnable {
        fn new() -> MockTestRunnable {
            MockTestRunnable
        }
    }
    impl Runnable for MockTestRunnable {
        fn run(&mut self, service_thread: &dyn ServiceThread) {}
    }

    #[test]
    fn test_start_and_shutdown() {
        let mock_runnable = MockTestRunnable::new();

        let mut service_thread =
            ServiceThreadStd::new("TestServiceThread".to_string(), mock_runnable);

        service_thread.start();
        assert!(service_thread.started.load(Ordering::SeqCst));
        assert!(!service_thread.stopped.load(Ordering::SeqCst));

        service_thread.shutdown_interrupt(false);
        assert!(!service_thread.started.load(Ordering::SeqCst));
        assert!(service_thread.stopped.load(Ordering::SeqCst));
    }

    #[test]
    fn test_make_stop() {
        let mock_runnable = MockTestRunnable::new();
        let mut service_thread =
            ServiceThreadStd::new("TestServiceThread".to_string(), mock_runnable);

        service_thread.start();
        service_thread.make_stop();
        assert!(service_thread.is_stopped());
    }

    #[test]
    fn test_wait_for_running() {
        let mock_runnable = MockTestRunnable::new();
        let mut service_thread =
            ServiceThreadStd::new("TestServiceThread".to_string(), mock_runnable);

        service_thread.start();
        service_thread.wait_for_running(100);
        assert!(service_thread.started.load(Ordering::SeqCst));
    }

    #[test]
    fn test_wakeup() {
        let mock_runnable = MockTestRunnable::new();
        let mut service_thread =
            ServiceThreadStd::new("TestServiceThread".to_string(), mock_runnable);

        service_thread.start();
        service_thread.wakeup();
        // We expect that the wakeup method is called successfully.
    }*/
}