MythTV  master
programinfoupdater.cpp
Go to the documentation of this file.
1 // MythTV headers
2 #include "programinfoupdater.h"
3 #include "mthreadpool.h"
4 #include "mythlogging.h"
5 #include "mythcorecontext.h"
6 #include "remoteutil.h"
7 
8 #include <unistd.h> // for usleep()
9 
11  uint recordedid, PIAction action, uint64_t filesize)
12 {
13  QMutexLocker locker(&m_lock);
15  {
16  QHash<uint,PIKeyData>::iterator it = m_needsUpdate.find(recordedid);
17  // If there is no action in the set we can insert
18  // If it is the same type of action we can overwrite,
19  // If it the new action is a full update we can overwrite
20  if (it == m_needsUpdate.end())
21  m_needsUpdate.insert(recordedid, PIKeyData(action, filesize));
22  else if (((*it).m_action == action) || (kPIUpdate == action))
23  (*it) = PIKeyData(action, filesize);
24  }
25  else
26  {
27  m_needsAddDelete.emplace_back(recordedid, action);
28  }
29 
30  // Start a new run() if one isn't already running..
31  // The lock prevents anything from getting stuck on a queue.
32  if (!m_isRunning)
33  {
34  m_isRunning = true;
35  MThreadPool::globalInstance()->start(this, "ProgramInfoUpdater");
36  }
37  else
38  m_moreWork.wakeAll();
39 }
40 
42 {
43  bool workDone = false;
44 
45  do {
46  workDone = false;
47 
48  // we don't need instant updates allow a few to queue up
49  // if they come in quick succession, this allows multiple
50  // updates to be consolidated into one update...
51  usleep(200 * 1000); // 200ms
52 
53  m_lock.lock();
54 
55  // send adds and deletes in the order they were queued
56  for (auto & pi : m_needsAddDelete)
57  {
58  if (kPIAdd != pi.m_action && kPIDelete != pi.m_action)
59  continue;
60 
61  QString type = (kPIAdd == pi.m_action) ? "ADD" : "DELETE";
62  QString msg = QString("RECORDING_LIST_CHANGE %1 %2")
63  .arg(type).arg(pi.m_recordedid);
64 
65  workDone = true;
67  }
68  m_needsAddDelete.clear();
69 
70  // Send updates in any old order, we just need
71  // one per updated ProgramInfo.
72  // NOLINTNEXTLINE(modernize-loop-convert)
73  for (auto itu = m_needsUpdate.begin(); itu != m_needsUpdate.end(); ++itu)
74  {
75  QString msg;
76 
77  if (kPIUpdateFileSize == (*itu).m_action)
78  {
79  msg = QString("UPDATE_FILE_SIZE %1 %2")
80  .arg(itu.key())
81  .arg((*itu).m_filesize);
82  }
83  else
84  {
85  msg = QString("MASTER_UPDATE_REC_INFO %1")
86  .arg(itu.key());
87  }
88 
89  workDone = true;
91  }
92  m_needsUpdate.clear();
93 
94  if ( workDone )
95  m_moreWork.wait(&m_lock, 1000);
96 
97  m_lock.unlock();
98  } while( workDone );
99 
100  m_isRunning = false;
101 }
102 
103 /*
104  * vim:ts=4:sw=4:ai:et:si:sts=4
105  */
PIKeyData
Definition: programinfoupdater.h:41
MythCoreContext::SendMessage
void SendMessage(const QString &message)
Definition: mythcorecontext.cpp:1526
kPIDelete
@ kPIDelete
Definition: programinfoupdater.h:20
ProgramInfoUpdater::run
void run(void) override
Definition: programinfoupdater.cpp:41
kPIAdd
@ kPIAdd
Definition: programinfoupdater.h:19
remoteutil.h
ProgramInfoUpdater::m_moreWork
QWaitCondition m_moreWork
Definition: programinfoupdater.h:59
ProgramInfoUpdater::m_lock
QMutex m_lock
Definition: programinfoupdater.h:58
ProgramInfoUpdater::m_needsUpdate
QHash< uint, PIKeyData > m_needsUpdate
Definition: programinfoupdater.h:62
mythlogging.h
programinfoupdater.h
PIAction
PIAction
Definition: programinfoupdater.h:18
ProgramInfoUpdater::insert
void insert(uint recordedid, PIAction action, uint64_t filesize=0ULL)
Definition: programinfoupdater.cpp:10
ProgramInfoUpdater::m_needsAddDelete
std::vector< PIKeyAction > m_needsAddDelete
Definition: programinfoupdater.h:61
uint
unsigned int uint
Definition: compat.h:140
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:57
mthreadpool.h
kPIUpdateFileSize
@ kPIUpdateFileSize
Definition: programinfoupdater.h:22
mythcorecontext.h
ProgramInfoUpdater::m_isRunning
bool m_isRunning
Definition: programinfoupdater.h:60
build_compdb.action
action
Definition: build_compdb.py:9
kPIUpdate
@ kPIUpdate
Definition: programinfoupdater.h:21
MThreadPool::globalInstance
static MThreadPool * globalInstance(void)
Definition: mthreadpool.cpp:307
MThreadPool::start
void start(QRunnable *runnable, const QString &debugName, int priority=0)
Definition: mthreadpool.cpp:342