Skip to content

Commit 70f130c

Browse files
committed
xrEngine/xrSheduler.h|cpp: Cleanup
I used std::move and emplace_back() to be conformant with the modern C++
1 parent 705f90a commit 70f130c

File tree

2 files changed

+60
-95
lines changed

2 files changed

+60
-95
lines changed

src/xrEngine/xrSheduler.cpp

Lines changed: 60 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ void CSheduler::Destroy()
3737
for (const auto& item : Items)
3838
Log(item.Object->shedule_Name().c_str());
3939
}
40-
#endif // DEBUG
40+
#endif
41+
4142
ItemsRT.clear();
4243
Items.clear();
4344
ItemsProcessed.clear();
@@ -101,48 +102,43 @@ void CSheduler::internal_Registration()
101102
Registration.clear();
102103
}
103104

104-
void CSheduler::internal_Register(ISheduled* O, BOOL RT)
105+
void CSheduler::internal_Register(ISheduled* object, BOOL realTime)
105106
{
106-
VERIFY(!O->GetSchedulerData().b_locked);
107-
if (RT)
107+
VERIFY(!object->GetSchedulerData().b_locked);
108+
109+
// Fill item structure
110+
Item item;
111+
item.dwTimeForExecute = Device.dwTimeGlobal;
112+
item.dwTimeOfLastExecute = Device.dwTimeGlobal;
113+
item.scheduled_name = object->shedule_Name();
114+
item.Object = object;
115+
116+
if (realTime)
108117
{
109-
// Fill item structure
110-
Item TNext;
111-
TNext.dwTimeForExecute = Device.dwTimeGlobal;
112-
TNext.dwTimeOfLastExecute = Device.dwTimeGlobal;
113-
TNext.Object = O;
114-
TNext.scheduled_name = O->shedule_Name();
115-
O->GetSchedulerData().b_RT = TRUE;
116-
117-
ItemsRT.push_back(TNext);
118+
object->GetSchedulerData().b_RT = TRUE;
119+
ItemsRT.emplace_back(std::move(item));
118120
}
119121
else
120122
{
121-
// Fill item structure
122-
Item TNext;
123-
TNext.dwTimeForExecute = Device.dwTimeGlobal;
124-
TNext.dwTimeOfLastExecute = Device.dwTimeGlobal;
125-
TNext.Object = O;
126-
TNext.scheduled_name = O->shedule_Name();
127-
O->GetSchedulerData().b_RT = FALSE;
123+
object->GetSchedulerData().b_RT = FALSE;
128124

129125
// Insert into priority Queue
130-
Push(TNext);
126+
Push(item);
131127
}
132128
}
133129

