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