45 if (!
Execute(std::max(iTimeout, 0)))
49 if (tTime >= tStopTime)
51 iTimeout = tStopTime - tTime;
71 while (procs.size() > 0)
72 Remove(procs[procs.size()-1]);
80 for (
int i = 0; i < inProcCnt; i++)
90 procs.push_back(pProc);
91 pProc->scheduler =
this;
99 if (pProc->scheduler !=
this)
102 pProc->scheduler =
nullptr;
103 auto pos = std::find(procs.begin(), procs.end(), pProc);
104 if (pos != procs.end())
123 for (
auto proc : procs)
125 tProcTick = std::min(tProcTick, proc->GetNextTick(tNow));
133 if (!procs.size())
return false;
138 if (iTimeout == -1 || tNow + iTimeout > tProcTick)
140 iTimeout = std::max<decltype(iTimeout)>(tProcTick - tNow, 0);
143 bool old = isInManualLoop;
144 isInManualLoop =
true;
146 isInManualLoop = old;
175 bool fGotThread = fThread;
176 if (fGotThread)
Stop();
180 if (fGotThread)
Start();
186 bool fGotThread = fThread;
187 if (fGotThread)
Stop();
191 if (fGotThread)
Start();
197 bool fGotThread = fThread;
198 if (fGotThread)
Stop();
202 if (fGotThread)
Start();
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);
224 if (!fThread)
return;
226 fRunThreadRun =
false;
229 #ifdef HAVE_WINTHREAD
231 HANDLE hThread =
reinterpret_cast<HANDLE
>(iThread);
232 if (WaitForSingleObject(hThread, 10000) == WAIT_TIMEOUT)
234 TerminateThread(hThread, -1);
235 #elif defined(HAVE_PTHREAD)
238 pthread_join(Thread,
nullptr);
245 #ifdef HAVE_WINTHREAD
246 void __cdecl StdSchedulerThread::_ThreadFunc(
void *pPar)
249 _endthreadex(pThread->ThreadFunc());
251 void __cdecl StdThread::_ThreadFunc(
void *pPar)
254 _endthreadex(pThread->ThreadFunc());
256 #elif defined(HAVE_PTHREAD)
257 void *StdSchedulerThread::_ThreadFunc(
void *pPar)
260 return reinterpret_cast<void *
>(pThread->ThreadFunc());
262 void *StdThread::_ThreadFunc(
void *pPar)
265 return reinterpret_cast<void *
>(pThread->ThreadFunc());
269 unsigned int StdSchedulerThread::ThreadFunc()
273 while (fRunThreadRun)
285 if (fStarted)
Stop();
287 fStopSignaled =
false;
288 #ifdef HAVE_WINTHREAD
289 iThread = _beginthread(_ThreadFunc, 0,
this);
290 fStarted = (iThread != -1);
291 #elif defined(HAVE_PTHREAD)
292 fStarted = !pthread_create(&Thread,
nullptr, _ThreadFunc,
this);
301 if (!fStarted)
return;
303 fStopSignaled =
true;
309 if (!fStarted)
return;
311 fStopSignaled =
true;
312 #ifdef HAVE_WINTHREAD
314 HANDLE hThread =
reinterpret_cast<HANDLE
>(iThread);
315 if (WaitForSingleObject(hThread, 10000) == WAIT_TIMEOUT)
317 TerminateThread(hThread, -1);
318 #elif defined(HAVE_PTHREAD)
321 pthread_join(Thread,
nullptr);
328 unsigned int StdThread::ThreadFunc()
344 return fStopSignaled;
static C4TimeMilliseconds Now()
void Set(StdSchedulerProc **ppProcs, int iProcCnt)
bool ScheduleProcs(int iTimeout=1000/36)
virtual bool DoScheduleProcs(int iTimeout)
void Added(StdSchedulerProc *pProc)
void StartOnCurrentThread()
void Removing(StdSchedulerProc *pProc)
void Remove(StdSchedulerProc *pProc)
void Add(StdSchedulerProc *pProc)
C4TimeMilliseconds GetNextTick(C4TimeMilliseconds tNow)
virtual C4TimeMilliseconds GetNextTick(C4TimeMilliseconds tNow)
bool ExecuteUntil(int iTimeout=-1)
virtual bool Execute(int iTimeout=-1, pollfd *readyfds=nullptr)=0
void Set(StdSchedulerProc **ppProcs, int iProcCnt)
void Remove(StdSchedulerProc *pProc)
void Add(StdSchedulerProc *pProc)
~StdSchedulerThread() override
virtual bool IsSelfDestruct()