Torc  0.1
torcrunlooposx.cpp
Go to the documentation of this file.
1 /* Class TorcRunLoopOSX
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2012-18
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 // Qt
24 #include <QTimer>
25 #include <QAtomicInt>
26 
27 // Torc
28 #include "torclocalcontext.h"
29 #include "torclogging.h"
30 #include "torccocoa.h"
31 #include "torcqthread.h"
32 #include "torcadminthread.h"
33 #include "torcrunlooposx.h"
34 
35 CFRunLoopRef gAdminRunLoop = 0;
36 QAtomicInt gAdminRunLoopRunning(0);
37 
56  : TorcQThread(QStringLiteral("OSXRunLoop")),
57  m_object(nullptr)
58 {
59 }
60 
62 {
63  delete m_object;
64 }
65 
67 {
68  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("OSX callback thread starting"));
69  gAdminRunLoop = CFRunLoopGetCurrent();
70  m_object = new CallbackObject();
71 }
72 
74 {
75  if (m_object)
76  m_object->disconnect();
77  delete m_object;
78  m_object = nullptr;
79  gAdminRunLoop = 0;
80  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("OSX callback thread stopping"));
81 }
82 
87 {
88  // schedule the run loop to start
89  QTimer::singleShot(10, Qt::CoarseTimer, this, SLOT(Run()));
90 }
91 
93 {
94  // start the run loop
95  if (gAdminRunLoopRunning.fetchAndAddOrdered(0))
96  CFRunLoopRun();
97 
98  // if it exits, schedule it to start again. This usually means there is nothing
99  // setup using a callback, so don't waste cycles and wait a while before restarting.
100  if (gAdminRunLoopRunning.fetchAndAddOrdered(0))
101  QTimer::singleShot(100, Qt::CoarseTimer, this, SLOT(Run()));
102 }
103 
107 static class TorcRunLoopOSX : public TorcAdminObject
108 {
109  public:
112  m_thread(nullptr),
113  m_createdThread(false)
114  {
115  }
116 
118  {
119  Destroy();
120  }
121 
122  void Create(void)
123  {
124  Destroy();
125 
126  // we no longer use a separate admin thread - this code is retained just in case...
127  if (true)
128  {
129  gAdminRunLoop = CFRunLoopGetMain();
130  return;
131  }
132 
133  m_createdThread = true;
134  gAdminRunLoopRunning.ref();
135 
136  m_thread = new TorcOSXCallbackThread();
137  m_thread->start();
138 
139  int count = 0;
140  while (count++ < 10 && !m_thread->isRunning())
141  QThread::msleep(count < 2 ? 10 : 100);
142 
143  if (!m_thread->isRunning())
144  LOG(VB_GENERAL, LOG_WARNING, QStringLiteral("OS X callback thread not started yet!"));
145  }
146 
147  void Destroy(void)
148  {
149  if (!m_createdThread)
150  return;
151 
152  m_createdThread = false;
153  gAdminRunLoopRunning.deref();
154 
155  if (gAdminRunLoop)
156  CFRunLoopStop(gAdminRunLoop);
157 
158  if (m_thread)
159  {
160  m_thread->quit();
161  m_thread->wait();
162  }
163 
164  delete m_thread;
165  m_thread = nullptr;
166  }
167 
168  private:
169  TorcOSXCallbackThread *m_thread;
170  bool m_createdThread;
172 
TorcQThread subclass to run a CFRunLoop.
QAtomicInt gAdminRunLoopRunning(0)
A factory class for automatically running objects outside of the main loop.
#define TORC_ADMIN_CRIT_PRIORITY
CFRunLoopRef gAdminRunLoop
A reference to the global administration CFRunLoop.
A simple class encapusulating a single timer to start the CFRunLoop.
void Destroy(void)
A TorcAdminObject to instanciate the TorcOSXCallbackThread singleton.
friend class TorcRunLoopOSX
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
A Torc specific wrapper around QThread.
Definition: torcqthread.h:7