134-
bool CSheduler::internal_Unregister(ISheduled* O, BOOL RT, bool warn_on_not_found)
130+
bool CSheduler::internal_Unregister(ISheduled* object, BOOL realTime, bool warn_on_not_found)
135131
{
136132
// the object may be already dead
137-
// VERIFY (!O->shedule.b_locked) ;
138-
if (RT)
133+
// VERIFY (!O->shedule.b_locked);
134+
if (realTime)
139135
{
140136
for (u32 i = 0; i < ItemsRT.size(); i++)
141137
{
142-
if (ItemsRT[i].Object == O)
138+
if (ItemsRT[i].Object == object)
143139
{
144140
#ifdef DEBUG_SCHEDULER
145-
Msg("SCHEDULER: internal unregister [%s][%x][%s]", "unknown", O, "true");
141+
Msg("SCHEDULER: internal unregister [%s][%x][%s]", "unknown", object, "true");
146142
#endif
147143
ItemsRT.erase(ItemsRT.begin() + i);
148144
return true;
@@ -153,20 +149,20 @@ bool CSheduler::internal_Unregister(ISheduled* O, BOOL RT, bool warn_on_not_foun
153149
{
154150
for (auto& item : Items)
155151
{
156-
if (item.Object == O)
152+
if (item.Object == object)
157153
{
158154
#ifdef DEBUG_SCHEDULER
159-
Msg("SCHEDULER: internal unregister [%s][%x][%s]", item.scheduled_name.c_str(), O, "false");
155+
Msg("SCHEDULER: internal unregister [%s][%x][%s]", item.scheduled_name.c_str(), object, "false");
160156
#endif
161157
item.Object = nullptr;
162158
return true;
163159
}
164160
}
165161
}
166-
if (m_current_step_obj == O)
162+
if (m_current_step_obj == object)
167163
{
168164
#ifdef DEBUG_SCHEDULER
169-
Msg("SCHEDULER: internal unregister (self unregistering) [%x][%s]", O, "false");
165+
Msg("SCHEDULER: internal unregister (self unregistering) [%x][%s]", object, "false");
170166
#endif
171167

172168
m_current_step_obj = nullptr;
@@ -175,8 +171,8 @@ bool CSheduler::internal_Unregister(ISheduled* O, BOOL RT, bool warn_on_not_foun
175171

176172
#ifdef DEBUG
177173
if (warn_on_not_found)
178-
Msg("! scheduled object %s tries to unregister but is not registered", O->shedule_Name().c_str());
179-
#endif // DEBUG
174+
Msg("! scheduled object %s tries to unregister but is not registered", object->shedule_Name().c_str());
175+
#endif
180176

181177
return false;
182178
}
@@ -303,9 +299,9 @@ void CSheduler::EnsureOrder(ISheduled* Before, ISheduled* After)
303299
}
304300
}
305301

306-
void CSheduler::Push(Item& I)
302+
void CSheduler::Push(Item& item)
307303
{
308-
Items.push_back(I);
304+
Items.emplace_back(std::move(item));
309305
std::push_heap(Items.begin(), Items.end());
310306
}
311307

@@ -320,42 +316,37 @@ void CSheduler::ProcessStep()
320316
// Normal priority
321317
const u32 dwTime = Device.dwTimeGlobal;
322318
CTimer eTimer;
319+
323320
for (int i = 0; !Items.empty() && Top().dwTimeForExecute < dwTime; ++i)
324321
{
325-
const u32 delta_ms = dwTime - Top().dwTimeForExecute;
326-
327322
// Update
328323
Item item = Top();
329-
#ifdef DEBUG_SCHEDULER
330-
Msg("SCHEDULER: process step [%s][%x][false]", item.scheduled_name.c_str(), item.Object);
331-
#endif
332-
u32 Elapsed = dwTime - item.dwTimeOfLastExecute;
333324

334-
const bool condition = nullptr == item.Object || !item.Object->shedule_Needed();
335-
if (condition)
325+
if (!item.Object || !item.Object->shedule_Needed())
336326
{
337-
// Erase element
338327
#ifdef DEBUG_SCHEDULER
339328
Msg("SCHEDULER: process unregister [%s][%x][%s]", item.scheduled_name.c_str(), item.Object, "false");
340329
#endif
341-
// if (T.Object)
342-
// Msg ("0x%08x UNREGISTERS because shedule_Needed() returned false",T.Object);
343-
// else
344-
// Msg ("UNREGISTERS unknown object");
330+
// Erase element
345331
Pop();
346332
continue;
347333
}
348334

335+
#ifdef DEBUG_SCHEDULER
336+
Msg("SCHEDULER: process step [%s][%x][false]", item.scheduled_name.c_str(), item.Object);
337+
#endif
338+
349339
// Insert into priority Queue
350340
Pop();
351341

352-
// Real update call
353-
// Msg ("------- %d:",Device.dwFrame);
342+
u32 Elapsed = dwTime - item.dwTimeOfLastExecute;
343+
344+
// Real update call
345+
// Msg("------- %d:", Device.dwFrame);
354346
#ifdef DEBUG
355347
item.Object->GetSchedulerData().dbg_startframe = Device.dwFrame;
356348
eTimer.Start();
357-
// LPCSTR _obj_name = T.Object->shedule_Name().c_str();
358-
#endif // DEBUG
349+
#endif
359350

360351
// Calc next update interval
361352
const u32 dwMin = _max(u32(30), item.Object->GetSchedulerData().t_min);
@@ -365,7 +356,7 @@ void CSheduler::ProcessStep()
365356
clamp(dwUpdate, u32(_max(dwMin, u32(20))), dwMax);
366357

367358
m_current_step_obj = item.Object;
368-
// try {
359+
369360
item.Object->shedule_Update(
370361
clampr(Elapsed, u32(1), u32(_max(u32(item.Object->GetSchedulerData().t_max), u32(1000)))));
371362
if (!m_current_step_obj)
@@ -376,40 +367,28 @@ void CSheduler::ProcessStep()
376367
#endif
377368
continue;
378369
}
379-
// } catch (...) {
380-
#ifdef DEBUG
381-
// Msg ("! xrSheduler: object '%s' raised an exception", _obj_name);
382-
// throw ;
383-
#endif // DEBUG
384-
// }
385-
m_current_step_obj = nullptr;
386370

387-
#ifdef DEBUG
388-
// u32 execTime = eTimer.GetElapsed_ms ();
389-
#endif // DEBUG
371+
m_current_step_obj = nullptr;
390372

391373
// Fill item structure
392-
Item TNext;
393-
TNext.dwTimeForExecute = dwTime + dwUpdate;
394-
TNext.dwTimeOfLastExecute = dwTime;
395-
TNext.Object = item.Object;
396-
TNext.scheduled_name = item.Object->shedule_Name();
397-
ItemsProcessed.push_back(TNext);
374+
item.dwTimeForExecute = dwTime + dwUpdate;
375+
item.dwTimeOfLastExecute = dwTime;
376+
ItemsProcessed.emplace_back(std::move(item));
377+
378+
#if 0 //def DEBUG
379+
auto itemName = item.Object->shedule_Name().c_str();
380+
const u32 delta_ms = dwTime - item.dwTimeForExecute;
381+
const u32 execTime = eTimer.GetElapsed_ms();
382+
VERIFY3(item.Object->dbg_update_shedule == item.Object->dbg_startframe,
383+
"Broken sequence of calls to 'shedule_Update'", itemName);
398384

399-
#ifdef DEBUG
400-
// u32 execTime = eTimer.GetElapsed_ms ();
401-
// VERIFY3 (T.Object->dbg_update_shedule == T.Object->dbg_startframe, "Broken sequence of calls to
402-
// 'shedule_Update'", _obj_name );
403385
if (delta_ms > 3 * dwUpdate)
404-
{
405-
// Msg ("! xrSheduler: failed to shedule object [%s] (%dms)", _obj_name, delta_ms );
406-
}
407-
// if (execTime> 15) {
408-
// Msg ("* xrSheduler: too much time consumed by object [%s] (%dms)", _obj_name, execTime );
409-
// }
410-
#endif // DEBUG
386+
Msg("! xrSheduler: failed to shedule object [%s] (%dms)", itemName, delta_ms);
387+
388+
if (execTime > 15)
389+
Msg("* xrSheduler: too much time consumed by object [%s] (%dms)", itemName, execTime);
390+
#endif
411391

412-
//
413392
if (i % 3 != 3 - 1)
414393
continue;
415394

@@ -431,16 +410,7 @@ void CSheduler::ProcessStep()
431410
// always try to decrease target
432411
psShedulerTarget -= psShedulerReaction;
433412
}
434-
/*
435-
void CSheduler::Switch ()
436-
{
437-
if (fibered)
438-
{
439-
fibered = FALSE;
440-
SwitchToFiber (fiber_main);
441-
}
442-
}
443-
*/
413+
444414
void CSheduler::Update()
445415
{
446416
// Initialize

src/xrEngine/xrSheduler.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#pragma once
2-
#ifndef XRSHEDULER_H_INCLUDED
3-
#define XRSHEDULER_H_INCLUDED
42

5-
//#include "ISheduled.h"
63
#include "xrCore/xrstring.h"
74
#include "xrCore/FTimer.h"
85

@@ -84,5 +81,3 @@ class ENGINE_API CSheduler
8481
return stats.Update;
8582
}
8683
};
87-
88-
#endif // XRSHEDULER_H_INCLUDED

0 commit comments

Comments
 (0)