text
stringlengths
0
2.2M
bslmt::QLock qlock2 = BSLMT_QLOCK_INITIALIZER;
ContextCase7 context1;
ContextCase7 context2;
context1.d_qlock = &qlock1;
context1.d_owner = -1;
context1.d_slots.clear();
context2.d_qlock = &qlock2;
context2.d_owner = -1;
context2.d_slots.clear();
DataCase7 data1 = { 0, &context1, &context2 };
DataCase7 data2 = { 0, &context2, &context1 };
MyTask task71(testCase7, &data1);
MyTask task72(testCase7, &data2);
for (int i=1; i < 10; ++i) {
bslmt::Barrier barrier(i*2);
data1.d_barrier = &barrier;
data2.d_barrier = &barrier;
ASSERT(context1.d_owner == -1);
ASSERT(context2.d_owner == -1);
context1.d_slots.assign(i, -1);
ASSERT(context1.d_slots.size()== i);
context2.d_slots.assign(i, -1);
ASSERT(context2.d_slots.size()== i);
ASSERT(0 == task71.start(i));
ASSERT(0 == task72.start(i));
ASSERT(0 == task71.stop());
ASSERT(0 == task72.stop());
for (int j=0; j < i; ++j) {
ASSERT(context1.d_slots[j] != -1);
ASSERT(context2.d_slots[j] != -1);
}
bslmt::QLockGuard guard1(&qlock1, false);
ASSERT(guard1.tryLock() == 0);
bslmt::QLockGuard guard2(&qlock2, false);
ASSERT(guard2.tryLock() == 0);
}
} break;
case 6: {
// --------------------------------------------------------------------
// Fairness Test
//
// Concerns:
// Verify that each thread obtains the lock in the order in
// which it requested it.
//
// Plan:
// A bunch of threads wait on a barrier, then compete for a QLock.
// Upon acquiring the lock, each thread sets a thread-specific flag,
// then sleeps very briefly, releases the lock, and loops. If the
// flag has already been set (i.e., second time in for that thread),
// assert that all of the other flags are also set, i.e., every other
// thread has had a turn before any thread has a second chance.
// Each thread exits after its second time in the critical region.
//
// Repeat above scenario several times, incrementing the number of
// threads by one on each iteration.
// --------------------------------------------------------------------
if (verbose) cout << "Fairness Test" << bsl::endl;
bslmt::QLock qlock = BSLMT_QLOCK_INITIALIZER;
DataCase6 data;
data.d_qlock = &qlock;
data.d_slots.clear();
MyTask task6(testCase6, &data);
for (int i=1; i < 10; ++i) {
data.d_slots.assign(i,0);
ASSERT(0 == task6.start(i));
ASSERT(0 == task6.stop());
bslmt::QLockGuard guard(data.d_qlock, false);
ASSERT(guard.tryLock() == 0);
}
} break;
case 5: {
// --------------------------------------------------------------------
// Multiple threads - multiple QLocks Test. At the same time this test
// can be used for performance evaluation for such scenario.