script: Improve dirty propagation and fix script-layout synchronization.

This fixes race conditions whereby layout and script could be running
simultaneously.
script: Add some workarounds for image cache task races
auto merge of #3844 : pcwalton/servo/script-layout-synchronization, r=jdm

This fixes race conditions whereby layout and script could be running
simultaneously.

r? @jdm 
cc @cgaebel