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 <QThread>
7 
8 #include "mythbaseexp.h"
9 #include "mythchrono.h"
10 
11 class MThreadInternal;
12 class QStringList;
13 class QRunnable;
14 class MThread;
15 
19 bool MBASE_PUBLIC is_current_thread(QThread *thread);
22 
50 {
51  friend class MThreadInternal;
52  public:
54  explicit MThread(const QString &objectName);
59  explicit MThread(const QString &objectName, QRunnable *runnable);
60  virtual ~MThread();
61 
62  MThread(const MThread &) = delete; // not copyable
63  MThread &operator=(const MThread &) = delete; // not copyable
64 
66  void RunProlog(void);
69  void RunEpilog(void);
70 
73  QThread *qthread(void);
74 
75  void setObjectName(const QString &name);
76  QString objectName(void) const;
77 
78  void setPriority(QThread::Priority priority);
79  QThread::Priority priority(void) const;
80 
81  bool isFinished(void) const;
82  bool isRunning(void) const;
83 
84  void setStackSize(uint stackSize);
85  uint stackSize(void) const;
86 
88  void exit(int retcode = 0);
90  void start(QThread::Priority p = QThread::InheritPriority);
98  void terminate(void);
99  void quit(void);
100 
103  static void ThreadSetup(const QString& name);
106  static void ThreadCleanup(void);
107 
108  public:
111  bool wait(std::chrono::milliseconds time = std::chrono::milliseconds::max());
112 
116  static void Cleanup(void);
117  static void GetAllThreadNames(QStringList &list);
118  static void GetAllRunningThreadNames(QStringList &list);
119 
120  protected:
126  virtual void run(void);
128  int exec(void);
129 
130  static void setTerminationEnabled(bool enabled = true);
131  static void usleep(std::chrono::microseconds time);
132  template<typename R, typename P>
133  static typename std::enable_if_t<std::chrono::treat_as_floating_point<R>::value, void>
134  usleep(std::chrono::duration<R,P> time) { usleep(duration_cast<std::chrono::microseconds>(time)); };
135 
136  MThreadInternal *m_thread {nullptr};
137  QRunnable *m_runnable {nullptr};
138  bool m_prologExecuted {true};
139  bool m_epilogExecuted {true};
140 };
141 
142 #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:134
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:140
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:49
mythchrono.h