OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
StdSchedulerThread Class Reference

#include <StdScheduler.h>

Inheritance diagram for StdSchedulerThread:
[legend]
Collaboration diagram for StdSchedulerThread:
[legend]

Public Member Functions

 StdSchedulerThread ()
 
virtual ~StdSchedulerThread ()
 
void Clear ()
 
void Set (StdSchedulerProc **ppProcs, int iProcCnt)
 
void Add (StdSchedulerProc *pProc)
 
void Remove (StdSchedulerProc *pProc)
 
bool Start ()
 
void Stop ()
 
int getProcCnt () const
 
bool hasProc (StdSchedulerProc *pProc)
 
bool IsInManualLoop ()
 
void Added (StdSchedulerProc *pProc)
 
void Removing (StdSchedulerProc *pProc)
 
void Changed (StdSchedulerProc *pProc)
 
void StartOnCurrentThread ()
 
C4TimeMilliseconds GetNextTick (C4TimeMilliseconds tNow)
 
bool ScheduleProcs (int iTimeout=1000/36)
 
void UnBlock ()
 

Protected Member Functions

virtual void OnError (StdSchedulerProc *)
 
virtual bool DoScheduleProcs (int iTimeout)
 

Detailed Description

Definition at line 277 of file StdScheduler.h.

Constructor & Destructor Documentation

StdSchedulerThread::StdSchedulerThread ( )

Definition at line 170 of file StdScheduler.cpp.

171  : fThread(false)
172 {
173 
174 }
StdSchedulerThread::~StdSchedulerThread ( )
virtual

Definition at line 176 of file StdScheduler.cpp.

References Clear().

177 {
178  Clear();
179 }

Here is the call graph for this function:

Member Function Documentation

void StdSchedulerThread::Add ( StdSchedulerProc pProc)

Definition at line 200 of file StdScheduler.cpp.

References StdScheduler::Add(), Start(), and Stop().

Referenced by C4NetIOMan::AddIO(), and C4InteractiveThread::AddProc().

201 {
202  // Thread is running? Stop it first
203  bool fGotThread = fThread;
204  if (fGotThread) Stop();
205  // Set
206  StdScheduler::Add(pProc);
207  // Restart
208  if (fGotThread) Start();
209 }
void Add(StdSchedulerProc *pProc)

Here is the call graph for this function:

Here is the caller graph for this function:

void StdScheduler::Added ( StdSchedulerProc pProc)
inherited

Definition at line 281 of file StdSchedulerMac.mm.

References SCHAdditions::assignAdditionForProc:, and SCHAdditions::requestAdditionsForScheduler:.

Referenced by StdScheduler::Add().

282 {
283  [[SCHAdditions requestAdditionsForScheduler:this] assignAdditionForProc:pProc];
284 }

Here is the call graph for this function:

Here is the caller graph for this function:

void StdScheduler::Changed ( StdSchedulerProc pProc)
inherited

Definition at line 294 of file StdSchedulerMac.mm.

References SCHAdditions::additionForProc:, SCHAddition::changed, and SCHAdditions::requestAdditionsForScheduler:.

295 {
296  [[[SCHAdditions requestAdditionsForScheduler:this] additionForProc:pProc] changed];
297 }

Here is the call graph for this function:

void StdSchedulerThread::Clear ( )

Definition at line 181 of file StdScheduler.cpp.

References StdScheduler::Clear(), and Stop().

Referenced by C4NetIOMan::Clear(), and ~StdSchedulerThread().

182 {
183  // Stop thread
184  if (fThread) Stop();
185  // Clear scheduler
187 }

Here is the call graph for this function:

Here is the caller graph for this function:

virtual bool StdScheduler::DoScheduleProcs ( int  iTimeout)
protectedvirtualinherited

Reimplemented in C4AbstractApp.

Referenced by C4AbstractApp::DoScheduleProcs(), and StdScheduler::ScheduleProcs().

Here is the caller graph for this function:

C4TimeMilliseconds StdScheduler::GetNextTick ( C4TimeMilliseconds  tNow)
inherited

Definition at line 133 of file StdScheduler.cpp.

References C4TimeMilliseconds::PositiveInfinity.

Referenced by C4AbstractApp::Run(), and StdScheduler::ScheduleProcs().

134 {
136  for (auto proc : procs)
137  {
138  tProcTick = std::min(tProcTick, proc->GetNextTick(tNow));
139  }
140  return tProcTick;
141 }

Here is the caller graph for this function:

int StdScheduler::getProcCnt ( ) const
inlineinherited

Definition at line 249 of file StdScheduler.h.

Referenced by C4InteractiveThread::AddProc(), and C4InteractiveThread::RemoveProc().

249 { return procs.size()-1; } // ignore internal NoopNotifyProc

Here is the caller graph for this function:

bool StdScheduler::hasProc ( StdSchedulerProc pProc)
inlineinherited

Definition at line 250 of file StdScheduler.h.

Referenced by C4InteractiveThread::RemoveProc().

250 { return std::find(procs.begin(), procs.end(), pProc) != procs.end(); }

Here is the caller graph for this function:

bool StdScheduler::IsInManualLoop ( )
inlineinherited

Definition at line 251 of file StdScheduler.h.

251 { return isInManualLoop; }
virtual void StdScheduler::OnError ( StdSchedulerProc )
inlineprotectedvirtualinherited

Definition at line 272 of file StdScheduler.h.

272 { }
void StdSchedulerThread::Remove ( StdSchedulerProc pProc)

Definition at line 211 of file StdScheduler.cpp.

References StdScheduler::Remove(), Start(), and Stop().

