text
stringlengths 0
2.2M
|
---|
// ----------------------------------------------------------------------------
|
// Test Case 7: Multiple-threads, 2-qlocks
|
//
|
// Concerns:
|
// Verify that each thread obtains the lock in the order in
|
// which it requested it.
|
//
|
// Plan:
|
// The two qlocks are completely independent. A thread locking one QLock
|
// does not wait for a thread that holds the lock to the other. Two context
|
// structures, each with a QLock, a few threads on each context. In the
|
// critical region, each thread sets a flag in its context, reads the other
|
// context's flag, sleeps briefly, clears its flag, exits the critical
|
// region, and loops. When every thread's flag has been represented in the
|
// other context structure, all threads exit. An error is reported if the
|
// maximum loop count is reached.
|
// ----------------------------------------------------------------------------
|
struct ContextCase7 {
|
bslmt::QLock *d_qlock;
|
bsls::AtomicInt d_owner;
|
bsl::vector<int> d_slots;
|
};
|
struct DataCase7 {
|
bslmt::Barrier *d_barrier; // common barrier
|
ContextCase7 *d_myContext;
|
ContextCase7 *d_otherContext;
|
};
|
void *testCase7(int threadNum, const MyTask& task)
|
{
|
DataCase7 *data = reinterpret_cast<DataCase7 *> (task.arg());
|
ASSERT(data != 0);
|
ASSERT(threadNum > 0);
|
data->d_barrier->wait();
|
bool flgExit = false;
|
while(!flgExit) {
|
// Lock own context
|
bslmt::QLockGuard guard(data->d_myContext->d_qlock);
|
// set owner of this context
|
data->d_myContext->d_owner = threadNum-1;
|
// Read other context owner
|
int otherOwner = data->d_otherContext->d_owner;
|
// if other owner is valid, mark it visible in this context
|
if (otherOwner != -1) {
|
ASSERT(otherOwner < data->d_myContext->d_slots.size());
|
data->d_myContext->d_slots[otherOwner] = otherOwner;
|
}
|
// lets threads of other context to see me
|
bslmt::ThreadUtil::yield();
|
// exit flag is OK if this thread has been seen in other context and we
|
// have seen all threads in other context
|
flgExit = (data->d_otherContext->d_slots[threadNum-1] != -1);
|
for (bsl::size_t i=0; i < data->d_myContext->d_slots.size(); ++i) {
|
if (data->d_myContext->d_slots[i] == -1) {
|
flgExit = false;
|
break;
|
}
|
}
|
// Clear the owner of my context
|
data->d_myContext->d_owner = -1;
|
}
|
return 0;
|
}
|
// ----------------------------------------------------------------------------
|
// Test 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.
|
// ----------------------------------------------------------------------------
|
struct DataCase6 {
|
bsl::vector<int> d_slots;
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.