[Qt] Improve rpc console history behavior
o Added threadsafety.h - a set of macros using the -Wthread-safety
  feature in clang.  These macros should primarily be used to
  document which locks protect a given piece of data.  Secondary it
  can be used to document the set of held and excluded locks when
  entering a function.
o Added AnnotatedMixin which adds locking annotations to the mutex
  API, compatible with clang's -Wthread-safety
Simplify CMutexLock

o Remove unused Leave and GetLock functions
o Make Enter and TryEnter private.
o Simplify Enter and TryEnter.
  boost::unique_lock doesn't really know whether the
  mutex it wraps is locked or not when the defer_lock
  option is used.
  The boost::recursive_mutex does not expose this
  information, so unique_lock only infers this
  knowledge.  When taking the lock is defered, it
  (randomly) assumes that the lock is not taken.
  boost::unique_lock has the following definition:

        unique_lock(Mutex& m_,defer_lock_t):
            m(&m_),is_locked(false)
        {}

        bool owns_lock() const
        {
            return is_locked;
        }
  Thus it is a mistake to check owns_lock() in Enter
  and TryEnter - they will always return false.
Added comments to sync.h to make it easier to understand the macros
cleanup sync.{h,cpp}
mutex debugging routines: LocksHeld() and AssertLockHeld()
Use mutex pointer instead of name for AssertLockHeld

This makes it useable for non-global locks such as the wallet and
keystore locks.
Document cs_wallet lock and add AssertLockHeld

Add locking assertions to wallet to all methods that
access internal fields and do not aquire the cs_wallet mutex.

Conflicts:
	src/wallet.cpp
	src/wallet.h
remove unused CWallet::ScanForWalletTransaction()
Wallet locking fixes for -DDEBUG_LOCKORDER

Compiling with -DDEBUG_LOCKORDER and running the qa/rpc-test/ regression
tests uncovered a couple of wallet methods that should (but didn't)
acquire the cs_wallet mutext.

I also changed the AssertLockHeld() routine print to stderr and
abort, instead of printing to debug.log and then assert()'ing.
It is annoying to look in debug.log to find out which
AssertLockHeld is failing.

Conflicts:
	qa/rpc-tests/txnmall.sh
	src/sync.cpp
	src/wallet.cpp
Add AssertLockHeld for cs_main to ChainActive-using functions

All functions that use ChainActive but do not aquire the cs_main
lock themselves, need to be called with the cs_main lock held.

This commit adds assertions to all externally callable functions
that use chainActive or chainMostWork.

This will flag usages when built with -DDEBUG_LOCKORDER.

Conflicts:
	src/main.cpp
	src/qt/transactiondesc.cpp
	src/qt/transactionrecord.cpp
LoadWallet: acquire cs_wallet mutex before clearing setKeyPool

Make the function mutex-aware, to prevent having to lock cs_wallet
at the call site in Init.
Solve chainActive-related locking issues

- In wallet and GUI code LOCK cs_main as well as cs_wallet when
  necessary
- In main.cpp SendMessages move the TRY_LOCK(cs_main) up, to encompass the call
  to IsInitialBlockDownload.
- Make ActivateBestChain, AddToBlockIndex, IsInitialBlockDownload,
  InitBlockIndex acquire the cs_main lock

Fixes #3997

Conflicts:
	src/main.cpp
	src/qt/clientmodel.cpp
	src/qt/transactiondesc.cpp
	src/qt/transactiontablemodel.cpp
	src/qt/walletmodel.cpp
	src/wallet.cpp
wallet: add missing locks
refactor mempool locks
remove fClient
key.cpp: fail with a friendlier message on missing ssl EC support

Previously if bitcoind is linked with an OpenSSL which is compiled
without EC support, this is seen as an assertion failure "pKey !=
NULL" at key.cpp:134, which occurs after several seconds. It is an
esoteric piece of knowledge to interpret this as "oops, I linked
with the wrong OpenSSL", and because of the delay it may not even
be noticed.

The new output is

: OpenSSL appears to lack support for elliptic curve cryptography. For
more information, visit
https://en.bitcoin.it/wiki/OpenSSL_and_EC_Libraries
: Initialization sanity check failed. Bitcoin Core is shutting down.

which occurs immediately after attempted startup.

This also blocks in an InitSanityCheck() function which currently only
checks for EC support but should eventually do more. See #4081.

Conflicts:
	src/init.cpp
	src/key.h
Remove unused Print/PrintHex functions

You can just use HexStr(script) or script.ToString() for debugging, no
need for these extra functions.

Conflicts:
	src/script.h
	src/util.h
remove unused UPnP code from main.h

Conflicts:
	src/main.h