Make the layout context thread-safe

r? @metajack