Skip to content

Commit c79dfa4

Browse files
committed
Added comments and asserts.
1 parent 8bbdf11 commit c79dfa4

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

common/autoresetevent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class AutoResetEvent
4141
if (m_status.compare_exchange_weak(oldStatus, newStatus, std::memory_order_release, std::memory_order_relaxed))
4242
break;
4343
// The compare-exchange failed, likely because another thread changed m_status.
44-
// oldStatus now has its latest value (passed by reference). Retry the CAS loop.
44+
// oldStatus has been updated. Retry the CAS loop.
4545
}
4646
if (oldStatus < 0)
4747
m_sema.signal(); // Release one waiting thread.

common/bitfield.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ struct BitFieldMember : BitField<T>
5555

5656
void set(T v)
5757
{
58-
assert(v <= Maximum);
58+
assert(v <= Maximum); // v must fit inside the bitfield member
5959
this->value = (this->value & ~Mask) | (v << Offset);
6060
}
6161

@@ -66,7 +66,7 @@ struct BitFieldMember : BitField<T>
6666

6767
void add(T v)
6868
{
69-
assert(get() + v <= Maximum);
69+
assert(get() + v <= Maximum); // result must fit inside the bitfield member
7070
this->value += v << Offset;
7171
}
7272

@@ -77,7 +77,7 @@ struct BitFieldMember : BitField<T>
7777

7878
void sub(T v)
7979
{
80-
assert(get() >= v);
80+
assert(get() >= v); // result must not underflow
8181
this->value -= v << Offset;
8282
}
8383

@@ -108,7 +108,11 @@ struct BitFieldArray : BitField<T>
108108
BitFieldArray() = delete;
109109

110110
T maximum() const { return Maximum; }
111-
int offset(int i) const { return BaseOffset + BitsPerItem * i; }
111+
int offset(int i) const
112+
{
113+
assert(i >= 0 && i < NumItems); // array index must be in range
114+
return BaseOffset + BitsPerItem * i;
115+
}
112116
T one(int i) const { return T(1) << offset(i); }
113117
T mask(int i) const { return Maximum << offset(i); }
114118
int numItems() const { return NumItems; }
@@ -120,7 +124,7 @@ struct BitFieldArray : BitField<T>
120124

121125
void set(int i, T v)
122126
{
123-
assert(v <= Maximum);
127+
assert(v <= Maximum); // v must fit inside the bitfield member
124128
this->value = (this->value & ~mask(i)) | (v << offset(i));
125129
}
126130

@@ -131,7 +135,7 @@ struct BitFieldArray : BitField<T>
131135

132136
void add(int i, T v)
133137
{
134-
assert(get(i) + v <= Maximum);
138+
assert(get(i) + v <= Maximum); // result must fit inside the bitfield member
135139
this->value += v << offset(i);
136140
}
137141

@@ -143,7 +147,7 @@ struct BitFieldArray : BitField<T>
143147

144148
void sub(int i, T v)
145149
{
146-
assert(get(i) >= v);
150+
assert(get(i) >= v); // result must not underflow
147151
this->value -= v << offset(i);
148152
}
149153

common/diningphilosophers.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class LockReducedDiningPhilosophers
238238
// Sanity check.
239239
for (int i = 0; i < m_numPhilos; i++)
240240
assert(newStatus.philos().get(i) <= IntType(m_numPhilos));
241-
// CAS until successful.
241+
// CAS until successful. On failure, oldStatus will be updated with the latest value.
242242
if (m_allStatus.compare_exchange_strong(oldStatus.value, newStatus.value, std::memory_order_relaxed))
243243
break;
244244
}
@@ -273,7 +273,7 @@ class LockReducedDiningPhilosophers
273273
// Sanity check.
274274
for (int i = 0; i < m_numPhilos; i++)
275275
assert(newStatus.philos().get(i) <= IntType(m_numPhilos));
276-
// CAS until successful.
276+
// CAS until successful. On failure, oldStatus will be updated with the latest value.
277277
if (m_allStatus.compare_exchange_strong(oldStatus.value, newStatus.value, std::memory_order_relaxed))
278278
break;
279279
}

common/rwlock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class NonRecursiveRWLock
4747
{
4848
newStatus.readers().add(1);
4949
}
50+
// CAS until successful. On failure, oldStatus will be updated with the latest value.
5051
}
5152
while (!m_status.compare_exchange_weak(oldStatus.value, newStatus.value,
5253
std::memory_order_acquire, std::memory_order_relaxed));
@@ -93,6 +94,7 @@ class NonRecursiveRWLock
9394
newStatus.waitToRead().set(0);
9495
newStatus.readers().set(waitToRead);
9596
}
97+
// CAS until successful. On failure, oldStatus will be updated with the latest value.
9698
}
9799
while (!m_status.compare_exchange_weak(oldStatus.value, newStatus.value,
98100
std::memory_order_release, std::memory_order_relaxed));

0 commit comments

Comments
 (0)