text
stringlengths
0
2.2M
// This class provides a functor to be passed to the ThreadMultiplexor for
// testing. The job the functor does is to increment a counter controlled
// by a mutex. The start of the job can be coordinated with a semaphore,
// allowing for forced contention.
// DATA
int d_timesCalled;
int d_maxProcessors;
bslmt::Mutex d_mutex;
bslmt::Semaphore *d_semaphore; // (held, not owned)
bdlmt::ThreadMultiplexor *d_multiplexor; // (held, not owned)
public:
// CREATORS
UsageTestChecker(bdlmt::ThreadMultiplexor *multiplexor);
~UsageTestChecker();
// MANIPULATORS
operator TestQueue::Job ();
// Return an invokable functor which calls the 'eval' method of this
// object.
void eval();
// Perform the function of this object.
void reset();
// Reset the counter variables of this object to 0.
void setSemaphore(bslmt::Semaphore* semaphore);
// Specify the 'semaphore' which will control the start of the job.
// ACCESSORS
int timesCalled() const;
// Return the number of times the functor was invoked.
int maxProcessors() const;
// Return the maximal value of the "processors" attribute of the
// multiplexor when executing this functor.
};
// CREATORS
UsageTestChecker::UsageTestChecker(bdlmt::ThreadMultiplexor *multiplexor)
: d_timesCalled(0)
, d_maxProcessors(0)
, d_semaphore(0)
, d_multiplexor(multiplexor)
{}
UsageTestChecker::~UsageTestChecker()
{
}
// MANIPULATORS
UsageTestChecker::operator TestQueue::Job()
{
return bdlf::BindUtil::bind(&UsageTestChecker::eval, this);
}
void UsageTestChecker::eval() {
if (d_semaphore) {
d_semaphore->wait();
}
d_mutex.lock();
++d_timesCalled;
d_maxProcessors =
bsl::max(d_maxProcessors, d_multiplexor->numProcessors());
d_mutex.unlock();
}
void UsageTestChecker::reset() {
d_timesCalled = d_maxProcessors = 0;
}
void UsageTestChecker::setSemaphore(bslmt::Semaphore* semaphore) {
d_semaphore = semaphore;
}
// ACCESSORS
int UsageTestChecker::timesCalled() const {
return d_timesCalled;
}
int UsageTestChecker::maxProcessors() const {
return d_maxProcessors;
}
// ============================================================================
// TEST CASE 6: SUPPORT FUNCTIONS
// ----------------------------------------------------------------------------
namespace TEST_CASE_6 {
void testCase6(bslmt::Semaphore *startSemaphore,
bdlmt::ThreadMultiplexor *mX,
int numJobs,
const bsl::function<void()>& job)
{
startSemaphore->wait();
for (; 0 < numJobs; --numJobs) {