Fix deadlock when QQmlEngine is destroyed during compilation.
Before blocking to wait for the thread to finish ensure the thread
itself is not waiting for the main thread to complete some action
otherwise both threads will be waiting on the same wait condition that
neither is able to wake. Likewise ensure the shutdown action is
processed if the last scheduled event is currently being processed.
Task-number: QTBUG-35581
Change-Id: I483fc56d0d398493f9fa907f3ab203439bfb9221
Reviewed-by: Robin Burchell <robin+qt@viroteck.net>
Reviewed-by: Lars Knoll <lars.knoll@digia.com>