Add lock on writing to GPIO.

TODO:Need to verify it works as expected.