2023-02-08 23:58:59 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdio.h>
|
|
|
|
#if __linux
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#endif
|
|
|
|
#include <signal.h>
|
|
|
|
#include <algorithm>
|
2023-02-22 17:35:30 +00:00
|
|
|
#include <time.h>
|
2023-02-08 23:58:59 +00:00
|
|
|
|
|
|
|
#include "trick/SysThread.hh"
|
|
|
|
|
|
|
|
bool Trick::SysThread::shutdown_finished = false;
|
|
|
|
|
2023-02-22 17:35:30 +00:00
|
|
|
|
2023-02-08 23:58:59 +00:00
|
|
|
// Construct On First Use to avoid the Static Initialization Fiasco
|
|
|
|
pthread_mutex_t& Trick::SysThread::list_mutex() {
|
|
|
|
static pthread_mutex_t list_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
return list_mutex;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_cond_t& Trick::SysThread::list_empty_cv() {
|
|
|
|
static pthread_cond_t list_empty_cv = PTHREAD_COND_INITIALIZER;
|
|
|
|
return list_empty_cv;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Trick::SysThread *>& Trick::SysThread::all_sys_threads() {
|
|
|
|
static std::vector<SysThread *> all_sys_threads;
|
|
|
|
return all_sys_threads;
|
|
|
|
}
|
|
|
|
|
2023-02-22 17:35:30 +00:00
|
|
|
Trick::SysThread::SysThread(std::string in_name) : ThreadBase(in_name) {
|
2023-02-08 23:58:59 +00:00
|
|
|
pthread_mutex_lock(&(list_mutex()));
|
|
|
|
all_sys_threads().push_back(this);
|
|
|
|
pthread_mutex_unlock(&(list_mutex()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Trick::SysThread::~SysThread() {
|
|
|
|
pthread_mutex_lock(&(list_mutex()));
|
|
|
|
if (!shutdown_finished) {
|
|
|
|
all_sys_threads().erase(std::remove(all_sys_threads().begin(), all_sys_threads().end(), this), all_sys_threads().end());
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&(list_mutex()));
|
|
|
|
}
|
|
|
|
|
|
|
|
int Trick::SysThread::ensureAllShutdown() {
|
|
|
|
pthread_mutex_lock(&(list_mutex()));
|
|
|
|
|
2023-02-22 17:35:30 +00:00
|
|
|
// Cancel all threads
|
2023-02-08 23:58:59 +00:00
|
|
|
for (SysThread * thread : all_sys_threads()) {
|
|
|
|
thread->cancel_thread();
|
|
|
|
}
|
|
|
|
|
2023-02-22 17:35:30 +00:00
|
|
|
// Join all threads
|
|
|
|
for (SysThread * thread : all_sys_threads()) {
|
|
|
|
thread->join_thread();
|
2023-02-08 23:58:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 17:35:30 +00:00
|
|
|
// Success!
|
2023-02-08 23:58:59 +00:00
|
|
|
shutdown_finished = true;
|
|
|
|
pthread_mutex_unlock(&(list_mutex()));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|