In add_callback, hold the lock while writing to the waker pipe.

This protects against a shutdown race condition seen occasionally in
the ThreadedResolver unittests.  This slightly increases contention
on the callback lock in multi-threaded scenarios, but the cost is
limited by the fact that we only write to the pipe once per IOLoop
iteration.

Closes #875.