Skip to content

Commit ec7f74f

Browse files
committed
core(TLS): add cleanup() method
1 parent 526220a commit ec7f74f

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

modules/core/include/opencv2/core/utility.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,9 @@ class CV_EXPORTS TLSDataContainer
627627
virtual void deleteDataInstance(void* pData) const = 0;
628628

629629
int key_;
630+
631+
public:
632+
void cleanup(); //! Release created TLS data container objects. It is similar to release() call, but it keeps TLS container valid.
630633
};
631634

632635
// Main TLS data class
@@ -638,13 +641,15 @@ class TLSData : protected TLSDataContainer
638641
inline ~TLSData() { release(); } // Release key and delete associated data
639642
inline T* get() const { return (T*)getData(); } // Get data associated with key
640643

641-
// Get data from all threads
644+
// Get data from all threads
642645
inline void gather(std::vector<T*> &data) const
643646
{
644647
std::vector<void*> &dataVoid = reinterpret_cast<std::vector<void*>&>(data);
645648
gatherData(dataVoid);
646649
}
647650

651+
inline void cleanup() { TLSDataContainer::cleanup(); }
652+
648653
private:
649654
virtual void* createDataInstance() const {return new T;} // Wrapper to allocate data by template
650655
virtual void deleteDataInstance(void* pData) const {delete (T*)pData;} // Wrapper to release data by template

modules/core/src/system.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ class TlsStorage
10861086
}
10871087

10881088
// Release TLS storage index and pass associated data to caller
1089-
void releaseSlot(size_t slotIdx, std::vector<void*> &dataVec)
1089+
void releaseSlot(size_t slotIdx, std::vector<void*> &dataVec, bool keepSlot = false)
10901090
{
10911091
AutoLock guard(mtxGlobalAccess);
10921092
CV_Assert(tlsSlots.size() > slotIdx);
@@ -1099,12 +1099,13 @@ class TlsStorage
10991099
if (thread_slots.size() > slotIdx && thread_slots[slotIdx])
11001100
{
11011101
dataVec.push_back(thread_slots[slotIdx]);
1102-
threads[i]->slots[slotIdx] = 0;
1102+
thread_slots[slotIdx] = NULL;
11031103
}
11041104
}
11051105
}
11061106

1107-
tlsSlots[slotIdx] = 0;
1107+
if (!keepSlot)
1108+
tlsSlots[slotIdx] = 0;
11081109
}
11091110

11101111
// Get data by TLS storage index
@@ -1196,9 +1197,18 @@ void TLSDataContainer::release()
11961197
std::vector<void*> data;
11971198
data.reserve(32);
11981199
getTlsStorage().releaseSlot(key_, data); // Release key and get stored data for proper destruction
1200+
key_ = -1;
1201+
for(size_t i = 0; i < data.size(); i++) // Delete all associated data
1202+
deleteDataInstance(data[i]);
1203+
}
1204+
1205+
void TLSDataContainer::cleanup()
1206+
{
1207+
std::vector<void*> data;
1208+
data.reserve(32);
1209+
getTlsStorage().releaseSlot(key_, data, true); // Extract stored data with removal from TLS tables
11991210
for(size_t i = 0; i < data.size(); i++) // Delete all associated data
12001211
deleteDataInstance(data[i]);
1201-
key_ = -1;
12021212
}
12031213

12041214
void* TLSDataContainer::getData() const

0 commit comments

Comments
 (0)