text
stringlengths
0
2.2M
bslmt::QLock *d_qlock;
};
void *testCase6(int threadNum, const MyTask& task)
{
DataCase6 *data = reinterpret_cast<DataCase6 *>(task.arg());
ASSERT(data != 0);
ASSERT(threadNum > 0);
bsl::size_t i = 0;
for (i = 0; i < data->d_slots.size(); ++i) {
ASSERT(data->d_slots[i] == 0);
}
task.barrier()->wait();
{
bslmt::QLockGuard guard(data->d_qlock);
// sleep enough to allow other threads be enqueued
bslmt::ThreadUtil::microSleep(1000 * 250);
ASSERT(data->d_slots[threadNum - 1] == 0);
data->d_slots[threadNum - 1] = 1;
}
{
bslmt::QLockGuard guard(data->d_qlock);
// check that all threads have had their turn
for (i = 0; i < data->d_slots.size(); ++i) {
ASSERT(data->d_slots[i] != 0);
}
}
return 0;
}
// ----------------------------------------------------------------------------
// Case 5 Multiple threads - multiple QLocks Test. At the same time this test
// can be used for performance evaluation for such scenario.
//
// Concerns:
// To test behavior and performance in case of many threads
// and many QLocks.
//
// Plan:
// Create array of data slots. Each element-slot is protected by
// its own QLock. Create multiple threads. Each thread in the loop
// obtains a lock for the random slot. Upon acquiring the lock for
// the slot, the thread stores locally the slot data, manipulates
// them with slot data, ensures the slot data is not modified by
// other threads, restores the original slot data, and releases
// the slot lock. Repeat above action in the loop.
//
// Repeat above scenario several times, incrementing the number of
// threads by one on each iteration.
// ----------------------------------------------------------------------------
struct CaseData5 {
int d_numIter;
int d_numElements;
bslmt::QLock *d_qlocks;
bslmt::Mutex *d_mutexes;
int *d_slots;
};
void *testCase5_fn1(int threadNum, const MyTask& task)
{
CaseData5 *data = reinterpret_cast< CaseData5 *> (task.arg());
Rand rand (threadNum);
for (int i=0; i < data->d_numIter; ++i) {
// get random slot
int slotIndex = rand.get() % data->d_numElements;
bslmt::QLockGuard guard(&data->d_qlocks[slotIndex]);
data->d_slots[slotIndex] = 0;
ASSERT(data->d_slots[slotIndex] == 0);
data->d_slots[slotIndex] |= 1;
ASSERT(data->d_slots[slotIndex] == 1);
data->d_slots[slotIndex] <<= 1;
ASSERT(data->d_slots[slotIndex] == 2);
data->d_slots[slotIndex] ^= 2;
ASSERT(data->d_slots[slotIndex] == 0);
guard.unlock();
}
return 0;
}