Skip to content

Commit

Permalink
Merge pull request #699 from leapmotion/ref-setcurrent
Browse files Browse the repository at this point in the history
Performance improvement to SetCurrent
  • Loading branch information
Jonathan Marsden committed Jul 31, 2015
2 parents d517d63 + ba8c446 commit 4961d58
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion autowiring/CoreContext.h
Expand Up @@ -1000,7 +1000,7 @@ class CoreContext:
/// context is assigned before invoking a CoreRunnable instance's Run method, and it's also assigned
/// when a context is first constructed by a thread.
/// </remarks>
static std::shared_ptr<CoreContext> CurrentContextOrNull(void);
static const std::shared_ptr<CoreContext>& CurrentContextOrNull(void);

/// <summary>
/// Identical to CurrentContextNoCheck, except returns the global context instead of a null pointer
Expand Down
14 changes: 11 additions & 3 deletions src/autowiring/CoreContext.cpp
Expand Up @@ -617,9 +617,10 @@ bool CoreContext::DelayUntilInitiated(void) {
return !IsShutdown();
}

std::shared_ptr<CoreContext> CoreContext::CurrentContextOrNull(void) {
const std::shared_ptr<CoreContext>& CoreContext::CurrentContextOrNull(void) {
static const std::shared_ptr<CoreContext> empty;
auto retVal = autoCurrentContext.get();
return retVal ? *retVal : nullptr;
return retVal ? *retVal : empty;
}

std::shared_ptr<CoreContext> CoreContext::CurrentContext(void) {
Expand Down Expand Up @@ -1269,7 +1270,14 @@ std::ostream& operator<<(std::ostream& os, const CoreContext& rhs) {
}

std::shared_ptr<CoreContext> CoreContext::SetCurrent(const std::shared_ptr<CoreContext>& ctxt) {
std::shared_ptr<CoreContext> retVal = CoreContext::CurrentContextOrNull();
const auto& currentContext = CurrentContextOrNull();

// Short-circuit test, no need to proceed if we aren't changing the context:
if (currentContext == ctxt)
return currentContext;

// Value is changing, update:
auto retVal = currentContext;
if (ctxt)
autoCurrentContext.reset(new std::shared_ptr<CoreContext>(ctxt));
else
Expand Down

0 comments on commit 4961d58

Please sign in to comment.