Skip to content

Commit 5451f22

Browse files
committed
Add asserts
1 parent e80dcf9 commit 5451f22

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

offload/include/PerThreadTable.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ template <typename ObjectType> struct PerThread {
3434
PerThread(PerThread &&) = delete;
3535
PerThread &operator=(const PerThread &) = delete;
3636
PerThread &operator=(PerThread &&) = delete;
37-
~PerThread() { ThreadDataList.clear(); }
37+
~PerThread() {
38+
assert(Mutex.try_lock() &&
39+
"Cannot be deleted while other threads are adding entries");
40+
ThreadDataList.clear();
41+
}
3842

3943
private:
4044
PerThreadData &getThreadData() {
@@ -60,6 +64,8 @@ template <typename ObjectType> struct PerThread {
6064
ObjectType &get() { return getThreadEntry(); }
6165

6266
template <class ClearFuncTy> void clear(ClearFuncTy ClearFunc) {
67+
assert(Mutex.try_lock() &&
68+
"Clear cannot be called while other threads are adding entries");
6369
for (std::shared_ptr<PerThreadData> ThreadData : ThreadDataList) {
6470
if (!ThreadData->ThreadEntry)
6571
continue;
@@ -115,7 +121,11 @@ template <typename ContainerType, typename ObjectType> struct PerThreadTable {
115121
PerThreadTable(PerThreadTable &&) = delete;
116122
PerThreadTable &operator=(const PerThreadTable &) = delete;
117123
PerThreadTable &operator=(PerThreadTable &&) = delete;
118-
~PerThreadTable() { ThreadDataList.clear(); }
124+
~PerThreadTable() {
125+
assert(Mutex.try_lock() &&
126+
"Cannot be deleted while other threads are adding entries");
127+
ThreadDataList.clear();
128+
}
119129

120130
private:
121131
PerThreadData &getThreadData() {
@@ -176,6 +186,8 @@ template <typename ContainerType, typename ObjectType> struct PerThreadTable {
176186
}
177187

178188
template <class ClearFuncTy> void clear(ClearFuncTy ClearFunc) {
189+
assert(Mutex.try_lock() &&
190+
"Clear cannot be called while other threads are adding entries");
179191
for (std::shared_ptr<PerThreadData> ThreadData : ThreadDataList) {
180192
if (!ThreadData->ThreadEntry || ThreadData->NElements == 0)
181193
continue;
@@ -200,6 +212,8 @@ template <typename ContainerType, typename ObjectType> struct PerThreadTable {
200212
}
201213

202214
template <class DeinitFuncTy> llvm::Error deinit(DeinitFuncTy DeinitFunc) {
215+
assert(Mutex.try_lock() &&
216+
"Deinit cannot be called while other threads are adding entries");
203217
for (std::shared_ptr<PerThreadData> ThreadData : ThreadDataList) {
204218
if (!ThreadData->ThreadEntry || ThreadData->NElements == 0)
205219
continue;

0 commit comments

Comments
 (0)