OpenClonk
StdSchedulerThread Class Reference

#include <StdScheduler.h>

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

Public Member Functions

 StdSchedulerThread ()
 
 ~StdSchedulerThread () override
 
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 273 of file StdScheduler.h.

Constructor & Destructor Documentation

◆ StdSchedulerThread()

StdSchedulerThread::StdSchedulerThread ( )
default

◆ ~StdSchedulerThread()

StdSchedulerThread::~StdSchedulerThread ( )
override

Definition at line 159 of file StdScheduler.cpp.

160 {
161  Clear();
162 }

References Clear().

Here is the call graph for this function:

Member Function Documentation

◆ Add()

void StdSchedulerThread::Add ( StdSchedulerProc pProc)

Definition at line 183 of file StdScheduler.cpp.

184 {
185  // Thread is running? Stop it first
186  bool fGotThread = fThread;
187  if (fGotThread) Stop();
188  // Set
189  StdScheduler::Add(pProc);
190  // Restart
191  if (fGotThread) Start();
192 }
void Add(StdSchedulerProc *pProc)

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

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Added()

void StdScheduler::Added ( StdSchedulerProc pProc)
inherited

Definition at line 281 of file StdSchedulerMac.mm.

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

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

Referenced by StdScheduler::Add().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Changed()

void StdScheduler::Changed ( StdSchedulerProc pProc)
inherited

Definition at line 294 of file StdSchedulerMac.mm.

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

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

Here is the call graph for this function:

◆ Clear()

void StdSchedulerThread::Clear ( )

Definition at line 164 of file StdScheduler.cpp.

165 {
166  // Stop thread
167  if (fThread) Stop();
168  // Clear scheduler
170 }

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

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ DoScheduleProcs()

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:

◆ GetNextTick()

C4TimeMilliseconds StdScheduler::GetNextTick ( C4TimeMilliseconds  tNow)
inherited

Definition at line 120 of file StdScheduler.cpp.

121 {
123  for (auto proc : procs)
124  {
125  tProcTick = std::min(tProcTick, proc->GetNextTick(tNow));
126  }
127  return tProcTick;
128 }

References C4TimeMilliseconds::PositiveInfinity.

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

Here is the caller graph for this function:

◆ getProcCnt()

int StdScheduler::getProcCnt ( ) const
inlineinherited

Definition at line 245 of file StdScheduler.h.

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

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

Here is the caller graph for this function:

◆ hasProc()

bool StdScheduler::hasProc ( StdSchedulerProc pProc)
inlineinherited

Definition at line 246 of file StdScheduler.h.

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

Referenced by C4InteractiveThread::RemoveProc().

Here is the caller graph for this function:

◆ IsInManualLoop()

bool StdScheduler::IsInManualLoop ( )
inlineinherited

Definition at line 247 of file StdScheduler.h.

247 { return isInManualLoop; }

◆ OnError()

virtual void StdScheduler::OnError ( StdSchedulerProc )
inlineprotectedvirtualinherited

Definition at line 268 of file StdScheduler.h.

268 { }

◆ Remove()

void StdSchedulerThread::Remove ( StdSchedulerProc pProc)

Definition at line 194 of file StdScheduler.cpp.

195 {
196  // Thread is running? Stop it first
197  bool fGotThread = fThread;
198  if (fGotThread) Stop();
199  // Set
200  StdScheduler::Remove(pProc);
201  // Restart
202  if (fGotThread) Start();
203 }
void Remove(StdSchedulerProc *pProc)

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

Referenced by C4InteractiveThread::RemoveProc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Removing()

void StdScheduler::Removing ( StdSchedulerProc pProc)
inherited

Definition at line 286 of file StdSchedulerMac.mm.

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

References SCHAdditions::requestAdditionsForScheduler:.

Referenced by StdScheduler::Remove().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ScheduleProcs()

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

Definition at line 130 of file StdScheduler.cpp.

131 {
132  // Needs at least one process to work properly
133  if (!procs.size()) return false;
134 
135  // Get timeout
137  C4TimeMilliseconds tProcTick = GetNextTick(tNow);
138  if (iTimeout == -1 || tNow + iTimeout > tProcTick)
139  {
140  iTimeout = std::max<decltype(iTimeout)>(tProcTick - tNow, 0);
141  }
142 
143  bool old = isInManualLoop;
144  isInManualLoop = true;
145  bool res = DoScheduleProcs(iTimeout);
146  isInManualLoop = old;
147  return res;
148 }
static C4TimeMilliseconds Now()
virtual bool DoScheduleProcs(int iTimeout)
C4TimeMilliseconds GetNextTick(C4TimeMilliseconds tNow)

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(), and C4AbstractApp::Run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Set()

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

Definition at line 172 of file StdScheduler.cpp.

173 {
174  // Thread is running? Stop it first
175  bool fGotThread = fThread;
176  if (fGotThread) Stop();
177  // Set
178  StdScheduler::Set(ppProcs, iProcCnt);
179  // Restart
180  if (fGotThread) Start();
181 }
void Set(StdSchedulerProc **ppProcs, int iProcCnt)

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

Here is the call graph for this function:

◆ Start()

bool StdSchedulerThread::Start ( )

Definition at line 205 of file StdScheduler.cpp.

206 {
207  // already running? stop
208  if (fThread) Stop();
209  // begin thread
210  fRunThreadRun = true;
211 #ifdef HAVE_WINTHREAD
212  iThread = _beginthread(_ThreadFunc, 0, this);
213  fThread = (iThread != -1);
214 #elif defined(HAVE_PTHREAD)
215  fThread = !pthread_create(&Thread, nullptr, _ThreadFunc, this);
216 #endif
217  // success?
218  return fThread;
219 }

References Stop().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ StartOnCurrentThread()

void StdScheduler::StartOnCurrentThread ( )
inherited

Definition at line 276 of file StdSchedulerMac.mm.

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

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

Here is the call graph for this function:

◆ Stop()

void StdSchedulerThread::Stop ( )

Definition at line 221 of file StdScheduler.cpp.

222 {
223  // Not running?
224  if (!fThread) return;
225  // Set flag
226  fRunThreadRun = false;
227  // Unblock
228  UnBlock();
229 #ifdef HAVE_WINTHREAD
230  // Wait for thread to terminate itself
231  HANDLE hThread = reinterpret_cast<HANDLE>(iThread);
232  if (WaitForSingleObject(hThread, 10000) == WAIT_TIMEOUT)
233  // ... or kill it in case it refuses to do so
234  TerminateThread(hThread, -1);
235 #elif defined(HAVE_PTHREAD)
236  // wait for thread to terminate itself
237  // (without security - let's trust these unwashed hackers for once)
238  pthread_join(Thread, nullptr);
239 #endif
240  fThread = false;
241  // ok
242  return;
243 }

References StdScheduler::UnBlock().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UnBlock()

void StdScheduler::UnBlock ( )
inherited

Definition at line 150 of file StdScheduler.cpp.

151 {
152  Unblocker.Notify();
153 }

Referenced by Stop().

Here is the caller graph for this function:

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