forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasyncTaskChain.cxx
More file actions
1461 lines (1285 loc) · 41.2 KB
/
asyncTaskChain.cxx
File metadata and controls
1461 lines (1285 loc) · 41.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file asyncTaskChain.cxx
* @author drose
* @date 2006-08-23
*/
#include "asyncTaskChain.h"
#include "asyncTaskManager.h"
#include "event.h"
#include "mutexHolder.h"
#include "indent.h"
#include "pStatClient.h"
#include "pStatTimer.h"
#include "clockObject.h"
#include "config_event.h"
#include <algorithm>
#include <stdio.h> // For sprintf/snprintf
using std::max;
using std::ostream;
using std::ostringstream;
using std::string;
TypeHandle AsyncTaskChain::_type_handle;
PStatCollector AsyncTaskChain::_task_pcollector("Task");
PStatCollector AsyncTaskChain::_wait_pcollector("Wait");
/**
*
*/
AsyncTaskChain::
AsyncTaskChain(AsyncTaskManager *manager, const string &name) :
Namable(name),
_manager(manager),
_cvar(manager->_lock),
_tick_clock(false),
_timeslice_priority(false),
_num_threads(0),
_thread_priority(TP_normal),
_frame_budget(-1.0),
_frame_sync(false),
_num_busy_threads(0),
_num_tasks(0),
_num_awaiting_tasks(0),
_state(S_initial),
_current_sort(-INT_MAX),
_pickup_mode(false),
_needs_cleanup(false),
_current_frame(0),
_time_in_frame(0.0),
_block_till_next_frame(false),
_next_implicit_sort(0)
{
}
/**
*
*/
AsyncTaskChain::
~AsyncTaskChain() {
// We only grab the lock if _needs_cleanup is true. This way, the temporary
// AsyncTaskChain objects created (and destructed) within the task manager
// won't risk a double-lock.
if (_needs_cleanup) {
MutexHolder holder(_manager->_lock);
do_cleanup();
}
}
/**
* Sets the tick_clock flag. When this is true, get_clock()->tick() will be
* called automatically at each task epoch. This is false by default.
*/
void AsyncTaskChain::
set_tick_clock(bool tick_clock) {
MutexHolder holder(_manager->_lock);
_tick_clock = tick_clock;
}
/**
* Returns the tick_clock flag. See set_tick_clock().
*/
bool AsyncTaskChain::
get_tick_clock() const {
MutexHolder holder(_manager->_lock);
return _tick_clock;
}
/**
* Changes the number of threads for this task chain. This may require
* stopping the threads if they are already running.
*/
void AsyncTaskChain::
set_num_threads(int num_threads) {
nassertv(num_threads >= 0);
if (task_cat.is_debug()) {
do_output(task_cat.debug());
task_cat.debug(false)
<< ": set_num_threads(" << num_threads << ")\n";
}
if (!Thread::is_threading_supported()) {
num_threads = 0;
}
MutexHolder holder(_manager->_lock);
if (_num_threads != num_threads) {
do_stop_threads();
_num_threads = num_threads;
if (_num_tasks != 0) {
do_start_threads();
}
}
}
/**
* Returns the number of threads that will be servicing tasks for this chain.
* Also see get_num_running_threads().
*/
int AsyncTaskChain::
get_num_threads() const {
MutexHolder holder(_manager->_lock);
return _num_threads;
}
/**
* Returns the number of threads that have been created and are actively
* running. This will return 0 before the threads have been started; it will
* also return 0 if thread support is not available.
*/
int AsyncTaskChain::
get_num_running_threads() const {
MutexHolder holder(_manager->_lock);
return _threads.size();
}
/**
* Changes the priority associated with threads that serve this task chain.
* This may require stopping the threads if they are already running.
*/
void AsyncTaskChain::
set_thread_priority(ThreadPriority priority) {
MutexHolder holder(_manager->_lock);
if (_thread_priority != priority) {
do_stop_threads();
_thread_priority = priority;
if (_num_tasks != 0) {
do_start_threads();
}
}
}
/**
* Returns the priority associated with threads that serve this task chain.
*/
ThreadPriority AsyncTaskChain::
get_thread_priority() const {
MutexHolder holder(_manager->_lock);
return _thread_priority;
}
/**
* Sets the maximum amount of time per frame the tasks on this chain are
* granted for execution. If this is less than zero, there is no limit; if it
* is >= 0, it represents a maximum amount of time (in seconds) that will be
* used to execute tasks. If this time is exceeded in any one frame, the task
* chain will stop executing tasks until the next frame, as defined by the
* TaskManager's clock.
*/
void AsyncTaskChain::
set_frame_budget(double frame_budget) {
MutexHolder holder(_manager->_lock);
_frame_budget = frame_budget;
}
/**
* Returns the maximum amount of time per frame the tasks on this chain are
* granted for execution. See set_frame_budget().
*/
double AsyncTaskChain::
get_frame_budget() const {
MutexHolder holder(_manager->_lock);
return _frame_budget;
}
/**
* Sets the frame_sync flag. When this flag is true, this task chain will be
* forced to sync with the TaskManager's clock. It will run no faster than
* one epoch per clock frame.
*
* When this flag is false, the default, the task chain will finish all of its
* tasks and then immediately start from the first task again, regardless of
* the clock frame. When it is true, the task chain will finish all of its
* tasks and then wait for the clock to tick to the next frame before resuming
* the first task.
*
* This only makes sense for threaded task chains. Non-threaded task chains
* are automatically synchronous.
*/
void AsyncTaskChain::
set_frame_sync(bool frame_sync) {
MutexHolder holder(_manager->_lock);
_frame_sync = frame_sync;
}
/**
* Returns the frame_sync flag. See set_frame_sync().
*/
bool AsyncTaskChain::
get_frame_sync() const {
MutexHolder holder(_manager->_lock);
return _frame_sync;
}
/**
* Sets the timeslice_priority flag. This changes the interpretation of
* priority, and the number of times per epoch each task will run.
*
* When this flag is true, some tasks might not run in any given epoch.
* Instead, tasks with priority higher than 1 will be given precedence, in
* proportion to the amount of time they have already used. This gives
* higher-priority tasks more runtime than lower-priority tasks. Each task
* gets the amount of time proportional to its priority value, so a task with
* priority 100 will get five times as much processing time as a task with
* priority 20. For these purposes, priority values less than 1 are deemed to
* be equal to 1.
*
* When this flag is false (the default), all tasks are run exactly once each
* epoch, round-robin style. Priority is only used to determine which task
* runs first within tasks of the same sort value.
*/
void AsyncTaskChain::
set_timeslice_priority(bool timeslice_priority) {
MutexHolder holder(_manager->_lock);
_timeslice_priority = timeslice_priority;
}
/**
* Returns the timeslice_priority flag. This changes the interpretation of
* priority, and the number of times per epoch each task will run. See
* set_timeslice_priority().
*/
bool AsyncTaskChain::
get_timeslice_priority() const {
MutexHolder holder(_manager->_lock);
return _timeslice_priority;
}
/**
* Stops any threads that are currently running. If any tasks are still
* pending and have not yet been picked up by a thread, they will not be
* serviced unless poll() or start_threads() is later called.
*/
void AsyncTaskChain::
stop_threads() {
if (_state == S_started || _state == S_interrupted) {
// Clean up all of the threads.
MutexHolder holder(_manager->_lock);
do_stop_threads();
}
}
/**
* Starts any requested threads to service the tasks on the queue. This is
* normally not necessary, since adding a task will start the threads
* automatically.
*/
void AsyncTaskChain::
start_threads() {
if (_state == S_initial || _state == S_interrupted) {
MutexHolder holder(_manager->_lock);
do_start_threads();
}
}
/**
* Returns true if the indicated task has been added to this AsyncTaskChain,
* false otherwise.
*/
bool AsyncTaskChain::
has_task(AsyncTask *task) const {
MutexHolder holder(_manager->_lock);
if (task->_chain != this) {
nassertr(!do_has_task(task), false);
return false;
}
if (task->_state == AsyncTask::S_servicing_removed) {
return false;
}
// The task might not actually be in the active queue, since it might be
// being serviced right now. That's OK.
return true;
}
/**
* Blocks until the task list is empty.
*/
void AsyncTaskChain::
wait_for_tasks() {
MutexHolder holder(_manager->_lock);
do_wait_for_tasks();
}
/**
* Returns the number of tasks that are currently active or sleeping within
* the task chain.
*/
int AsyncTaskChain::
get_num_tasks() const {
MutexHolder holder(_manager->_lock);
return _num_tasks;
}
/**
* Returns the set of tasks that are active or sleeping on the task chain, at
* the time of the call.
*/
AsyncTaskCollection AsyncTaskChain::
get_tasks() const {
MutexHolder holder(_manager->_lock);
AsyncTaskCollection result = do_get_active_tasks();
result.add_tasks_from(do_get_sleeping_tasks());
return result;
}
/**
* Returns the set of tasks that are active (and not sleeping) on the task
* chain, at the time of the call.
*/
AsyncTaskCollection AsyncTaskChain::
get_active_tasks() const {
MutexHolder holder(_manager->_lock);
return do_get_active_tasks();
}
/**
* Returns the set of tasks that are sleeping (and not active) on the task
* chain, at the time of the call.
*/
AsyncTaskCollection AsyncTaskChain::
get_sleeping_tasks() const {
MutexHolder holder(_manager->_lock);
return do_get_sleeping_tasks();
}
/**
* Runs through all the tasks in the task list, once, if the task chain is
* running in single-threaded mode (no threads available). This method does
* nothing in threaded mode, so it may safely be called in either case.
*
* Normally, you would not call this function directly; instead, call
* AsyncTaskManager::poll(), which polls all of the task chains in sequence.
*/
void AsyncTaskChain::
poll() {
MutexHolder holder(_manager->_lock);
do_poll();
}
/**
* Returns the scheduled time (on the manager's clock) of the next sleeping
* task, on any task chain, to awaken. Returns -1 if there are no sleeping
* tasks.
*/
double AsyncTaskChain::
get_next_wake_time() const {
MutexHolder holder(_manager->_lock);
return do_get_next_wake_time();
}
/**
*
*/
void AsyncTaskChain::
output(ostream &out) const {
MutexHolder holder(_manager->_lock);
do_output(out);
}
/**
*
*/
void AsyncTaskChain::
write(ostream &out, int indent_level) const {
MutexHolder holder(_manager->_lock);
do_write(out, indent_level);
}
/**
* Adds the indicated task to the active queue. It is an error if the task is
* already added to this or any other active queue.
*
* This is normally called only by the AsyncTaskManager. Assumes the lock is
* already held.
*/
void AsyncTaskChain::
do_add(AsyncTask *task) {
nassertv(task->_chain == nullptr &&
task->_manager == nullptr &&
task->_chain_name == get_name() &&
task->_state == AsyncTask::S_inactive);
nassertv(!do_has_task(task));
do_start_threads();
task->_chain = this;
task->_manager = _manager;
double now = _manager->_clock->get_frame_time();
task->_start_time = now;
task->_start_frame = _manager->_clock->get_frame_count();
// Remember the order in which tasks were added to the chain.
task->_implicit_sort = _next_implicit_sort++;
_manager->add_task_by_name(task);
if (task->has_delay()) {
// This is a deferred task. Add it to the sleeping queue.
task->_wake_time = now + task->get_delay();
task->_start_time = task->_wake_time;
task->_state = AsyncTask::S_sleeping;
_sleeping.push_back(task);
push_heap(_sleeping.begin(), _sleeping.end(), AsyncTaskSortWakeTime());
} else {
// This is an active task. Add it to the active set.
task->_state = AsyncTask::S_active;
if (task_cat.is_spam()) {
task_cat.spam()
<< "Adding " << *task << " with sort " << task->get_sort()
<< " to chain " << get_name() << " with current_sort "
<< _current_sort << "\n";
}
if (task->get_sort() >= _current_sort) {
// It will run this frame.
_active.push_back(task);
push_heap(_active.begin(), _active.end(), AsyncTaskSortPriority());
} else {
// It will run next frame.
_next_active.push_back(task);
}
}
++_num_tasks;
++(_manager->_num_tasks);
_needs_cleanup = true;
_cvar.notify_all();
}
/**
* Removes the indicated task from this chain. Returns true if removed, false
* otherwise. Assumes the lock is already held. The task->upon_death()
* method is called with clean_exit=false if upon_death is given.
*/
bool AsyncTaskChain::
do_remove(AsyncTask *task, bool upon_death) {
nassertr(task->_chain == this, false);
switch (task->_state) {
case AsyncTask::S_servicing:
// This task is being serviced. upon_death will be called afterwards.
task->_state = AsyncTask::S_servicing_removed;
return true;
case AsyncTask::S_servicing_removed:
// Being serviced, though it is already marked to be removed afterwards.
return false;
case AsyncTask::S_sleeping:
// Sleeping, easy.
{
int index = find_task_on_heap(_sleeping, task);
nassertr(index != -1, false);
PT(AsyncTask) hold_task = task;
_sleeping.erase(_sleeping.begin() + index);
make_heap(_sleeping.begin(), _sleeping.end(), AsyncTaskSortWakeTime());
cleanup_task(task, upon_death, false);
}
return true;
case AsyncTask::S_active:
{
// Active, but not being serviced, easy.
PT(AsyncTask) hold_task = task;
int index = find_task_on_heap(_active, task);
if (index != -1) {
_active.erase(_active.begin() + index);
make_heap(_active.begin(), _active.end(), AsyncTaskSortPriority());
} else {
index = find_task_on_heap(_next_active, task);
if (index != -1) {
_next_active.erase(_next_active.begin() + index);
} else {
index = find_task_on_heap(_this_active, task);
nassertr(index != -1, false);
}
}
cleanup_task(task, upon_death, false);
return true;
}
default:
break;
}
return false;
}
/**
* Blocks until the task list is empty. Assumes the lock is held.
*/
void AsyncTaskChain::
do_wait_for_tasks() {
do_start_threads();
if (_threads.empty()) {
// Non-threaded case.
while (_num_tasks > 0) {
if (_state == S_shutdown || _state == S_interrupted) {
return;
}
do_poll();
}
} else {
// Threaded case.
while (_num_tasks > 0) {
if (_state == S_shutdown || _state == S_interrupted) {
return;
}
PStatTimer timer(_wait_pcollector);
_cvar.wait();
}
}
}
/**
* Stops all threads and messily empties the task list. This is intended to
* be called on destruction only. Assumes the lock is already held.
*/
void AsyncTaskChain::
do_cleanup() {
if (task_cat.is_spam()) {
do_output(task_cat.spam());
task_cat.spam(false)
<< ": do_cleanup()\n";
}
do_stop_threads();
_num_threads = 0;
// Don't call the upon_death functions while we clean up the tasks.
// Instead, store all the tasks in a list as we clean them up, and then call
// the upon_death functions all at once. We do this because calling
// upon_death wil release the lock, allowing the iterators to become
// invalid.
TaskHeap dead;
dead.reserve(_num_tasks);
_needs_cleanup = false;
TaskHeap::const_iterator ti;
for (ti = _active.begin(); ti != _active.end(); ++ti) {
AsyncTask *task = (*ti);
dead.push_back(task);
cleanup_task(task, false, false);
}
for (ti = _this_active.begin(); ti != _this_active.end(); ++ti) {
AsyncTask *task = (*ti);
dead.push_back(task);
cleanup_task(task, false, false);
}
for (ti = _next_active.begin(); ti != _next_active.end(); ++ti) {
AsyncTask *task = (*ti);
dead.push_back(task);
cleanup_task(task, false, false);
}
for (ti = _sleeping.begin(); ti != _sleeping.end(); ++ti) {
AsyncTask *task = (*ti);
dead.push_back(task);
cleanup_task(task, false, false);
}
// There might still be one task remaining: the currently-executing task.
nassertv(_num_tasks == 0 || _num_tasks == 1);
// Now go back and call the upon_death functions.
_manager->_lock.unlock();
for (ti = dead.begin(); ti != dead.end(); ++ti) {
(*ti)->upon_death(_manager, false);
}
_manager->_lock.lock();
if (task_cat.is_spam()) {
do_output(task_cat.spam());
task_cat.spam(false)
<< ": done do_cleanup()\n";
}
}
/**
* Returns true if the task is on one of the task lists, false if it is not
* (false may mean that the task is currently being serviced). Assumes the
* lock is currently held.
*/
bool AsyncTaskChain::
do_has_task(AsyncTask *task) const {
return (find_task_on_heap(_active, task) != -1 ||
find_task_on_heap(_next_active, task) != -1 ||
find_task_on_heap(_sleeping, task) != -1 ||
find_task_on_heap(_this_active, task) != -1);
}
/**
* Returns the index number of the indicated task within the specified task
* list, or -1 if the task is not found in the list (this may mean that it is
* currently being serviced). Assumes that the lock is currently held.
*/
int AsyncTaskChain::
find_task_on_heap(const TaskHeap &heap, AsyncTask *task) const {
for (int i = 0; i < (int)heap.size(); ++i) {
if (heap[i] == task) {
return i;
}
}
return -1;
}
/**
* Pops a single task off the active queue, services it, and restores it to
* the end of the queue. This is called internally only within one of the
* task threads. Assumes the lock is already held.
*
* Note that the lock may be temporarily released by this method.
*/
void AsyncTaskChain::
service_one_task(AsyncTaskChain::AsyncTaskChainThread *thread) {
if (!_active.empty()) {
PT(AsyncTask) task = _active.front();
pop_heap(_active.begin(), _active.end(), AsyncTaskSortPriority());
_active.pop_back();
if (thread != nullptr) {
thread->_servicing = task;
}
if (task_cat.is_spam()) {
task_cat.spam()
<< "Servicing " << *task << " in "
<< *Thread::get_current_thread() << "\n";
}
nassertv(task->get_sort() == _current_sort);
nassertv(task->_state == AsyncTask::S_active);
task->_state = AsyncTask::S_servicing;
task->_servicing_thread = thread;
AsyncTask::DoneStatus ds = task->unlock_and_do_task();
if (thread != nullptr) {
thread->_servicing = nullptr;
}
task->_servicing_thread = nullptr;
if (task->_chain == this) {
if (task->_state == AsyncTask::S_servicing_removed) {
// This task wants to kill itself.
cleanup_task(task, true, false);
} else if (task->_chain_name != get_name()) {
// The task wants to jump to a different chain.
PT(AsyncTask) hold_task = task;
cleanup_task(task, false, false);
task->jump_to_task_chain(_manager);
} else {
switch (ds) {
case AsyncTask::DS_cont:
// The task is still alive; put it on the next frame's active queue.
task->_state = AsyncTask::S_active;
_next_active.push_back(task);
_cvar.notify_all();
break;
case AsyncTask::DS_again:
// The task wants to sleep again.
{
double now = _manager->_clock->get_frame_time();
task->_wake_time = now + task->get_delay();
task->_start_time = task->_wake_time;
task->_state = AsyncTask::S_sleeping;
_sleeping.push_back(task);
push_heap(_sleeping.begin(), _sleeping.end(), AsyncTaskSortWakeTime());
if (task_cat.is_spam()) {
task_cat.spam()
<< "Sleeping " << *task << ", wake time at "
<< task->_wake_time - now << "\n";
}
_cvar.notify_all();
}
break;
case AsyncTask::DS_pickup:
// The task wants to run again this frame if possible.
task->_state = AsyncTask::S_active;
_this_active.push_back(task);
_cvar.notify_all();
break;
case AsyncTask::DS_interrupt:
// The task had an exception and wants to raise a big flag.
task->_state = AsyncTask::S_active;
_next_active.push_back(task);
if (_state == S_started) {
_state = S_interrupted;
_cvar.notify_all();
}
break;
case AsyncTask::DS_await:
// The task wants to wait for another one to finish.
task->_state = AsyncTask::S_awaiting;
_cvar.notify_all();
++_num_awaiting_tasks;
break;
default:
// The task has finished.
cleanup_task(task, true, true);
}
}
} else {
task_cat.error()
<< "Task is no longer on chain " << get_name()
<< ": " << *task << "\n";
}
if (task_cat.is_spam()) {
task_cat.spam()
<< "Done servicing " << *task << " in "
<< *Thread::get_current_thread() << "\n";
}
}
thread_consider_yield();
}
/**
* Called internally when a task has completed (or been interrupted) and is
* about to be removed from the active queue. Assumes the lock is held.
*
* If upon_death is true, then task->upon_death() will also be called, with
* the indicated clean_exit parameter.
*
* Note that the lock may be temporarily released by this method.
*/
void AsyncTaskChain::
cleanup_task(AsyncTask *task, bool upon_death, bool clean_exit) {
if (task_cat.is_spam()) {
do_output(task_cat.spam());
task_cat.spam(false)
<< ": cleanup_task(" << *task << ", " << upon_death << ", " << clean_exit
<< ")\n";
}
nassertv(task->_chain == this);
task->_state = AsyncTask::S_inactive;
task->_chain = nullptr;
--_num_tasks;
--(_manager->_num_tasks);
_manager->remove_task_by_name(task);
if (upon_death) {
_manager->_lock.unlock();
if (task->set_future_state(clean_exit ? AsyncFuture::FS_finished
: AsyncFuture::FS_cancelled)) {
task->notify_done(clean_exit);
}
task->upon_death(_manager, clean_exit);
_manager->_lock.lock();
}
task->_manager = nullptr;
}
/**
* Called internally when all tasks of a given sort value have been completed,
* and it is time to increment to the next sort value, or begin the next
* epoch. Assumes the lock is held.
*
* Returns true if there are more tasks on the queue after this operation, or
* false if the task list is empty and we need to wait.
*/
bool AsyncTaskChain::
finish_sort_group() {
nassertr(_num_busy_threads == 0, true);
if (!_threads.empty()) {
PStatClient::thread_tick(get_name());
}
if (!_active.empty()) {
// There are more tasks; just set the next sort value.
nassertr(_current_sort < _active.front()->get_sort(), true);
_current_sort = _active.front()->get_sort();
_cvar.notify_all();
return true;
}
// There are no more tasks in this epoch; advance to the next epoch.
if (!_this_active.empty() && _frame_budget >= 0.0) {
// Enter pickup mode. This is a special mode at the end of the epoch in
// which we are just re-running the tasks that think they can still run
// within the frame, in an attempt to use up our frame budget.
if (task_cat.is_spam()) {
do_output(task_cat.spam());
task_cat.spam(false)
<< ": next epoch (pickup mode)\n";
}
_pickup_mode = true;
_active.swap(_this_active);
} else {
// Not in pickup mode.
if (task_cat.is_spam()) {
do_output(task_cat.spam());
task_cat.spam(false)
<< ": next epoch\n";
}
_pickup_mode = false;
// Here, there's no difference between _this_active and _next_active.
// Combine them.
_next_active.insert(_next_active.end(), _this_active.begin(), _this_active.end());
_this_active.clear();
_active.swap(_next_active);
// We only tick the clock and wake sleepers in normal mode, the first time
// through the task list; not in pickup mode when we are re-running the
// stragglers just to use up our frame budget.
if (_tick_clock) {
if (task_cat.is_spam()) {
do_output(task_cat.spam());
task_cat.spam(false)
<< ": tick clock\n";
}
_manager->_clock->tick();
_manager->_frame_cvar.notify_all();
} else if (_frame_sync) {
// If we're a synced chain, we have to wait at the end of the epoch for
// someone else to tick the clock.
_block_till_next_frame = true;
}
// Check for any sleeping tasks that need to be woken.
double now = _manager->_clock->get_frame_time();
while (!_sleeping.empty() && _sleeping.front()->_wake_time <= now) {
PT(AsyncTask) task = _sleeping.front();
if (task_cat.is_spam()) {
task_cat.spam()
<< "Waking " << *task << ", wake time at "
<< task->_wake_time - now << "\n";
}
pop_heap(_sleeping.begin(), _sleeping.end(), AsyncTaskSortWakeTime());
_sleeping.pop_back();
task->_state = AsyncTask::S_active;
task->_start_frame = _manager->_clock->get_frame_count();
_active.push_back(task);
}
if (task_cat.is_spam()) {
if (_sleeping.empty()) {
task_cat.spam()
<< "No more tasks on sleeping queue.\n";
} else {
task_cat.spam()
<< "Next sleeper: " << *_sleeping.front() << ", wake time at "
<< _sleeping.front()->_wake_time - now << "\n";
}
}
// Any tasks that are on the active queue at the beginning of the epoch
// are deemed to have run one frame (or to be about to).
TaskHeap::const_iterator ti;
for (ti = _active.begin(); ti != _active.end(); ++ti) {
AsyncTask *task = (*ti);
++task->_num_frames;
}
}
if (_timeslice_priority) {
filter_timeslice_priority();
}
nassertr((size_t)_num_tasks == _active.size() + _this_active.size() + _next_active.size() + _sleeping.size() + (size_t)_num_awaiting_tasks, true);
make_heap(_active.begin(), _active.end(), AsyncTaskSortPriority());
_current_sort = -INT_MAX;
if (!_active.empty()) {
// Signal the threads to start executing the first task again.
_cvar.notify_all();
return true;
}
// There are no tasks to be had anywhere. Chill.
_pickup_mode = false;
nassertr(_this_active.empty(), false);
return false;
}
/**
* Called to filter the _active tasks list when we are in the special
* timeslice_priority mode. In this mode, go through and postpone any tasks
* that have already exceeded their priority budget for this epoch.
*
* Assumes the lock is already held.
*/
void AsyncTaskChain::
filter_timeslice_priority() {
if (_active.empty()) {
return;
}
nassertv(_timeslice_priority);
// We must first sum up the average per-epoch runtime of each task.
double net_runtime = 0.0;
int net_priority = 0;
TaskHeap::iterator ti;
for (ti = _active.begin(); ti != _active.end(); ++ti) {
AsyncTask *task = (*ti);
double runtime = max(task->get_average_dt(), 0.0);
int priority = max(task->_priority, 1);
net_runtime += runtime;
net_priority += priority;
}
// That gives us a timeslice budget per priority value.
double average_budget = net_runtime / (double)net_priority;
TaskHeap keep, postpone;
for (ti = _active.begin(); ti != _active.end(); ++ti) {
AsyncTask *task = (*ti);
double runtime = max(task->get_average_dt(), 0.0);
int priority = max(task->_priority, 1);
double consumed = runtime / (double)priority;
// cerr << *task << " consumed " << consumed << " vs. " << average_budget
// << "\n";
if (consumed > average_budget) {
// Postpone. Run this task next epoch.
postpone.push_back(task);
} else {
// Keep, and run this task this epoch.
keep.push_back(task);
}
}
if (keep.empty()) {
// Hmm, nothing to keep. Grab the postponed task with the highest
// priority and keep that instead.
nassertv(!postpone.empty());
ti = postpone.begin();
TaskHeap::iterator max_ti = ti;
++ti;
while (ti != postpone.end()) {
if ((*ti)->_priority > (*max_ti)->_priority) {
max_ti = ti;
}
}
// cerr << "Nothing to keep, keeping " << *(*max_ti) << " instead\n";
keep.push_back(*max_ti);