Commit Graph

5 Commits

Author SHA1 Message Date
Aikar
b4b649122a
Remove the immediate dispatch optimization and adjust thread executors
hopefully will resolve #1049
2018-03-18 20:09:12 -04:00
Aikar
242e82f25b
Fix more issues with async scheduler and cancelling pending task
was a race condition, so do need to use the head/parsePending logic still
2018-03-17 14:59:03 -04:00
Aikar
09d1277482
Fix isCurrentlyRunning for sync tasks
Not sure where this would of ever been used, but for correctness sake.
2018-03-17 13:14:44 -04:00
Aikar
26b1519733
check that a task is valid before executing incase it was cancelled elsewhere
also set runners in the short circuit path so we know of the pending task incase its long running
2018-03-17 12:58:27 -04:00
Aikar
7dfbe44247
Improved Async Task Scheduler
The Craft Scheduler still uses the primary thread for task scheduling.
This results in the main thread still having to do work as part of the
dispatching of async tasks.

If plugins make use of lots of async tasks, such as particle emitters
that want to keep the logic off the main thread, the main thread still
receives quite a bit of load from processing all of these queued tasks.

Additionally, resizing and managing the pending entries for all of
these asynchronous tasks takes up time on the main thread too.

This commit replaces the implementation of the scheduler when working
with asynchronous tasks, by forwarding calls to the new scheduler.

The Async Scheduler uses a single thread executor for "management" tasks.
The Management Thread is responsible for all adding and dispatching of
scheduled tasks.

The mainThreadHeartbeat will send a heartbeat task to the management thread
with the currentTick value, so that it can find which tasks to execute.

Scheduling of an async tasks also dispatches a management task, ensuring
that any Queue resizing operation occurs off of the main thread.

The async queue uses a complete separate PriorityQueue, ensuring that resize
operations are decoupled from the sync tasks queue.

Additionally, an optimization was made that if a plugin schedules
a single, non repeating, no delay task, that we immediately dispatch it
to the executor pool instead of scheduling it. This avoids an unnecessary
round trip through the queue, as well as will reduce the size growth of the
queue if a plugin schedules lots of asynchronous tasks.
2018-03-16 23:09:51 -04:00