Referenced by C4InteractiveThread::RemoveProc().

212 {
213  // Thread is running? Stop it first
214  bool fGotThread = fThread;
215  if (fGotThread) Stop();
216  // Set
217  StdScheduler::Remove(pProc);
218  // Restart
219  if (fGotThread) Start();
220 }
void Remove(StdSchedulerProc *pProc)

Here is the call graph for this function:

Here is the caller graph for this function:

void StdScheduler::Removing ( StdSchedulerProc pProc)
inherited

Definition at line 286 of file StdSchedulerMac.mm.

References SCHAdditions::requestAdditionsForScheduler:.

Referenced by StdScheduler::Remove().

287 {
288  auto x = [SCHAdditions requestAdditionsForScheduler:this];
289  [x removeAdditionForProc:pProc];
290  if ([x numberOfAdditions] == 0)
291  [SCHAdditions removeAdditions:x];
292 }

Here is the call graph for this function:

Here is the caller graph for this function:

bool StdScheduler::ScheduleProcs ( int  iTimeout = 1000/36)
inherited

Definition at line 143 of file StdScheduler.cpp.

References StdScheduler::DoScheduleProcs(), StdScheduler::GetNextTick(), and C4TimeMilliseconds::Now().

Referenced by C4AulDebug::DebugStep(), C4Network2::DoLobby(), C4GUI::Dialog::DoModal(), C4ScenarioListLoader::DoProcessCallback(), C4GUI::Dialog::Execute(), C4Network2::FinalInit(), C4Game::InitNetworkFromAddress(), C4Network2::LeaguePlrAuth(), C4Network2::LeagueStart(), C4AulDebug::Listen(), C4Network2::RetrieveRes(), C4AbstractApp::Run(), and C4AppDelegate::suggestQuitting:.

144 {
145  // Needs at least one process to work properly
146  if (!procs.size()) return false;
147 
148  // Get timeout
150  C4TimeMilliseconds tProcTick = GetNextTick(tNow);
151  if (iTimeout == -1 || tNow + iTimeout > tProcTick)
152  {
153  iTimeout = std::max<decltype(iTimeout)>(tProcTick - tNow, 0);
154  }
155 
156  bool old = isInManualLoop;
157  isInManualLoop = true;
158  bool res = DoScheduleProcs(iTimeout);
159  isInManualLoop = old;
160  return res;
161 }
C4TimeMilliseconds GetNextTick(C4TimeMilliseconds tNow)
virtual bool DoScheduleProcs(int iTimeout)
static C4TimeMilliseconds Now()

Here is the call graph for this function:

Here is the caller graph for this function:

void StdSchedulerThread::Set ( StdSchedulerProc **  ppProcs,
int  iProcCnt 
)

Definition at line 189 of file StdScheduler.cpp.

References StdScheduler::Set(), Start(), and Stop().

190 {
191  // Thread is running? Stop it first
192  bool fGotThread = fThread;
193  if (fGotThread) Stop();
194  // Set
195  StdScheduler::Set(ppProcs, iProcCnt);
196  // Restart
197  if (fGotThread) Start();
198 }
void Set(StdSchedulerProc **ppProcs, int iProcCnt)

Here is the call graph for this function:

bool StdSchedulerThread::Start ( )

Definition at line 222 of file StdScheduler.cpp.

References Stop().

Referenced by Add(), C4InteractiveThread::AddProc(), main(), Remove(), and Set().

223 {
224  // already running? stop
225  if (fThread) Stop();
226  // begin thread
227  fRunThreadRun = true;
228 #ifdef HAVE_WINTHREAD
229  iThread = _beginthread(_ThreadFunc, 0, this);
230  fThread = (iThread != -1);
231 #elif defined(HAVE_PTHREAD)
232  fThread = !pthread_create(&Thread, nullptr, _ThreadFunc, this);
233 #endif
234  // success?
235  return fThread;
236 }

Here is the call graph for this function:

Here is the caller graph for this function:

void StdScheduler::StartOnCurrentThread ( )
inherited

Definition at line 276 of file StdSchedulerMac.mm.

References SCHAdditions::requestAdditionsForScheduler:, and SCHAdditions::start.

277 {
278  [[SCHAdditions requestAdditionsForScheduler:this] start];
279 }

Here is the call graph for this function:

void StdSchedulerThread::Stop ( )

Definition at line 238 of file StdScheduler.cpp.

References StdScheduler::UnBlock().

Referenced by Add(), Clear(), main(), Remove(), C4InteractiveThread::RemoveProc(), Set(), and Start().

239 {
240  // Not running?
241  if (!fThread) return;
242  // Set flag
243  fRunThreadRun = false;
244  // Unblock
245  UnBlock();
246 #ifdef HAVE_WINTHREAD
247  // Wait for thread to terminate itself
248  HANDLE hThread = reinterpret_cast<HANDLE>(iThread);
249  if (WaitForSingleObject(hThread, 10000) == WAIT_TIMEOUT)
250  // ... or kill it in case it refuses to do so
251  TerminateThread(hThread, -1);
252 #elif defined(HAVE_PTHREAD)
253  // wait for thread to terminate itself
254  // (without security - let's trust these unwashed hackers for once)
255  pthread_join(Thread, nullptr);
256 #endif
257  fThread = false;
258  // ok
259  return;
260 }

Here is the call graph for this function:

Here is the caller graph for this function:

void StdScheduler::UnBlock ( )
inherited

Definition at line 163 of file StdScheduler.cpp.

Referenced by Stop().

164 {
165  Unblocker.Notify();
166 }

Here is the caller graph for this function:


The documentation for this class was generated from the following files: