diff options
Diffstat (limited to 'src/mongo/db/client.h')
-rw-r--r-- | src/mongo/db/client.h | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/mongo/db/client.h b/src/mongo/db/client.h index 99b172505e3..74130191b99 100644 --- a/src/mongo/db/client.h +++ b/src/mongo/db/client.h @@ -47,6 +47,7 @@ #include "mongo/transport/session.h" #include "mongo/util/concurrency/spin_lock.h" #include "mongo/util/decorable.h" +#include "mongo/util/invariant.h" #include "mongo/util/net/hostandport.h" namespace mongo { @@ -242,6 +243,37 @@ private: PseudoRandom _prng; }; +/** + * Utility class to temporarily swap which client is bound to the running thread. + * + * Use this class to bind a client to the current thread for the duration of the + * AlternativeClientRegion's lifetime, restoring the prior client, if any, at the + * end of the block. + */ +class AlternativeClientRegion { +public: + explicit AlternativeClientRegion(ServiceContext::UniqueClient& clientToUse) + : _alternateClient(&clientToUse) { + invariant(clientToUse); + if (Client::getCurrent()) { + _originalClient = Client::releaseCurrent(); + } + Client::setCurrent(std::move(*_alternateClient)); + } + + ~AlternativeClientRegion() { + *_alternateClient = Client::releaseCurrent(); + if (_originalClient) { + Client::setCurrent(std::move(_originalClient)); + } + } + +private: + ServiceContext::UniqueClient _originalClient; + ServiceContext::UniqueClient* const _alternateClient; +}; + + /** get the Client object for this thread. */ Client& cc(); |