Skip to content

Commit d078fa8

Browse files
authored
Remove double locking in prepareForWrite (#771)
* Remove double locking in `prepareForWrite` The same check is performed later so the first locking is unnecessary * Remove the double locking also in LinkCollectionImpl.h --------- Co-authored-by: jmcarcell <jmcarcell@users.noreply.github.com>
1 parent aa5927b commit d078fa8

File tree

2 files changed

+2
-23
lines changed

2 files changed

+2
-23
lines changed

include/podio/detail/LinkCollectionImpl.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,6 @@ class LinkCollection : public podio::CollectionBase {
261261
}
262262

263263
void prepareForWrite() const override {
264-
// TODO: Replace this double locking pattern with an atomic and only one
265-
// lock. Problem: std::atomic is not default movable
266-
{
267-
std::lock_guard lock{*m_storageMtx};
268-
// If the collection has been prepared already there is nothing to do
269-
if (m_isPrepared) {
270-
return;
271-
}
272-
}
273-
274264
std::lock_guard lock{*m_storageMtx};
275265
// by the time we acquire the lock another thread might have already
276266
// succeeded, so we need to check again now

python/templates/Collection.cc.jinja2

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
#endif
2323

2424
// standard includes
25-
#include <stdexcept>
2625
#include <iomanip>
26+
#include <mutex>
27+
#include <stdexcept>
2728

2829
{{ utils.namespace_open(class.namespace) }}
2930

@@ -104,19 +105,7 @@ void {{ collection_type }}::clear() {
104105
}
105106

106107
void {{ collection_type }}::prepareForWrite() const {
107-
// TODO: Replace this double locking pattern here with an atomic and only one
108-
// lock. Problem: std::atomic is not default movable.
109-
{
110-
std::lock_guard lock{*m_storageMtx};
111-
// If the collection has been prepared already there is nothing to do
112-
if (m_isPrepared) {
113-
return;
114-
}
115-
}
116-
117108
std::lock_guard lock{*m_storageMtx};
118-
// by the time we acquire the lock another thread might have already
119-
// succeeded, so we need to check again now
120109
if (m_isPrepared) {
121110
return;
122111
}

0 commit comments

Comments
 (0)