MB-11559: Ensure conn buffers are acquired for all event loops

Second event loop in thread.c wasn't acquiring a loan on read/write
buffers, causing a segfault if that event loop tried to process a
command.

Fix by refactoring the two event loops into a common function.

Also fix a related crash when freeing UPR connections - ensure that
the TAP & UPR flags are cleared before returning buffers in
conn_cleanup(), otherwise the cleanup at the end of the event loop may
dereference a NULL thread ptr.

Change-Id: I372b06f76db577e4dc3628534f5dcf1ce3472add
Reviewed-on: http://review.couchbase.org/38968
Reviewed-by: Trond Norbye <trond.norbye@gmail.com>
Tested-by: Trond Norbye <trond.norbye@gmail.com>