Skip to content

Working with m_TransferMap has become thread-safe. #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/TransferImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void LibUSB::TransferImpl::Start()
}

// 3. Ensure that the callback can reach this transfer.
m_TransferMap.insert(make_pair(this, shared_from_this()));
InsertTransfer(make_pair(this, shared_from_this()));

// 4. Submit the transfer
int Result = libusb_submit_transfer(m_pTransfer.get());
Expand Down Expand Up @@ -225,7 +225,8 @@ void LibUSB::TransferImpl::AsyncTransferCallback( libusb_transfer* pTransfer )
TransferImpl* index = (TransferImpl*) pTransfer->user_data;

/// Obtain the transfer's shared_ptr
m_TransferMap[index].lock()->NotifyComplete();
auto transfer = GetTransfer(index);
transfer.lock()->NotifyComplete();


}
Expand All @@ -248,7 +249,7 @@ void LibUSB::TransferImpl::NotifyComplete()


// Remove the TransferMap entry.
m_TransferMap.erase(this);
EraseTransfer(this);
}

void LibUSB::TransferImpl::Reset()
Expand Down Expand Up @@ -462,7 +463,8 @@ LibUSB::TransferResult_t LibUSB::TransferImpl::Result() const
}


std::map<LibUSB::TransferImpl*, std::weak_ptr<LibUSB::TransferImpl>> LibUSB::TransferImpl::m_TransferMap;
LibUSB::TransferImpl::TransferStorage LibUSB::TransferImpl::m_TransferMap;
std::mutex LibUSB::TransferImpl::m_TransferMap_mutex;


// **********************************
Expand Down
24 changes: 22 additions & 2 deletions src/TransferImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,29 @@ namespace LibUSB

private:


/// Weak pointers to all active transfer objects
static std::map<TransferImpl*, std::weak_ptr<TransferImpl>> m_TransferMap;
using TransferStorage = std::map<TransferImpl*, std::weak_ptr<TransferImpl>>;
static TransferStorage m_TransferMap;
static std::mutex m_TransferMap_mutex;

/// Adding a new transfer object to the storage.
template<class T>
inline static std::pair<TransferStorage::iterator, bool> InsertTransfer(T&& value) {
std::lock_guard<std::mutex> lk(m_TransferMap_mutex);
return m_TransferMap.insert(std::move(value));
}

/// Deleting an transfer object from storage.
inline static size_t EraseTransfer(TransferImpl* index) {
std::lock_guard<std::mutex> lk(m_TransferMap_mutex);
return m_TransferMap.erase(index);
}

/// Retrieving an transfer object from storage.
inline static std::weak_ptr<TransferImpl> GetTransfer(TransferImpl* index) {
std::lock_guard<std::mutex> lk(m_TransferMap_mutex);
return m_TransferMap[index];
}

/// Pointer to the Parent Transfer object
std::weak_ptr<Transfer> m_pParentTransfer;
Expand Down