Skip to content

Commit

Permalink
Merge pull request #55 from leapmotion/ref-stdlock
Browse files Browse the repository at this point in the history
Updating atomic object to make use of a lock heirarchy scheme
  • Loading branch information
Gabriel Hare committed Aug 16, 2014
2 parents 3b20c77 + 5289002 commit 7e0bd3c
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions autowiring/atomic_object.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) 2010 - 2014 Leap Motion. All rights reserved. Proprietary and confidential.
#pragma once
#include "unlock_object.h"
#include MEMORY_HEADER
#include MUTEX_HEADER

#include "unlock_object.h"
#include TYPE_TRAITS_HEADER

template<class object, class lock> class unlock_object;

Expand Down Expand Up @@ -60,12 +60,19 @@ class atomic_object {
atomic_object<object, lock>& operator = (const atomic_object<object>& source) {
if (this == &source)
return *this;
//IMPORTANT: Aquisition of both locks must be atomic.
//The following code:
// m_initialized = source.initialized(m_object);
//could deadlock with its counterpart in source.
std::lock(m_lock, source.m_lock);

// Use memory well-ordering to establish a lock heirarchy
if(this < &source) {
m_lock.lock();
source.m_lock.lock();
}
else {
source.m_lock.lock();
m_lock.lock();
}

m_object = source.m_object;

m_lock.unlock();
source.m_lock.unlock();
return *this;
Expand Down

0 comments on commit 7e0bd3c

Please sign in to comment.