MythTV  master
taskqueue.h
Go to the documentation of this file.
1 // Program Name: taskqueue.h
3 // Created : Oct. 24, 2005
4 //
5 // Purpose : Used to process delayed tasks
6 //
7 // Copyright (c) 2005 David Blain <dblain@mythtv.org>
8 //
9 // Licensed under the GPL v2 or later, see COPYING for details
10 //
12 
13 #ifndef TASKQUEUE_H
14 #define TASKQUEUE_H
15 
16 // POSIX headers
17 #include <sys/types.h>
18 #ifndef _WIN32
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #endif // _WIN32
23 
24 // C++ headers
25 #include <map>
26 
27 // Qt headers
28 #include <QMutex>
29 
30 // MythTV headers
31 #include "referencecounter.h"
32 #include "upnputil.h"
33 #include "mthread.h"
34 #include "upnpexp.h"
35 
36 class Task;
37 class TaskQueue;
38 
40 // Typedefs
42 
43 using TaskMap = std::multimap< TaskTime, Task *>;
44 
47 //
48 // Task Class Definition
49 //
52 
53 class Task : public ReferenceCounter
54 {
55  protected:
56  static long m_nTaskCount;
57 
58  long m_nTaskId;
59 
60  protected:
61 
62  // Destructor protected to force use of Release Method
63  ~Task() override = default;
64 
65  public:
66 
67  explicit Task(const QString &debugName);
68 
69  long Id() const { return( m_nTaskId ); }
70 
71  virtual void Execute( TaskQueue *pQueue ) = 0;
72  virtual QString Name () = 0;
73 
74 };
75 
76 
78 // TaskQueue Singleton
80 
82 {
83  private:
84 
85  // Singleton instance used by all.
87 
88  protected:
89 
91  QMutex m_mutex;
92  bool m_bTermRequested {false};
93 
94  protected:
95 
96  bool IsTermRequested();
97 
98  void run() override; // MThread
99 
100  private:
101 
102  // ------------------------------------------------------------------
103  // Private so the singleton pattern can be inforced.
104  // ------------------------------------------------------------------
105 
106  TaskQueue();
107 
108  public:
109 
110  static TaskQueue* Instance();
111  static void Shutdown();
112 
113  ~TaskQueue() override;
114 
115  void RequestTerminate ( );
116 
117  void Clear ( );
118  void AddTask ( long msec , Task *pTask );
119  void AddTask ( TaskTime tt, Task *pTask );
120  void AddTask ( Task *pTask );
121 
122  Task *GetNextExpiredTask ( TaskTime tt, long nWithinMilliSecs = 50 );
123 
124 };
125 
126 #endif // TASKQUEUE_H
TaskQueue::m_mapTasks
TaskMap m_mapTasks
Definition: taskqueue.h:90
Task::Execute
virtual void Execute(TaskQueue *pQueue)=0
TaskTime
struct timeval TaskTime
Definition: httpserver.h:45
TaskQueue
Definition: taskqueue.h:81
TaskMap
std::multimap< TaskTime, Task * > TaskMap
Definition: taskqueue.h:43
TaskQueue::m_mutex
QMutex m_mutex
Definition: taskqueue.h:91
Task::Name
virtual QString Name()=0
Task
Definition: taskqueue.h:53
Task::Id
long Id() const
Definition: taskqueue.h:69
TaskQueue::g_pTaskQueue
static TaskQueue * g_pTaskQueue
Definition: taskqueue.h:86
MThread::run
virtual void run(void)
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
Definition: mthread.cpp:316
referencecounter.h
Task::m_nTaskCount
static long m_nTaskCount
Definition: taskqueue.h:56
Task::m_nTaskId
long m_nTaskId
Definition: taskqueue.h:58
Task::Task
Task(const QString &debugName)
Definition: taskqueue.cpp:41
Clear
#define Clear(a)
Definition: audiooutputopensles.cpp:49
Task::~Task
~Task() override=default
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
mthread.h
UPNP_PUBLIC
#define UPNP_PUBLIC
Definition: upnpexp.h:9
upnputil.h
ReferenceCounter
General purpose reference counter.
Definition: referencecounter.h:26
upnpexp.h