Skip to content

Commit

Permalink
[libcxx testing] Remove ALLOW_RETRIES from lock_guard tests
Browse files Browse the repository at this point in the history
These two tests were clumsily using time measurements to determine
whether std::lock_guard was working correctly. In practice, this
approach merely verified that the underlying lock properly waits.

Now these two tests verify that lock is acquired, not dropped
prematurely, and finally, actually dropped at the end of the scope.
  • Loading branch information
davezarzycki committed May 18, 2020
1 parent 87b235d commit a675c1d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,30 @@
//
// UNSUPPORTED: libcpp-has-no-threads

// ALLOW_RETRIES: 2

// <mutex>

// template <class Mutex> class lock_guard;

// lock_guard(mutex_type& m, adopt_lock_t);

#include <mutex>
#include <thread>
#include <cstdlib>
#include <cassert>

#include "test_macros.h"

std::mutex m;

typedef std::chrono::system_clock Clock;
typedef Clock::time_point time_point;
typedef Clock::duration duration;
typedef std::chrono::milliseconds ms;
typedef std::chrono::nanoseconds ns;

void f()
int main()
{
time_point t0 = Clock::now();
time_point t1;
{
{
m.lock();
std::lock_guard<std::mutex> lg(m, std::adopt_lock);
t1 = Clock::now();
}
ns d = t1 - t0 - ms(250);
assert(d < ms(50)); // within 50ms
}
assert(m.try_lock() == false);
}

int main(int, char**)
{
m.lock();
std::thread t(f);
std::this_thread::sleep_for(ms(250));
m.unlock();
t.join();
m.lock();
m.unlock();

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
//
// UNSUPPORTED: libcpp-has-no-threads

// ALLOW_RETRIES: 2

// <mutex>

// template <class Mutex> class lock_guard;
Expand All @@ -20,43 +18,26 @@
// -> lock_guard<_Mutex>; // C++17

#include <mutex>
#include <thread>
#include <cstdlib>
#include <cassert>

#include "test_macros.h"

std::mutex m;

typedef std::chrono::system_clock Clock;
typedef Clock::time_point time_point;
typedef Clock::duration duration;
typedef std::chrono::milliseconds ms;
typedef std::chrono::nanoseconds ns;

void f()
int main()
{
time_point t0 = Clock::now();
time_point t1;
{
{
std::lock_guard<std::mutex> lg(m);
t1 = Clock::now();
}
ns d = t1 - t0 - ms(250);
assert(d < ms(200)); // within 200ms
}
assert(m.try_lock() == false);
}

int main(int, char**)
{
m.lock();
std::thread t(f);
std::this_thread::sleep_for(ms(250));
m.unlock();
t.join();
m.lock();
m.unlock();

#ifdef __cpp_deduction_guides
std::lock_guard lg(m);
static_assert((std::is_same<decltype(lg), std::lock_guard<decltype(m)>>::value), "" );
std::lock_guard lg(m);
static_assert((std::is_same<decltype(lg), std::lock_guard<decltype(m)>>::value), "" );
#endif

return 0;
Expand Down

0 comments on commit a675c1d

Please sign in to comment.