MythTV  master
mthread.h
Go to the documentation of this file.
1 #ifndef MYTH_THREAD_H
2 #define MYTH_THREAD_H
3 
4 #include <climits>
5 
6 #include <QRunnable>
7 #include <QThread>
8 
9 #include "mythbaseexp.h"
10 #include "mythchrono.h"
11 
12 class MThreadInternal;
13 class MThread;
14 
18 bool MBASE_PUBLIC is_current_thread(QThread *thread);
21 
49 {
50  friend class MThreadInternal;
51  public:
53  explicit MThread(const QString &objectName);
58  explicit MThread(const QString &objectName, QRunnable *runnable);
59  virtual ~MThread();
60 
61  MThread(const MThread &) = delete; // not copyable
62  MThread &operator=(const MThread &) = delete; // not copyable
63 
65  void RunProlog(void);
68  void RunEpilog(void);
69 
72  QThread *qthread(void);
73 
74  void setObjectName(const QString &name);
75  QString objectName(void) const;
76 
77  void setPriority(QThread::Priority priority);
78  QThread::Priority priority(void) const;
79 
80  bool isFinished(void) const;
81  bool isRunning(void) const;
82 
83  void setStackSize(uint stackSize);
84  uint stackSize(void) const;
85 
87  void exit(int retcode = 0);
89  void start(QThread::Priority p = QThread::InheritPriority);
97  void terminate(void);
98  void quit(void);
99 
102  static void ThreadSetup(const QString& name);
105  static void ThreadCleanup(void);
106 
107  public:
110  bool wait(std::chrono::milliseconds time = std::chrono::milliseconds::max());
111 
115  static void Cleanup(void);
116  static void GetAllThreadNames(QStringList &list);
117  static void GetAllRunningThreadNames(QStringList &list);
118 
119  protected:
125  virtual void run(void);
127  int exec(void);
128 
129  static void setTerminationEnabled(bool enabled = true);
130  static void usleep(std::chrono::microseconds time);
131  template<typename R, typename P>
132  static typename std::enable_if_t<std::chrono::treat_as_floating_point<R>::value, void>
133  usleep(std::chrono::duration<R,P> time) { usleep(duration_cast<std::chrono::microseconds>(time)); };
134 
135  MThreadInternal *m_thread {nullptr};
136  QRunnable *m_runnable {nullptr};
137  bool m_prologExecuted {true};
138  bool m_epilogExecuted {true};
139 };
140 
141 #endif // MYTH_THREAD_H
MThread::usleep
static std::enable_if_t< std::chrono::treat_as_floating_point< R >::value, void > usleep(std::chrono::duration< R, P > time)
Definition: mthread.h:133
MThreadInternal::exec
int exec(void)
Definition: mthread.cpp:81
MThreadInternal::run
void run(void) override
Definition: mthread.cpp:78
mythbaseexp.h
quit
@ quit
Definition: lirc_client.h:30
MBASE_PUBLIC
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
is_current_thread
bool MBASE_PUBLIC is_current_thread(MThread *thread)
Use this to determine if you are in the named thread.
Definition: mthread.cpp:40
MThreadInternal
Definition: mthread.cpp:74
hardwareprofile.config.p
p
Definition: config.py:33
uint
unsigned int uint
Definition: compat.h:144
isRunning
static bool isRunning(const char *program)
Returns true if a program containing the specified string is running on this machine.
Definition: mythtv/programs/mythshutdown/main.cpp:202
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
mythchrono.h