* Requests that the thread, if started, terminate. Optionally * (but by default) waits for the thread to terminate. * * You may call this on the thread object from any other thread * (but not from within the thread itself -- just return from * your Run function instead). * * Does nothing if the thread was never started, or is already * terminated. Note, however, that Term
| 198 | * t.Terminate(SyscallThread::WaitType::kRequestOnly). |
| 199 | */ |
| 200 | void Terminate(enum WaitType waittype = WaitType::kWait) { |
| 201 | if (state_ == ThreadState::kNotStarted) { |
| 202 | // If never started, there is nothing to terminate. |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | // We should signal the thread if: |
| 207 | // - it's gotten past kReady (but this test would race |
| 208 | // with the thread itself, so we just assume it has), and |
| 209 | // - no one else asked it to exit yet, and |
| 210 | // - it's not already on its way out, or done. |
| 211 | // (if someone else already asked, that someone-else also |
| 212 | // kicked off any signalling required). |
| 213 | bool send_signal = !exit_requested_ && state_ < ThreadState::kExiting; |
| 214 | exit_requested_ = true; |
| 215 | if (send_signal) { |
| 216 | SendSignal(); |
| 217 | } |
| 218 | if (waittype == WaitType::kWait) { |
| 219 | WaitFor(); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /*! |
| 224 | * Waits for the thread to finish. Note that this may result in |